alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.olt; |
| 17 | |
| 18 | |
| 19 | import com.google.common.base.Strings; |
| 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Deactivate; |
| 23 | import org.apache.felix.scr.annotations.Modified; |
| 24 | import org.apache.felix.scr.annotations.Property; |
| 25 | import org.apache.felix.scr.annotations.Reference; |
| 26 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 27 | import org.onlab.packet.VlanId; |
| 28 | import org.onlab.util.Tools; |
| 29 | import org.onosproject.core.ApplicationId; |
| 30 | import org.onosproject.core.CoreService; |
| 31 | import org.onosproject.net.DeviceId; |
| 32 | import org.onosproject.net.PortNumber; |
| 33 | import org.onosproject.net.device.DeviceEvent; |
| 34 | import org.onosproject.net.device.DeviceListener; |
| 35 | import org.onosproject.net.device.DeviceService; |
| 36 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 37 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 38 | import org.onosproject.net.flow.TrafficSelector; |
| 39 | import org.onosproject.net.flow.TrafficTreatment; |
| 40 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 41 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 42 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 43 | import org.osgi.service.component.ComponentContext; |
| 44 | import org.slf4j.Logger; |
| 45 | |
| 46 | import java.util.Dictionary; |
| 47 | |
| 48 | import static org.slf4j.LoggerFactory.getLogger; |
| 49 | |
| 50 | /** |
| 51 | * Sample mobility application. Cleans up flowmods when a host moves. |
| 52 | */ |
| 53 | @Component(immediate = true) |
| 54 | public 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 | |
alshabib | 17ff25f | 2015-06-01 10:57:31 -0700 | [diff] [blame] | 71 | public static final int OFFSET = 200; |
| 72 | |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 73 | public static final int UPLINK_PORT = 129; |
alshabib | b37fd08 | 2015-06-05 15:32:15 -0700 | [diff] [blame] | 74 | public static final int GFAST_UPLINK_PORT = 100; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 75 | |
| 76 | public static final String OLT_DEVICE = "of:90e2ba82f97791e9"; |
alshabib | 1af3b71 | 2015-06-05 14:55:24 -0700 | [diff] [blame] | 77 | public static final String GFAST_DEVICE = "of:0011223344551357"; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 78 | |
| 79 | @Property(name = "uplinkPort", intValue = UPLINK_PORT, |
| 80 | label = "The OLT's uplink port number") |
| 81 | private int uplinkPort = UPLINK_PORT; |
| 82 | |
alshabib | b37fd08 | 2015-06-05 15:32:15 -0700 | [diff] [blame] | 83 | @Property(name = "gfastUplink", intValue = GFAST_UPLINK_PORT, |
| 84 | label = "The OLT's uplink port number") |
| 85 | private int gfastUplink = GFAST_UPLINK_PORT; |
| 86 | |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 87 | //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 | |
alshabib | 1af3b71 | 2015-06-05 14:55:24 -0700 | [diff] [blame] | 92 | @Property(name = "gfastDevice", value = GFAST_DEVICE, |
| 93 | label = "The gfast device id") |
| 94 | private String gfastDevice = GFAST_DEVICE; |
| 95 | |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 96 | |
| 97 | @Activate |
| 98 | public void activate() { |
alshabib | 1af3b71 | 2015-06-05 14:55:24 -0700 | [diff] [blame] | 99 | appId = coreService.registerApplication("org.onosproject.olt"); |
alshabib | beb2dc9 | 2015-06-05 13:35:13 -0700 | [diff] [blame] | 100 | |
Jonathan Hart | dfc3b86 | 2015-07-01 14:49:56 -0700 | [diff] [blame] | 101 | /*deviceService.addListener(deviceListener); |
Jonathan Hart | 80ba4a7 | 2015-06-10 18:19:41 -0700 | [diff] [blame] | 102 | |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 103 | deviceService.getPorts(DeviceId.deviceId(oltDevice)).stream().forEach( |
| 104 | port -> { |
alshabib | ca62750 | 2015-06-05 15:19:43 -0700 | [diff] [blame] | 105 | if (!port.number().isLogical() && port.isEnabled()) { |
alshabib | beb2dc9 | 2015-06-05 13:35:13 -0700 | [diff] [blame] | 106 | short vlanId = fetchVlanId(port.number()); |
alshabib | ca62750 | 2015-06-05 15:19:43 -0700 | [diff] [blame] | 107 | if (vlanId > 0) { |
alshabib | b37fd08 | 2015-06-05 15:32:15 -0700 | [diff] [blame] | 108 | provisionVlanOnPort(oltDevice, uplinkPort, port.number(), (short) 7); |
| 109 | provisionVlanOnPort(oltDevice, uplinkPort, port.number(), vlanId); |
alshabib | ca62750 | 2015-06-05 15:19:43 -0700 | [diff] [blame] | 110 | } |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 111 | } |
| 112 | } |
Jonathan Hart | dfc3b86 | 2015-07-01 14:49:56 -0700 | [diff] [blame] | 113 | );*/ |
alshabib | 1af3b71 | 2015-06-05 14:55:24 -0700 | [diff] [blame] | 114 | |
| 115 | |
| 116 | deviceService.getPorts(DeviceId.deviceId(gfastDevice)).stream().forEach( |
| 117 | port -> { |
alshabib | ca62750 | 2015-06-05 15:19:43 -0700 | [diff] [blame] | 118 | if (!port.number().isLogical() && port.isEnabled()) { |
alshabib | 1af3b71 | 2015-06-05 14:55:24 -0700 | [diff] [blame] | 119 | short vlanId = (short) (fetchVlanId(port.number()) + OFFSET); |
alshabib | ca62750 | 2015-06-05 15:19:43 -0700 | [diff] [blame] | 120 | if (vlanId > 0) { |
alshabib | b37fd08 | 2015-06-05 15:32:15 -0700 | [diff] [blame] | 121 | provisionVlanOnPort(gfastDevice, gfastUplink, port.number(), vlanId); |
alshabib | ca62750 | 2015-06-05 15:19:43 -0700 | [diff] [blame] | 122 | } |
alshabib | 1af3b71 | 2015-06-05 14:55:24 -0700 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | ); |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 126 | log.info("Started with Application ID {}", appId.id()); |
| 127 | } |
| 128 | |
| 129 | @Deactivate |
| 130 | public void deactivate() { |
| 131 | log.info("Stopped"); |
| 132 | } |
| 133 | |
| 134 | @Modified |
| 135 | public void modified(ComponentContext context) { |
| 136 | Dictionary<?, ?> properties = context.getProperties(); |
| 137 | |
| 138 | |
| 139 | String s = Tools.get(properties, "uplinkPort"); |
| 140 | uplinkPort = Strings.isNullOrEmpty(s) ? UPLINK_PORT : Integer.parseInt(s); |
| 141 | |
| 142 | s = Tools.get(properties, "oltDevice"); |
| 143 | oltDevice = Strings.isNullOrEmpty(s) ? OLT_DEVICE : s; |
| 144 | |
| 145 | } |
| 146 | |
alshabib | beb2dc9 | 2015-06-05 13:35:13 -0700 | [diff] [blame] | 147 | |
| 148 | private short fetchVlanId(PortNumber port) { |
| 149 | long p = port.toLong() + OFFSET; |
| 150 | if (p > 4095) { |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 151 | log.warn("Port Number {} exceeds vlan max", port); |
alshabib | beb2dc9 | 2015-06-05 13:35:13 -0700 | [diff] [blame] | 152 | return -1; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 153 | } |
alshabib | beb2dc9 | 2015-06-05 13:35:13 -0700 | [diff] [blame] | 154 | return (short) p; |
| 155 | } |
| 156 | |
| 157 | |
alshabib | b37fd08 | 2015-06-05 15:32:15 -0700 | [diff] [blame] | 158 | private void provisionVlanOnPort(String deviceId, int uplinkPort, PortNumber p, short vlanId) { |
alshabib | ca62750 | 2015-06-05 15:19:43 -0700 | [diff] [blame] | 159 | DeviceId did = DeviceId.deviceId(deviceId); |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 160 | |
| 161 | TrafficSelector upstream = DefaultTrafficSelector.builder() |
alshabib | beb2dc9 | 2015-06-05 13:35:13 -0700 | [diff] [blame] | 162 | .matchVlanId(VlanId.vlanId(vlanId)) |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 163 | .matchInPort(p) |
| 164 | .build(); |
| 165 | |
| 166 | TrafficSelector downStream = DefaultTrafficSelector.builder() |
alshabib | beb2dc9 | 2015-06-05 13:35:13 -0700 | [diff] [blame] | 167 | .matchVlanId(VlanId.vlanId(vlanId)) |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 168 | .matchInPort(PortNumber.portNumber(uplinkPort)) |
| 169 | .build(); |
| 170 | |
| 171 | TrafficTreatment upstreamTreatment = DefaultTrafficTreatment.builder() |
| 172 | .setOutput(PortNumber.portNumber(uplinkPort)) |
| 173 | .build(); |
| 174 | |
| 175 | TrafficTreatment downStreamTreatment = DefaultTrafficTreatment.builder() |
| 176 | .setOutput(p) |
| 177 | .build(); |
| 178 | |
| 179 | |
| 180 | ForwardingObjective upFwd = DefaultForwardingObjective.builder() |
| 181 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
| 182 | .withPriority(1000) |
| 183 | .makePermanent() |
| 184 | .withSelector(upstream) |
| 185 | .fromApp(appId) |
| 186 | .withTreatment(upstreamTreatment) |
| 187 | .add(); |
| 188 | |
| 189 | ForwardingObjective downFwd = DefaultForwardingObjective.builder() |
| 190 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
| 191 | .withPriority(1000) |
| 192 | .makePermanent() |
| 193 | .withSelector(downStream) |
| 194 | .fromApp(appId) |
| 195 | .withTreatment(downStreamTreatment) |
| 196 | .add(); |
| 197 | |
alshabib | ca62750 | 2015-06-05 15:19:43 -0700 | [diff] [blame] | 198 | flowObjectiveService.forward(did, upFwd); |
| 199 | flowObjectiveService.forward(did, downFwd); |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 200 | |
| 201 | } |
| 202 | |
| 203 | private class InternalDeviceListener implements DeviceListener { |
| 204 | @Override |
| 205 | public void event(DeviceEvent event) { |
| 206 | DeviceId devId = DeviceId.deviceId(oltDevice); |
| 207 | switch (event.type()) { |
| 208 | case PORT_ADDED: |
| 209 | case PORT_UPDATED: |
| 210 | if (devId.equals(event.subject().id()) && event.port().isEnabled()) { |
alshabib | beb2dc9 | 2015-06-05 13:35:13 -0700 | [diff] [blame] | 211 | short vlanId = fetchVlanId(event.port().number()); |
alshabib | b37fd08 | 2015-06-05 15:32:15 -0700 | [diff] [blame] | 212 | provisionVlanOnPort(gfastDevice, uplinkPort, event.port().number(), vlanId); |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 213 | } |
| 214 | break; |
| 215 | case DEVICE_ADDED: |
| 216 | case DEVICE_UPDATED: |
| 217 | case DEVICE_REMOVED: |
| 218 | case DEVICE_SUSPENDED: |
| 219 | case DEVICE_AVAILABILITY_CHANGED: |
| 220 | case PORT_REMOVED: |
| 221 | case PORT_STATS_UPDATED: |
| 222 | default: |
| 223 | return; |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | |
| 229 | } |
| 230 | |
| 231 | |