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; |
Sho SHIMIZU | 00c9cfa | 2015-09-02 18:38:12 -0700 | [diff] [blame] | 32 | import org.onosproject.net.Port; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 33 | import org.onosproject.net.PortNumber; |
| 34 | import org.onosproject.net.device.DeviceEvent; |
| 35 | import org.onosproject.net.device.DeviceListener; |
| 36 | import org.onosproject.net.device.DeviceService; |
| 37 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 38 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 39 | import org.onosproject.net.flow.TrafficSelector; |
| 40 | import org.onosproject.net.flow.TrafficTreatment; |
| 41 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 42 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 43 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 44 | import org.osgi.service.component.ComponentContext; |
| 45 | import org.slf4j.Logger; |
| 46 | |
| 47 | import java.util.Dictionary; |
| 48 | |
| 49 | import static org.slf4j.LoggerFactory.getLogger; |
| 50 | |
| 51 | /** |
| 52 | * Sample mobility application. Cleans up flowmods when a host moves. |
| 53 | */ |
| 54 | @Component(immediate = true) |
| 55 | public 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 | |
alshabib | 17ff25f | 2015-06-01 10:57:31 -0700 | [diff] [blame] | 72 | public static final int OFFSET = 200; |
| 73 | |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 74 | public static final int UPLINK_PORT = 129; |
alshabib | b37fd08 | 2015-06-05 15:32:15 -0700 | [diff] [blame] | 75 | public static final int GFAST_UPLINK_PORT = 100; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 76 | |
| 77 | public static final String OLT_DEVICE = "of:90e2ba82f97791e9"; |
alshabib | 1af3b71 | 2015-06-05 14:55:24 -0700 | [diff] [blame] | 78 | public static final String GFAST_DEVICE = "of:0011223344551357"; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 79 | |
| 80 | @Property(name = "uplinkPort", intValue = UPLINK_PORT, |
| 81 | label = "The OLT's uplink port number") |
| 82 | private int uplinkPort = UPLINK_PORT; |
| 83 | |
alshabib | b37fd08 | 2015-06-05 15:32:15 -0700 | [diff] [blame] | 84 | @Property(name = "gfastUplink", intValue = GFAST_UPLINK_PORT, |
| 85 | label = "The OLT's uplink port number") |
| 86 | private int gfastUplink = GFAST_UPLINK_PORT; |
| 87 | |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 88 | //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 | |
alshabib | 1af3b71 | 2015-06-05 14:55:24 -0700 | [diff] [blame] | 93 | @Property(name = "gfastDevice", value = GFAST_DEVICE, |
| 94 | label = "The gfast device id") |
| 95 | private String gfastDevice = GFAST_DEVICE; |
| 96 | |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 97 | |
| 98 | @Activate |
| 99 | public void activate() { |
alshabib | 1af3b71 | 2015-06-05 14:55:24 -0700 | [diff] [blame] | 100 | appId = coreService.registerApplication("org.onosproject.olt"); |
alshabib | beb2dc9 | 2015-06-05 13:35:13 -0700 | [diff] [blame] | 101 | |
Jonathan Hart | dfc3b86 | 2015-07-01 14:49:56 -0700 | [diff] [blame] | 102 | /*deviceService.addListener(deviceListener); |
Jonathan Hart | 80ba4a7 | 2015-06-10 18:19:41 -0700 | [diff] [blame] | 103 | |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 104 | deviceService.getPorts(DeviceId.deviceId(oltDevice)).stream().forEach( |
| 105 | port -> { |
alshabib | ca62750 | 2015-06-05 15:19:43 -0700 | [diff] [blame] | 106 | if (!port.number().isLogical() && port.isEnabled()) { |
alshabib | beb2dc9 | 2015-06-05 13:35:13 -0700 | [diff] [blame] | 107 | short vlanId = fetchVlanId(port.number()); |
alshabib | ca62750 | 2015-06-05 15:19:43 -0700 | [diff] [blame] | 108 | if (vlanId > 0) { |
alshabib | b37fd08 | 2015-06-05 15:32:15 -0700 | [diff] [blame] | 109 | provisionVlanOnPort(oltDevice, uplinkPort, port.number(), (short) 7); |
| 110 | provisionVlanOnPort(oltDevice, uplinkPort, port.number(), vlanId); |
alshabib | ca62750 | 2015-06-05 15:19:43 -0700 | [diff] [blame] | 111 | } |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 112 | } |
| 113 | } |
Jonathan Hart | dfc3b86 | 2015-07-01 14:49:56 -0700 | [diff] [blame] | 114 | );*/ |
alshabib | 1af3b71 | 2015-06-05 14:55:24 -0700 | [diff] [blame] | 115 | |
| 116 | |
Sho SHIMIZU | 00c9cfa | 2015-09-02 18:38:12 -0700 | [diff] [blame] | 117 | 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 | } |
alshabib | ca62750 | 2015-06-05 15:19:43 -0700 | [diff] [blame] | 125 | } |
Sho SHIMIZU | 00c9cfa | 2015-09-02 18:38:12 -0700 | [diff] [blame] | 126 | ); |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 127 | 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 | |
alshabib | beb2dc9 | 2015-06-05 13:35:13 -0700 | [diff] [blame] | 148 | |
| 149 | private short fetchVlanId(PortNumber port) { |
| 150 | long p = port.toLong() + OFFSET; |
| 151 | if (p > 4095) { |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 152 | log.warn("Port Number {} exceeds vlan max", port); |
alshabib | beb2dc9 | 2015-06-05 13:35:13 -0700 | [diff] [blame] | 153 | return -1; |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 154 | } |
alshabib | beb2dc9 | 2015-06-05 13:35:13 -0700 | [diff] [blame] | 155 | return (short) p; |
| 156 | } |
| 157 | |
| 158 | |
alshabib | b37fd08 | 2015-06-05 15:32:15 -0700 | [diff] [blame] | 159 | private void provisionVlanOnPort(String deviceId, int uplinkPort, PortNumber p, short vlanId) { |
alshabib | ca62750 | 2015-06-05 15:19:43 -0700 | [diff] [blame] | 160 | DeviceId did = DeviceId.deviceId(deviceId); |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 161 | |
| 162 | TrafficSelector upstream = DefaultTrafficSelector.builder() |
alshabib | beb2dc9 | 2015-06-05 13:35:13 -0700 | [diff] [blame] | 163 | .matchVlanId(VlanId.vlanId(vlanId)) |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 164 | .matchInPort(p) |
| 165 | .build(); |
| 166 | |
| 167 | TrafficSelector downStream = DefaultTrafficSelector.builder() |
alshabib | beb2dc9 | 2015-06-05 13:35:13 -0700 | [diff] [blame] | 168 | .matchVlanId(VlanId.vlanId(vlanId)) |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 169 | .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 | |
alshabib | ca62750 | 2015-06-05 15:19:43 -0700 | [diff] [blame] | 199 | flowObjectiveService.forward(did, upFwd); |
| 200 | flowObjectiveService.forward(did, downFwd); |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 201 | |
| 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()) { |
alshabib | beb2dc9 | 2015-06-05 13:35:13 -0700 | [diff] [blame] | 212 | short vlanId = fetchVlanId(event.port().number()); |
alshabib | b37fd08 | 2015-06-05 15:32:15 -0700 | [diff] [blame] | 213 | provisionVlanOnPort(gfastDevice, uplinkPort, event.port().number(), vlanId); |
alshabib | 0ccde6d | 2015-05-30 18:22:36 -0700 | [diff] [blame] | 214 | } |
| 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 | |