blob: fdf82a00f58b54a67f0f1ff1eea5e5a3d5f3f3ff [file] [log] [blame]
alshabibf0e7e702015-05-30 18:22:36 -07001/*
2 * Copyright 2014 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.olt;
17
alshabibf0e7e702015-05-30 18:22:36 -070018import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Deactivate;
alshabibf0e7e702015-05-30 18:22:36 -070021import org.apache.felix.scr.annotations.Reference;
22import org.apache.felix.scr.annotations.ReferenceCardinality;
Jonathan Harte533a422015-10-20 17:31:24 -070023import org.apache.felix.scr.annotations.Service;
alshabibf0e7e702015-05-30 18:22:36 -070024import org.onlab.packet.VlanId;
alshabibf0e7e702015-05-30 18:22:36 -070025import org.onosproject.core.ApplicationId;
26import org.onosproject.core.CoreService;
alshabib8e4fd2f2016-01-12 15:55:53 -080027import org.onosproject.event.AbstractListenerManager;
Jonathan Harte533a422015-10-20 17:31:24 -070028import org.onosproject.net.ConnectPoint;
alshabibf0e7e702015-05-30 18:22:36 -070029import org.onosproject.net.DeviceId;
30import org.onosproject.net.PortNumber;
Jonathan Harte533a422015-10-20 17:31:24 -070031import org.onosproject.net.config.ConfigFactory;
32import org.onosproject.net.config.NetworkConfigEvent;
33import org.onosproject.net.config.NetworkConfigListener;
34import org.onosproject.net.config.NetworkConfigRegistry;
35import org.onosproject.net.config.basics.SubjectFactories;
alshabibf0e7e702015-05-30 18:22:36 -070036import org.onosproject.net.device.DeviceEvent;
37import org.onosproject.net.device.DeviceListener;
38import org.onosproject.net.device.DeviceService;
39import org.onosproject.net.flow.DefaultTrafficSelector;
40import org.onosproject.net.flow.DefaultTrafficTreatment;
41import org.onosproject.net.flow.TrafficSelector;
42import org.onosproject.net.flow.TrafficTreatment;
43import org.onosproject.net.flowobjective.DefaultForwardingObjective;
44import org.onosproject.net.flowobjective.FlowObjectiveService;
45import org.onosproject.net.flowobjective.ForwardingObjective;
alshabib3ea82642016-01-12 18:06:53 -080046import org.onosproject.net.flowobjective.Objective;
47import org.onosproject.net.flowobjective.ObjectiveContext;
48import org.onosproject.net.flowobjective.ObjectiveError;
alshabib8e4fd2f2016-01-12 15:55:53 -080049import org.onosproject.olt.api.AccessDeviceEvent;
50import org.onosproject.olt.api.AccessDeviceListener;
alshabibf0e7e702015-05-30 18:22:36 -070051import org.slf4j.Logger;
52
Jonathan Harte533a422015-10-20 17:31:24 -070053import java.util.Map;
Jonathan Hart52998382015-11-10 16:09:22 -080054import java.util.Optional;
alshabib3ea82642016-01-12 18:06:53 -080055import java.util.concurrent.CompletableFuture;
Jonathan Harte533a422015-10-20 17:31:24 -070056import java.util.concurrent.ConcurrentHashMap;
alshabib3ea82642016-01-12 18:06:53 -080057import java.util.concurrent.ExecutorService;
58import java.util.concurrent.Executors;
alshabibf0e7e702015-05-30 18:22:36 -070059
alshabib3ea82642016-01-12 18:06:53 -080060import static org.onlab.util.Tools.groupedThreads;
alshabibf0e7e702015-05-30 18:22:36 -070061import static org.slf4j.LoggerFactory.getLogger;
62
63/**
Jonathan Harte533a422015-10-20 17:31:24 -070064 * Provisions rules on access devices.
alshabibf0e7e702015-05-30 18:22:36 -070065 */
Jonathan Harte533a422015-10-20 17:31:24 -070066@Service
alshabibf0e7e702015-05-30 18:22:36 -070067@Component(immediate = true)
alshabib8e4fd2f2016-01-12 15:55:53 -080068public class Olt
69 extends AbstractListenerManager<AccessDeviceEvent, AccessDeviceListener>
70 implements AccessDeviceService {
alshabibf0e7e702015-05-30 18:22:36 -070071 private final Logger log = getLogger(getClass());
72
73 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
74 protected FlowObjectiveService flowObjectiveService;
75
76 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
77 protected DeviceService deviceService;
78
79 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
80 protected CoreService coreService;
81
Jonathan Harte533a422015-10-20 17:31:24 -070082 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
83 protected NetworkConfigRegistry networkConfig;
84
alshabibf0e7e702015-05-30 18:22:36 -070085 private final DeviceListener deviceListener = new InternalDeviceListener();
86
87 private ApplicationId appId;
88
Jonathan Harte533a422015-10-20 17:31:24 -070089 private static final VlanId DEFAULT_VLAN = VlanId.vlanId((short) 0);
alshabibcf3eb952015-06-01 10:57:31 -070090
alshabib3ea82642016-01-12 18:06:53 -080091 private ExecutorService oltInstallers = Executors.newFixedThreadPool(4,
92 groupedThreads("onos/olt-service",
93 "olt-installer-%d"));
alshabib4ae2b402015-06-05 14:55:24 -070094
Jonathan Harte533a422015-10-20 17:31:24 -070095 private Map<DeviceId, AccessDeviceData> oltData = new ConcurrentHashMap<>();
96
97 private InternalNetworkConfigListener configListener =
98 new InternalNetworkConfigListener();
99 private static final Class<AccessDeviceConfig> CONFIG_CLASS =
100 AccessDeviceConfig.class;
101
102 private ConfigFactory<DeviceId, AccessDeviceConfig> configFactory =
103 new ConfigFactory<DeviceId, AccessDeviceConfig>(
104 SubjectFactories.DEVICE_SUBJECT_FACTORY, CONFIG_CLASS, "accessDevice") {
105 @Override
106 public AccessDeviceConfig createConfig() {
107 return new AccessDeviceConfig();
108 }
109 };
alshabibf0e7e702015-05-30 18:22:36 -0700110
111 @Activate
112 public void activate() {
alshabib4ae2b402015-06-05 14:55:24 -0700113 appId = coreService.registerApplication("org.onosproject.olt");
alshabibc4dfe852015-06-05 13:35:13 -0700114
alshabib8e4fd2f2016-01-12 15:55:53 -0800115 eventDispatcher.addSink(AccessDeviceEvent.class, listenerRegistry);
116
Jonathan Harte533a422015-10-20 17:31:24 -0700117 networkConfig.registerConfigFactory(configFactory);
118 networkConfig.addListener(configListener);
119
120 networkConfig.getSubjects(DeviceId.class, AccessDeviceConfig.class).forEach(
121 subject -> {
122 AccessDeviceConfig config = networkConfig.getConfig(subject, AccessDeviceConfig.class);
123 if (config != null) {
124 AccessDeviceData data = config.getOlt();
125 oltData.put(data.deviceId(), data);
126 }
127 }
128 );
129
alshabibf0e7e702015-05-30 18:22:36 -0700130 log.info("Started with Application ID {}", appId.id());
131 }
132
133 @Deactivate
134 public void deactivate() {
Jonathan Harte533a422015-10-20 17:31:24 -0700135 networkConfig.removeListener(configListener);
136 networkConfig.unregisterConfigFactory(configFactory);
alshabibf0e7e702015-05-30 18:22:36 -0700137 log.info("Stopped");
138 }
139
Jonathan Harte533a422015-10-20 17:31:24 -0700140 @Override
141 public void provisionSubscriber(ConnectPoint port, VlanId vlan) {
142 AccessDeviceData olt = oltData.get(port.deviceId());
143
144 if (olt == null) {
145 log.warn("No data found for OLT device {}", port.deviceId());
146 return;
147 }
148
Jonathan Hart52998382015-11-10 16:09:22 -0800149 provisionVlans(olt.deviceId(), olt.uplink(), port.port(), vlan, olt.vlan(),
alshabibb7a9e172016-01-13 11:23:53 -0800150 olt.defaultVlan());
151 }
152
153 @Override
154 public void removeSubscriber(ConnectPoint port) {
155 throw new UnsupportedOperationException();
156
Jonathan Harte533a422015-10-20 17:31:24 -0700157 }
158
159 private void provisionVlans(DeviceId deviceId, PortNumber uplinkPort,
160 PortNumber subscriberPort,
Jonathan Hart52998382015-11-10 16:09:22 -0800161 VlanId subscriberVlan, VlanId deviceVlan,
162 Optional<VlanId> defaultVlan) {
Jonathan Harte533a422015-10-20 17:31:24 -0700163
alshabib3ea82642016-01-12 18:06:53 -0800164 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture();
165 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture();
166
Jonathan Harte533a422015-10-20 17:31:24 -0700167 TrafficSelector upstream = DefaultTrafficSelector.builder()
Jonathan Hart52998382015-11-10 16:09:22 -0800168 .matchVlanId((defaultVlan.isPresent()) ? defaultVlan.get() : DEFAULT_VLAN)
Jonathan Harte533a422015-10-20 17:31:24 -0700169 .matchInPort(subscriberPort)
170 .build();
171
172 TrafficSelector downstream = DefaultTrafficSelector.builder()
173 .matchVlanId(deviceVlan)
174 .matchInPort(uplinkPort)
alshabibb7a9e172016-01-13 11:23:53 -0800175 .matchInnerVlanId(subscriberVlan)
Jonathan Harte533a422015-10-20 17:31:24 -0700176 .build();
177
178 TrafficTreatment upstreamTreatment = DefaultTrafficTreatment.builder()
alshabib3ea82642016-01-12 18:06:53 -0800179 .pushVlan()
Jonathan Harte533a422015-10-20 17:31:24 -0700180 .setVlanId(subscriberVlan)
181 .pushVlan()
182 .setVlanId(deviceVlan)
183 .setOutput(uplinkPort)
184 .build();
185
186 TrafficTreatment downstreamTreatment = DefaultTrafficTreatment.builder()
187 .popVlan()
Jonathan Hart52998382015-11-10 16:09:22 -0800188 .setVlanId((defaultVlan.isPresent()) ? defaultVlan.get() : DEFAULT_VLAN)
Jonathan Harte533a422015-10-20 17:31:24 -0700189 .setOutput(subscriberPort)
190 .build();
191
192
193 ForwardingObjective upFwd = DefaultForwardingObjective.builder()
194 .withFlag(ForwardingObjective.Flag.VERSATILE)
195 .withPriority(1000)
196 .makePermanent()
197 .withSelector(upstream)
198 .fromApp(appId)
199 .withTreatment(upstreamTreatment)
alshabib3ea82642016-01-12 18:06:53 -0800200 .add(new ObjectiveContext() {
201 @Override
202 public void onSuccess(Objective objective) {
203 upFuture.complete(null);
204 }
205
206 @Override
207 public void onError(Objective objective, ObjectiveError error) {
208 upFuture.complete(error);
209 }
210 });
Jonathan Harte533a422015-10-20 17:31:24 -0700211
212 ForwardingObjective downFwd = DefaultForwardingObjective.builder()
213 .withFlag(ForwardingObjective.Flag.VERSATILE)
214 .withPriority(1000)
215 .makePermanent()
216 .withSelector(downstream)
217 .fromApp(appId)
218 .withTreatment(downstreamTreatment)
alshabib3ea82642016-01-12 18:06:53 -0800219 .add(new ObjectiveContext() {
220 @Override
221 public void onSuccess(Objective objective) {
222 downFuture.complete(null);
223 }
224
225 @Override
226 public void onError(Objective objective, ObjectiveError error) {
227 downFuture.complete(error);
228 }
229 });
Jonathan Harte533a422015-10-20 17:31:24 -0700230
231 flowObjectiveService.forward(deviceId, upFwd);
232 flowObjectiveService.forward(deviceId, downFwd);
alshabib3ea82642016-01-12 18:06:53 -0800233
234 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
235 if (upStatus == null && downStatus == null) {
236 post(new AccessDeviceEvent(AccessDeviceEvent.Type.SUBSCRIBER_REGISTERED,
237 deviceId,
238 deviceVlan,
239 subscriberVlan));
240 } else if (downStatus != null) {
241 log.error("Subscriber with vlan {} on device {} " +
242 "on port {} failed downstream installation: {}",
243 subscriberVlan, deviceId, subscriberPort, downStatus);
244 } else if (upStatus != null) {
245 log.error("Subscriber with vlan {} on device {} " +
246 "on port {} failed upstream installation: {}",
247 subscriberVlan, deviceId, subscriberPort, upStatus);
248 }
249 }, oltInstallers);
250
Jonathan Harte533a422015-10-20 17:31:24 -0700251 }
252
alshabibf0e7e702015-05-30 18:22:36 -0700253 private class InternalDeviceListener implements DeviceListener {
254 @Override
255 public void event(DeviceEvent event) {
alshabib3ea82642016-01-12 18:06:53 -0800256 DeviceId devId = event.subject().id();
257 if (!oltData.containsKey(devId)) {
258 log.debug("Device {} is not an OLT", devId);
alshabib8e4fd2f2016-01-12 15:55:53 -0800259 return;
260 }
alshabibf0e7e702015-05-30 18:22:36 -0700261 switch (event.type()) {
262 case PORT_ADDED:
263 case PORT_UPDATED:
alshabibf0e7e702015-05-30 18:22:36 -0700264 break;
265 case DEVICE_ADDED:
alshabib8e4fd2f2016-01-12 15:55:53 -0800266 post(new AccessDeviceEvent(
267 AccessDeviceEvent.Type.DEVICE_CONNECTED, devId,
268 null, null));
269 break;
alshabibf0e7e702015-05-30 18:22:36 -0700270 case DEVICE_REMOVED:
alshabib8e4fd2f2016-01-12 15:55:53 -0800271 post(new AccessDeviceEvent(
272 AccessDeviceEvent.Type.DEVICE_DISCONNECTED, devId,
273 null, null));
274 break;
275 case DEVICE_UPDATED:
alshabibf0e7e702015-05-30 18:22:36 -0700276 case DEVICE_SUSPENDED:
277 case DEVICE_AVAILABILITY_CHANGED:
278 case PORT_REMOVED:
279 case PORT_STATS_UPDATED:
280 default:
281 return;
282 }
283 }
284 }
285
Jonathan Harte533a422015-10-20 17:31:24 -0700286 private class InternalNetworkConfigListener implements NetworkConfigListener {
287 @Override
288 public void event(NetworkConfigEvent event) {
289 switch (event.type()) {
290
291 case CONFIG_ADDED:
292 case CONFIG_UPDATED:
293 if (event.configClass().equals(CONFIG_CLASS)) {
294 AccessDeviceConfig config =
295 networkConfig.getConfig((DeviceId) event.subject(), CONFIG_CLASS);
296 if (config != null) {
297 oltData.put(config.getOlt().deviceId(), config.getOlt());
298 }
299 }
300 break;
301 case CONFIG_UNREGISTERED:
302 case CONFIG_REMOVED:
303 default:
304 break;
305 }
306 }
307 }
alshabibf0e7e702015-05-30 18:22:36 -0700308
309}