blob: f3cbb1d1c8d25a708f7eb8dd051b6f7ce9872935 [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;
alshabib3d581752015-06-05 15:32:15 -070074 public static final int GFAST_UPLINK_PORT = 100;
alshabibf0e7e702015-05-30 18:22:36 -070075
76 public static final String OLT_DEVICE = "of:90e2ba82f97791e9";
alshabib4ae2b402015-06-05 14:55:24 -070077 public static final String GFAST_DEVICE = "of:0011223344551357";
alshabibf0e7e702015-05-30 18:22:36 -070078
79 @Property(name = "uplinkPort", intValue = UPLINK_PORT,
80 label = "The OLT's uplink port number")
81 private int uplinkPort = UPLINK_PORT;
82
alshabib3d581752015-06-05 15:32:15 -070083 @Property(name = "gfastUplink", intValue = GFAST_UPLINK_PORT,
84 label = "The OLT's uplink port number")
85 private int gfastUplink = GFAST_UPLINK_PORT;
86
alshabibf0e7e702015-05-30 18:22:36 -070087 //TODO: replace this with an annotation lookup
88 @Property(name = "oltDevice", value = OLT_DEVICE,
89 label = "The OLT device id")
90 private String oltDevice = OLT_DEVICE;
91
alshabib4ae2b402015-06-05 14:55:24 -070092 @Property(name = "gfastDevice", value = GFAST_DEVICE,
93 label = "The gfast device id")
94 private String gfastDevice = GFAST_DEVICE;
95
alshabibf0e7e702015-05-30 18:22:36 -070096
97 @Activate
98 public void activate() {
alshabib4ae2b402015-06-05 14:55:24 -070099 appId = coreService.registerApplication("org.onosproject.olt");
alshabibc4dfe852015-06-05 13:35:13 -0700100
alshabibf0e7e702015-05-30 18:22:36 -0700101 deviceService.getPorts(DeviceId.deviceId(oltDevice)).stream().forEach(
102 port -> {
alshabibde5538f92015-06-05 15:19:43 -0700103 if (!port.number().isLogical() && port.isEnabled()) {
alshabibc4dfe852015-06-05 13:35:13 -0700104 short vlanId = fetchVlanId(port.number());
alshabibde5538f92015-06-05 15:19:43 -0700105 if (vlanId > 0) {
alshabib3d581752015-06-05 15:32:15 -0700106 provisionVlanOnPort(oltDevice, uplinkPort, port.number(), (short) 7);
107 provisionVlanOnPort(oltDevice, uplinkPort, port.number(), vlanId);
alshabibde5538f92015-06-05 15:19:43 -0700108 }
alshabibf0e7e702015-05-30 18:22:36 -0700109 }
110 }
111 );
alshabib4ae2b402015-06-05 14:55:24 -0700112
113
114 deviceService.getPorts(DeviceId.deviceId(gfastDevice)).stream().forEach(
115 port -> {
alshabibde5538f92015-06-05 15:19:43 -0700116 if (!port.number().isLogical() && port.isEnabled()) {
alshabib4ae2b402015-06-05 14:55:24 -0700117 short vlanId = (short) (fetchVlanId(port.number()) + OFFSET);
alshabibde5538f92015-06-05 15:19:43 -0700118 if (vlanId > 0) {
alshabib3d581752015-06-05 15:32:15 -0700119 provisionVlanOnPort(gfastDevice, gfastUplink, port.number(), vlanId);
alshabibde5538f92015-06-05 15:19:43 -0700120 }
alshabib4ae2b402015-06-05 14:55:24 -0700121 }
122 }
123 );
alshabibf0e7e702015-05-30 18:22:36 -0700124 log.info("Started with Application ID {}", appId.id());
125 }
126
127 @Deactivate
128 public void deactivate() {
129 log.info("Stopped");
130 }
131
132 @Modified
133 public void modified(ComponentContext context) {
134 Dictionary<?, ?> properties = context.getProperties();
135
136
137 String s = Tools.get(properties, "uplinkPort");
138 uplinkPort = Strings.isNullOrEmpty(s) ? UPLINK_PORT : Integer.parseInt(s);
139
140 s = Tools.get(properties, "oltDevice");
141 oltDevice = Strings.isNullOrEmpty(s) ? OLT_DEVICE : s;
142
143 }
144
alshabibc4dfe852015-06-05 13:35:13 -0700145
146 private short fetchVlanId(PortNumber port) {
147 long p = port.toLong() + OFFSET;
148 if (p > 4095) {
alshabibf0e7e702015-05-30 18:22:36 -0700149 log.warn("Port Number {} exceeds vlan max", port);
alshabibc4dfe852015-06-05 13:35:13 -0700150 return -1;
alshabibf0e7e702015-05-30 18:22:36 -0700151 }
alshabibc4dfe852015-06-05 13:35:13 -0700152 return (short) p;
153 }
154
155
alshabib3d581752015-06-05 15:32:15 -0700156 private void provisionVlanOnPort(String deviceId, int uplinkPort, PortNumber p, short vlanId) {
alshabibde5538f92015-06-05 15:19:43 -0700157 DeviceId did = DeviceId.deviceId(deviceId);
alshabibf0e7e702015-05-30 18:22:36 -0700158
159 TrafficSelector upstream = DefaultTrafficSelector.builder()
alshabibc4dfe852015-06-05 13:35:13 -0700160 .matchVlanId(VlanId.vlanId(vlanId))
alshabibf0e7e702015-05-30 18:22:36 -0700161 .matchInPort(p)
162 .build();
163
164 TrafficSelector downStream = DefaultTrafficSelector.builder()
alshabibc4dfe852015-06-05 13:35:13 -0700165 .matchVlanId(VlanId.vlanId(vlanId))
alshabibf0e7e702015-05-30 18:22:36 -0700166 .matchInPort(PortNumber.portNumber(uplinkPort))
167 .build();
168
169 TrafficTreatment upstreamTreatment = DefaultTrafficTreatment.builder()
170 .setOutput(PortNumber.portNumber(uplinkPort))
171 .build();
172
173 TrafficTreatment downStreamTreatment = DefaultTrafficTreatment.builder()
174 .setOutput(p)
175 .build();
176
177
178 ForwardingObjective upFwd = DefaultForwardingObjective.builder()
179 .withFlag(ForwardingObjective.Flag.VERSATILE)
180 .withPriority(1000)
181 .makePermanent()
182 .withSelector(upstream)
183 .fromApp(appId)
184 .withTreatment(upstreamTreatment)
185 .add();
186
187 ForwardingObjective downFwd = DefaultForwardingObjective.builder()
188 .withFlag(ForwardingObjective.Flag.VERSATILE)
189 .withPriority(1000)
190 .makePermanent()
191 .withSelector(downStream)
192 .fromApp(appId)
193 .withTreatment(downStreamTreatment)
194 .add();
195
alshabibde5538f92015-06-05 15:19:43 -0700196 flowObjectiveService.forward(did, upFwd);
197 flowObjectiveService.forward(did, downFwd);
alshabibf0e7e702015-05-30 18:22:36 -0700198
199 }
200
201 private class InternalDeviceListener implements DeviceListener {
202 @Override
203 public void event(DeviceEvent event) {
204 DeviceId devId = DeviceId.deviceId(oltDevice);
205 switch (event.type()) {
206 case PORT_ADDED:
207 case PORT_UPDATED:
208 if (devId.equals(event.subject().id()) && event.port().isEnabled()) {
alshabibc4dfe852015-06-05 13:35:13 -0700209 short vlanId = fetchVlanId(event.port().number());
alshabib3d581752015-06-05 15:32:15 -0700210 provisionVlanOnPort(gfastDevice, uplinkPort, event.port().number(), vlanId);
alshabibf0e7e702015-05-30 18:22:36 -0700211 }
212 break;
213 case DEVICE_ADDED:
214 case DEVICE_UPDATED:
215 case DEVICE_REMOVED:
216 case DEVICE_SUSPENDED:
217 case DEVICE_AVAILABILITY_CHANGED:
218 case PORT_REMOVED:
219 case PORT_STATS_UPDATED:
220 default:
221 return;
222 }
223 }
224 }
225
226
227}
228
229