blob: 8b753359354109070b2bb41f22d2d0dd46da26c2 [file] [log] [blame]
Kyuhwi Choic5b33ea2016-04-26 11:45:32 +09001/*
2 * Copyright 2016-present 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
Hyunsun Moon71701292016-05-09 12:00:54 -070017package org.onosproject.scalablegateway.impl;
Kyuhwi Choic5b33ea2016-04-26 11:45:32 +090018
Kyuhwi Choidc2973b2016-05-13 14:54:31 +090019import com.google.common.collect.Lists;
Kyuhwi Choidc2973b2016-05-13 14:54:31 +090020import org.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Deactivate;
23
24import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
26import org.apache.felix.scr.annotations.Service;
Kyuhwi Choi70537882016-06-24 17:24:12 +090027import org.onlab.util.KryoNamespace;
Kyuhwi Choidc2973b2016-05-13 14:54:31 +090028import org.onosproject.core.ApplicationId;
29import org.onosproject.core.CoreService;
30
Kyuhwi Choic5b33ea2016-04-26 11:45:32 +090031import org.onosproject.core.GroupId;
32import org.onosproject.net.DeviceId;
Kyuhwi Choidc2973b2016-05-13 14:54:31 +090033import org.onosproject.net.Port;
Kyuhwi Choic5b33ea2016-04-26 11:45:32 +090034import org.onosproject.net.PortNumber;
Kyuhwi Choidc2973b2016-05-13 14:54:31 +090035import org.onosproject.net.config.ConfigFactory;
36import org.onosproject.net.config.NetworkConfigEvent;
37import org.onosproject.net.config.NetworkConfigListener;
38import org.onosproject.net.config.NetworkConfigRegistry;
39import org.onosproject.net.config.NetworkConfigService;
40import org.onosproject.net.config.basics.SubjectFactories;
Kyuhwi Choi70537882016-06-24 17:24:12 +090041import org.onosproject.net.device.DeviceEvent;
42import org.onosproject.net.device.DeviceListener;
Kyuhwi Choidc2973b2016-05-13 14:54:31 +090043import org.onosproject.net.device.DeviceService;
Kyuhwi Choib0718212016-06-01 11:33:27 +090044import org.onosproject.net.driver.DriverService;
45import org.onosproject.net.group.Group;
46import org.onosproject.net.group.GroupDescription;
47import org.onosproject.net.group.GroupService;
Hyunsun Moon71701292016-05-09 12:00:54 -070048import org.onosproject.scalablegateway.api.GatewayNode;
Kyuhwi Choidc2973b2016-05-13 14:54:31 +090049import org.onosproject.scalablegateway.api.GatewayNodeConfig;
Hyunsun Moon71701292016-05-09 12:00:54 -070050import org.onosproject.scalablegateway.api.ScalableGatewayService;
Kyuhwi Choic5b33ea2016-04-26 11:45:32 +090051
52import java.util.List;
Kyuhwi Choidc2973b2016-05-13 14:54:31 +090053
Kyuhwi Choi70537882016-06-24 17:24:12 +090054import org.onosproject.store.serializers.KryoNamespaces;
55import org.onosproject.store.service.ConsistentMap;
56import org.onosproject.store.service.Serializer;
57import org.onosproject.store.service.StorageService;
58import org.onosproject.store.service.Versioned;
Kyuhwi Choidc2973b2016-05-13 14:54:31 +090059import org.slf4j.Logger;
60import org.slf4j.LoggerFactory;
61
62import static com.google.common.base.Preconditions.checkNotNull;
Kyuhwi Choic5b33ea2016-04-26 11:45:32 +090063
64/**
65 * Manages gateway node for gateway scalability.
66 */
Kyuhwi Choidc2973b2016-05-13 14:54:31 +090067
68@Service
69@Component(immediate = true)
Kyuhwi Choic5b33ea2016-04-26 11:45:32 +090070public class ScalableGatewayManager implements ScalableGatewayService {
Kyuhwi Choidc2973b2016-05-13 14:54:31 +090071 private final Logger log = LoggerFactory.getLogger(getClass());
72
73 private ApplicationId appId;
74 private static final String APP_ID = "org.onosproject.scalablegateway";
75 private static final String APP_NAME = "scalablegateway";
76 private static final String GATEWAYNODE_CAN_NOT_BE_NULL = "The gateway node can not be null";
77 private static final String PORT_CAN_NOT_BE_NULL = "The port can not be null";
78 private static final String FAIL_ADD_GATEWAY = "Adding process is failed as existing deivce id";
79 private static final String FAIL_REMOVE_GATEWAY = "Removing process is failed as unknown deivce id";
80 private static final String PORT_NAME = "portName";
Kyuhwi Choi70537882016-06-24 17:24:12 +090081 private static final String GATEWAYNODE_MAP_NAME = "gatewaynode-map";
Kyuhwi Choidc2973b2016-05-13 14:54:31 +090082
83 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
84 protected CoreService coreService;
85
86 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
87 protected NetworkConfigService configService;
88
89 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
90 protected NetworkConfigRegistry configRegistry;
91
92 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
93 protected DeviceService deviceService;
94
Kyuhwi Choib0718212016-06-01 11:33:27 +090095 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
96 protected DriverService driverService;
97
98 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
99 protected GroupService groupService;
100
Kyuhwi Choi70537882016-06-24 17:24:12 +0900101 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
102 protected StorageService storageService;
103
Kyuhwi Choidc2973b2016-05-13 14:54:31 +0900104 private GatewayNodeConfig config;
Kyuhwi Choib0718212016-06-01 11:33:27 +0900105 private SelectGroupHandler selectGroupHandler;
Kyuhwi Choidc2973b2016-05-13 14:54:31 +0900106
107 private final NetworkConfigListener configListener = new InternalConfigListener();
Kyuhwi Choi70537882016-06-24 17:24:12 +0900108 private InternalDeviceListener internalDeviceListener = new InternalDeviceListener();
Kyuhwi Choidc2973b2016-05-13 14:54:31 +0900109
110 private final ConfigFactory configFactory =
111 new ConfigFactory(SubjectFactories.APP_SUBJECT_FACTORY, GatewayNodeConfig.class, APP_NAME) {
112 @Override
113 public GatewayNodeConfig createConfig() {
114 return new GatewayNodeConfig();
115 }
116 };
Kyuhwi Choi70537882016-06-24 17:24:12 +0900117 private ConsistentMap<DeviceId, GatewayNode> gatewayNodeMap; // Map<GatewayNode`s Id, GatewayNode object>
118 private static final KryoNamespace.Builder GATEWAYNODE_SERIALIZER = KryoNamespace.newBuilder()
119 .register(KryoNamespaces.API)
120 .register(DeviceId.class)
121 .register(GatewayNode.class);
Kyuhwi Choidc2973b2016-05-13 14:54:31 +0900122
123 @Activate
124 protected void activate() {
125 appId = coreService.registerApplication(APP_ID);
126 configRegistry.registerConfigFactory(configFactory);
127 configService.addListener(configListener);
Kyuhwi Choi70537882016-06-24 17:24:12 +0900128 deviceService.addListener(internalDeviceListener);
Kyuhwi Choidc2973b2016-05-13 14:54:31 +0900129
Kyuhwi Choib0718212016-06-01 11:33:27 +0900130 selectGroupHandler = new SelectGroupHandler(groupService, deviceService, driverService, appId);
Kyuhwi Choidc2973b2016-05-13 14:54:31 +0900131 readConfiguration();
132
Kyuhwi Choi70537882016-06-24 17:24:12 +0900133 gatewayNodeMap = storageService.<DeviceId, GatewayNode>consistentMapBuilder()
134 .withSerializer(Serializer.using(GATEWAYNODE_SERIALIZER.build()))
135 .withName(GATEWAYNODE_MAP_NAME)
136 .withApplicationId(appId)
137 .build();
138
Kyuhwi Choidc2973b2016-05-13 14:54:31 +0900139 log.info("started");
140 }
141
142 @Deactivate
143 protected void deactivate() {
144 gatewayNodeMap.clear();
145
Kyuhwi Choi70537882016-06-24 17:24:12 +0900146 deviceService.removeListener(internalDeviceListener);
Kyuhwi Choidc2973b2016-05-13 14:54:31 +0900147 configService.removeListener(configListener);
148
149 log.info("stopped");
150 }
Kyuhwi Choic5b33ea2016-04-26 11:45:32 +0900151
152 @Override
153 public GatewayNode getGatewayNode(DeviceId deviceId) {
Kyuhwi Choi70537882016-06-24 17:24:12 +0900154 return checkNotNull(gatewayNodeMap.get(deviceId).value(), GATEWAYNODE_CAN_NOT_BE_NULL);
Kyuhwi Choic5b33ea2016-04-26 11:45:32 +0900155 }
156
157 @Override
158 public List<PortNumber> getGatewayExternalPorts(DeviceId deviceId) {
Kyuhwi Choi70537882016-06-24 17:24:12 +0900159 GatewayNode gatewayNode = checkNotNull(gatewayNodeMap.get(deviceId).value(), GATEWAYNODE_CAN_NOT_BE_NULL);
Kyuhwi Choidc2973b2016-05-13 14:54:31 +0900160 List<PortNumber> portNumbers = Lists.newArrayList();
161 gatewayNode.getGatewayExternalInterfaceNames()
162 .stream()
163 .forEach(name -> portNumbers.add(findPortNumFromPortName(gatewayNode.getGatewayDeviceId(), name)));
164 return portNumbers;
165 }
166
167 private PortNumber findPortNumFromPortName(DeviceId gatewayDeviceId, String name) {
168 Port port = deviceService.getPorts(gatewayDeviceId)
169 .stream()
170 .filter(p -> p.annotations().value(PORT_NAME).equals(name))
171 .iterator()
172 .next();
173 return checkNotNull(port, PORT_CAN_NOT_BE_NULL).number();
174
Kyuhwi Choic5b33ea2016-04-26 11:45:32 +0900175 }
176
177 @Override
178 public GroupId getGroupIdForGatewayLoadBalance(DeviceId srcDeviceId) {
Kyuhwi Choib0718212016-06-01 11:33:27 +0900179 GroupDescription description = selectGroupHandler.createSelectGroupInVxlan(srcDeviceId, getGatewayNodes());
180 groupService.addGroup(description);
181 Group group = groupService.getGroup(description.deviceId(), description.appCookie());
182 return group != null ? group.id() : null;
Kyuhwi Choic5b33ea2016-04-26 11:45:32 +0900183 }
184
185 @Override
186 public List<GatewayNode> getGatewayNodes() {
Kyuhwi Choidc2973b2016-05-13 14:54:31 +0900187 List<GatewayNode> gatewayNodeList = Lists.newArrayList();
188 gatewayNodeMap.values()
189 .stream()
Kyuhwi Choi70537882016-06-24 17:24:12 +0900190 .map(Versioned::value)
Kyuhwi Choidc2973b2016-05-13 14:54:31 +0900191 .forEach(gatewayNode -> gatewayNodeList.add(gatewayNode));
192 return gatewayNodeList;
193
Kyuhwi Choic5b33ea2016-04-26 11:45:32 +0900194 }
195
196 @Override
197 public List<DeviceId> getGatewayDeviceIds() {
Kyuhwi Choidc2973b2016-05-13 14:54:31 +0900198 List<DeviceId> deviceIdList = Lists.newArrayList();
199 gatewayNodeMap.values()
200 .stream()
Kyuhwi Choi70537882016-06-24 17:24:12 +0900201 .map(Versioned::value)
Kyuhwi Choidc2973b2016-05-13 14:54:31 +0900202 .forEach(gatewayNode -> deviceIdList.add(gatewayNode.getGatewayDeviceId()));
203 return deviceIdList;
204
Kyuhwi Choic5b33ea2016-04-26 11:45:32 +0900205 }
206
207 @Override
208 public boolean addGatewayNode(GatewayNode gatewayNode) {
Kyuhwi Choidc2973b2016-05-13 14:54:31 +0900209 gatewayNodeMap.putIfAbsent(gatewayNode.getGatewayDeviceId(), gatewayNode);
Kyuhwi Choi70537882016-06-24 17:24:12 +0900210 updateGatewayLoadBalance(gatewayNode, true);
Kyuhwi Choidc2973b2016-05-13 14:54:31 +0900211 return true;
Kyuhwi Choic5b33ea2016-04-26 11:45:32 +0900212 }
213
214 @Override
215 public boolean deleteGatewayNode(GatewayNode gatewayNode) {
Kyuhwi Choi70537882016-06-24 17:24:12 +0900216 boolean result = gatewayNodeMap.remove(gatewayNode.getGatewayDeviceId(), gatewayNode);
217 if (result) {
218 updateGatewayLoadBalance(gatewayNode, false);
219 }
220 return result;
221 }
222
223 private void updateGatewayLoadBalance(GatewayNode gatewayNode, boolean nodeInsertion) {
224 deviceService.getAvailableDevices().forEach(device ->
225 groupService.getGroups(device.id(), appId).forEach(group ->
226 selectGroupHandler.updateBucketToSelectGroupInVxlan(device.id(), group.appCookie(),
227 Lists.newArrayList(gatewayNode), nodeInsertion)));
Kyuhwi Choidc2973b2016-05-13 14:54:31 +0900228 }
229
230 private class InternalConfigListener implements NetworkConfigListener {
231 @Override
232 public void event(NetworkConfigEvent event) {
233 if (!event.configClass().equals(GatewayNodeConfig.class)) {
234 return;
235 }
236 switch (event.type()) {
237 case CONFIG_UPDATED:
238 gatewayNodeMap.clear();
239 readConfiguration();
240 break;
241 case CONFIG_ADDED:
242 readConfiguration();
243 break;
244 default:
245 log.debug("Unsupportable event type is occurred");
246 break;
247 }
248 }
249 }
250
Kyuhwi Choi70537882016-06-24 17:24:12 +0900251 private class InternalDeviceListener implements DeviceListener {
252
253 @Override
254 public void event(DeviceEvent deviceEvent) {
255 if (deviceEvent.type() == DeviceEvent.Type.DEVICE_SUSPENDED ||
256 deviceEvent.type() == DeviceEvent.Type.DEVICE_REMOVED) {
257 deleteGatewayNode(getGatewayNode(deviceEvent.subject().id()));
258 }
259 }
260 }
261
Kyuhwi Choidc2973b2016-05-13 14:54:31 +0900262 private void readConfiguration() {
263 config = configService.getConfig(appId, GatewayNodeConfig.class);
264 if (config == null) {
265 log.error("No configuration found");
266 return;
267 }
268
269 config.gatewayNodes().forEach(gatewayNode -> addGatewayNode(gatewayNode));
270
271 log.info("ScalableGateway configured");
Kyuhwi Choic5b33ea2016-04-26 11:45:32 +0900272 }
273}