blob: bdff1fa0554f590c81963a198206a54664aca69c [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(),
150 olt.defaultVlan());
Jonathan Harte533a422015-10-20 17:31:24 -0700151 }
152
153 private void provisionVlans(DeviceId deviceId, PortNumber uplinkPort,
154 PortNumber subscriberPort,
Jonathan Hart52998382015-11-10 16:09:22 -0800155 VlanId subscriberVlan, VlanId deviceVlan,
156 Optional<VlanId> defaultVlan) {
Jonathan Harte533a422015-10-20 17:31:24 -0700157
alshabib3ea82642016-01-12 18:06:53 -0800158 CompletableFuture<ObjectiveError> downFuture = new CompletableFuture();
159 CompletableFuture<ObjectiveError> upFuture = new CompletableFuture();
160
Jonathan Harte533a422015-10-20 17:31:24 -0700161 TrafficSelector upstream = DefaultTrafficSelector.builder()
Jonathan Hart52998382015-11-10 16:09:22 -0800162 .matchVlanId((defaultVlan.isPresent()) ? defaultVlan.get() : DEFAULT_VLAN)
Jonathan Harte533a422015-10-20 17:31:24 -0700163 .matchInPort(subscriberPort)
164 .build();
165
166 TrafficSelector downstream = DefaultTrafficSelector.builder()
167 .matchVlanId(deviceVlan)
168 .matchInPort(uplinkPort)
169 .build();
170
171 TrafficTreatment upstreamTreatment = DefaultTrafficTreatment.builder()
alshabib3ea82642016-01-12 18:06:53 -0800172 .pushVlan()
Jonathan Harte533a422015-10-20 17:31:24 -0700173 .setVlanId(subscriberVlan)
174 .pushVlan()
175 .setVlanId(deviceVlan)
176 .setOutput(uplinkPort)
177 .build();
178
179 TrafficTreatment downstreamTreatment = DefaultTrafficTreatment.builder()
180 .popVlan()
Jonathan Hart52998382015-11-10 16:09:22 -0800181 .setVlanId((defaultVlan.isPresent()) ? defaultVlan.get() : DEFAULT_VLAN)
Jonathan Harte533a422015-10-20 17:31:24 -0700182 .setOutput(subscriberPort)
183 .build();
184
185
186 ForwardingObjective upFwd = DefaultForwardingObjective.builder()
187 .withFlag(ForwardingObjective.Flag.VERSATILE)
188 .withPriority(1000)
189 .makePermanent()
190 .withSelector(upstream)
191 .fromApp(appId)
192 .withTreatment(upstreamTreatment)
alshabib3ea82642016-01-12 18:06:53 -0800193 .add(new ObjectiveContext() {
194 @Override
195 public void onSuccess(Objective objective) {
196 upFuture.complete(null);
197 }
198
199 @Override
200 public void onError(Objective objective, ObjectiveError error) {
201 upFuture.complete(error);
202 }
203 });
Jonathan Harte533a422015-10-20 17:31:24 -0700204
205 ForwardingObjective downFwd = DefaultForwardingObjective.builder()
206 .withFlag(ForwardingObjective.Flag.VERSATILE)
207 .withPriority(1000)
208 .makePermanent()
209 .withSelector(downstream)
210 .fromApp(appId)
211 .withTreatment(downstreamTreatment)
alshabib3ea82642016-01-12 18:06:53 -0800212 .add(new ObjectiveContext() {
213 @Override
214 public void onSuccess(Objective objective) {
215 downFuture.complete(null);
216 }
217
218 @Override
219 public void onError(Objective objective, ObjectiveError error) {
220 downFuture.complete(error);
221 }
222 });
Jonathan Harte533a422015-10-20 17:31:24 -0700223
224 flowObjectiveService.forward(deviceId, upFwd);
225 flowObjectiveService.forward(deviceId, downFwd);
alshabib3ea82642016-01-12 18:06:53 -0800226
227 upFuture.thenAcceptBothAsync(downFuture, (upStatus, downStatus) -> {
228 if (upStatus == null && downStatus == null) {
229 post(new AccessDeviceEvent(AccessDeviceEvent.Type.SUBSCRIBER_REGISTERED,
230 deviceId,
231 deviceVlan,
232 subscriberVlan));
233 } else if (downStatus != null) {
234 log.error("Subscriber with vlan {} on device {} " +
235 "on port {} failed downstream installation: {}",
236 subscriberVlan, deviceId, subscriberPort, downStatus);
237 } else if (upStatus != null) {
238 log.error("Subscriber with vlan {} on device {} " +
239 "on port {} failed upstream installation: {}",
240 subscriberVlan, deviceId, subscriberPort, upStatus);
241 }
242 }, oltInstallers);
243
Jonathan Harte533a422015-10-20 17:31:24 -0700244 }
245
246 @Override
247 public void removeSubscriber(ConnectPoint port) {
248 throw new UnsupportedOperationException("Not yet implemented");
alshabibf0e7e702015-05-30 18:22:36 -0700249 }
250
251 private class InternalDeviceListener implements DeviceListener {
252 @Override
253 public void event(DeviceEvent event) {
alshabib3ea82642016-01-12 18:06:53 -0800254 DeviceId devId = event.subject().id();
255 if (!oltData.containsKey(devId)) {
256 log.debug("Device {} is not an OLT", devId);
alshabib8e4fd2f2016-01-12 15:55:53 -0800257 return;
258 }
alshabibf0e7e702015-05-30 18:22:36 -0700259 switch (event.type()) {
260 case PORT_ADDED:
261 case PORT_UPDATED:
alshabibf0e7e702015-05-30 18:22:36 -0700262 break;
263 case DEVICE_ADDED:
alshabib8e4fd2f2016-01-12 15:55:53 -0800264 post(new AccessDeviceEvent(
265 AccessDeviceEvent.Type.DEVICE_CONNECTED, devId,
266 null, null));
267 break;
alshabibf0e7e702015-05-30 18:22:36 -0700268 case DEVICE_REMOVED:
alshabib8e4fd2f2016-01-12 15:55:53 -0800269 post(new AccessDeviceEvent(
270 AccessDeviceEvent.Type.DEVICE_DISCONNECTED, devId,
271 null, null));
272 break;
273 case DEVICE_UPDATED:
alshabibf0e7e702015-05-30 18:22:36 -0700274 case DEVICE_SUSPENDED:
275 case DEVICE_AVAILABILITY_CHANGED:
276 case PORT_REMOVED:
277 case PORT_STATS_UPDATED:
278 default:
279 return;
280 }
281 }
282 }
283
Jonathan Harte533a422015-10-20 17:31:24 -0700284 private class InternalNetworkConfigListener implements NetworkConfigListener {
285 @Override
286 public void event(NetworkConfigEvent event) {
287 switch (event.type()) {
288
289 case CONFIG_ADDED:
290 case CONFIG_UPDATED:
291 if (event.configClass().equals(CONFIG_CLASS)) {
292 AccessDeviceConfig config =
293 networkConfig.getConfig((DeviceId) event.subject(), CONFIG_CLASS);
294 if (config != null) {
295 oltData.put(config.getOlt().deviceId(), config.getOlt());
296 }
297 }
298 break;
299 case CONFIG_UNREGISTERED:
300 case CONFIG_REMOVED:
301 default:
302 break;
303 }
304 }
305 }
alshabibf0e7e702015-05-30 18:22:36 -0700306
307}