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