Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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.grouphandler; |
| 17 | |
| 18 | import static org.slf4j.LoggerFactory.getLogger; |
| 19 | |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame^] | 20 | import java.net.URI; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 21 | import java.util.Arrays; |
| 22 | import java.util.HashMap; |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame^] | 23 | import java.util.HashSet; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 24 | import java.util.List; |
| 25 | |
| 26 | import org.apache.felix.scr.annotations.Activate; |
| 27 | import org.apache.felix.scr.annotations.Component; |
| 28 | import org.apache.felix.scr.annotations.Deactivate; |
| 29 | import org.apache.felix.scr.annotations.Reference; |
| 30 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 31 | import org.onlab.packet.MacAddress; |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame^] | 32 | import org.onlab.util.KryoNamespace; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 33 | import org.onosproject.core.ApplicationId; |
| 34 | import org.onosproject.core.CoreService; |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame^] | 35 | import org.onosproject.mastership.MastershipService; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 36 | import org.onosproject.net.Device; |
| 37 | import org.onosproject.net.DeviceId; |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame^] | 38 | import org.onosproject.net.MastershipRole; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 39 | import org.onosproject.net.device.DeviceEvent; |
| 40 | import org.onosproject.net.device.DeviceListener; |
| 41 | import org.onosproject.net.device.DeviceService; |
| 42 | import org.onosproject.net.group.GroupService; |
| 43 | import org.onosproject.net.link.LinkEvent; |
| 44 | import org.onosproject.net.link.LinkListener; |
| 45 | import org.onosproject.net.link.LinkService; |
| 46 | import org.onosproject.net.topology.TopologyService; |
| 47 | import org.slf4j.Logger; |
| 48 | |
| 49 | /** |
| 50 | * Sample application to verify group subsystem end to end. |
| 51 | * This application expects a network of maximum of six connected |
| 52 | * devices for the test to work. For every device in the network, |
| 53 | * this test application launches a default group handler function |
| 54 | * that creates ECMP groups for every neighbor the device is |
| 55 | * connected to. |
| 56 | */ |
| 57 | @Component(immediate = true) |
| 58 | public class DefaultGroupHandlerApp { |
| 59 | |
| 60 | private final Logger log = getLogger(getClass()); |
| 61 | |
| 62 | private final DeviceProperties config = new DeviceConfiguration(); |
| 63 | private ApplicationId appId; |
| 64 | private HashMap<DeviceId, DefaultGroupHandler> dghMap = |
| 65 | new HashMap<DeviceId, DefaultGroupHandler>(); |
| 66 | |
| 67 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 68 | protected TopologyService topologyService; |
| 69 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 70 | protected DeviceService deviceService; |
| 71 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 72 | protected LinkService linkService; |
| 73 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 74 | protected GroupService groupService; |
| 75 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 76 | protected CoreService coreService; |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame^] | 77 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 78 | protected MastershipService mastershipService; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 79 | |
| 80 | private DeviceListener deviceListener = new InternalDeviceListener(); |
| 81 | private LinkListener linkListener = new InternalLinkListener(); |
| 82 | |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame^] | 83 | protected KryoNamespace.Builder kryo = new KryoNamespace.Builder() |
| 84 | .register(URI.class) |
| 85 | .register(HashSet.class) |
| 86 | .register(DeviceId.class) |
| 87 | .register(NeighborSet.class); |
| 88 | |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 89 | @Activate |
| 90 | public void activate() { |
| 91 | appId = coreService.registerApplication("org.onosproject.defaultgrouphandler"); |
| 92 | log.info("DefaultGroupHandlerApp Activating"); |
| 93 | deviceService.addListener(deviceListener); |
| 94 | linkService.addListener(linkListener); |
| 95 | for (Device device: deviceService.getDevices()) { |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame^] | 96 | if (mastershipService. |
| 97 | getLocalRole(device.id()) == MastershipRole.MASTER) { |
| 98 | log.debug("Initiating default group handling for {}", device.id()); |
| 99 | DefaultGroupHandler dgh = DefaultGroupHandler.createGroupHandler(device.id(), |
| 100 | appId, |
| 101 | config, |
| 102 | linkService, |
| 103 | groupService); |
| 104 | dgh.createGroups(); |
| 105 | dghMap.put(device.id(), dgh); |
| 106 | } else { |
| 107 | log.debug("Activate: Local role {} " |
| 108 | + "is not MASTER for device {}", |
| 109 | mastershipService. |
| 110 | getLocalRole(device.id()), |
| 111 | device.id()); |
| 112 | } |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 113 | } |
| 114 | log.info("Activated"); |
| 115 | } |
| 116 | |
| 117 | @Deactivate |
| 118 | public void deactivate() { |
| 119 | dghMap.clear(); |
| 120 | } |
| 121 | |
| 122 | public class DeviceConfiguration implements DeviceProperties { |
| 123 | private final List<Integer> allSegmentIds = |
| 124 | Arrays.asList(101, 102, 103, 104, 105, 106); |
| 125 | private HashMap<DeviceId, Integer> deviceSegmentIdMap = |
| 126 | new HashMap<DeviceId, Integer>() { |
| 127 | { |
| 128 | put(DeviceId.deviceId("of:0000000000000001"), 101); |
| 129 | put(DeviceId.deviceId("of:0000000000000002"), 102); |
| 130 | put(DeviceId.deviceId("of:0000000000000003"), 103); |
| 131 | put(DeviceId.deviceId("of:0000000000000004"), 104); |
| 132 | put(DeviceId.deviceId("of:0000000000000005"), 105); |
| 133 | put(DeviceId.deviceId("of:0000000000000006"), 106); |
| 134 | } |
| 135 | }; |
| 136 | private final HashMap<DeviceId, MacAddress> deviceMacMap = |
| 137 | new HashMap<DeviceId, MacAddress>() { |
| 138 | { |
| 139 | put(DeviceId.deviceId("of:0000000000000001"), |
| 140 | MacAddress.valueOf("00:00:00:00:00:01")); |
| 141 | put(DeviceId.deviceId("of:0000000000000002"), |
| 142 | MacAddress.valueOf("00:00:00:00:00:02")); |
| 143 | put(DeviceId.deviceId("of:0000000000000003"), |
| 144 | MacAddress.valueOf("00:00:00:00:00:03")); |
| 145 | put(DeviceId.deviceId("of:0000000000000004"), |
| 146 | MacAddress.valueOf("00:00:00:00:00:04")); |
| 147 | put(DeviceId.deviceId("of:0000000000000005"), |
| 148 | MacAddress.valueOf("00:00:00:00:00:05")); |
| 149 | put(DeviceId.deviceId("of:0000000000000006"), |
| 150 | MacAddress.valueOf("00:00:00:00:00:06")); |
| 151 | } |
| 152 | }; |
| 153 | |
| 154 | @Override |
| 155 | public int getSegmentId(DeviceId deviceId) { |
| 156 | if (deviceSegmentIdMap.get(deviceId) != null) { |
| 157 | log.debug("getSegmentId for device{} is {}", |
| 158 | deviceId, |
| 159 | deviceSegmentIdMap.get(deviceId)); |
| 160 | return deviceSegmentIdMap.get(deviceId); |
| 161 | } else { |
| 162 | throw new IllegalStateException(); |
| 163 | } |
| 164 | } |
| 165 | @Override |
| 166 | public MacAddress getDeviceMac(DeviceId deviceId) { |
| 167 | if (deviceMacMap.get(deviceId) != null) { |
| 168 | log.debug("getDeviceMac for device{} is {}", |
| 169 | deviceId, |
| 170 | deviceMacMap.get(deviceId)); |
| 171 | return deviceMacMap.get(deviceId); |
| 172 | } else { |
| 173 | throw new IllegalStateException(); |
| 174 | } |
| 175 | } |
| 176 | @Override |
| 177 | public boolean isEdgeDevice(DeviceId deviceId) { |
| 178 | return true; |
| 179 | } |
| 180 | @Override |
| 181 | public List<Integer> getAllDeviceSegmentIds() { |
| 182 | return allSegmentIds; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | private class InternalDeviceListener implements DeviceListener { |
| 187 | |
| 188 | @Override |
| 189 | public void event(DeviceEvent event) { |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame^] | 190 | if (mastershipService. |
| 191 | getLocalRole(event.subject().id()) != MastershipRole.MASTER) { |
| 192 | log.debug("Local role {} is not MASTER for device {}", |
| 193 | mastershipService. |
| 194 | getLocalRole(event.subject().id()), |
| 195 | event.subject().id()); |
| 196 | return; |
| 197 | } |
| 198 | switch (event.type()) { |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 199 | case DEVICE_ADDED: |
| 200 | log.debug("Initiating default group handling for {}", event.subject().id()); |
| 201 | DefaultGroupHandler dgh = DefaultGroupHandler.createGroupHandler( |
| 202 | event.subject().id(), |
| 203 | appId, |
| 204 | config, |
| 205 | linkService, |
| 206 | groupService); |
| 207 | dgh.createGroups(); |
| 208 | dghMap.put(event.subject().id(), dgh); |
| 209 | break; |
| 210 | case PORT_REMOVED: |
| 211 | if (dghMap.get(event.subject().id()) != null) { |
| 212 | dghMap.get(event.subject().id()).portDown(event.port().number()); |
| 213 | } |
| 214 | break; |
| 215 | default: |
| 216 | break; |
| 217 | } |
| 218 | |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | private class InternalLinkListener implements LinkListener { |
| 223 | |
| 224 | @Override |
| 225 | public void event(LinkEvent event) { |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame^] | 226 | if (mastershipService. |
| 227 | getLocalRole(event.subject().src().deviceId()) != |
| 228 | MastershipRole.MASTER) { |
| 229 | log.debug("InternalLinkListener: Local role {} " |
| 230 | + "is not MASTER for device {}", |
| 231 | mastershipService. |
| 232 | getLocalRole(event.subject().src().deviceId()), |
| 233 | event.subject().src().deviceId()); |
| 234 | return; |
| 235 | } |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 236 | switch (event.type()) { |
| 237 | case LINK_ADDED: |
| 238 | if (dghMap.get(event.subject().src().deviceId()) != null) { |
| 239 | dghMap.get(event.subject().src().deviceId()).linkUp(event.subject()); |
| 240 | } |
| 241 | break; |
| 242 | default: |
| 243 | break; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | } |
| 248 | } |