blob: 37e601c73c8d1abd0c5ac2047fd74e15d27f0034 [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
Kyuhwi Choi176c83d2016-07-14 11:39:37 +0900158 public PortNumber getGatewayExternalPort(DeviceId deviceId) {
Kyuhwi Choi70537882016-06-24 17:24:12 +0900159 GatewayNode gatewayNode = checkNotNull(gatewayNodeMap.get(deviceId).value(), GATEWAYNODE_CAN_NOT_BE_NULL);
Kyuhwi Choi176c83d2016-07-14 11:39:37 +0900160 String externalInterfaceName = gatewayNode.getGatewayExternalInterfaceName();
161 Optional<Port> port = deviceService.getPorts(deviceId)
Kyuhwi Choidc2973b2016-05-13 14:54:31 +0900162 .stream()
Kyuhwi Choi176c83d2016-07-14 11:39:37 +0900163 .filter(p -> p.annotations().value(PORT_NAME).equals(externalInterfaceName))
sangho6032f342016-07-07 14:32:03 +0900164 .findFirst();
Kyuhwi Choidc2973b2016-05-13 14:54:31 +0900165
sangho6032f342016-07-07 14:32:03 +0900166 if (!port.isPresent()) {
Kyuhwi Choi176c83d2016-07-14 11:39:37 +0900167 log.error("Cannot find port {} in gateway device {}", externalInterfaceName, deviceId);
sangho6032f342016-07-07 14:32:03 +0900168 return null;
169 }
170
171 return port.get().number();
Kyuhwi Choic5b33ea2016-04-26 11:45:32 +0900172 }
173
174 @Override
175 public GroupId getGroupIdForGatewayLoadBalance(DeviceId srcDeviceId) {
Kyuhwi Choib0718212016-06-01 11:33:27 +0900176 GroupDescription description = selectGroupHandler.createSelectGroupInVxlan(srcDeviceId, getGatewayNodes());
177 groupService.addGroup(description);
178 Group group = groupService.getGroup(description.deviceId(), description.appCookie());
179 return group != null ? group.id() : null;
Kyuhwi Choic5b33ea2016-04-26 11:45:32 +0900180 }
181
182 @Override
183 public List<GatewayNode> getGatewayNodes() {
Kyuhwi Choidc2973b2016-05-13 14:54:31 +0900184 List<GatewayNode> gatewayNodeList = Lists.newArrayList();
185 gatewayNodeMap.values()
186 .stream()
Kyuhwi Choi70537882016-06-24 17:24:12 +0900187 .map(Versioned::value)
Kyuhwi Choidc2973b2016-05-13 14:54:31 +0900188 .forEach(gatewayNode -> gatewayNodeList.add(gatewayNode));
189 return gatewayNodeList;
190
Kyuhwi Choic5b33ea2016-04-26 11:45:32 +0900191 }
192
193 @Override
194 public List<DeviceId> getGatewayDeviceIds() {
Kyuhwi Choidc2973b2016-05-13 14:54:31 +0900195 List<DeviceId> deviceIdList = Lists.newArrayList();
196 gatewayNodeMap.values()
197 .stream()
Kyuhwi Choi70537882016-06-24 17:24:12 +0900198 .map(Versioned::value)
Kyuhwi Choidc2973b2016-05-13 14:54:31 +0900199 .forEach(gatewayNode -> deviceIdList.add(gatewayNode.getGatewayDeviceId()));
200 return deviceIdList;
201
Kyuhwi Choic5b33ea2016-04-26 11:45:32 +0900202 }
203
204 @Override
205 public boolean addGatewayNode(GatewayNode gatewayNode) {
Hyunsun Moond331afd2016-07-20 02:19:20 -0700206 Versioned<GatewayNode> existingNode = gatewayNodeMap.putIfAbsent(
207 gatewayNode.getGatewayDeviceId(), gatewayNode);
208 if (existingNode == null) {
209 updateGatewayLoadBalance(gatewayNode, true);
210 log.info("Added {} to gateway pool", gatewayNode);
211 return true;
212 } else {
213 return false;
214 }
Kyuhwi Choic5b33ea2016-04-26 11:45:32 +0900215 }
216
217 @Override
218 public boolean deleteGatewayNode(GatewayNode gatewayNode) {
Kyuhwi Choi70537882016-06-24 17:24:12 +0900219 boolean result = gatewayNodeMap.remove(gatewayNode.getGatewayDeviceId(), gatewayNode);
220 if (result) {
221 updateGatewayLoadBalance(gatewayNode, false);
222 }
223 return result;
224 }
225
226 private void updateGatewayLoadBalance(GatewayNode gatewayNode, boolean nodeInsertion) {
227 deviceService.getAvailableDevices().forEach(device ->
228 groupService.getGroups(device.id(), appId).forEach(group ->
229 selectGroupHandler.updateBucketToSelectGroupInVxlan(device.id(), group.appCookie(),
230 Lists.newArrayList(gatewayNode), nodeInsertion)));
Kyuhwi Choidc2973b2016-05-13 14:54:31 +0900231 }
232
233 private class InternalConfigListener implements NetworkConfigListener {
234 @Override
235 public void event(NetworkConfigEvent event) {
236 if (!event.configClass().equals(GatewayNodeConfig.class)) {
237 return;
238 }
239 switch (event.type()) {
240 case CONFIG_UPDATED:
241 gatewayNodeMap.clear();
242 readConfiguration();
243 break;
244 case CONFIG_ADDED:
245 readConfiguration();
246 break;
247 default:
248 log.debug("Unsupportable event type is occurred");
249 break;
250 }
251 }
252 }
253
Kyuhwi Choi70537882016-06-24 17:24:12 +0900254 private class InternalDeviceListener implements DeviceListener {
255
256 @Override
257 public void event(DeviceEvent deviceEvent) {
258 if (deviceEvent.type() == DeviceEvent.Type.DEVICE_SUSPENDED ||
259 deviceEvent.type() == DeviceEvent.Type.DEVICE_REMOVED) {
260 deleteGatewayNode(getGatewayNode(deviceEvent.subject().id()));
261 }
262 }
263 }
264
Kyuhwi Choidc2973b2016-05-13 14:54:31 +0900265 private void readConfiguration() {
266 config = configService.getConfig(appId, GatewayNodeConfig.class);
267 if (config == null) {
268 log.error("No configuration found");
269 return;
270 }
271
272 config.gatewayNodes().forEach(gatewayNode -> addGatewayNode(gatewayNode));
273
274 log.info("ScalableGateway configured");
Kyuhwi Choic5b33ea2016-04-26 11:45:32 +0900275 }
276}