alshabib | f0e7e70 | 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 | |
| 71 | public static final int UPLINK_PORT = 129; |
| 72 | |
| 73 | public static final String OLT_DEVICE = "of:90e2ba82f97791e9"; |
| 74 | |
| 75 | @Property(name = "uplinkPort", intValue = UPLINK_PORT, |
| 76 | label = "The OLT's uplink port number") |
| 77 | private int uplinkPort = UPLINK_PORT; |
| 78 | |
| 79 | //TODO: replace this with an annotation lookup |
| 80 | @Property(name = "oltDevice", value = OLT_DEVICE, |
| 81 | label = "The OLT device id") |
| 82 | private String oltDevice = OLT_DEVICE; |
| 83 | |
| 84 | |
| 85 | @Activate |
| 86 | public void activate() { |
| 87 | appId = coreService.registerApplication("org.onosproject.mobility"); |
| 88 | deviceService.getPorts(DeviceId.deviceId(oltDevice)).stream().forEach( |
| 89 | port -> { |
| 90 | if (port.isEnabled()) { |
| 91 | provisionVlanOnPort(port.number()); |
| 92 | } |
| 93 | } |
| 94 | ); |
| 95 | log.info("Started with Application ID {}", appId.id()); |
| 96 | } |
| 97 | |
| 98 | @Deactivate |
| 99 | public void deactivate() { |
| 100 | log.info("Stopped"); |
| 101 | } |
| 102 | |
| 103 | @Modified |
| 104 | public void modified(ComponentContext context) { |
| 105 | Dictionary<?, ?> properties = context.getProperties(); |
| 106 | |
| 107 | |
| 108 | String s = Tools.get(properties, "uplinkPort"); |
| 109 | uplinkPort = Strings.isNullOrEmpty(s) ? UPLINK_PORT : Integer.parseInt(s); |
| 110 | |
| 111 | s = Tools.get(properties, "oltDevice"); |
| 112 | oltDevice = Strings.isNullOrEmpty(s) ? OLT_DEVICE : s; |
| 113 | |
| 114 | } |
| 115 | |
| 116 | private void provisionVlanOnPort(PortNumber p) { |
| 117 | long port = p.toLong(); |
| 118 | if (port > 4095) { |
| 119 | log.warn("Port Number {} exceeds vlan max", port); |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | TrafficSelector upstream = DefaultTrafficSelector.builder() |
| 124 | .matchVlanId( |
| 125 | VlanId.vlanId((short) port)) |
| 126 | .matchInPort(p) |
| 127 | .build(); |
| 128 | |
| 129 | TrafficSelector downStream = DefaultTrafficSelector.builder() |
| 130 | .matchVlanId( |
| 131 | VlanId.vlanId((short) port)) |
| 132 | .matchInPort(PortNumber.portNumber(uplinkPort)) |
| 133 | .build(); |
| 134 | |
| 135 | TrafficTreatment upstreamTreatment = DefaultTrafficTreatment.builder() |
| 136 | .setOutput(PortNumber.portNumber(uplinkPort)) |
| 137 | .build(); |
| 138 | |
| 139 | TrafficTreatment downStreamTreatment = DefaultTrafficTreatment.builder() |
| 140 | .setOutput(p) |
| 141 | .build(); |
| 142 | |
| 143 | |
| 144 | ForwardingObjective upFwd = DefaultForwardingObjective.builder() |
| 145 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
| 146 | .withPriority(1000) |
| 147 | .makePermanent() |
| 148 | .withSelector(upstream) |
| 149 | .fromApp(appId) |
| 150 | .withTreatment(upstreamTreatment) |
| 151 | .add(); |
| 152 | |
| 153 | ForwardingObjective downFwd = DefaultForwardingObjective.builder() |
| 154 | .withFlag(ForwardingObjective.Flag.VERSATILE) |
| 155 | .withPriority(1000) |
| 156 | .makePermanent() |
| 157 | .withSelector(downStream) |
| 158 | .fromApp(appId) |
| 159 | .withTreatment(downStreamTreatment) |
| 160 | .add(); |
| 161 | |
| 162 | flowObjectiveService.forward(DeviceId.deviceId(oltDevice), upFwd); |
| 163 | flowObjectiveService.forward(DeviceId.deviceId(oltDevice), downFwd); |
| 164 | |
| 165 | } |
| 166 | |
| 167 | private class InternalDeviceListener implements DeviceListener { |
| 168 | @Override |
| 169 | public void event(DeviceEvent event) { |
| 170 | DeviceId devId = DeviceId.deviceId(oltDevice); |
| 171 | switch (event.type()) { |
| 172 | case PORT_ADDED: |
| 173 | case PORT_UPDATED: |
| 174 | if (devId.equals(event.subject().id()) && event.port().isEnabled()) { |
| 175 | provisionVlanOnPort(event.port().number()); |
| 176 | } |
| 177 | break; |
| 178 | case DEVICE_ADDED: |
| 179 | case DEVICE_UPDATED: |
| 180 | case DEVICE_REMOVED: |
| 181 | case DEVICE_SUSPENDED: |
| 182 | case DEVICE_AVAILABILITY_CHANGED: |
| 183 | case PORT_REMOVED: |
| 184 | case PORT_STATS_UPDATED: |
| 185 | default: |
| 186 | return; |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | |
| 192 | } |
| 193 | |
| 194 | |