blob: eaec689c4ce41d2eb0954d08d4db71b6aef838d0 [file] [log] [blame]
Hyunsun Moone7e4bb32016-05-16 04:32:45 -07001/*
2 * Copyright 2016-present Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.cordvtn.impl.service;
17
18import com.google.common.collect.Maps;
19import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.onlab.packet.Ethernet;
23import org.onlab.packet.IpPrefix;
24import org.onosproject.cordconfig.access.AccessAgentConfig;
25import org.onosproject.cordconfig.access.AccessAgentData;
26import org.onosproject.cordvtn.api.CordVtnConfig;
27import org.onosproject.cordvtn.api.Instance;
28import org.onosproject.cordvtn.api.InstanceHandler;
29import org.onosproject.cordvtn.impl.CordVtnInstanceHandler;
30import org.onosproject.net.DeviceId;
31import org.onosproject.net.PortNumber;
32import org.onosproject.net.config.ConfigFactory;
33import org.onosproject.net.config.NetworkConfigEvent;
34import org.onosproject.net.config.NetworkConfigListener;
35import org.onosproject.net.config.basics.SubjectFactories;
36import org.onosproject.net.flow.DefaultFlowRule;
37import org.onosproject.net.flow.DefaultTrafficSelector;
38import org.onosproject.net.flow.DefaultTrafficTreatment;
39import org.onosproject.net.flow.FlowRule;
40import org.onosproject.net.flow.TrafficSelector;
41import org.onosproject.net.flow.TrafficTreatment;
42import org.onosproject.xosclient.api.VtnService;
43
44import java.util.Map;
45import java.util.Set;
46
47import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
48import static org.onlab.util.Tools.groupedThreads;
49import static org.onosproject.cordvtn.impl.CordVtnPipeline.PRIORITY_MANAGEMENT;
50import static org.onosproject.cordvtn.impl.CordVtnPipeline.TABLE_ACCESS_TYPE;
51
52/**
53 * Provides network connectivity for OLT agent instances.
54 */
55@Component(immediate = true)
56public class OltAgentInstanceHandler extends CordVtnInstanceHandler implements InstanceHandler {
57
58 private static final Class<AccessAgentConfig> CONFIG_CLASS = AccessAgentConfig.class;
59 private ConfigFactory<DeviceId, AccessAgentConfig> configFactory =
60 new ConfigFactory<DeviceId, AccessAgentConfig>(
61 SubjectFactories.DEVICE_SUBJECT_FACTORY, CONFIG_CLASS, "accessAgent") {
62 @Override
63 public AccessAgentConfig createConfig() {
64 return new AccessAgentConfig();
65 }
66 };
67
68 private Map<DeviceId, AccessAgentData> oltAgentData = Maps.newConcurrentMap();
69 private IpPrefix mgmtIpRange = null;
70
71 @Activate
72 protected void activate() {
73 eventExecutor = newSingleThreadScheduledExecutor(groupedThreads("onos/cordvtn-olt", "event-handler"));
74 serviceType = VtnService.ServiceType.OLT_AGENT;
75
76 configRegistry.registerConfigFactory(configFactory);
77 configListener = new InternalConfigListener();
78
79 super.activate();
80 }
81
82 @Deactivate
83 protected void deactivate() {
84 super.deactivate();
85 }
86
87 @Override
88 public void instanceDetected(Instance instance) {
89 log.info("OLT agent instance detected {}", instance);
90
91 managementAccessRule(instance.deviceId(), true);
92 // TODO implement
93 }
94
95 @Override
96 public void instanceRemoved(Instance instance) {
97 log.info("OLT agent instance removed {}", instance);
98
99 if (getInstances(instance.serviceId()).isEmpty()) {
100 nodeManager.completeNodes().stream().forEach(node ->
101 managementAccessRule(node.intBrId(), false));
102 }
103
104 // TODO implement
105 }
106
107 private void managementAccessRule(DeviceId deviceId, boolean install) {
108 // TODO remove this rule after long term management network is done
109 if (mgmtIpRange != null) {
110 TrafficSelector selector = DefaultTrafficSelector.builder()
111 .matchEthType(Ethernet.TYPE_IPV4)
112 .matchIPDst(mgmtIpRange)
113 .build();
114
115 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
116 .setOutput(PortNumber.LOCAL)
117 .build();
118
119 FlowRule flowRule = DefaultFlowRule.builder()
120 .fromApp(appId)
121 .withSelector(selector)
122 .withTreatment(treatment)
123 .withPriority(PRIORITY_MANAGEMENT)
124 .forDevice(deviceId)
125 .forTable(TABLE_ACCESS_TYPE)
126 .makePermanent()
127 .build();
128
129 pipeline.processFlowRule(install, flowRule);
130 }
131 }
132
133 private void readAccessAgentConfig() {
134
135 Set<DeviceId> deviceSubjects = configRegistry.getSubjects(DeviceId.class, CONFIG_CLASS);
136 deviceSubjects.stream().forEach(subject -> {
137 AccessAgentConfig config = configRegistry.getConfig(subject, CONFIG_CLASS);
138 if (config != null) {
139 oltAgentData.put(subject, config.getAgent());
140 }
141 });
142 }
143
144 @Override
145 protected void readConfiguration() {
146 CordVtnConfig config = configRegistry.getConfig(appId, CordVtnConfig.class);
147 if (config == null) {
148 log.debug("No configuration found");
149 return;
150 }
151
152 osAccess = config.openstackAccess();
153 xosAccess = config.xosAccess();
154 mgmtIpRange = config.managementIpRange();
155 }
156
157 public class InternalConfigListener implements NetworkConfigListener {
158
159 @Override
160 public void event(NetworkConfigEvent event) {
161
162 switch (event.type()) {
163 case CONFIG_UPDATED:
164 case CONFIG_ADDED:
165 if (event.configClass().equals(CordVtnConfig.class)) {
166 readConfiguration();
167 } else if (event.configClass().equals(CONFIG_CLASS)) {
168 readAccessAgentConfig();
169 }
170 break;
171 default:
172 break;
173 }
174 }
175 }
176}