Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
Marc De Leenheer | 4908775 | 2014-10-23 13:54:09 -0700 | [diff] [blame] | 19 | package org.onlab.onos.optical.testapp; |
| 20 | |
| 21 | import static org.slf4j.LoggerFactory.getLogger; |
| 22 | |
| 23 | import java.util.HashMap; |
| 24 | import java.util.Map; |
| 25 | |
| 26 | import org.apache.felix.scr.annotations.Activate; |
Marc De Leenheer | 4908775 | 2014-10-23 13:54:09 -0700 | [diff] [blame] | 27 | import org.apache.felix.scr.annotations.Deactivate; |
| 28 | import org.apache.felix.scr.annotations.Reference; |
| 29 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Thomas Vachuska | e0f804a | 2014-10-27 23:40:48 -0700 | [diff] [blame] | 30 | import org.onlab.onos.core.ApplicationId; |
| 31 | import org.onlab.onos.core.CoreService; |
Marc De Leenheer | 4908775 | 2014-10-23 13:54:09 -0700 | [diff] [blame] | 32 | import org.onlab.onos.net.Device; |
| 33 | import org.onlab.onos.net.DeviceId; |
| 34 | import org.onlab.onos.net.PortNumber; |
| 35 | import org.onlab.onos.net.device.DeviceEvent; |
| 36 | import org.onlab.onos.net.device.DeviceListener; |
| 37 | import org.onlab.onos.net.device.DeviceService; |
| 38 | import org.onlab.onos.net.flow.DefaultFlowRule; |
| 39 | import org.onlab.onos.net.flow.DefaultTrafficSelector; |
| 40 | import org.onlab.onos.net.flow.DefaultTrafficTreatment; |
| 41 | import org.onlab.onos.net.flow.FlowRule; |
| 42 | import org.onlab.onos.net.flow.FlowRuleService; |
| 43 | import org.onlab.onos.net.flow.TrafficSelector; |
| 44 | import org.onlab.onos.net.flow.TrafficTreatment; |
| 45 | import org.slf4j.Logger; |
| 46 | |
| 47 | /** |
| 48 | * Sample reactive forwarding application. |
| 49 | */ |
weibit | 253c865 | 2014-10-23 16:30:03 -0700 | [diff] [blame] | 50 | //@Component(immediate = true) |
Marc De Leenheer | 4908775 | 2014-10-23 13:54:09 -0700 | [diff] [blame] | 51 | public class LambdaForwarding { |
| 52 | |
| 53 | private final Logger log = getLogger(getClass()); |
| 54 | |
| 55 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 56 | protected FlowRuleService flowRuleService; |
| 57 | |
| 58 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 59 | protected CoreService coreService; |
| 60 | |
| 61 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 62 | protected DeviceService deviceService; |
| 63 | |
| 64 | private ApplicationId appId; |
| 65 | |
| 66 | private final InternalDeviceListener listener = new InternalDeviceListener(); |
| 67 | |
| 68 | private final Map<DeviceId, Integer> uglyMap = new HashMap<>(); |
| 69 | |
| 70 | @Activate |
| 71 | public void activate() { |
| 72 | appId = coreService.registerApplication("org.onlab.onos.fwd"); |
| 73 | |
Marc De Leenheer | 4908775 | 2014-10-23 13:54:09 -0700 | [diff] [blame] | 74 | uglyMap.put(DeviceId.deviceId("of:0000ffffffffff01"), 1); |
| 75 | uglyMap.put(DeviceId.deviceId("of:0000ffffffffff02"), 2); |
| 76 | uglyMap.put(DeviceId.deviceId("of:0000ffffffffff03"), 3); |
| 77 | |
Marc De Leenheer | 9eb47ec | 2014-10-23 14:15:49 -0700 | [diff] [blame] | 78 | deviceService.addListener(listener); |
| 79 | |
| 80 | for (Device d : deviceService.getDevices()) { |
| 81 | pushRules(d); |
| 82 | } |
| 83 | |
| 84 | |
Marc De Leenheer | 4908775 | 2014-10-23 13:54:09 -0700 | [diff] [blame] | 85 | log.info("Started with Application ID {}", appId.id()); |
| 86 | } |
| 87 | |
| 88 | @Deactivate |
| 89 | public void deactivate() { |
| 90 | flowRuleService.removeFlowRulesById(appId); |
| 91 | |
| 92 | log.info("Stopped"); |
| 93 | } |
| 94 | |
| 95 | |
| 96 | private void pushRules(Device device) { |
| 97 | |
| 98 | TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder(); |
| 99 | TrafficTreatment.Builder tbuilder = DefaultTrafficTreatment.builder(); |
| 100 | int inport; |
| 101 | int outport; |
| 102 | short lambda = 10; |
Praseed Balakrishnan | 64369da | 2014-10-23 15:55:20 -0700 | [diff] [blame] | 103 | byte sigType = 1; |
| 104 | Integer switchNumber = uglyMap.get(device.id()); |
| 105 | if (switchNumber == null) { |
| 106 | return; |
| 107 | } |
Marc De Leenheer | 4908775 | 2014-10-23 13:54:09 -0700 | [diff] [blame] | 108 | |
Marc De Leenheer | 4908775 | 2014-10-23 13:54:09 -0700 | [diff] [blame] | 109 | switch (switchNumber) { |
| 110 | case 1: |
| 111 | inport = 10; |
| 112 | outport = 20; |
| 113 | sbuilder.matchInport(PortNumber.portNumber(inport)); |
| 114 | tbuilder.setOutput(PortNumber.portNumber(outport)).setLambda(lambda); |
| 115 | break; |
| 116 | case 2: |
| 117 | inport = 21; |
| 118 | outport = 11; |
Praseed Balakrishnan | 64369da | 2014-10-23 15:55:20 -0700 | [diff] [blame] | 119 | sbuilder.matchLambda(lambda). |
| 120 | matchInport(PortNumber.portNumber(inport)); // match sigtype |
Marc De Leenheer | 4908775 | 2014-10-23 13:54:09 -0700 | [diff] [blame] | 121 | tbuilder.setOutput(PortNumber.portNumber(outport)); |
| 122 | break; |
| 123 | case 3: |
| 124 | inport = 30; |
| 125 | outport = 31; |
Praseed Balakrishnan | 64369da | 2014-10-23 15:55:20 -0700 | [diff] [blame] | 126 | sbuilder.matchLambda(lambda). |
| 127 | matchInport(PortNumber.portNumber(inport)); |
Marc De Leenheer | 4908775 | 2014-10-23 13:54:09 -0700 | [diff] [blame] | 128 | tbuilder.setOutput(PortNumber.portNumber(outport)).setLambda(lambda); |
| 129 | break; |
| 130 | default: |
| 131 | } |
Marc De Leenheer | 4908775 | 2014-10-23 13:54:09 -0700 | [diff] [blame] | 132 | |
| 133 | TrafficTreatment treatement = tbuilder.build(); |
| 134 | TrafficSelector selector = sbuilder.build(); |
| 135 | |
| 136 | FlowRule f = new DefaultFlowRule(device.id(), selector, |
| 137 | treatement, 100, appId, 600, false); |
| 138 | |
| 139 | flowRuleService.applyFlowRules(f); |
| 140 | |
| 141 | |
| 142 | |
| 143 | } |
| 144 | |
| 145 | public class InternalDeviceListener implements DeviceListener { |
| 146 | |
| 147 | @Override |
| 148 | public void event(DeviceEvent event) { |
| 149 | switch (event.type()) { |
| 150 | case DEVICE_ADDED: |
| 151 | pushRules(event.subject()); |
| 152 | break; |
| 153 | case DEVICE_AVAILABILITY_CHANGED: |
| 154 | break; |
| 155 | case DEVICE_MASTERSHIP_CHANGED: |
| 156 | break; |
| 157 | case DEVICE_REMOVED: |
| 158 | break; |
| 159 | case DEVICE_SUSPENDED: |
| 160 | break; |
| 161 | case DEVICE_UPDATED: |
| 162 | break; |
| 163 | case PORT_ADDED: |
| 164 | break; |
| 165 | case PORT_REMOVED: |
| 166 | break; |
| 167 | case PORT_UPDATED: |
| 168 | break; |
| 169 | default: |
| 170 | break; |
| 171 | |
| 172 | } |
| 173 | |
| 174 | } |
| 175 | |
| 176 | } |
| 177 | |
| 178 | |
| 179 | } |
| 180 | |
| 181 | |