blob: d8183d5fc55eeb800b76c2cee0438b545b129337 [file] [log] [blame]
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -08001/*
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 */
16package org.onosproject.grouphandler;
17
18import static org.slf4j.LoggerFactory.getLogger;
19
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -070020import java.net.URI;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080021import java.util.Arrays;
22import java.util.HashMap;
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -070023import java.util.HashSet;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080024import java.util.List;
25
26import org.apache.felix.scr.annotations.Activate;
27import org.apache.felix.scr.annotations.Component;
28import org.apache.felix.scr.annotations.Deactivate;
29import org.apache.felix.scr.annotations.Reference;
30import org.apache.felix.scr.annotations.ReferenceCardinality;
31import org.onlab.packet.MacAddress;
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -070032import org.onlab.util.KryoNamespace;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080033import org.onosproject.core.ApplicationId;
34import org.onosproject.core.CoreService;
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -070035import org.onosproject.mastership.MastershipService;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080036import org.onosproject.net.Device;
37import org.onosproject.net.DeviceId;
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -070038import org.onosproject.net.MastershipRole;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080039import org.onosproject.net.device.DeviceEvent;
40import org.onosproject.net.device.DeviceListener;
41import org.onosproject.net.device.DeviceService;
42import org.onosproject.net.group.GroupService;
43import org.onosproject.net.link.LinkEvent;
44import org.onosproject.net.link.LinkListener;
45import org.onosproject.net.link.LinkService;
46import org.onosproject.net.topology.TopologyService;
47import 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)
58public 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 Vavilapalli717361f2015-03-16 12:06:04 -070077 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
78 protected MastershipService mastershipService;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080079
80 private DeviceListener deviceListener = new InternalDeviceListener();
81 private LinkListener linkListener = new InternalLinkListener();
82
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -070083 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 Vavilapallied12ae52015-02-09 14:43:19 -080089 @Activate
90 public void activate() {
91 appId = coreService.registerApplication("org.onosproject.defaultgrouphandler");
92 log.info("DefaultGroupHandlerApp Activating");
sanghob35a6192015-04-01 13:05:26 -070093
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080094 deviceService.addListener(deviceListener);
95 linkService.addListener(linkListener);
96 for (Device device: deviceService.getDevices()) {
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -070097 if (mastershipService.
98 getLocalRole(device.id()) == MastershipRole.MASTER) {
99 log.debug("Initiating default group handling for {}", device.id());
100 DefaultGroupHandler dgh = DefaultGroupHandler.createGroupHandler(device.id(),
101 appId,
102 config,
103 linkService,
104 groupService);
105 dgh.createGroups();
106 dghMap.put(device.id(), dgh);
107 } else {
108 log.debug("Activate: Local role {} "
109 + "is not MASTER for device {}",
110 mastershipService.
111 getLocalRole(device.id()),
112 device.id());
113 }
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800114 }
sanghob35a6192015-04-01 13:05:26 -0700115
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800116 log.info("Activated");
117 }
118
119 @Deactivate
120 public void deactivate() {
121 dghMap.clear();
122 }
123
124 public class DeviceConfiguration implements DeviceProperties {
125 private final List<Integer> allSegmentIds =
126 Arrays.asList(101, 102, 103, 104, 105, 106);
127 private HashMap<DeviceId, Integer> deviceSegmentIdMap =
128 new HashMap<DeviceId, Integer>() {
129 {
130 put(DeviceId.deviceId("of:0000000000000001"), 101);
131 put(DeviceId.deviceId("of:0000000000000002"), 102);
132 put(DeviceId.deviceId("of:0000000000000003"), 103);
133 put(DeviceId.deviceId("of:0000000000000004"), 104);
134 put(DeviceId.deviceId("of:0000000000000005"), 105);
135 put(DeviceId.deviceId("of:0000000000000006"), 106);
136 }
137 };
138 private final HashMap<DeviceId, MacAddress> deviceMacMap =
139 new HashMap<DeviceId, MacAddress>() {
140 {
141 put(DeviceId.deviceId("of:0000000000000001"),
142 MacAddress.valueOf("00:00:00:00:00:01"));
143 put(DeviceId.deviceId("of:0000000000000002"),
144 MacAddress.valueOf("00:00:00:00:00:02"));
145 put(DeviceId.deviceId("of:0000000000000003"),
146 MacAddress.valueOf("00:00:00:00:00:03"));
147 put(DeviceId.deviceId("of:0000000000000004"),
148 MacAddress.valueOf("00:00:00:00:00:04"));
149 put(DeviceId.deviceId("of:0000000000000005"),
150 MacAddress.valueOf("00:00:00:00:00:05"));
151 put(DeviceId.deviceId("of:0000000000000006"),
152 MacAddress.valueOf("00:00:00:00:00:06"));
153 }
154 };
155
156 @Override
157 public int getSegmentId(DeviceId deviceId) {
158 if (deviceSegmentIdMap.get(deviceId) != null) {
159 log.debug("getSegmentId for device{} is {}",
160 deviceId,
161 deviceSegmentIdMap.get(deviceId));
162 return deviceSegmentIdMap.get(deviceId);
163 } else {
164 throw new IllegalStateException();
165 }
166 }
167 @Override
168 public MacAddress getDeviceMac(DeviceId deviceId) {
169 if (deviceMacMap.get(deviceId) != null) {
170 log.debug("getDeviceMac for device{} is {}",
171 deviceId,
172 deviceMacMap.get(deviceId));
173 return deviceMacMap.get(deviceId);
174 } else {
175 throw new IllegalStateException();
176 }
177 }
178 @Override
179 public boolean isEdgeDevice(DeviceId deviceId) {
180 return true;
181 }
182 @Override
183 public List<Integer> getAllDeviceSegmentIds() {
184 return allSegmentIds;
185 }
186 }
187
188 private class InternalDeviceListener implements DeviceListener {
189
190 @Override
191 public void event(DeviceEvent event) {
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700192 if (mastershipService.
193 getLocalRole(event.subject().id()) != MastershipRole.MASTER) {
194 log.debug("Local role {} is not MASTER for device {}",
195 mastershipService.
196 getLocalRole(event.subject().id()),
197 event.subject().id());
198 return;
199 }
200 switch (event.type()) {
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800201 case DEVICE_ADDED:
202 log.debug("Initiating default group handling for {}", event.subject().id());
203 DefaultGroupHandler dgh = DefaultGroupHandler.createGroupHandler(
204 event.subject().id(),
205 appId,
206 config,
207 linkService,
208 groupService);
209 dgh.createGroups();
210 dghMap.put(event.subject().id(), dgh);
211 break;
212 case PORT_REMOVED:
213 if (dghMap.get(event.subject().id()) != null) {
214 dghMap.get(event.subject().id()).portDown(event.port().number());
215 }
216 break;
217 default:
218 break;
219 }
220
221 }
222 }
223
224 private class InternalLinkListener implements LinkListener {
225
226 @Override
227 public void event(LinkEvent event) {
Srikanth Vavilapalli717361f2015-03-16 12:06:04 -0700228 if (mastershipService.
229 getLocalRole(event.subject().src().deviceId()) !=
230 MastershipRole.MASTER) {
231 log.debug("InternalLinkListener: Local role {} "
232 + "is not MASTER for device {}",
233 mastershipService.
234 getLocalRole(event.subject().src().deviceId()),
235 event.subject().src().deviceId());
236 return;
237 }
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800238 switch (event.type()) {
239 case LINK_ADDED:
240 if (dghMap.get(event.subject().src().deviceId()) != null) {
241 dghMap.get(event.subject().src().deviceId()).linkUp(event.subject());
242 }
243 break;
244 default:
245 break;
246 }
247 }
248
249 }
250}