Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 8e57fd5 | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 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 | */ |
alshabib | b4d3171 | 2016-06-01 18:51:03 -0700 | [diff] [blame] | 16 | package org.opencord.cordvtn.impl; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 17 | |
Hyunsun Moon | 8b530e3 | 2016-02-03 00:11:11 -0800 | [diff] [blame] | 18 | import com.google.common.collect.Lists; |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 19 | import com.google.common.collect.Maps; |
Hyunsun Moon | 7dca9b3 | 2015-10-08 22:25:30 -0700 | [diff] [blame] | 20 | import com.google.common.collect.Sets; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [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 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 26 | import org.apache.felix.scr.annotations.Service; |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 27 | import org.onlab.packet.Ethernet; |
Hyunsun Moon | 8b530e3 | 2016-02-03 00:11:11 -0800 | [diff] [blame] | 28 | import org.onlab.packet.Ip4Address; |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 29 | import org.onlab.packet.Ip4Prefix; |
alshabib | b4d3171 | 2016-06-01 18:51:03 -0700 | [diff] [blame] | 30 | import org.opencord.cordvtn.api.CordVtnNode; |
| 31 | import org.opencord.cordvtn.api.CordVtnService; |
| 32 | import org.opencord.cordvtn.api.Instance; |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 33 | import org.onosproject.core.DefaultGroupId; |
| 34 | import org.onosproject.core.GroupId; |
| 35 | import org.onosproject.net.DeviceId; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 36 | import org.onosproject.net.Host; |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 37 | import org.onosproject.net.PortNumber; |
| 38 | import org.onosproject.net.flow.DefaultFlowRule; |
| 39 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 40 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 41 | import org.onosproject.net.flow.FlowRule; |
| 42 | import org.onosproject.net.flow.TrafficSelector; |
| 43 | import org.onosproject.net.flow.TrafficTreatment; |
| 44 | import org.onosproject.net.flow.instructions.ExtensionTreatment; |
| 45 | import org.onosproject.net.group.DefaultGroupDescription; |
| 46 | import org.onosproject.net.group.DefaultGroupKey; |
| 47 | import org.onosproject.net.group.Group; |
| 48 | import org.onosproject.net.group.GroupBucket; |
| 49 | import org.onosproject.net.group.GroupBuckets; |
| 50 | import org.onosproject.net.group.GroupDescription; |
| 51 | import org.onosproject.net.group.GroupKey; |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 52 | import org.onosproject.net.group.GroupService; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 53 | import org.onosproject.net.host.HostEvent; |
| 54 | import org.onosproject.net.host.HostListener; |
Hyunsun Moon | 486ed1b | 2016-05-13 18:58:35 -0700 | [diff] [blame] | 55 | import org.onosproject.xosclient.api.VtnService; |
Hyunsun Moon | 5f51f62 | 2016-05-13 04:17:53 -0700 | [diff] [blame] | 56 | import org.onosproject.xosclient.api.VtnServiceId; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 57 | import org.slf4j.Logger; |
| 58 | |
Hyunsun Moon | 8b530e3 | 2016-02-03 00:11:11 -0800 | [diff] [blame] | 59 | import java.util.List; |
Hyunsun Moon | 7dca9b3 | 2015-10-08 22:25:30 -0700 | [diff] [blame] | 60 | import java.util.Map; |
Hyunsun Moon | d05b32e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 61 | import java.util.Objects; |
Hyunsun Moon | 05f528a | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 62 | import java.util.Set; |
Hyunsun Moon | 05f528a | 2015-11-04 17:34:35 -0800 | [diff] [blame] | 63 | import java.util.stream.Collectors; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 64 | |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 65 | import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 66 | import static org.onlab.util.Tools.groupedThreads; |
alshabib | b4d3171 | 2016-06-01 18:51:03 -0700 | [diff] [blame] | 67 | import static org.opencord.cordvtn.impl.CordVtnPipeline.*; |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 68 | import static org.onosproject.net.group.DefaultGroupBucket.createSelectGroupBucket; |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 69 | import static org.slf4j.LoggerFactory.getLogger; |
| 70 | |
| 71 | /** |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 72 | * Provisions service dependency capabilities between network services. |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 73 | */ |
| 74 | @Component(immediate = true) |
| 75 | @Service |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 76 | public class CordVtn extends CordVtnInstanceHandler implements CordVtnService { |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 77 | |
| 78 | protected final Logger log = getLogger(getClass()); |
| 79 | |
| 80 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 81 | protected GroupService groupService; |
| 82 | |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 83 | @Activate |
| 84 | protected void activate() { |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 85 | eventExecutor = newSingleThreadScheduledExecutor(groupedThreads("onos/cordvtn", "event-handler")); |
| 86 | hostListener = new InternalHostListener(); |
| 87 | super.activate(); |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | @Deactivate |
| 91 | protected void deactivate() { |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 92 | super.deactivate(); |
Hyunsun Moon | de37257 | 2016-01-14 03:42:47 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | @Override |
Hyunsun Moon | 486ed1b | 2016-05-13 18:58:35 -0700 | [diff] [blame] | 96 | public void createServiceDependency(VtnServiceId tServiceId, VtnServiceId pServiceId, |
Hyunsun Moon | 5f7ed8a | 2016-02-10 17:02:37 -0800 | [diff] [blame] | 97 | boolean isBidirectional) { |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 98 | VtnService tService = getVtnService(tServiceId); |
| 99 | VtnService pService = getVtnService(pServiceId); |
Hyunsun Moon | 97eaf50 | 2015-12-07 14:06:28 -0800 | [diff] [blame] | 100 | |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 101 | if (tService == null || pService == null) { |
Hyunsun Moon | 486ed1b | 2016-05-13 18:58:35 -0700 | [diff] [blame] | 102 | log.error("Failed to create dependency between {} and {}", |
| 103 | tServiceId, pServiceId); |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 104 | return; |
| 105 | } |
| 106 | |
Hyunsun Moon | 486ed1b | 2016-05-13 18:58:35 -0700 | [diff] [blame] | 107 | log.info("Created dependency between {} and {}", tService.name(), pService.name()); |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 108 | serviceDependencyRules(tService, pService, isBidirectional, true); |
Hyunsun Moon | 9274aaf | 2015-12-04 11:35:25 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | @Override |
Hyunsun Moon | 486ed1b | 2016-05-13 18:58:35 -0700 | [diff] [blame] | 112 | public void removeServiceDependency(VtnServiceId tServiceId, VtnServiceId pServiceId) { |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 113 | VtnService tService = getVtnService(tServiceId); |
| 114 | VtnService pService = getVtnService(pServiceId); |
Hyunsun Moon | 97eaf50 | 2015-12-07 14:06:28 -0800 | [diff] [blame] | 115 | |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 116 | if (tService == null || pService == null) { |
Hyunsun Moon | 486ed1b | 2016-05-13 18:58:35 -0700 | [diff] [blame] | 117 | log.error("Failed to remove dependency between {} and {}", |
| 118 | tServiceId, pServiceId); |
Hyunsun Moon | ba29007 | 2015-12-16 20:53:23 -0800 | [diff] [blame] | 119 | return; |
| 120 | } |
| 121 | |
Hyunsun Moon | 486ed1b | 2016-05-13 18:58:35 -0700 | [diff] [blame] | 122 | log.info("Removed dependency between {} and {}", tService.name(), pService.name()); |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 123 | serviceDependencyRules(tService, pService, true, false); |
Hyunsun Moon | 9274aaf | 2015-12-04 11:35:25 -0800 | [diff] [blame] | 124 | } |
| 125 | |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 126 | @Override |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 127 | public void instanceDetected(Instance instance) { |
| 128 | VtnService service = getVtnService(instance.serviceId()); |
| 129 | if (service == null) { |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 130 | return; |
| 131 | } |
| 132 | |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 133 | // TODO get bidirectional information from XOS once XOS supports |
| 134 | service.tenantServices().stream().forEach( |
| 135 | tServiceId -> createServiceDependency(tServiceId, service.id(), true)); |
| 136 | service.providerServices().stream().forEach( |
| 137 | pServiceId -> createServiceDependency(service.id(), pServiceId, true)); |
Hyunsun Moon | 01556a5 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 138 | |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 139 | updateProviderServiceInstances(service); |
Hyunsun Moon | 4edb017 | 2015-11-07 22:08:43 -0800 | [diff] [blame] | 140 | } |
| 141 | |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 142 | @Override |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 143 | public void instanceRemoved(Instance instance) { |
| 144 | VtnService service = getVtnService(instance.serviceId()); |
| 145 | if (service == null) { |
Hyunsun Moon | 01556a5 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 146 | return; |
| 147 | } |
| 148 | |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 149 | if (!service.providerServices().isEmpty()) { |
| 150 | removeInstanceFromTenantService(instance, service); |
Hyunsun Moon | d05b32e | 2016-03-02 19:27:26 -0800 | [diff] [blame] | 151 | } |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 152 | if (!service.tenantServices().isEmpty()) { |
| 153 | updateProviderServiceInstances(service); |
Hyunsun Moon | 01556a5 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 157 | private void updateProviderServiceInstances(VtnService service) { |
| 158 | GroupKey groupKey = getGroupKey(service.id()); |
| 159 | |
| 160 | Set<DeviceId> devices = nodeManager.completeNodes().stream() |
| 161 | .map(CordVtnNode::intBrId) |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 162 | .collect(Collectors.toSet()); |
Hyunsun Moon | 01556a5 | 2016-02-12 12:48:47 -0800 | [diff] [blame] | 163 | |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 164 | for (DeviceId deviceId : devices) { |
| 165 | Group group = groupService.getGroup(deviceId, groupKey); |
| 166 | if (group == null) { |
| 167 | log.trace("No group exists for service {} in {}", service.id(), deviceId); |
| 168 | continue; |
Hyunsun Moon | 7004fcf | 2016-03-08 04:36:02 -0800 | [diff] [blame] | 169 | } |
| 170 | |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 171 | List<GroupBucket> oldBuckets = group.buckets().buckets(); |
| 172 | List<GroupBucket> newBuckets = getServiceGroupBuckets( |
| 173 | deviceId, service.vni(), getInstances(service.id())).buckets(); |
Hyunsun Moon | 7004fcf | 2016-03-08 04:36:02 -0800 | [diff] [blame] | 174 | |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 175 | if (oldBuckets.equals(newBuckets)) { |
| 176 | continue; |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 177 | } |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 178 | |
| 179 | List<GroupBucket> bucketsToRemove = Lists.newArrayList(oldBuckets); |
| 180 | bucketsToRemove.removeAll(newBuckets); |
| 181 | if (!bucketsToRemove.isEmpty()) { |
| 182 | groupService.removeBucketsFromGroup( |
| 183 | deviceId, |
| 184 | groupKey, |
| 185 | new GroupBuckets(bucketsToRemove), |
| 186 | groupKey, appId); |
| 187 | } |
| 188 | |
| 189 | List<GroupBucket> bucketsToAdd = Lists.newArrayList(newBuckets); |
| 190 | bucketsToAdd.removeAll(oldBuckets); |
| 191 | if (!bucketsToAdd.isEmpty()) { |
| 192 | groupService.addBucketsToGroup( |
| 193 | deviceId, |
| 194 | groupKey, |
| 195 | new GroupBuckets(bucketsToAdd), |
| 196 | groupKey, appId); |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | private void removeInstanceFromTenantService(Instance instance, VtnService service) { |
| 202 | service.providerServices().stream().forEach(pServiceId -> { |
| 203 | Map<DeviceId, Set<PortNumber>> inPorts = Maps.newHashMap(); |
| 204 | Map<DeviceId, GroupId> outGroups = Maps.newHashMap(); |
| 205 | |
| 206 | inPorts.put(instance.deviceId(), Sets.newHashSet(instance.portNumber())); |
| 207 | outGroups.put(instance.deviceId(), getGroupId(pServiceId, instance.deviceId())); |
| 208 | |
| 209 | inServiceRule(inPorts, outGroups, false); |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 210 | }); |
| 211 | } |
| 212 | |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 213 | private void serviceDependencyRules(VtnService tService, VtnService pService, |
| 214 | boolean isBidirectional, boolean install) { |
| 215 | Map<DeviceId, GroupId> outGroups = Maps.newHashMap(); |
| 216 | Map<DeviceId, Set<PortNumber>> inPorts = Maps.newHashMap(); |
| 217 | |
| 218 | nodeManager.completeNodes().stream().forEach(node -> { |
| 219 | DeviceId deviceId = node.intBrId(); |
| 220 | GroupId groupId = createServiceGroup(deviceId, pService); |
| 221 | outGroups.put(deviceId, groupId); |
| 222 | |
| 223 | Set<PortNumber> tServiceInstances = getInstances(tService.id()) |
| 224 | .stream() |
| 225 | .filter(instance -> instance.deviceId().equals(deviceId)) |
| 226 | .map(Instance::portNumber) |
| 227 | .collect(Collectors.toSet()); |
| 228 | inPorts.put(deviceId, tServiceInstances); |
| 229 | }); |
| 230 | |
| 231 | Ip4Prefix srcRange = tService.subnet().getIp4Prefix(); |
| 232 | Ip4Prefix dstRange = pService.subnet().getIp4Prefix(); |
| 233 | |
| 234 | indirectAccessRule(srcRange, pService.serviceIp().getIp4Address(), outGroups, install); |
| 235 | directAccessRule(srcRange, dstRange, install); |
| 236 | if (isBidirectional) { |
| 237 | directAccessRule(dstRange, srcRange, install); |
| 238 | } |
| 239 | inServiceRule(inPorts, outGroups, install); |
Hyunsun Moon | b5f92e5 | 2016-02-17 15:02:06 -0800 | [diff] [blame] | 240 | } |
| 241 | |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 242 | private void indirectAccessRule(Ip4Prefix srcRange, Ip4Address serviceIp, |
| 243 | Map<DeviceId, GroupId> outGroups, boolean install) { |
| 244 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 245 | .matchEthType(Ethernet.TYPE_IPV4) |
| 246 | .matchIPSrc(srcRange) |
| 247 | .matchIPDst(serviceIp.toIpPrefix()) |
| 248 | .build(); |
| 249 | |
| 250 | for (Map.Entry<DeviceId, GroupId> outGroup : outGroups.entrySet()) { |
| 251 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 252 | .group(outGroup.getValue()) |
| 253 | .build(); |
| 254 | |
| 255 | FlowRule flowRule = DefaultFlowRule.builder() |
| 256 | .fromApp(appId) |
| 257 | .withSelector(selector) |
| 258 | .withTreatment(treatment) |
| 259 | .withPriority(PRIORITY_HIGH) |
| 260 | .forDevice(outGroup.getKey()) |
| 261 | .forTable(TABLE_ACCESS_TYPE) |
| 262 | .makePermanent() |
| 263 | .build(); |
| 264 | |
| 265 | pipeline.processFlowRule(install, flowRule); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | private void directAccessRule(Ip4Prefix srcRange, Ip4Prefix dstRange, boolean install) { |
| 270 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 271 | .matchEthType(Ethernet.TYPE_IPV4) |
| 272 | .matchIPSrc(srcRange) |
| 273 | .matchIPDst(dstRange) |
| 274 | .build(); |
| 275 | |
| 276 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 277 | .transition(TABLE_DST_IP) |
| 278 | .build(); |
| 279 | |
| 280 | nodeManager.completeNodes().stream().forEach(node -> { |
| 281 | DeviceId deviceId = node.intBrId(); |
| 282 | FlowRule flowRuleDirect = DefaultFlowRule.builder() |
| 283 | .fromApp(appId) |
| 284 | .withSelector(selector) |
| 285 | .withTreatment(treatment) |
| 286 | .withPriority(PRIORITY_DEFAULT) |
| 287 | .forDevice(deviceId) |
| 288 | .forTable(TABLE_ACCESS_TYPE) |
| 289 | .makePermanent() |
| 290 | .build(); |
| 291 | |
| 292 | pipeline.processFlowRule(install, flowRuleDirect); |
| 293 | }); |
| 294 | } |
| 295 | |
| 296 | private void inServiceRule(Map<DeviceId, Set<PortNumber>> inPorts, |
| 297 | Map<DeviceId, GroupId> outGroups, boolean install) { |
| 298 | for (Map.Entry<DeviceId, Set<PortNumber>> entry : inPorts.entrySet()) { |
| 299 | Set<PortNumber> ports = entry.getValue(); |
| 300 | DeviceId deviceId = entry.getKey(); |
| 301 | |
| 302 | GroupId groupId = outGroups.get(deviceId); |
| 303 | if (groupId == null) { |
| 304 | continue; |
| 305 | } |
| 306 | |
| 307 | ports.stream().forEach(port -> { |
| 308 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 309 | .matchInPort(port) |
| 310 | .build(); |
| 311 | |
| 312 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 313 | .group(groupId) |
| 314 | .build(); |
| 315 | |
| 316 | FlowRule flowRule = DefaultFlowRule.builder() |
| 317 | .fromApp(appId) |
| 318 | .withSelector(selector) |
| 319 | .withTreatment(treatment) |
| 320 | .withPriority(PRIORITY_DEFAULT) |
| 321 | .forDevice(deviceId) |
| 322 | .forTable(TABLE_IN_SERVICE) |
| 323 | .makePermanent() |
| 324 | .build(); |
| 325 | |
| 326 | pipeline.processFlowRule(install, flowRule); |
| 327 | }); |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | private GroupId getGroupId(VtnServiceId serviceId, DeviceId deviceId) { |
| 332 | return new DefaultGroupId(Objects.hash(serviceId, deviceId)); |
| 333 | } |
| 334 | |
| 335 | private GroupKey getGroupKey(VtnServiceId serviceId) { |
| 336 | return new DefaultGroupKey(serviceId.id().getBytes()); |
| 337 | } |
| 338 | |
| 339 | private GroupId createServiceGroup(DeviceId deviceId, VtnService service) { |
| 340 | GroupKey groupKey = getGroupKey(service.id()); |
| 341 | Group group = groupService.getGroup(deviceId, groupKey); |
| 342 | GroupId groupId = getGroupId(service.id(), deviceId); |
| 343 | |
| 344 | if (group != null) { |
| 345 | log.debug("Group {} is already exist in {}", service.id(), deviceId); |
| 346 | return groupId; |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 347 | } |
| 348 | |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 349 | GroupBuckets buckets = getServiceGroupBuckets( |
| 350 | deviceId, service.vni(), getInstances(service.id())); |
| 351 | GroupDescription groupDescription = new DefaultGroupDescription( |
| 352 | deviceId, |
| 353 | GroupDescription.Type.SELECT, |
| 354 | buckets, |
| 355 | groupKey, |
| 356 | groupId.id(), |
| 357 | appId); |
Hyunsun Moon | 486ed1b | 2016-05-13 18:58:35 -0700 | [diff] [blame] | 358 | |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 359 | groupService.addGroup(groupDescription); |
| 360 | return groupId; |
| 361 | } |
| 362 | |
| 363 | private GroupBuckets getServiceGroupBuckets(DeviceId deviceId, long tunnelId, |
| 364 | Set<Instance> instances) { |
| 365 | List<GroupBucket> buckets = Lists.newArrayList(); |
| 366 | instances.stream().forEach(instance -> { |
| 367 | Ip4Address tunnelIp = nodeManager.dpIp(instance.deviceId()).getIp4Address(); |
| 368 | TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder(); |
| 369 | |
| 370 | if (deviceId.equals(instance.deviceId())) { |
| 371 | tBuilder.setEthDst(instance.mac()) |
| 372 | .setOutput(instance.portNumber()); |
| 373 | } else { |
| 374 | ExtensionTreatment tunnelDst = |
| 375 | pipeline.tunnelDstTreatment(deviceId, tunnelIp); |
| 376 | tBuilder.setEthDst(instance.mac()) |
| 377 | .extension(tunnelDst, deviceId) |
| 378 | .setTunnelId(tunnelId) |
| 379 | .setOutput(nodeManager.tunnelPort(instance.deviceId())); |
| 380 | } |
| 381 | buckets.add(createSelectGroupBucket(tBuilder.build())); |
| 382 | }); |
| 383 | return new GroupBuckets(buckets); |
Hyunsun Moon | 61e79ee | 2016-04-14 19:04:23 -0700 | [diff] [blame] | 384 | } |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 385 | |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 386 | private class InternalHostListener implements HostListener { |
| 387 | |
| 388 | @Override |
| 389 | public void event(HostEvent event) { |
Hyunsun Moon | cb79944 | 2016-01-15 20:03:18 -0800 | [diff] [blame] | 390 | Host host = event.subject(); |
Hyunsun Moon | e58a4ad | 2016-03-08 21:59:13 -0800 | [diff] [blame] | 391 | if (!mastershipService.isLocalMaster(host.location().deviceId())) { |
| 392 | // do not allow to proceed without mastership |
| 393 | return; |
| 394 | } |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 395 | |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 396 | Instance instance = Instance.of(host); |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 397 | switch (event.type()) { |
Hyunsun Moon | e58a4ad | 2016-03-08 21:59:13 -0800 | [diff] [blame] | 398 | case HOST_UPDATED: |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 399 | case HOST_ADDED: |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 400 | eventExecutor.execute(() -> instanceDetected(instance)); |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 401 | break; |
| 402 | case HOST_REMOVED: |
Hyunsun Moon | e7e4bb3 | 2016-05-16 04:32:45 -0700 | [diff] [blame] | 403 | eventExecutor.execute(() -> instanceRemoved(instance)); |
Hyunsun Moon | 3fc17f7 | 2016-01-24 21:47:06 -0800 | [diff] [blame] | 404 | break; |
| 405 | default: |
| 406 | break; |
| 407 | } |
Hyunsun Moon | 022272f | 2016-01-11 15:30:42 -0800 | [diff] [blame] | 408 | } |
| 409 | } |
Hyunsun Moon | e9d7599 | 2015-09-15 22:39:16 -0700 | [diff] [blame] | 410 | } |