blob: d31ec050a4a50c86c8532f0e4652832563f6590e [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
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
alshabib17ff25f2015-06-01 10:57:31 -070071 public static final int OFFSET = 200;
72
alshabib0ccde6d2015-05-30 18:22:36 -070073 public static final int UPLINK_PORT = 129;
74
75 public static final String OLT_DEVICE = "of:90e2ba82f97791e9";
76
77 @Property(name = "uplinkPort", intValue = UPLINK_PORT,
78 label = "The OLT's uplink port number")
79 private int uplinkPort = UPLINK_PORT;
80
81 //TODO: replace this with an annotation lookup
82 @Property(name = "oltDevice", value = OLT_DEVICE,
83 label = "The OLT device id")
84 private String oltDevice = OLT_DEVICE;
85
86
87 @Activate
88 public void activate() {
89 appId = coreService.registerApplication("org.onosproject.mobility");
alshabibbeb2dc92015-06-05 13:35:13 -070090
alshabib0ccde6d2015-05-30 18:22:36 -070091 deviceService.getPorts(DeviceId.deviceId(oltDevice)).stream().forEach(
92 port -> {
93 if (port.isEnabled()) {
alshabibbeb2dc92015-06-05 13:35:13 -070094 short vlanId = fetchVlanId(port.number());
95 provisionVlanOnPort(port.number(), (short) 7);
96 provisionVlanOnPort(port.number(), vlanId);
alshabib0ccde6d2015-05-30 18:22:36 -070097 }
98 }
99 );
100 log.info("Started with Application ID {}", appId.id());
101 }
102
103 @Deactivate
104 public void deactivate() {
105 log.info("Stopped");
106 }
107
108 @Modified
109 public void modified(ComponentContext context) {
110 Dictionary<?, ?> properties = context.getProperties();
111
112
113 String s = Tools.get(properties, "uplinkPort");
114 uplinkPort = Strings.isNullOrEmpty(s) ? UPLINK_PORT : Integer.parseInt(s);
115
116 s = Tools.get(properties, "oltDevice");
117 oltDevice = Strings.isNullOrEmpty(s) ? OLT_DEVICE : s;
118
119 }
120
alshabibbeb2dc92015-06-05 13:35:13 -0700121
122 private short fetchVlanId(PortNumber port) {
123 long p = port.toLong() + OFFSET;
124 if (p > 4095) {
alshabib0ccde6d2015-05-30 18:22:36 -0700125 log.warn("Port Number {} exceeds vlan max", port);
alshabibbeb2dc92015-06-05 13:35:13 -0700126 return -1;
alshabib0ccde6d2015-05-30 18:22:36 -0700127 }
alshabibbeb2dc92015-06-05 13:35:13 -0700128 return (short) p;
129 }
130
131
132 private void provisionVlanOnPort(PortNumber p, short vlanId) {
133
alshabib0ccde6d2015-05-30 18:22:36 -0700134
135 TrafficSelector upstream = DefaultTrafficSelector.builder()
alshabibbeb2dc92015-06-05 13:35:13 -0700136 .matchVlanId(VlanId.vlanId(vlanId))
alshabib0ccde6d2015-05-30 18:22:36 -0700137 .matchInPort(p)
138 .build();
139
140 TrafficSelector downStream = DefaultTrafficSelector.builder()
alshabibbeb2dc92015-06-05 13:35:13 -0700141 .matchVlanId(VlanId.vlanId(vlanId))
alshabib0ccde6d2015-05-30 18:22:36 -0700142 .matchInPort(PortNumber.portNumber(uplinkPort))
143 .build();
144
145 TrafficTreatment upstreamTreatment = DefaultTrafficTreatment.builder()
146 .setOutput(PortNumber.portNumber(uplinkPort))
147 .build();
148
149 TrafficTreatment downStreamTreatment = DefaultTrafficTreatment.builder()
150 .setOutput(p)
151 .build();
152
153
154 ForwardingObjective upFwd = DefaultForwardingObjective.builder()
155 .withFlag(ForwardingObjective.Flag.VERSATILE)
156 .withPriority(1000)
157 .makePermanent()
158 .withSelector(upstream)
159 .fromApp(appId)
160 .withTreatment(upstreamTreatment)
161 .add();
162
163 ForwardingObjective downFwd = DefaultForwardingObjective.builder()
164 .withFlag(ForwardingObjective.Flag.VERSATILE)
165 .withPriority(1000)
166 .makePermanent()
167 .withSelector(downStream)
168 .fromApp(appId)
169 .withTreatment(downStreamTreatment)
170 .add();
171
172 flowObjectiveService.forward(DeviceId.deviceId(oltDevice), upFwd);
173 flowObjectiveService.forward(DeviceId.deviceId(oltDevice), downFwd);
174
175 }
176
177 private class InternalDeviceListener implements DeviceListener {
178 @Override
179 public void event(DeviceEvent event) {
180 DeviceId devId = DeviceId.deviceId(oltDevice);
181 switch (event.type()) {
182 case PORT_ADDED:
183 case PORT_UPDATED:
184 if (devId.equals(event.subject().id()) && event.port().isEnabled()) {
alshabibbeb2dc92015-06-05 13:35:13 -0700185 short vlanId = fetchVlanId(event.port().number());
186 provisionVlanOnPort(event.port().number(), vlanId);
alshabib0ccde6d2015-05-30 18:22:36 -0700187 }
188 break;
189 case DEVICE_ADDED:
190 case DEVICE_UPDATED:
191 case DEVICE_REMOVED:
192 case DEVICE_SUSPENDED:
193 case DEVICE_AVAILABILITY_CHANGED:
194 case PORT_REMOVED:
195 case PORT_STATS_UPDATED:
196 default:
197 return;
198 }
199 }
200 }
201
202
203}
204
205