Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [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.onlab.onos.optical.testapp; |
| 17 | |
| 18 | import static org.slf4j.LoggerFactory.getLogger; |
| 19 | |
| 20 | import java.util.HashMap; |
| 21 | import java.util.Map; |
| 22 | |
| 23 | import org.apache.felix.scr.annotations.Activate; |
| 24 | import org.apache.felix.scr.annotations.Deactivate; |
| 25 | import org.apache.felix.scr.annotations.Reference; |
| 26 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 27 | import org.onlab.onos.core.ApplicationId; |
| 28 | import org.onlab.onos.core.CoreService; |
| 29 | import org.onlab.onos.net.Device; |
| 30 | import org.onlab.onos.net.DeviceId; |
| 31 | import org.onlab.onos.net.PortNumber; |
| 32 | import org.onlab.onos.net.device.DeviceEvent; |
| 33 | import org.onlab.onos.net.device.DeviceListener; |
| 34 | import org.onlab.onos.net.device.DeviceService; |
| 35 | import org.onlab.onos.net.flow.DefaultFlowRule; |
| 36 | import org.onlab.onos.net.flow.DefaultTrafficSelector; |
| 37 | import org.onlab.onos.net.flow.DefaultTrafficTreatment; |
| 38 | import org.onlab.onos.net.flow.FlowRule; |
| 39 | import org.onlab.onos.net.flow.FlowRuleService; |
| 40 | import org.onlab.onos.net.flow.TrafficSelector; |
| 41 | import org.onlab.onos.net.flow.TrafficTreatment; |
| 42 | import org.onlab.packet.Ethernet; |
| 43 | import org.slf4j.Logger; |
| 44 | |
| 45 | /** |
| 46 | * Sample reactive forwarding application. |
| 47 | */ |
| 48 | //@Component(immediate = true) |
| 49 | public class MPLSForwarding { |
| 50 | |
| 51 | private final Logger log = getLogger(getClass()); |
| 52 | |
| 53 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 54 | protected FlowRuleService flowRuleService; |
| 55 | |
| 56 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 57 | protected CoreService coreService; |
| 58 | |
| 59 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 60 | protected DeviceService deviceService; |
| 61 | |
| 62 | private ApplicationId appId; |
| 63 | |
| 64 | private final InternalDeviceListener listener = new InternalDeviceListener(); |
| 65 | |
| 66 | private final Map<DeviceId, Integer> uglyMap = new HashMap<>(); |
| 67 | |
| 68 | @Activate |
| 69 | public void activate() { |
| 70 | appId = coreService.registerApplication("org.onlab.onos.testapp" + |
| 71 | ".mplsfwd"); |
| 72 | |
| 73 | uglyMap.put(DeviceId.deviceId("of:0000000000000001"), 1); |
| 74 | uglyMap.put(DeviceId.deviceId("of:0000000000000002"), 2); |
| 75 | uglyMap.put(DeviceId.deviceId("of:0000000000000003"), 3); |
| 76 | |
| 77 | deviceService.addListener(listener); |
| 78 | |
| 79 | for (Device d : deviceService.getDevices()) { |
| 80 | pushRules(d); |
| 81 | } |
| 82 | |
| 83 | |
| 84 | log.info("Started with Application ID {}", appId.id()); |
| 85 | } |
| 86 | |
| 87 | @Deactivate |
| 88 | public void deactivate() { |
| 89 | flowRuleService.removeFlowRulesById(appId); |
| 90 | |
| 91 | log.info("Stopped"); |
| 92 | } |
| 93 | |
| 94 | |
| 95 | private void pushRules(Device device) { |
| 96 | |
| 97 | TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder(); |
| 98 | TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder(); |
| 99 | int inport = 1; |
| 100 | int outport = 2; |
| 101 | Integer mplsLabel = 101; |
| 102 | Integer switchNumber = uglyMap.get(device.id()); |
| 103 | if (switchNumber == null) { |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | switch (switchNumber) { |
| 108 | case 1: |
| 109 | sbuilder.matchInport(PortNumber.portNumber(inport)); |
| 110 | tbuilder.setOutput(PortNumber.portNumber(outport)) |
| 111 | .pushMpls() |
| 112 | .setMpls(mplsLabel); |
| 113 | break; |
| 114 | case 2: |
| 115 | sbuilder.matchMplsLabel(mplsLabel) |
| 116 | .matchEthType(Ethernet.MPLS_UNICAST) |
| 117 | .matchInport(PortNumber.portNumber(inport)); |
| 118 | tbuilder.setOutput(PortNumber.portNumber(outport)); |
| 119 | break; |
| 120 | case 3: |
| 121 | sbuilder.matchMplsLabel(mplsLabel) |
| 122 | .matchEthType(Ethernet.MPLS_UNICAST) |
| 123 | .matchInport(PortNumber.portNumber(inport)); |
| 124 | tbuilder.popMpls().setOutput(PortNumber.portNumber(outport)); |
| 125 | break; |
| 126 | default: |
| 127 | } |
| 128 | |
| 129 | TrafficTreatment treatement = tbuilder.build(); |
| 130 | TrafficSelector selector = sbuilder.build(); |
| 131 | |
| 132 | FlowRule f = new DefaultFlowRule(device.id(), selector, |
| 133 | treatement, 100, appId, 600, false); |
| 134 | |
| 135 | flowRuleService.applyFlowRules(f); |
| 136 | } |
| 137 | |
| 138 | |
| 139 | public class InternalDeviceListener implements DeviceListener { |
| 140 | |
| 141 | @Override |
| 142 | public void event(DeviceEvent event) { |
| 143 | switch (event.type()) { |
| 144 | case DEVICE_ADDED: |
| 145 | pushRules(event.subject()); |
| 146 | break; |
| 147 | case DEVICE_AVAILABILITY_CHANGED: |
| 148 | break; |
Praseed Balakrishnan | 8c67d17 | 2014-11-10 10:15:41 -0800 | [diff] [blame] | 149 | case DEVICE_REMOVED: |
| 150 | break; |
| 151 | case DEVICE_SUSPENDED: |
| 152 | break; |
| 153 | case DEVICE_UPDATED: |
| 154 | break; |
| 155 | case PORT_ADDED: |
| 156 | break; |
| 157 | case PORT_REMOVED: |
| 158 | break; |
| 159 | case PORT_UPDATED: |
| 160 | break; |
| 161 | default: |
| 162 | break; |
| 163 | |
| 164 | } |
| 165 | |
| 166 | } |
| 167 | |
| 168 | } |
| 169 | |
| 170 | |
| 171 | } |
| 172 | |
| 173 | |