blob: 7d9780ee26f5848bc004b318a64e39c3c3d61385 [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
18
19import com.google.common.base.Strings;
20import org.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Deactivate;
23import org.apache.felix.scr.annotations.Modified;
24import org.apache.felix.scr.annotations.Property;
25import org.apache.felix.scr.annotations.Reference;
26import org.apache.felix.scr.annotations.ReferenceCardinality;
27import org.onlab.packet.VlanId;
28import org.onlab.util.Tools;
29import org.onosproject.core.ApplicationId;
30import org.onosproject.core.CoreService;
31import org.onosproject.net.DeviceId;
32import org.onosproject.net.PortNumber;
33import org.onosproject.net.device.DeviceEvent;
34import org.onosproject.net.device.DeviceListener;
35import org.onosproject.net.device.DeviceService;
36import org.onosproject.net.flow.DefaultTrafficSelector;
37import org.onosproject.net.flow.DefaultTrafficTreatment;
38import org.onosproject.net.flow.TrafficSelector;
39import org.onosproject.net.flow.TrafficTreatment;
40import org.onosproject.net.flowobjective.DefaultForwardingObjective;
41import org.onosproject.net.flowobjective.FlowObjectiveService;
42import org.onosproject.net.flowobjective.ForwardingObjective;
43import org.osgi.service.component.ComponentContext;
44import org.slf4j.Logger;
45
46import java.util.Dictionary;
47
48import static org.slf4j.LoggerFactory.getLogger;
49
50/**
51 * Sample mobility application. Cleans up flowmods when a host moves.
52 */
53@Component(immediate = true)
54public class OLT {
55
56 private final Logger log = getLogger(getClass());
57
58 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
59 protected FlowObjectiveService flowObjectiveService;
60
61 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
62 protected DeviceService deviceService;
63
64 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
65 protected CoreService coreService;
66
67 private final DeviceListener deviceListener = new InternalDeviceListener();
68
69 private ApplicationId appId;
70
alshabibcf3eb952015-06-01 10:57:31 -070071 public static final int OFFSET = 200;
72
alshabibf0e7e702015-05-30 18:22:36 -070073 public static final int UPLINK_PORT = 129;
74
75 public static final String OLT_DEVICE = "of:90e2ba82f97791e9";
alshabib4ae2b402015-06-05 14:55:24 -070076 public static final String GFAST_DEVICE = "of:0011223344551357";
alshabibf0e7e702015-05-30 18:22:36 -070077
78 @Property(name = "uplinkPort", intValue = UPLINK_PORT,
79 label = "The OLT's uplink port number")
80 private int uplinkPort = UPLINK_PORT;
81
82 //TODO: replace this with an annotation lookup
83 @Property(name = "oltDevice", value = OLT_DEVICE,
84 label = "The OLT device id")
85 private String oltDevice = OLT_DEVICE;
86
alshabib4ae2b402015-06-05 14:55:24 -070087 @Property(name = "gfastDevice", value = GFAST_DEVICE,
88 label = "The gfast device id")
89 private String gfastDevice = GFAST_DEVICE;
90
alshabibf0e7e702015-05-30 18:22:36 -070091
92 @Activate
93 public void activate() {
alshabib4ae2b402015-06-05 14:55:24 -070094 appId = coreService.registerApplication("org.onosproject.olt");
alshabibc4dfe852015-06-05 13:35:13 -070095
alshabibf0e7e702015-05-30 18:22:36 -070096 deviceService.getPorts(DeviceId.deviceId(oltDevice)).stream().forEach(
97 port -> {
alshabibde5538f92015-06-05 15:19:43 -070098 if (!port.number().isLogical() && port.isEnabled()) {
alshabibc4dfe852015-06-05 13:35:13 -070099 short vlanId = fetchVlanId(port.number());
alshabibde5538f92015-06-05 15:19:43 -0700100 if (vlanId > 0) {
101 provisionVlanOnPort(oltDevice, port.number(), (short) 7);
102 provisionVlanOnPort(oltDevice, port.number(), vlanId);
103 }
alshabibf0e7e702015-05-30 18:22:36 -0700104 }
105 }
106 );
alshabib4ae2b402015-06-05 14:55:24 -0700107
108
109 deviceService.getPorts(DeviceId.deviceId(gfastDevice)).stream().forEach(
110 port -> {
alshabibde5538f92015-06-05 15:19:43 -0700111 if (!port.number().isLogical() && port.isEnabled()) {
alshabib4ae2b402015-06-05 14:55:24 -0700112 short vlanId = (short) (fetchVlanId(port.number()) + OFFSET);
alshabibde5538f92015-06-05 15:19:43 -0700113 if (vlanId > 0) {
114 provisionVlanOnPort(gfastDevice, port.number(), vlanId);
115 }
alshabib4ae2b402015-06-05 14:55:24 -0700116 }
117 }
118 );
alshabibf0e7e702015-05-30 18:22:36 -0700119 log.info("Started with Application ID {}", appId.id());
120 }
121
122 @Deactivate
123 public void deactivate() {
124 log.info("Stopped");
125 }
126
127 @Modified
128 public void modified(ComponentContext context) {
129 Dictionary<?, ?> properties = context.getProperties();
130
131
132 String s = Tools.get(properties, "uplinkPort");
133 uplinkPort = Strings.isNullOrEmpty(s) ? UPLINK_PORT : Integer.parseInt(s);
134
135 s = Tools.get(properties, "oltDevice");
136 oltDevice = Strings.isNullOrEmpty(s) ? OLT_DEVICE : s;
137
138 }
139
alshabibc4dfe852015-06-05 13:35:13 -0700140
141 private short fetchVlanId(PortNumber port) {
142 long p = port.toLong() + OFFSET;
143 if (p > 4095) {
alshabibf0e7e702015-05-30 18:22:36 -0700144 log.warn("Port Number {} exceeds vlan max", port);
alshabibc4dfe852015-06-05 13:35:13 -0700145 return -1;
alshabibf0e7e702015-05-30 18:22:36 -0700146 }
alshabibc4dfe852015-06-05 13:35:13 -0700147 return (short) p;
148 }
149
150
alshabibde5538f92015-06-05 15:19:43 -0700151 private void provisionVlanOnPort(String deviceId, PortNumber p, short vlanId) {
152 DeviceId did = DeviceId.deviceId(deviceId);
alshabibf0e7e702015-05-30 18:22:36 -0700153
154 TrafficSelector upstream = DefaultTrafficSelector.builder()
alshabibc4dfe852015-06-05 13:35:13 -0700155 .matchVlanId(VlanId.vlanId(vlanId))
alshabibf0e7e702015-05-30 18:22:36 -0700156 .matchInPort(p)
157 .build();
158
159 TrafficSelector downStream = DefaultTrafficSelector.builder()
alshabibc4dfe852015-06-05 13:35:13 -0700160 .matchVlanId(VlanId.vlanId(vlanId))
alshabibf0e7e702015-05-30 18:22:36 -0700161 .matchInPort(PortNumber.portNumber(uplinkPort))
162 .build();
163
164 TrafficTreatment upstreamTreatment = DefaultTrafficTreatment.builder()
165 .setOutput(PortNumber.portNumber(uplinkPort))
166 .build();
167
168 TrafficTreatment downStreamTreatment = DefaultTrafficTreatment.builder()
169 .setOutput(p)
170 .build();
171
172
173 ForwardingObjective upFwd = DefaultForwardingObjective.builder()
174 .withFlag(ForwardingObjective.Flag.VERSATILE)
175 .withPriority(1000)
176 .makePermanent()
177 .withSelector(upstream)
178 .fromApp(appId)
179 .withTreatment(upstreamTreatment)
180 .add();
181
182 ForwardingObjective downFwd = DefaultForwardingObjective.builder()
183 .withFlag(ForwardingObjective.Flag.VERSATILE)
184 .withPriority(1000)
185 .makePermanent()
186 .withSelector(downStream)
187 .fromApp(appId)
188 .withTreatment(downStreamTreatment)
189 .add();
190
alshabibde5538f92015-06-05 15:19:43 -0700191 flowObjectiveService.forward(did, upFwd);
192 flowObjectiveService.forward(did, downFwd);
alshabibf0e7e702015-05-30 18:22:36 -0700193
194 }
195
196 private class InternalDeviceListener implements DeviceListener {
197 @Override
198 public void event(DeviceEvent event) {
199 DeviceId devId = DeviceId.deviceId(oltDevice);
200 switch (event.type()) {
201 case PORT_ADDED:
202 case PORT_UPDATED:
203 if (devId.equals(event.subject().id()) && event.port().isEnabled()) {
alshabibc4dfe852015-06-05 13:35:13 -0700204 short vlanId = fetchVlanId(event.port().number());
alshabibde5538f92015-06-05 15:19:43 -0700205 provisionVlanOnPort(gfastDevice, event.port().number(), vlanId);
alshabibf0e7e702015-05-30 18:22:36 -0700206 }
207 break;
208 case DEVICE_ADDED:
209 case DEVICE_UPDATED:
210 case DEVICE_REMOVED:
211 case DEVICE_SUSPENDED:
212 case DEVICE_AVAILABILITY_CHANGED:
213 case PORT_REMOVED:
214 case PORT_STATS_UPDATED:
215 default:
216 return;
217 }
218 }
219 }
220
221
222}
223
224