blob: 383684e42f74254363bf83db5a4e1ad19e9a17c9 [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;
sangho6032f342016-07-07 14:32:03 +090053import java.util.Optional;
Kyuhwi Choidc2973b2016-05-13 14:54:31 +090054
Kyuhwi Choi70537882016-06-24 17:24:12 +090055import org.onosproject.store.serializers.KryoNamespaces;
56import org.onosproject.store.service.ConsistentMap;
57import org.onosproject.store.service.Serializer;
58import org.onosproject.store.service.StorageService;
59import org.onosproject.store.service.Versioned;
Kyuhwi Choidc2973b2016-05-13 14:54:31 +090060import org.slf4j.Logger;
61import org.slf4j.LoggerFactory;
62
63import static com.google.common.base.Preconditions.checkNotNull;
Kyuhwi Choic5b33ea2016-04-26 11:45:32 +090064
65/**
66 * Manages gateway node for gateway scalability.
67 */
Kyuhwi Choidc2973b2016-05-13 14:54:31 +090068
69@Service
70@Component(immediate = true)
Kyuhwi Choic5b33ea2016-04-26 11:45:32 +090071public class ScalableGatewayManager implements ScalableGatewayService {
Kyuhwi Choidc2973b2016-05-13 14:54:31 +090072 private final Logger log = LoggerFactory.getLogger(getClass());
73
74 private ApplicationId appId;
75 private static final String APP_ID = "org.onosproject.scalablegateway";
76 private static final String APP_NAME = "scalablegateway";
77 private static final String GATEWAYNODE_CAN_NOT_BE_NULL = "The gateway node can not be null";
78 private static final String PORT_CAN_NOT_BE_NULL = "The port can not be null";
79 private static final String FAIL_ADD_GATEWAY = "Adding process is failed as existing deivce id";
80 private static final String FAIL_REMOVE_GATEWAY = "Removing process is failed as unknown deivce id";
81 private static final String PORT_NAME = "portName";
Kyuhwi Choi70537882016-06-24 17:24:12 +090082 private static final String GATEWAYNODE_MAP_NAME = "gatewaynode-map";
Kyuhwi Choidc2973b2016-05-13 14:54:31 +090083
84 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
85 protected CoreService coreService;
86
87 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
88 protected NetworkConfigService configService;
89
90 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
91 protected NetworkConfigRegistry configRegistry;
92
93 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
94 protected DeviceService deviceService;
95
Kyuhwi Choib0718212016-06-01 11:33:27 +090096 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
97 protected DriverService driverService;
98
99 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
100 protected GroupService groupService;
101
Kyuhwi Choi70537882016-06-24 17:24:12 +0900102 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
103 protected StorageService storageService;
104
Kyuhwi Choidc2973b2016-05-13 14:54:31 +0900105 private GatewayNodeConfig config;
Kyuhwi Choib0718212016-06-01 11:33:27 +0900106 private SelectGroupHandler selectGroupHandler;
Kyuhwi Choidc2973b2016-05-13 14:54:31 +0900107
108 private final NetworkConfigListener configListener = new InternalConfigListener();
Kyuhwi Choi70537882016-06-24 17:24:12 +0900109 private InternalDeviceListener internalDeviceListener = new InternalDeviceListener();
Kyuhwi Choidc2973b2016-05-13 14:54:31 +0900110
111 private final ConfigFactory configFactory =
112 new ConfigFactory(SubjectFactories.APP_SUBJECT_FACTORY, GatewayNodeConfig.class, APP_NAME) {
113 @Override
114 public GatewayNodeConfig createConfig() {
115 return new GatewayNodeConfig();
116 }
117 };
Kyuhwi Choi70537882016-06-24 17:24:12 +0900118 private ConsistentMap<DeviceId, GatewayNode> gatewayNodeMap; // Map<GatewayNode`s Id, GatewayNode object>
119 private static final KryoNamespace.Builder GATEWAYNODE_SERIALIZER = KryoNamespace.newBuilder()
120 .register(KryoNamespaces.API)
121 .register(DeviceId.class)
122 .register(GatewayNode.class);
Kyuhwi Choidc2973b2016-05-13 14:54:31 +0900123
124 @Activate
125 protected void activate() {
126 appId = coreService.registerApplication(APP_ID);
127 configRegistry.registerConfigFactory(configFactory);
128 configService.addListener(configListener);
Kyuhwi Choi70537882016-06-24 17:24:12 +0900129 deviceService.addListener(internalDeviceListener);
Kyuhwi Choidc2973b2016-05-13 14:54:31 +0900130
Kyuhwi Choib0718212016-06-01 11:33:27 +0900131 selectGroupHandler = new SelectGroupHandler(groupService, deviceService, driverService, appId);
Kyuhwi Choidc2973b2016-05-13 14:54:31 +0900132
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) {
sangho6032f342016-07-07 14:32:03 +0900168 Optional<Port> port = deviceService.getPorts(gatewayDeviceId)
Kyuhwi Choidc2973b2016-05-13 14:54:31 +0900169 .stream()
170 .filter(p -> p.annotations().value(PORT_NAME).equals(name))
sangho6032f342016-07-07 14:32:03 +0900171 .findFirst();
Kyuhwi Choidc2973b2016-05-13 14:54:31 +0900172
sangho6032f342016-07-07 14:32:03 +0900173 if (!port.isPresent()) {
174 log.error("Cannot find port {} in gateway device {}", name, gatewayDeviceId);
175 return null;
176 }
177
178 return port.get().number();
Kyuhwi Choic5b33ea2016-04-26 11:45:32 +0900179 }
180
181 @Override
182 public GroupId getGroupIdForGatewayLoadBalance(DeviceId srcDeviceId) {
Kyuhwi Choib0718212016-06-01 11:33:27 +0900183 GroupDescription description = selectGroupHandler.createSelectGroupInVxlan(srcDeviceId, getGatewayNodes());
184 groupService.addGroup(description);
185 Group group = groupService.getGroup(description.deviceId(), description.appCookie());
186 return group != null ? group.id() : null;
Kyuhwi Choic5b33ea2016-04-26 11:45:32 +0900187 }
188
189 @Override
190 public List<GatewayNode> getGatewayNodes() {
Kyuhwi Choidc2973b2016-05-13 14:54:31 +0900191 List<GatewayNode> gatewayNodeList = Lists.newArrayList();
192 gatewayNodeMap.values()
193 .stream()
Kyuhwi Choi70537882016-06-24 17:24:12 +0900194 .map(Versioned::value)
Kyuhwi Choidc2973b2016-05-13 14:54:31 +0900195 .forEach(gatewayNode -> gatewayNodeList.add(gatewayNode));
196 return gatewayNodeList;
197
Kyuhwi Choic5b33ea2016-04-26 11:45:32 +0900198 }
199
200 @Override
201 public List<DeviceId> getGatewayDeviceIds() {
Kyuhwi Choidc2973b2016-05-13 14:54:31 +0900202 List<DeviceId> deviceIdList = Lists.newArrayList();
203 gatewayNodeMap.values()
204 .stream()
Kyuhwi Choi70537882016-06-24 17:24:12 +0900205 .map(Versioned::value)
Kyuhwi Choidc2973b2016-05-13 14:54:31 +0900206 .forEach(gatewayNode -> deviceIdList.add(gatewayNode.getGatewayDeviceId()));
207 return deviceIdList;
208
Kyuhwi Choic5b33ea2016-04-26 11:45:32 +0900209 }
210
211 @Override
212 public boolean addGatewayNode(GatewayNode gatewayNode) {
Kyuhwi Choidc2973b2016-05-13 14:54:31 +0900213 gatewayNodeMap.putIfAbsent(gatewayNode.getGatewayDeviceId(), gatewayNode);
Kyuhwi Choi70537882016-06-24 17:24:12 +0900214 updateGatewayLoadBalance(gatewayNode, true);
Kyuhwi Choidc2973b2016-05-13 14:54:31 +0900215 return true;
Kyuhwi Choic5b33ea2016-04-26 11:45:32 +0900216 }
217
218 @Override
219 public boolean deleteGatewayNode(GatewayNode gatewayNode) {
Kyuhwi Choi70537882016-06-24 17:24:12 +0900220 boolean result = gatewayNodeMap.remove(gatewayNode.getGatewayDeviceId(), gatewayNode);
221 if (result) {
222 updateGatewayLoadBalance(gatewayNode, false);
223 }
224 return result;
225 }
226
227 private void updateGatewayLoadBalance(GatewayNode gatewayNode, boolean nodeInsertion) {
228 deviceService.getAvailableDevices().forEach(device ->
229 groupService.getGroups(device.id(), appId).forEach(group ->
230 selectGroupHandler.updateBucketToSelectGroupInVxlan(device.id(), group.appCookie(),
231 Lists.newArrayList(gatewayNode), nodeInsertion)));
Kyuhwi Choidc2973b2016-05-13 14:54:31 +0900232 }
233
234 private class InternalConfigListener implements NetworkConfigListener {
235 @Override
236 public void event(NetworkConfigEvent event) {
237 if (!event.configClass().equals(GatewayNodeConfig.class)) {
238 return;
239 }
240 switch (event.type()) {
241 case CONFIG_UPDATED:
242 gatewayNodeMap.clear();
243 readConfiguration();
244 break;
245 case CONFIG_ADDED:
246 readConfiguration();
247 break;
248 default:
249 log.debug("Unsupportable event type is occurred");
250 break;
251 }
252 }
253 }
254
Kyuhwi Choi70537882016-06-24 17:24:12 +0900255 private class InternalDeviceListener implements DeviceListener {
256
257 @Override
258 public void event(DeviceEvent deviceEvent) {
259 if (deviceEvent.type() == DeviceEvent.Type.DEVICE_SUSPENDED ||
260 deviceEvent.type() == DeviceEvent.Type.DEVICE_REMOVED) {
261 deleteGatewayNode(getGatewayNode(deviceEvent.subject().id()));
262 }
263 }
264 }
265
Kyuhwi Choidc2973b2016-05-13 14:54:31 +0900266 private void readConfiguration() {
267 config = configService.getConfig(appId, GatewayNodeConfig.class);
268 if (config == null) {
269 log.error("No configuration found");
270 return;
271 }
272
273 config.gatewayNodes().forEach(gatewayNode -> addGatewayNode(gatewayNode));
274
275 log.info("ScalableGateway configured");
Kyuhwi Choic5b33ea2016-04-26 11:45:32 +0900276 }
277}