Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [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 | */ |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 16 | package org.opencord.cordvtn.impl.handler; |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 17 | |
| 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Reference; |
| 22 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 23 | import org.onlab.packet.Ethernet; |
| 24 | import org.onosproject.net.PortNumber; |
| 25 | import org.onosproject.net.flow.DefaultFlowRule; |
| 26 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 27 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 28 | import org.onosproject.net.flow.FlowRule; |
| 29 | import org.onosproject.net.flow.TrafficSelector; |
| 30 | import org.onosproject.net.flow.TrafficTreatment; |
| 31 | import org.onosproject.xosclient.api.VtnService; |
| 32 | import org.opencord.cordvtn.impl.AbstractInstanceHandler; |
| 33 | import org.opencord.cordvtn.api.Instance; |
| 34 | import org.opencord.cordvtn.api.InstanceHandler; |
| 35 | import org.opencord.cordvtn.impl.CordVtnPipeline; |
| 36 | |
| 37 | import java.util.Optional; |
| 38 | |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 39 | import static org.onosproject.xosclient.api.VtnServiceApi.ServiceType.MANAGEMENT; |
| 40 | |
| 41 | /** |
| 42 | * Provides network connectivity for management network connected instances. |
| 43 | */ |
| 44 | @Component(immediate = true) |
| 45 | public class ManagementInstanceHandler extends AbstractInstanceHandler implements InstanceHandler { |
| 46 | |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 47 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 48 | protected CordVtnPipeline pipeline; |
| 49 | |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 50 | @Activate |
| 51 | protected void activate() { |
| 52 | serviceType = Optional.of(MANAGEMENT); |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 53 | super.activate(); |
| 54 | } |
| 55 | |
| 56 | @Deactivate |
| 57 | protected void deactivate() { |
| 58 | super.deactivate(); |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public void instanceDetected(Instance instance) { |
Hyunsun Moon | 315b9a6 | 2016-06-23 14:48:04 -0700 | [diff] [blame] | 63 | log.info("Instance is detected {}", instance); |
| 64 | |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 65 | VtnService service = getVtnService(instance.serviceId()); |
| 66 | if (service == null) { |
| 67 | log.warn("Failed to get VtnService for {}", instance); |
| 68 | return; |
| 69 | } |
Hyunsun Moon | 315b9a6 | 2016-06-23 14:48:04 -0700 | [diff] [blame] | 70 | localMgmtNetworkRules(instance, service, true); |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public void instanceRemoved(Instance instance) { |
Hyunsun Moon | 315b9a6 | 2016-06-23 14:48:04 -0700 | [diff] [blame] | 75 | log.info("Instance is removed {}", instance); |
| 76 | |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 77 | VtnService service = getVtnService(instance.serviceId()); |
| 78 | if (service == null) { |
| 79 | log.warn("Failed to get VtnService for {}", instance); |
| 80 | return; |
| 81 | } |
| 82 | |
Hyunsun Moon | 315b9a6 | 2016-06-23 14:48:04 -0700 | [diff] [blame] | 83 | // TODO check if any stale management network rules are |
| 84 | localMgmtNetworkRules(instance, service, false); |
| 85 | } |
| 86 | |
| 87 | private void localMgmtNetworkRules(Instance instance, VtnService service, boolean install) { |
| 88 | managementPerInstanceRule(instance, install); |
| 89 | if (install) { |
| 90 | managementBaseRule(instance, service, true); |
| 91 | } else if (!hostService.getConnectedHosts(instance.deviceId()).stream() |
| 92 | .filter(host -> Instance.of(host).serviceId().equals(service.id())) |
| 93 | .findAny() |
| 94 | .isPresent()) { |
| 95 | managementBaseRule(instance, service, false); |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
Hyunsun Moon | 315b9a6 | 2016-06-23 14:48:04 -0700 | [diff] [blame] | 99 | private void managementBaseRule(Instance instance, VtnService service, boolean install) { |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 100 | TrafficSelector selector = DefaultTrafficSelector.builder() |
Hyunsun Moon | 315b9a6 | 2016-06-23 14:48:04 -0700 | [diff] [blame] | 101 | .matchEthType(Ethernet.TYPE_ARP) |
| 102 | .matchArpTpa(service.serviceIp().getIp4Address()) |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 103 | .build(); |
| 104 | |
| 105 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
Hyunsun Moon | 315b9a6 | 2016-06-23 14:48:04 -0700 | [diff] [blame] | 106 | .setOutput(PortNumber.LOCAL) |
Hyunsun Moon | acbc8ef | 2016-06-21 17:58:45 -0700 | [diff] [blame] | 107 | .build(); |
| 108 | |
| 109 | FlowRule flowRule = DefaultFlowRule.builder() |
| 110 | .fromApp(appId) |
| 111 | .withSelector(selector) |
| 112 | .withTreatment(treatment) |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 113 | .withPriority(CordVtnPipeline.PRIORITY_MANAGEMENT) |
| 114 | .forDevice(instance.deviceId()) |
Hyunsun Moon | 315b9a6 | 2016-06-23 14:48:04 -0700 | [diff] [blame] | 115 | .forTable(CordVtnPipeline.TABLE_ZERO) |
| 116 | .makePermanent() |
| 117 | .build(); |
| 118 | |
| 119 | pipeline.processFlowRule(install, flowRule); |
| 120 | |
| 121 | selector = DefaultTrafficSelector.builder() |
| 122 | .matchInPort(PortNumber.LOCAL) |
| 123 | .matchEthType(Ethernet.TYPE_IPV4) |
| 124 | .matchIPDst(service.subnet()) |
| 125 | .build(); |
| 126 | |
| 127 | treatment = DefaultTrafficTreatment.builder() |
| 128 | .transition(CordVtnPipeline.TABLE_DST_IP) |
| 129 | .build(); |
| 130 | |
| 131 | flowRule = DefaultFlowRule.builder() |
| 132 | .fromApp(appId) |
| 133 | .withSelector(selector) |
| 134 | .withTreatment(treatment) |
| 135 | .withPriority(CordVtnPipeline.PRIORITY_MANAGEMENT) |
| 136 | .forDevice(instance.deviceId()) |
| 137 | .forTable(CordVtnPipeline.TABLE_ZERO) |
| 138 | .makePermanent() |
| 139 | .build(); |
| 140 | |
| 141 | pipeline.processFlowRule(install, flowRule); |
| 142 | |
| 143 | selector = DefaultTrafficSelector.builder() |
| 144 | .matchEthType(Ethernet.TYPE_IPV4) |
| 145 | .matchIPDst(service.serviceIp().toIpPrefix()) |
| 146 | .build(); |
| 147 | |
| 148 | treatment = DefaultTrafficTreatment.builder() |
| 149 | .setOutput(PortNumber.LOCAL) |
| 150 | .build(); |
| 151 | |
| 152 | flowRule = DefaultFlowRule.builder() |
| 153 | .fromApp(appId) |
| 154 | .withSelector(selector) |
| 155 | .withTreatment(treatment) |
| 156 | .withPriority(CordVtnPipeline.PRIORITY_MANAGEMENT) |
| 157 | .forDevice(instance.deviceId()) |
| 158 | .forTable(CordVtnPipeline.TABLE_ZERO) |
| 159 | .makePermanent() |
| 160 | .build(); |
| 161 | |
| 162 | pipeline.processFlowRule(install, flowRule); |
| 163 | } |
| 164 | |
| 165 | private void managementPerInstanceRule(Instance instance, boolean install) { |
| 166 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 167 | .matchInPort(PortNumber.LOCAL) |
| 168 | .matchEthType(Ethernet.TYPE_ARP) |
| 169 | .matchArpTpa(instance.ipAddress().getIp4Address()) |
| 170 | .build(); |
| 171 | |
| 172 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 173 | .setOutput(instance.portNumber()) |
| 174 | .build(); |
| 175 | |
| 176 | FlowRule flowRule = DefaultFlowRule.builder() |
| 177 | .fromApp(appId) |
| 178 | .withSelector(selector) |
| 179 | .withTreatment(treatment) |
| 180 | .withPriority(CordVtnPipeline.PRIORITY_MANAGEMENT) |
| 181 | .forDevice(instance.deviceId()) |
| 182 | .forTable(CordVtnPipeline.TABLE_ZERO) |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 183 | .makePermanent() |
| 184 | .build(); |
| 185 | |
| 186 | pipeline.processFlowRule(install, flowRule); |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 187 | |
| 188 | selector = DefaultTrafficSelector.builder() |
| 189 | .matchEthType(Ethernet.TYPE_IPV4) |
| 190 | .matchIPDst(instance.ipAddress().toIpPrefix()) |
| 191 | .build(); |
| 192 | |
| 193 | treatment = DefaultTrafficTreatment.builder() |
| 194 | .setEthDst(instance.mac()) |
| 195 | .setOutput(instance.portNumber()) |
| 196 | .build(); |
| 197 | |
| 198 | flowRule = DefaultFlowRule.builder() |
| 199 | .fromApp(appId) |
| 200 | .withSelector(selector) |
| 201 | .withTreatment(treatment) |
| 202 | .withPriority(CordVtnPipeline.PRIORITY_DEFAULT) |
| 203 | .forDevice(instance.deviceId()) |
Hyunsun Moon | fb41794 | 2016-06-23 14:48:20 -0700 | [diff] [blame] | 204 | .forTable(CordVtnPipeline.TABLE_DST_IP) |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 205 | .makePermanent() |
| 206 | .build(); |
| 207 | |
| 208 | pipeline.processFlowRule(install, flowRule); |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 209 | } |
| 210 | } |