blob: d5d7d277f4071386f5e653b69324a6a7db1b3615 [file] [log] [blame]
alshabib0ccde6d2015-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
alshabib0ccde6d2015-05-30 18:22:36 -070018import com.google.common.base.Strings;
19import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Modified;
23import org.apache.felix.scr.annotations.Property;
24import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
Jonathan Hart3e594642015-10-20 17:31:24 -070026import org.apache.felix.scr.annotations.Service;
alshabib0ccde6d2015-05-30 18:22:36 -070027import org.onlab.packet.VlanId;
28import org.onlab.util.Tools;
29import org.onosproject.core.ApplicationId;
30import org.onosproject.core.CoreService;
Jonathan Hart3e594642015-10-20 17:31:24 -070031import org.onosproject.net.ConnectPoint;
alshabib0ccde6d2015-05-30 18:22:36 -070032import org.onosproject.net.DeviceId;
Sho SHIMIZU00c9cfa2015-09-02 18:38:12 -070033import org.onosproject.net.Port;
alshabib0ccde6d2015-05-30 18:22:36 -070034import org.onosproject.net.PortNumber;
Jonathan Hart3e594642015-10-20 17:31:24 -070035import org.onosproject.net.config.ConfigFactory;
36import org.onosproject.net.config.NetworkConfigEvent;
37import org.onosproject.net.config.NetworkConfigListener;
38import org.onosproject.net.config.NetworkConfigRegistry;
39import org.onosproject.net.config.basics.SubjectFactories;
alshabib0ccde6d2015-05-30 18:22:36 -070040import org.onosproject.net.device.DeviceEvent;
41import org.onosproject.net.device.DeviceListener;
42import org.onosproject.net.device.DeviceService;
43import org.onosproject.net.flow.DefaultTrafficSelector;
44import org.onosproject.net.flow.DefaultTrafficTreatment;
45import org.onosproject.net.flow.TrafficSelector;
46import org.onosproject.net.flow.TrafficTreatment;
47import org.onosproject.net.flowobjective.DefaultForwardingObjective;
48import org.onosproject.net.flowobjective.FlowObjectiveService;
49import org.onosproject.net.flowobjective.ForwardingObjective;
50import org.osgi.service.component.ComponentContext;
51import org.slf4j.Logger;
52
53import java.util.Dictionary;
Jonathan Hart3e594642015-10-20 17:31:24 -070054import java.util.Map;
Jonathan Hart48327842015-11-10 16:09:22 -080055import java.util.Optional;
Jonathan Hart3e594642015-10-20 17:31:24 -070056import java.util.concurrent.ConcurrentHashMap;
alshabib0ccde6d2015-05-30 18:22:36 -070057
58import static org.slf4j.LoggerFactory.getLogger;
59
60/**
Jonathan Hart3e594642015-10-20 17:31:24 -070061 * Provisions rules on access devices.
alshabib0ccde6d2015-05-30 18:22:36 -070062 */
Jonathan Hart3e594642015-10-20 17:31:24 -070063@Service
alshabib0ccde6d2015-05-30 18:22:36 -070064@Component(immediate = true)
Jonathan Hart3e594642015-10-20 17:31:24 -070065public class OLT implements AccessDeviceService {
alshabib0ccde6d2015-05-30 18:22:36 -070066 private final Logger log = getLogger(getClass());
67
68 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
69 protected FlowObjectiveService flowObjectiveService;
70
71 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
72 protected DeviceService deviceService;
73
74 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
75 protected CoreService coreService;
76
Jonathan Hart3e594642015-10-20 17:31:24 -070077 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
78 protected NetworkConfigRegistry networkConfig;
79
alshabib0ccde6d2015-05-30 18:22:36 -070080 private final DeviceListener deviceListener = new InternalDeviceListener();
81
82 private ApplicationId appId;
83
Jonathan Hart3e594642015-10-20 17:31:24 -070084 private static final VlanId DEFAULT_VLAN = VlanId.vlanId((short) 0);
alshabib17ff25f2015-06-01 10:57:31 -070085 public static final int OFFSET = 200;
86
alshabib0ccde6d2015-05-30 18:22:36 -070087 public static final int UPLINK_PORT = 129;
alshabibb37fd082015-06-05 15:32:15 -070088 public static final int GFAST_UPLINK_PORT = 100;
alshabib0ccde6d2015-05-30 18:22:36 -070089
90 public static final String OLT_DEVICE = "of:90e2ba82f97791e9";
alshabib1af3b712015-06-05 14:55:24 -070091 public static final String GFAST_DEVICE = "of:0011223344551357";
alshabib0ccde6d2015-05-30 18:22:36 -070092
93 @Property(name = "uplinkPort", intValue = UPLINK_PORT,
94 label = "The OLT's uplink port number")
95 private int uplinkPort = UPLINK_PORT;
96
alshabibb37fd082015-06-05 15:32:15 -070097 @Property(name = "gfastUplink", intValue = GFAST_UPLINK_PORT,
98 label = "The OLT's uplink port number")
99 private int gfastUplink = GFAST_UPLINK_PORT;
100
alshabib0ccde6d2015-05-30 18:22:36 -0700101 //TODO: replace this with an annotation lookup
102 @Property(name = "oltDevice", value = OLT_DEVICE,
103 label = "The OLT device id")
104 private String oltDevice = OLT_DEVICE;
105
alshabib1af3b712015-06-05 14:55:24 -0700106 @Property(name = "gfastDevice", value = GFAST_DEVICE,
107 label = "The gfast device id")
108 private String gfastDevice = GFAST_DEVICE;
109
Jonathan Hart3e594642015-10-20 17:31:24 -0700110 private Map<DeviceId, AccessDeviceData> oltData = new ConcurrentHashMap<>();
111
112 private InternalNetworkConfigListener configListener =
113 new InternalNetworkConfigListener();
114 private static final Class<AccessDeviceConfig> CONFIG_CLASS =
115 AccessDeviceConfig.class;
116
117 private ConfigFactory<DeviceId, AccessDeviceConfig> configFactory =
118 new ConfigFactory<DeviceId, AccessDeviceConfig>(
119 SubjectFactories.DEVICE_SUBJECT_FACTORY, CONFIG_CLASS, "accessDevice") {
120 @Override
121 public AccessDeviceConfig createConfig() {
122 return new AccessDeviceConfig();
123 }
124 };
alshabib0ccde6d2015-05-30 18:22:36 -0700125
126 @Activate
127 public void activate() {
alshabib1af3b712015-06-05 14:55:24 -0700128 appId = coreService.registerApplication("org.onosproject.olt");
alshabibbeb2dc92015-06-05 13:35:13 -0700129
Jonathan Hart3e594642015-10-20 17:31:24 -0700130 networkConfig.registerConfigFactory(configFactory);
131 networkConfig.addListener(configListener);
132
133 networkConfig.getSubjects(DeviceId.class, AccessDeviceConfig.class).forEach(
134 subject -> {
135 AccessDeviceConfig config = networkConfig.getConfig(subject, AccessDeviceConfig.class);
136 if (config != null) {
137 AccessDeviceData data = config.getOlt();
138 oltData.put(data.deviceId(), data);
139 }
140 }
141 );
142
Jonathan Hartdfc3b862015-07-01 14:49:56 -0700143 /*deviceService.addListener(deviceListener);
Jonathan Hart80ba4a72015-06-10 18:19:41 -0700144
alshabib0ccde6d2015-05-30 18:22:36 -0700145 deviceService.getPorts(DeviceId.deviceId(oltDevice)).stream().forEach(
146 port -> {
alshabibca627502015-06-05 15:19:43 -0700147 if (!port.number().isLogical() && port.isEnabled()) {
alshabibbeb2dc92015-06-05 13:35:13 -0700148 short vlanId = fetchVlanId(port.number());
alshabibca627502015-06-05 15:19:43 -0700149 if (vlanId > 0) {
alshabibb37fd082015-06-05 15:32:15 -0700150 provisionVlanOnPort(oltDevice, uplinkPort, port.number(), (short) 7);
151 provisionVlanOnPort(oltDevice, uplinkPort, port.number(), vlanId);
alshabibca627502015-06-05 15:19:43 -0700152 }
alshabib0ccde6d2015-05-30 18:22:36 -0700153 }
154 }
Jonathan Hartdfc3b862015-07-01 14:49:56 -0700155 );*/
alshabib1af3b712015-06-05 14:55:24 -0700156
157
Sho SHIMIZU00c9cfa2015-09-02 18:38:12 -0700158 deviceService.getPorts(DeviceId.deviceId(gfastDevice)).stream()
159 .filter(port -> !port.number().isLogical())
160 .filter(Port::isEnabled)
161 .forEach(port -> {
162 short vlanId = (short) (fetchVlanId(port.number()) + OFFSET);
163 if (vlanId > 0) {
164 provisionVlanOnPort(gfastDevice, gfastUplink, port.number(), vlanId);
165 }
alshabibca627502015-06-05 15:19:43 -0700166 }
Sho SHIMIZU00c9cfa2015-09-02 18:38:12 -0700167 );
alshabib0ccde6d2015-05-30 18:22:36 -0700168 log.info("Started with Application ID {}", appId.id());
169 }
170
171 @Deactivate
172 public void deactivate() {
Jonathan Hart3e594642015-10-20 17:31:24 -0700173 networkConfig.removeListener(configListener);
174 networkConfig.unregisterConfigFactory(configFactory);
alshabib0ccde6d2015-05-30 18:22:36 -0700175 log.info("Stopped");
176 }
177
178 @Modified
179 public void modified(ComponentContext context) {
180 Dictionary<?, ?> properties = context.getProperties();
181
alshabib0ccde6d2015-05-30 18:22:36 -0700182 String s = Tools.get(properties, "uplinkPort");
183 uplinkPort = Strings.isNullOrEmpty(s) ? UPLINK_PORT : Integer.parseInt(s);
184
185 s = Tools.get(properties, "oltDevice");
186 oltDevice = Strings.isNullOrEmpty(s) ? OLT_DEVICE : s;
alshabib0ccde6d2015-05-30 18:22:36 -0700187 }
188
alshabibbeb2dc92015-06-05 13:35:13 -0700189 private short fetchVlanId(PortNumber port) {
190 long p = port.toLong() + OFFSET;
191 if (p > 4095) {
alshabib0ccde6d2015-05-30 18:22:36 -0700192 log.warn("Port Number {} exceeds vlan max", port);
alshabibbeb2dc92015-06-05 13:35:13 -0700193 return -1;
alshabib0ccde6d2015-05-30 18:22:36 -0700194 }
alshabibbeb2dc92015-06-05 13:35:13 -0700195 return (short) p;
196 }
197
alshabibb37fd082015-06-05 15:32:15 -0700198 private void provisionVlanOnPort(String deviceId, int uplinkPort, PortNumber p, short vlanId) {
alshabibca627502015-06-05 15:19:43 -0700199 DeviceId did = DeviceId.deviceId(deviceId);
alshabib0ccde6d2015-05-30 18:22:36 -0700200
201 TrafficSelector upstream = DefaultTrafficSelector.builder()
alshabibbeb2dc92015-06-05 13:35:13 -0700202 .matchVlanId(VlanId.vlanId(vlanId))
alshabib0ccde6d2015-05-30 18:22:36 -0700203 .matchInPort(p)
204 .build();
205
206 TrafficSelector downStream = DefaultTrafficSelector.builder()
alshabibbeb2dc92015-06-05 13:35:13 -0700207 .matchVlanId(VlanId.vlanId(vlanId))
alshabib0ccde6d2015-05-30 18:22:36 -0700208 .matchInPort(PortNumber.portNumber(uplinkPort))
209 .build();
210
211 TrafficTreatment upstreamTreatment = DefaultTrafficTreatment.builder()
212 .setOutput(PortNumber.portNumber(uplinkPort))
213 .build();
214
215 TrafficTreatment downStreamTreatment = DefaultTrafficTreatment.builder()
216 .setOutput(p)
217 .build();
218
219
220 ForwardingObjective upFwd = DefaultForwardingObjective.builder()
221 .withFlag(ForwardingObjective.Flag.VERSATILE)
222 .withPriority(1000)
223 .makePermanent()
224 .withSelector(upstream)
225 .fromApp(appId)
226 .withTreatment(upstreamTreatment)
227 .add();
228
229 ForwardingObjective downFwd = DefaultForwardingObjective.builder()
230 .withFlag(ForwardingObjective.Flag.VERSATILE)
231 .withPriority(1000)
232 .makePermanent()
233 .withSelector(downStream)
234 .fromApp(appId)
235 .withTreatment(downStreamTreatment)
236 .add();
237
alshabibca627502015-06-05 15:19:43 -0700238 flowObjectiveService.forward(did, upFwd);
239 flowObjectiveService.forward(did, downFwd);
Jonathan Hart3e594642015-10-20 17:31:24 -0700240 }
alshabib0ccde6d2015-05-30 18:22:36 -0700241
Jonathan Hart3e594642015-10-20 17:31:24 -0700242 @Override
243 public void provisionSubscriber(ConnectPoint port, VlanId vlan) {
244 AccessDeviceData olt = oltData.get(port.deviceId());
245
246 if (olt == null) {
247 log.warn("No data found for OLT device {}", port.deviceId());
248 return;
249 }
250
Jonathan Hart48327842015-11-10 16:09:22 -0800251 provisionVlans(olt.deviceId(), olt.uplink(), port.port(), vlan, olt.vlan(),
252 olt.defaultVlan());
Jonathan Hart3e594642015-10-20 17:31:24 -0700253 }
254
255 private void provisionVlans(DeviceId deviceId, PortNumber uplinkPort,
256 PortNumber subscriberPort,
Jonathan Hart48327842015-11-10 16:09:22 -0800257 VlanId subscriberVlan, VlanId deviceVlan,
258 Optional<VlanId> defaultVlan) {
Jonathan Hart3e594642015-10-20 17:31:24 -0700259
260 TrafficSelector upstream = DefaultTrafficSelector.builder()
Jonathan Hart48327842015-11-10 16:09:22 -0800261 .matchVlanId((defaultVlan.isPresent()) ? defaultVlan.get() : DEFAULT_VLAN)
Jonathan Hart3e594642015-10-20 17:31:24 -0700262 .matchInPort(subscriberPort)
263 .build();
264
265 TrafficSelector downstream = DefaultTrafficSelector.builder()
266 .matchVlanId(deviceVlan)
267 .matchInPort(uplinkPort)
268 .build();
269
270 TrafficTreatment upstreamTreatment = DefaultTrafficTreatment.builder()
271 .setVlanId(subscriberVlan)
272 .pushVlan()
273 .setVlanId(deviceVlan)
274 .setOutput(uplinkPort)
275 .build();
276
277 TrafficTreatment downstreamTreatment = DefaultTrafficTreatment.builder()
278 .popVlan()
Jonathan Hart48327842015-11-10 16:09:22 -0800279 .setVlanId((defaultVlan.isPresent()) ? defaultVlan.get() : DEFAULT_VLAN)
Jonathan Hart3e594642015-10-20 17:31:24 -0700280 .setOutput(subscriberPort)
281 .build();
282
283
284 ForwardingObjective upFwd = DefaultForwardingObjective.builder()
285 .withFlag(ForwardingObjective.Flag.VERSATILE)
286 .withPriority(1000)
287 .makePermanent()
288 .withSelector(upstream)
289 .fromApp(appId)
290 .withTreatment(upstreamTreatment)
291 .add();
292
293 ForwardingObjective downFwd = DefaultForwardingObjective.builder()
294 .withFlag(ForwardingObjective.Flag.VERSATILE)
295 .withPriority(1000)
296 .makePermanent()
297 .withSelector(downstream)
298 .fromApp(appId)
299 .withTreatment(downstreamTreatment)
300 .add();
301
302 flowObjectiveService.forward(deviceId, upFwd);
303 flowObjectiveService.forward(deviceId, downFwd);
304 }
305
306 @Override
307 public void removeSubscriber(ConnectPoint port) {
308 throw new UnsupportedOperationException("Not yet implemented");
alshabib0ccde6d2015-05-30 18:22:36 -0700309 }
310
311 private class InternalDeviceListener implements DeviceListener {
312 @Override
313 public void event(DeviceEvent event) {
314 DeviceId devId = DeviceId.deviceId(oltDevice);
315 switch (event.type()) {
316 case PORT_ADDED:
317 case PORT_UPDATED:
318 if (devId.equals(event.subject().id()) && event.port().isEnabled()) {
alshabibbeb2dc92015-06-05 13:35:13 -0700319 short vlanId = fetchVlanId(event.port().number());
alshabibb37fd082015-06-05 15:32:15 -0700320 provisionVlanOnPort(gfastDevice, uplinkPort, event.port().number(), vlanId);
alshabib0ccde6d2015-05-30 18:22:36 -0700321 }
322 break;
323 case DEVICE_ADDED:
324 case DEVICE_UPDATED:
325 case DEVICE_REMOVED:
326 case DEVICE_SUSPENDED:
327 case DEVICE_AVAILABILITY_CHANGED:
328 case PORT_REMOVED:
329 case PORT_STATS_UPDATED:
330 default:
331 return;
332 }
333 }
334 }
335
Jonathan Hart3e594642015-10-20 17:31:24 -0700336 private class InternalNetworkConfigListener implements NetworkConfigListener {
337 @Override
338 public void event(NetworkConfigEvent event) {
339 switch (event.type()) {
340
341 case CONFIG_ADDED:
342 case CONFIG_UPDATED:
343 if (event.configClass().equals(CONFIG_CLASS)) {
344 AccessDeviceConfig config =
345 networkConfig.getConfig((DeviceId) event.subject(), CONFIG_CLASS);
346 if (config != null) {
347 oltData.put(config.getOlt().deviceId(), config.getOlt());
348 }
349 }
350 break;
351 case CONFIG_UNREGISTERED:
352 case CONFIG_REMOVED:
353 default:
354 break;
355 }
356 }
357 }
alshabib0ccde6d2015-05-30 18:22:36 -0700358
359}