Kyuhwi Choi | c5b33ea | 2016-04-26 11:45:32 +0900 | [diff] [blame] | 1 | /* |
| 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 Moon | 7170129 | 2016-05-09 12:00:54 -0700 | [diff] [blame] | 17 | package org.onosproject.scalablegateway.impl; |
Kyuhwi Choi | c5b33ea | 2016-04-26 11:45:32 +0900 | [diff] [blame] | 18 | |
Hyunsun Moon | f9a16ed | 2016-07-20 21:59:48 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableList; |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 20 | import com.google.common.collect.Lists; |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 26 | import org.apache.felix.scr.annotations.Service; |
Kyuhwi Choi | 7053788 | 2016-06-24 17:24:12 +0900 | [diff] [blame] | 27 | import org.onlab.util.KryoNamespace; |
Hyunsun Moon | f9a16ed | 2016-07-20 21:59:48 -0700 | [diff] [blame] | 28 | import org.onlab.util.Tools; |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 29 | import org.onosproject.core.ApplicationId; |
| 30 | import org.onosproject.core.CoreService; |
Kyuhwi Choi | c5b33ea | 2016-04-26 11:45:32 +0900 | [diff] [blame] | 31 | import org.onosproject.core.GroupId; |
| 32 | import org.onosproject.net.DeviceId; |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 33 | import org.onosproject.net.Port; |
Kyuhwi Choi | c5b33ea | 2016-04-26 11:45:32 +0900 | [diff] [blame] | 34 | import org.onosproject.net.PortNumber; |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 35 | import org.onosproject.net.config.ConfigFactory; |
| 36 | import org.onosproject.net.config.NetworkConfigEvent; |
| 37 | import org.onosproject.net.config.NetworkConfigListener; |
| 38 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 39 | import org.onosproject.net.config.NetworkConfigService; |
| 40 | import org.onosproject.net.config.basics.SubjectFactories; |
Kyuhwi Choi | 7053788 | 2016-06-24 17:24:12 +0900 | [diff] [blame] | 41 | import org.onosproject.net.device.DeviceEvent; |
| 42 | import org.onosproject.net.device.DeviceListener; |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 43 | import org.onosproject.net.device.DeviceService; |
Kyuhwi Choi | b071821 | 2016-06-01 11:33:27 +0900 | [diff] [blame] | 44 | import org.onosproject.net.driver.DriverService; |
| 45 | import org.onosproject.net.group.Group; |
Hyunsun Moon | f9a16ed | 2016-07-20 21:59:48 -0700 | [diff] [blame] | 46 | import org.onosproject.net.group.GroupKey; |
Kyuhwi Choi | b071821 | 2016-06-01 11:33:27 +0900 | [diff] [blame] | 47 | import org.onosproject.net.group.GroupService; |
Hyunsun Moon | 7170129 | 2016-05-09 12:00:54 -0700 | [diff] [blame] | 48 | import org.onosproject.scalablegateway.api.GatewayNode; |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 49 | import org.onosproject.scalablegateway.api.GatewayNodeConfig; |
Hyunsun Moon | 7170129 | 2016-05-09 12:00:54 -0700 | [diff] [blame] | 50 | import org.onosproject.scalablegateway.api.ScalableGatewayService; |
Kyuhwi Choi | 7053788 | 2016-06-24 17:24:12 +0900 | [diff] [blame] | 51 | import org.onosproject.store.serializers.KryoNamespaces; |
| 52 | import org.onosproject.store.service.ConsistentMap; |
| 53 | import org.onosproject.store.service.Serializer; |
| 54 | import org.onosproject.store.service.StorageService; |
| 55 | import org.onosproject.store.service.Versioned; |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 56 | import org.slf4j.Logger; |
| 57 | import org.slf4j.LoggerFactory; |
| 58 | |
sangho | 09a6ffe | 2016-08-23 16:55:38 +0900 | [diff] [blame^] | 59 | import java.util.List; |
| 60 | import java.util.Objects; |
| 61 | import java.util.Optional; |
| 62 | |
Hyunsun Moon | f9a16ed | 2016-07-20 21:59:48 -0700 | [diff] [blame] | 63 | import static org.onosproject.net.AnnotationKeys.PORT_NAME; |
Kyuhwi Choi | c5b33ea | 2016-04-26 11:45:32 +0900 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * Manages gateway node for gateway scalability. |
| 67 | */ |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 68 | |
| 69 | @Service |
| 70 | @Component(immediate = true) |
Kyuhwi Choi | c5b33ea | 2016-04-26 11:45:32 +0900 | [diff] [blame] | 71 | public class ScalableGatewayManager implements ScalableGatewayService { |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 72 | 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"; |
Kyuhwi Choi | 7053788 | 2016-06-24 17:24:12 +0900 | [diff] [blame] | 77 | private static final String GATEWAYNODE_MAP_NAME = "gatewaynode-map"; |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 78 | |
| 79 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 80 | protected CoreService coreService; |
| 81 | |
| 82 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 83 | protected NetworkConfigService configService; |
| 84 | |
| 85 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 86 | protected NetworkConfigRegistry configRegistry; |
| 87 | |
| 88 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 89 | protected DeviceService deviceService; |
| 90 | |
Kyuhwi Choi | b071821 | 2016-06-01 11:33:27 +0900 | [diff] [blame] | 91 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 92 | protected DriverService driverService; |
| 93 | |
| 94 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 95 | protected GroupService groupService; |
| 96 | |
Kyuhwi Choi | 7053788 | 2016-06-24 17:24:12 +0900 | [diff] [blame] | 97 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 98 | protected StorageService storageService; |
| 99 | |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 100 | private GatewayNodeConfig config; |
Kyuhwi Choi | b071821 | 2016-06-01 11:33:27 +0900 | [diff] [blame] | 101 | private SelectGroupHandler selectGroupHandler; |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 102 | |
| 103 | private final NetworkConfigListener configListener = new InternalConfigListener(); |
Hyunsun Moon | f9a16ed | 2016-07-20 21:59:48 -0700 | [diff] [blame] | 104 | private final InternalDeviceListener internalDeviceListener = new InternalDeviceListener(); |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 105 | |
| 106 | private final ConfigFactory configFactory = |
| 107 | new ConfigFactory(SubjectFactories.APP_SUBJECT_FACTORY, GatewayNodeConfig.class, APP_NAME) { |
| 108 | @Override |
| 109 | public GatewayNodeConfig createConfig() { |
| 110 | return new GatewayNodeConfig(); |
| 111 | } |
| 112 | }; |
Hyunsun Moon | f9a16ed | 2016-07-20 21:59:48 -0700 | [diff] [blame] | 113 | |
| 114 | private ConsistentMap<DeviceId, GatewayNode> gatewayNodeMap; // Map<GatewayNode Id, GatewayNode object> |
Kyuhwi Choi | 7053788 | 2016-06-24 17:24:12 +0900 | [diff] [blame] | 115 | private static final KryoNamespace.Builder GATEWAYNODE_SERIALIZER = KryoNamespace.newBuilder() |
| 116 | .register(KryoNamespaces.API) |
Kyuhwi Choi | 7053788 | 2016-06-24 17:24:12 +0900 | [diff] [blame] | 117 | .register(GatewayNode.class); |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 118 | |
| 119 | @Activate |
| 120 | protected void activate() { |
| 121 | appId = coreService.registerApplication(APP_ID); |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 122 | |
Kyuhwi Choi | 7053788 | 2016-06-24 17:24:12 +0900 | [diff] [blame] | 123 | gatewayNodeMap = storageService.<DeviceId, GatewayNode>consistentMapBuilder() |
| 124 | .withSerializer(Serializer.using(GATEWAYNODE_SERIALIZER.build())) |
| 125 | .withName(GATEWAYNODE_MAP_NAME) |
| 126 | .withApplicationId(appId) |
| 127 | .build(); |
| 128 | |
Daniel Park | d4d8801 | 2016-08-05 08:56:53 +0900 | [diff] [blame] | 129 | configRegistry.registerConfigFactory(configFactory); |
| 130 | configService.addListener(configListener); |
| 131 | deviceService.addListener(internalDeviceListener); |
| 132 | |
| 133 | selectGroupHandler = new SelectGroupHandler(groupService, deviceService, driverService, appId); |
| 134 | |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 135 | log.info("started"); |
| 136 | } |
| 137 | |
| 138 | @Deactivate |
| 139 | protected void deactivate() { |
Kyuhwi Choi | 7053788 | 2016-06-24 17:24:12 +0900 | [diff] [blame] | 140 | deviceService.removeListener(internalDeviceListener); |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 141 | configService.removeListener(configListener); |
| 142 | |
| 143 | log.info("stopped"); |
| 144 | } |
Kyuhwi Choi | c5b33ea | 2016-04-26 11:45:32 +0900 | [diff] [blame] | 145 | |
| 146 | @Override |
| 147 | public GatewayNode getGatewayNode(DeviceId deviceId) { |
Hyunsun Moon | f9a16ed | 2016-07-20 21:59:48 -0700 | [diff] [blame] | 148 | GatewayNode gatewayNode = gatewayNodeMap.get(deviceId).value(); |
| 149 | if (gatewayNode == null) { |
| 150 | log.warn("Gateway with device ID {} does not exist"); |
| 151 | return null; |
| 152 | } |
| 153 | return gatewayNode; |
Kyuhwi Choi | c5b33ea | 2016-04-26 11:45:32 +0900 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | @Override |
Hyunsun Moon | f9a16ed | 2016-07-20 21:59:48 -0700 | [diff] [blame] | 157 | public PortNumber getUplinkPort(DeviceId deviceId) { |
| 158 | GatewayNode gatewayNode = gatewayNodeMap.get(deviceId).value(); |
| 159 | if (gatewayNode == null) { |
| 160 | log.warn("Gateway with device ID {} does not exist"); |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame] | 161 | return null; |
| 162 | } |
| 163 | |
Hyunsun Moon | f9a16ed | 2016-07-20 21:59:48 -0700 | [diff] [blame] | 164 | Optional<Port> port = deviceService.getPorts(deviceId).stream() |
| 165 | .filter(p -> Objects.equals( |
| 166 | p.annotations().value(PORT_NAME), |
| 167 | gatewayNode.getUplinkIntf())) |
| 168 | .findFirst(); |
| 169 | if (!port.isPresent()) { |
| 170 | log.warn("Cannot find uplink interface from gateway {}", deviceId); |
| 171 | return null; |
| 172 | } |
sangho | 6032f34 | 2016-07-07 14:32:03 +0900 | [diff] [blame] | 173 | return port.get().number(); |
Kyuhwi Choi | c5b33ea | 2016-04-26 11:45:32 +0900 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | @Override |
Hyunsun Moon | f9a16ed | 2016-07-20 21:59:48 -0700 | [diff] [blame] | 177 | public synchronized GroupId getGatewayGroupId(DeviceId srcDeviceId) { |
| 178 | GroupKey groupKey = selectGroupHandler.getGroupKey(srcDeviceId); |
| 179 | Group group = groupService.getGroup(srcDeviceId, groupKey); |
| 180 | if (group == null) { |
| 181 | log.info("Created gateway group for {}", srcDeviceId); |
| 182 | return selectGroupHandler.createGatewayGroup(srcDeviceId, getGatewayNodes()); |
| 183 | } else { |
| 184 | return group.id(); |
| 185 | } |
Kyuhwi Choi | c5b33ea | 2016-04-26 11:45:32 +0900 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | @Override |
| 189 | public List<GatewayNode> getGatewayNodes() { |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 190 | List<GatewayNode> gatewayNodeList = Lists.newArrayList(); |
| 191 | gatewayNodeMap.values() |
| 192 | .stream() |
Kyuhwi Choi | 7053788 | 2016-06-24 17:24:12 +0900 | [diff] [blame] | 193 | .map(Versioned::value) |
Hyunsun Moon | f9a16ed | 2016-07-20 21:59:48 -0700 | [diff] [blame] | 194 | .forEach(gatewayNodeList::add); |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 195 | return gatewayNodeList; |
Kyuhwi Choi | c5b33ea | 2016-04-26 11:45:32 +0900 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | @Override |
| 199 | public List<DeviceId> getGatewayDeviceIds() { |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 200 | List<DeviceId> deviceIdList = Lists.newArrayList(); |
| 201 | gatewayNodeMap.values() |
| 202 | .stream() |
Kyuhwi Choi | 7053788 | 2016-06-24 17:24:12 +0900 | [diff] [blame] | 203 | .map(Versioned::value) |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 204 | .forEach(gatewayNode -> deviceIdList.add(gatewayNode.getGatewayDeviceId())); |
| 205 | return deviceIdList; |
Kyuhwi Choi | c5b33ea | 2016-04-26 11:45:32 +0900 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | @Override |
Hyunsun Moon | 5aa480b | 2016-08-03 12:23:14 -0700 | [diff] [blame] | 209 | public synchronized boolean addGatewayNode(GatewayNode gatewayNode) { |
sangho | 09a6ffe | 2016-08-23 16:55:38 +0900 | [diff] [blame^] | 210 | Versioned<GatewayNode> existingNode = gatewayNodeMap.put(gatewayNode.getGatewayDeviceId(), |
| 211 | gatewayNode); |
Hyunsun Moon | d331afd | 2016-07-20 02:19:20 -0700 | [diff] [blame] | 212 | if (existingNode == null) { |
Hyunsun Moon | f9a16ed | 2016-07-20 21:59:48 -0700 | [diff] [blame] | 213 | updateGatewayGroup(gatewayNode, true); |
sangho | 09a6ffe | 2016-08-23 16:55:38 +0900 | [diff] [blame^] | 214 | log.info("Gateway {} is added to Gateway pool", gatewayNode); |
| 215 | return true; |
| 216 | } else if (!existingNode.value().equals(gatewayNode)) { |
| 217 | updateGatewayGroup(existingNode.value(), false); |
| 218 | updateGatewayGroup(gatewayNode, true); |
| 219 | log.info("Gateway {} is updated", gatewayNode); |
Hyunsun Moon | d331afd | 2016-07-20 02:19:20 -0700 | [diff] [blame] | 220 | return true; |
| 221 | } else { |
| 222 | return false; |
| 223 | } |
Kyuhwi Choi | c5b33ea | 2016-04-26 11:45:32 +0900 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | @Override |
Hyunsun Moon | 5aa480b | 2016-08-03 12:23:14 -0700 | [diff] [blame] | 227 | public synchronized boolean deleteGatewayNode(GatewayNode gatewayNode) { |
Kyuhwi Choi | 7053788 | 2016-06-24 17:24:12 +0900 | [diff] [blame] | 228 | boolean result = gatewayNodeMap.remove(gatewayNode.getGatewayDeviceId(), gatewayNode); |
| 229 | if (result) { |
Hyunsun Moon | f9a16ed | 2016-07-20 21:59:48 -0700 | [diff] [blame] | 230 | updateGatewayGroup(gatewayNode, false); |
| 231 | log.info("Deleted gateway with device ID {}", gatewayNode.getGatewayDeviceId()); |
Kyuhwi Choi | 7053788 | 2016-06-24 17:24:12 +0900 | [diff] [blame] | 232 | } |
| 233 | return result; |
| 234 | } |
| 235 | |
Hyunsun Moon | f9a16ed | 2016-07-20 21:59:48 -0700 | [diff] [blame] | 236 | private void updateGatewayGroup(GatewayNode gatewayNode, boolean isInsert) { |
| 237 | Tools.stream(deviceService.getAvailableDevices()).forEach(device -> { |
| 238 | Tools.stream(groupService.getGroups(device.id(), appId)).forEach(group -> { |
| 239 | selectGroupHandler.updateGatewayGroupBuckets( |
| 240 | device.id(), |
| 241 | ImmutableList.of(gatewayNode), |
| 242 | isInsert); |
| 243 | log.trace("Updated gateway group on {}", device.id()); |
| 244 | }); |
| 245 | }); |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | private class InternalConfigListener implements NetworkConfigListener { |
| 249 | @Override |
| 250 | public void event(NetworkConfigEvent event) { |
| 251 | if (!event.configClass().equals(GatewayNodeConfig.class)) { |
| 252 | return; |
| 253 | } |
| 254 | switch (event.type()) { |
| 255 | case CONFIG_UPDATED: |
| 256 | gatewayNodeMap.clear(); |
| 257 | readConfiguration(); |
| 258 | break; |
| 259 | case CONFIG_ADDED: |
| 260 | readConfiguration(); |
| 261 | break; |
| 262 | default: |
| 263 | log.debug("Unsupportable event type is occurred"); |
| 264 | break; |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | |
Kyuhwi Choi | 7053788 | 2016-06-24 17:24:12 +0900 | [diff] [blame] | 269 | private class InternalDeviceListener implements DeviceListener { |
| 270 | |
| 271 | @Override |
| 272 | public void event(DeviceEvent deviceEvent) { |
| 273 | if (deviceEvent.type() == DeviceEvent.Type.DEVICE_SUSPENDED || |
| 274 | deviceEvent.type() == DeviceEvent.Type.DEVICE_REMOVED) { |
Hyunsun Moon | f9a16ed | 2016-07-20 21:59:48 -0700 | [diff] [blame] | 275 | DeviceId deviceId = deviceEvent.subject().id(); |
| 276 | deleteGatewayNode(getGatewayNode(deviceId)); |
| 277 | log.warn("Gateway with device ID {} is disconnected", deviceId); |
Kyuhwi Choi | 7053788 | 2016-06-24 17:24:12 +0900 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 282 | private void readConfiguration() { |
| 283 | config = configService.getConfig(appId, GatewayNodeConfig.class); |
| 284 | if (config == null) { |
| 285 | log.error("No configuration found"); |
| 286 | return; |
| 287 | } |
| 288 | |
Hyunsun Moon | f9a16ed | 2016-07-20 21:59:48 -0700 | [diff] [blame] | 289 | config.gatewayNodes().forEach(this::addGatewayNode); |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 290 | log.info("ScalableGateway configured"); |
Kyuhwi Choi | c5b33ea | 2016-04-26 11:45:32 +0900 | [diff] [blame] | 291 | } |
| 292 | } |