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 | |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 19 | import com.google.common.collect.Lists; |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Deactivate; |
| 23 | |
| 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; |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 28 | import org.onosproject.core.ApplicationId; |
| 29 | import org.onosproject.core.CoreService; |
| 30 | |
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; |
| 46 | import org.onosproject.net.group.GroupDescription; |
| 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 | c5b33ea | 2016-04-26 11:45:32 +0900 | [diff] [blame] | 51 | |
| 52 | import java.util.List; |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 53 | |
Kyuhwi Choi | 7053788 | 2016-06-24 17:24:12 +0900 | [diff] [blame^] | 54 | import org.onosproject.store.serializers.KryoNamespaces; |
| 55 | import org.onosproject.store.service.ConsistentMap; |
| 56 | import org.onosproject.store.service.Serializer; |
| 57 | import org.onosproject.store.service.StorageService; |
| 58 | import org.onosproject.store.service.Versioned; |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 59 | import org.slf4j.Logger; |
| 60 | import org.slf4j.LoggerFactory; |
| 61 | |
| 62 | import static com.google.common.base.Preconditions.checkNotNull; |
Kyuhwi Choi | c5b33ea | 2016-04-26 11:45:32 +0900 | [diff] [blame] | 63 | |
| 64 | /** |
| 65 | * Manages gateway node for gateway scalability. |
| 66 | */ |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 67 | |
| 68 | @Service |
| 69 | @Component(immediate = true) |
Kyuhwi Choi | c5b33ea | 2016-04-26 11:45:32 +0900 | [diff] [blame] | 70 | public class ScalableGatewayManager implements ScalableGatewayService { |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 71 | 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 Choi | 7053788 | 2016-06-24 17:24:12 +0900 | [diff] [blame^] | 81 | private static final String GATEWAYNODE_MAP_NAME = "gatewaynode-map"; |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 82 | |
| 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 Choi | b071821 | 2016-06-01 11:33:27 +0900 | [diff] [blame] | 95 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 96 | protected DriverService driverService; |
| 97 | |
| 98 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 99 | protected GroupService groupService; |
| 100 | |
Kyuhwi Choi | 7053788 | 2016-06-24 17:24:12 +0900 | [diff] [blame^] | 101 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 102 | protected StorageService storageService; |
| 103 | |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 104 | private GatewayNodeConfig config; |
Kyuhwi Choi | b071821 | 2016-06-01 11:33:27 +0900 | [diff] [blame] | 105 | private SelectGroupHandler selectGroupHandler; |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 106 | |
| 107 | private final NetworkConfigListener configListener = new InternalConfigListener(); |
Kyuhwi Choi | 7053788 | 2016-06-24 17:24:12 +0900 | [diff] [blame^] | 108 | private InternalDeviceListener internalDeviceListener = new InternalDeviceListener(); |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 109 | |
| 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 Choi | 7053788 | 2016-06-24 17:24:12 +0900 | [diff] [blame^] | 117 | 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 Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 122 | |
| 123 | @Activate |
| 124 | protected void activate() { |
| 125 | appId = coreService.registerApplication(APP_ID); |
| 126 | configRegistry.registerConfigFactory(configFactory); |
| 127 | configService.addListener(configListener); |
Kyuhwi Choi | 7053788 | 2016-06-24 17:24:12 +0900 | [diff] [blame^] | 128 | deviceService.addListener(internalDeviceListener); |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 129 | |
Kyuhwi Choi | b071821 | 2016-06-01 11:33:27 +0900 | [diff] [blame] | 130 | selectGroupHandler = new SelectGroupHandler(groupService, deviceService, driverService, appId); |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 131 | readConfiguration(); |
| 132 | |
Kyuhwi Choi | 7053788 | 2016-06-24 17:24:12 +0900 | [diff] [blame^] | 133 | gatewayNodeMap = storageService.<DeviceId, GatewayNode>consistentMapBuilder() |
| 134 | .withSerializer(Serializer.using(GATEWAYNODE_SERIALIZER.build())) |
| 135 | .withName(GATEWAYNODE_MAP_NAME) |
| 136 | .withApplicationId(appId) |
| 137 | .build(); |
| 138 | |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 139 | log.info("started"); |
| 140 | } |
| 141 | |
| 142 | @Deactivate |
| 143 | protected void deactivate() { |
| 144 | gatewayNodeMap.clear(); |
| 145 | |
Kyuhwi Choi | 7053788 | 2016-06-24 17:24:12 +0900 | [diff] [blame^] | 146 | deviceService.removeListener(internalDeviceListener); |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 147 | configService.removeListener(configListener); |
| 148 | |
| 149 | log.info("stopped"); |
| 150 | } |
Kyuhwi Choi | c5b33ea | 2016-04-26 11:45:32 +0900 | [diff] [blame] | 151 | |
| 152 | @Override |
| 153 | public GatewayNode getGatewayNode(DeviceId deviceId) { |
Kyuhwi Choi | 7053788 | 2016-06-24 17:24:12 +0900 | [diff] [blame^] | 154 | return checkNotNull(gatewayNodeMap.get(deviceId).value(), GATEWAYNODE_CAN_NOT_BE_NULL); |
Kyuhwi Choi | c5b33ea | 2016-04-26 11:45:32 +0900 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public List<PortNumber> getGatewayExternalPorts(DeviceId deviceId) { |
Kyuhwi Choi | 7053788 | 2016-06-24 17:24:12 +0900 | [diff] [blame^] | 159 | GatewayNode gatewayNode = checkNotNull(gatewayNodeMap.get(deviceId).value(), GATEWAYNODE_CAN_NOT_BE_NULL); |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 160 | 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 Choi | c5b33ea | 2016-04-26 11:45:32 +0900 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | @Override |
| 178 | public GroupId getGroupIdForGatewayLoadBalance(DeviceId srcDeviceId) { |
Kyuhwi Choi | b071821 | 2016-06-01 11:33:27 +0900 | [diff] [blame] | 179 | 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 Choi | c5b33ea | 2016-04-26 11:45:32 +0900 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public List<GatewayNode> getGatewayNodes() { |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 187 | List<GatewayNode> gatewayNodeList = Lists.newArrayList(); |
| 188 | gatewayNodeMap.values() |
| 189 | .stream() |
Kyuhwi Choi | 7053788 | 2016-06-24 17:24:12 +0900 | [diff] [blame^] | 190 | .map(Versioned::value) |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 191 | .forEach(gatewayNode -> gatewayNodeList.add(gatewayNode)); |
| 192 | return gatewayNodeList; |
| 193 | |
Kyuhwi Choi | c5b33ea | 2016-04-26 11:45:32 +0900 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | @Override |
| 197 | public List<DeviceId> getGatewayDeviceIds() { |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 198 | List<DeviceId> deviceIdList = Lists.newArrayList(); |
| 199 | gatewayNodeMap.values() |
| 200 | .stream() |
Kyuhwi Choi | 7053788 | 2016-06-24 17:24:12 +0900 | [diff] [blame^] | 201 | .map(Versioned::value) |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 202 | .forEach(gatewayNode -> deviceIdList.add(gatewayNode.getGatewayDeviceId())); |
| 203 | return deviceIdList; |
| 204 | |
Kyuhwi Choi | c5b33ea | 2016-04-26 11:45:32 +0900 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | @Override |
| 208 | public boolean addGatewayNode(GatewayNode gatewayNode) { |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 209 | gatewayNodeMap.putIfAbsent(gatewayNode.getGatewayDeviceId(), gatewayNode); |
Kyuhwi Choi | 7053788 | 2016-06-24 17:24:12 +0900 | [diff] [blame^] | 210 | updateGatewayLoadBalance(gatewayNode, true); |
Kyuhwi Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 211 | return true; |
Kyuhwi Choi | c5b33ea | 2016-04-26 11:45:32 +0900 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | @Override |
| 215 | public boolean deleteGatewayNode(GatewayNode gatewayNode) { |
Kyuhwi Choi | 7053788 | 2016-06-24 17:24:12 +0900 | [diff] [blame^] | 216 | 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 Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 228 | } |
| 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 Choi | 7053788 | 2016-06-24 17:24:12 +0900 | [diff] [blame^] | 251 | 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 Choi | dc2973b | 2016-05-13 14:54:31 +0900 | [diff] [blame] | 262 | 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 Choi | c5b33ea | 2016-04-26 11:45:32 +0900 | [diff] [blame] | 272 | } |
| 273 | } |