blob: c92f47a2492c9a4599e36683384ba480b3b6a6bd [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;
Sho SHIMIZU25005432015-09-02 18:38:12 -070032import org.onosproject.net.Port;
alshabibf0e7e702015-05-30 18:22:36 -070033import org.onosproject.net.PortNumber;
34import org.onosproject.net.device.DeviceEvent;
35import org.onosproject.net.device.DeviceListener;
36import org.onosproject.net.device.DeviceService;
37import org.onosproject.net.flow.DefaultTrafficSelector;
38import org.onosproject.net.flow.DefaultTrafficTreatment;
39import org.onosproject.net.flow.TrafficSelector;
40import org.onosproject.net.flow.TrafficTreatment;
41import org.onosproject.net.flowobjective.DefaultForwardingObjective;
42import org.onosproject.net.flowobjective.FlowObjectiveService;
43import org.onosproject.net.flowobjective.ForwardingObjective;
44import org.osgi.service.component.ComponentContext;
45import org.slf4j.Logger;
46
47import java.util.Dictionary;
48
49import static org.slf4j.LoggerFactory.getLogger;
50
51/**
52 * Sample mobility application. Cleans up flowmods when a host moves.
53 */
54@Component(immediate = true)
55public class OLT {
56
57 private final Logger log = getLogger(getClass());
58
59 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
60 protected FlowObjectiveService flowObjectiveService;
61
62 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
63 protected DeviceService deviceService;
64
65 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
66 protected CoreService coreService;
67
68 private final DeviceListener deviceListener = new InternalDeviceListener();
69
70 private ApplicationId appId;
71
alshabibcf3eb952015-06-01 10:57:31 -070072 public static final int OFFSET = 200;
73
alshabibf0e7e702015-05-30 18:22:36 -070074 public static final int UPLINK_PORT = 129;
alshabib3d581752015-06-05 15:32:15 -070075 public static final int GFAST_UPLINK_PORT = 100;
alshabibf0e7e702015-05-30 18:22:36 -070076
77 public static final String OLT_DEVICE = "of:90e2ba82f97791e9";
alshabib4ae2b402015-06-05 14:55:24 -070078 public static final String GFAST_DEVICE = "of:0011223344551357";
alshabibf0e7e702015-05-30 18:22:36 -070079
80 @Property(name = "uplinkPort", intValue = UPLINK_PORT,
81 label = "The OLT's uplink port number")
82 private int uplinkPort = UPLINK_PORT;
83
alshabib3d581752015-06-05 15:32:15 -070084 @Property(name = "gfastUplink", intValue = GFAST_UPLINK_PORT,
85 label = "The OLT's uplink port number")
86 private int gfastUplink = GFAST_UPLINK_PORT;
87
alshabibf0e7e702015-05-30 18:22:36 -070088 //TODO: replace this with an annotation lookup
89 @Property(name = "oltDevice", value = OLT_DEVICE,
90 label = "The OLT device id")
91 private String oltDevice = OLT_DEVICE;
92
alshabib4ae2b402015-06-05 14:55:24 -070093 @Property(name = "gfastDevice", value = GFAST_DEVICE,
94 label = "The gfast device id")
95 private String gfastDevice = GFAST_DEVICE;
96
alshabibf0e7e702015-05-30 18:22:36 -070097
98 @Activate
99 public void activate() {
alshabib4ae2b402015-06-05 14:55:24 -0700100 appId = coreService.registerApplication("org.onosproject.olt");
alshabibc4dfe852015-06-05 13:35:13 -0700101
Jonathan Harte78d7772015-07-01 14:49:56 -0700102 /*deviceService.addListener(deviceListener);
Jonathan Hart688c04f2015-06-10 18:19:41 -0700103
alshabibf0e7e702015-05-30 18:22:36 -0700104 deviceService.getPorts(DeviceId.deviceId(oltDevice)).stream().forEach(
105 port -> {
alshabibde5538f92015-06-05 15:19:43 -0700106 if (!port.number().isLogical() && port.isEnabled()) {
alshabibc4dfe852015-06-05 13:35:13 -0700107 short vlanId = fetchVlanId(port.number());
alshabibde5538f92015-06-05 15:19:43 -0700108 if (vlanId > 0) {
alshabib3d581752015-06-05 15:32:15 -0700109 provisionVlanOnPort(oltDevice, uplinkPort, port.number(), (short) 7);
110 provisionVlanOnPort(oltDevice, uplinkPort, port.number(), vlanId);
alshabibde5538f92015-06-05 15:19:43 -0700111 }
alshabibf0e7e702015-05-30 18:22:36 -0700112 }
113 }
Jonathan Harte78d7772015-07-01 14:49:56 -0700114 );*/
alshabib4ae2b402015-06-05 14:55:24 -0700115
116
Sho SHIMIZU25005432015-09-02 18:38:12 -0700117 deviceService.getPorts(DeviceId.deviceId(gfastDevice)).stream()
118 .filter(port -> !port.number().isLogical())
119 .filter(Port::isEnabled)
120 .forEach(port -> {
121 short vlanId = (short) (fetchVlanId(port.number()) + OFFSET);
122 if (vlanId > 0) {
123 provisionVlanOnPort(gfastDevice, gfastUplink, port.number(), vlanId);
124 }
alshabibde5538f92015-06-05 15:19:43 -0700125 }
Sho SHIMIZU25005432015-09-02 18:38:12 -0700126 );
alshabibf0e7e702015-05-30 18:22:36 -0700127 log.info("Started with Application ID {}", appId.id());
128 }
129
130 @Deactivate
131 public void deactivate() {
132 log.info("Stopped");
133 }
134
135 @Modified
136 public void modified(ComponentContext context) {
137 Dictionary<?, ?> properties = context.getProperties();
138
139
140 String s = Tools.get(properties, "uplinkPort");
141 uplinkPort = Strings.isNullOrEmpty(s) ? UPLINK_PORT : Integer.parseInt(s);
142
143 s = Tools.get(properties, "oltDevice");
144 oltDevice = Strings.isNullOrEmpty(s) ? OLT_DEVICE : s;
145
146 }
147
alshabibc4dfe852015-06-05 13:35:13 -0700148
149 private short fetchVlanId(PortNumber port) {
150 long p = port.toLong() + OFFSET;
151 if (p > 4095) {
alshabibf0e7e702015-05-30 18:22:36 -0700152 log.warn("Port Number {} exceeds vlan max", port);
alshabibc4dfe852015-06-05 13:35:13 -0700153 return -1;
alshabibf0e7e702015-05-30 18:22:36 -0700154 }
alshabibc4dfe852015-06-05 13:35:13 -0700155 return (short) p;
156 }
157
158
alshabib3d581752015-06-05 15:32:15 -0700159 private void provisionVlanOnPort(String deviceId, int uplinkPort, PortNumber p, short vlanId) {
alshabibde5538f92015-06-05 15:19:43 -0700160 DeviceId did = DeviceId.deviceId(deviceId);
alshabibf0e7e702015-05-30 18:22:36 -0700161
162 TrafficSelector upstream = DefaultTrafficSelector.builder()
alshabibc4dfe852015-06-05 13:35:13 -0700163 .matchVlanId(VlanId.vlanId(vlanId))
alshabibf0e7e702015-05-30 18:22:36 -0700164 .matchInPort(p)
165 .build();
166
167 TrafficSelector downStream = DefaultTrafficSelector.builder()
alshabibc4dfe852015-06-05 13:35:13 -0700168 .matchVlanId(VlanId.vlanId(vlanId))
alshabibf0e7e702015-05-30 18:22:36 -0700169 .matchInPort(PortNumber.portNumber(uplinkPort))
170 .build();
171
172 TrafficTreatment upstreamTreatment = DefaultTrafficTreatment.builder()
173 .setOutput(PortNumber.portNumber(uplinkPort))
174 .build();
175
176 TrafficTreatment downStreamTreatment = DefaultTrafficTreatment.builder()
177 .setOutput(p)
178 .build();
179
180
181 ForwardingObjective upFwd = DefaultForwardingObjective.builder()
182 .withFlag(ForwardingObjective.Flag.VERSATILE)
183 .withPriority(1000)
184 .makePermanent()
185 .withSelector(upstream)
186 .fromApp(appId)
187 .withTreatment(upstreamTreatment)
188 .add();
189
190 ForwardingObjective downFwd = DefaultForwardingObjective.builder()
191 .withFlag(ForwardingObjective.Flag.VERSATILE)
192 .withPriority(1000)
193 .makePermanent()
194 .withSelector(downStream)
195 .fromApp(appId)
196 .withTreatment(downStreamTreatment)
197 .add();
198
alshabibde5538f92015-06-05 15:19:43 -0700199 flowObjectiveService.forward(did, upFwd);
200 flowObjectiveService.forward(did, downFwd);
alshabibf0e7e702015-05-30 18:22:36 -0700201
202 }
203
204 private class InternalDeviceListener implements DeviceListener {
205 @Override
206 public void event(DeviceEvent event) {
207 DeviceId devId = DeviceId.deviceId(oltDevice);
208 switch (event.type()) {
209 case PORT_ADDED:
210 case PORT_UPDATED:
211 if (devId.equals(event.subject().id()) && event.port().isEnabled()) {
alshabibc4dfe852015-06-05 13:35:13 -0700212 short vlanId = fetchVlanId(event.port().number());
alshabib3d581752015-06-05 15:32:15 -0700213 provisionVlanOnPort(gfastDevice, uplinkPort, event.port().number(), vlanId);
alshabibf0e7e702015-05-30 18:22:36 -0700214 }
215 break;
216 case DEVICE_ADDED:
217 case DEVICE_UPDATED:
218 case DEVICE_REMOVED:
219 case DEVICE_SUSPENDED:
220 case DEVICE_AVAILABILITY_CHANGED:
221 case PORT_REMOVED:
222 case PORT_STATS_UPDATED:
223 default:
224 return;
225 }
226 }
227 }
228
229
230}
231
232