Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 80dff97 | 2017-08-03 22:46:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -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 | */ |
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 | |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableSet; |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 24 | import org.onlab.packet.Ethernet; |
| 25 | import org.onosproject.net.PortNumber; |
| 26 | import org.onosproject.net.flow.DefaultFlowRule; |
| 27 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 28 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 29 | import org.onosproject.net.flow.FlowRule; |
| 30 | import org.onosproject.net.flow.TrafficSelector; |
| 31 | import org.onosproject.net.flow.TrafficTreatment; |
Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 32 | import org.opencord.cordvtn.api.core.CordVtnPipeline; |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 33 | import org.opencord.cordvtn.api.core.Instance; |
| 34 | import org.opencord.cordvtn.api.core.InstanceHandler; |
Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 35 | import org.opencord.cordvtn.api.core.ServiceNetworkService; |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 36 | import org.opencord.cordvtn.api.net.ServiceNetwork; |
Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 37 | import org.opencord.cordvtn.api.node.CordVtnNodeService; |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 38 | |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 39 | import static org.opencord.cordvtn.api.net.ServiceNetwork.NetworkType.MANAGEMENT_HOST; |
| 40 | import static org.opencord.cordvtn.api.net.ServiceNetwork.NetworkType.MANAGEMENT_LOCAL; |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 41 | |
| 42 | /** |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 43 | * Provides local management network connectivity to the instance. The instance |
| 44 | * is only accessible via local management network from the host machine. |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 45 | */ |
| 46 | @Component(immediate = true) |
| 47 | public class ManagementInstanceHandler extends AbstractInstanceHandler implements InstanceHandler { |
| 48 | |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 49 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 50 | protected ServiceNetworkService snetService; |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 51 | |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 52 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 53 | protected CordVtnNodeService nodeService; |
| 54 | |
| 55 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 56 | protected CordVtnPipeline pipeline; |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 57 | |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 58 | @Activate |
| 59 | protected void activate() { |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 60 | netTypes = ImmutableSet.of(MANAGEMENT_LOCAL, MANAGEMENT_HOST); |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 61 | super.activate(); |
| 62 | } |
| 63 | |
| 64 | @Deactivate |
| 65 | protected void deactivate() { |
| 66 | super.deactivate(); |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public void instanceDetected(Instance instance) { |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 71 | ServiceNetwork vtnNet = getServiceNetwork(instance); |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 72 | |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 73 | switch (vtnNet.type()) { |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 74 | case MANAGEMENT_LOCAL: |
| 75 | log.info("LOCAL management instance is detected {}", instance); |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 76 | populateLocalManagementRules(instance, true); |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 77 | break; |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 78 | case MANAGEMENT_HOST: |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 79 | log.info("HOSTS management instance is detected {}", instance); |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 80 | populateHostsManagementRules(instance, true); |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 81 | break; |
| 82 | default: |
| 83 | break; |
| 84 | } |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public void instanceRemoved(Instance instance) { |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 89 | ServiceNetwork vtnNet = getServiceNetwork(instance); |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 90 | |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 91 | switch (vtnNet.type()) { |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 92 | case MANAGEMENT_LOCAL: |
| 93 | log.info("LOCAL management instance removed {}", instance); |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 94 | populateLocalManagementRules(instance, false); |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 95 | break; |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 96 | case MANAGEMENT_HOST: |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 97 | log.info("HOSTS management instance removed {}", instance); |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 98 | populateHostsManagementRules(instance, false); |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 99 | break; |
| 100 | default: |
| 101 | break; |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 105 | private void populateLocalManagementRules(Instance instance, boolean install) { |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 106 | TrafficSelector selector = DefaultTrafficSelector.builder() |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 107 | .matchEthType(Ethernet.TYPE_IPV4) |
| 108 | .matchIPDst(instance.ipAddress().toIpPrefix()) |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 109 | .build(); |
| 110 | |
| 111 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 112 | .setEthDst(instance.mac()) |
Hyunsun Moon | 315b9a6 | 2016-06-23 14:48:04 -0700 | [diff] [blame] | 113 | .setOutput(instance.portNumber()) |
| 114 | .build(); |
| 115 | |
| 116 | FlowRule flowRule = DefaultFlowRule.builder() |
| 117 | .fromApp(appId) |
| 118 | .withSelector(selector) |
| 119 | .withTreatment(treatment) |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 120 | .withPriority(CordVtnPipeline.PRIORITY_DEFAULT) |
| 121 | .forDevice(instance.deviceId()) |
Hyunsun Moon | 1e88fef | 2016-08-04 14:00:35 -0700 | [diff] [blame] | 122 | .forTable(CordVtnPipeline.TABLE_DST) |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 123 | .makePermanent() |
| 124 | .build(); |
| 125 | |
| 126 | pipeline.processFlowRule(install, flowRule); |
| 127 | } |
| 128 | |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 129 | private void populateHostsManagementRules(Instance instance, boolean install) { |
Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 130 | PortNumber hostMgmtPort = hostManagementPort(instance.deviceId()); |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 131 | if (hostMgmtPort == null) { |
| 132 | log.warn("Can not find host management port in {}", instance.deviceId()); |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 137 | .matchInPort(instance.portNumber()) |
| 138 | .build(); |
| 139 | |
| 140 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 141 | .setOutput(hostMgmtPort) |
| 142 | .build(); |
| 143 | |
| 144 | FlowRule flowRule = DefaultFlowRule.builder() |
| 145 | .fromApp(appId) |
| 146 | .withSelector(selector) |
| 147 | .withTreatment(treatment) |
Hyunsun Moon | 315b9a6 | 2016-06-23 14:48:04 -0700 | [diff] [blame] | 148 | .withPriority(CordVtnPipeline.PRIORITY_MANAGEMENT) |
| 149 | .forDevice(instance.deviceId()) |
Hyunsun Moon | c031d9b | 2016-08-04 13:57:22 -0700 | [diff] [blame] | 150 | .forTable(CordVtnPipeline.TABLE_IN_PORT) |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 151 | .makePermanent() |
| 152 | .build(); |
| 153 | |
| 154 | pipeline.processFlowRule(install, flowRule); |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 155 | |
| 156 | selector = DefaultTrafficSelector.builder() |
| 157 | .matchEthType(Ethernet.TYPE_IPV4) |
| 158 | .matchIPDst(instance.ipAddress().toIpPrefix()) |
| 159 | .build(); |
| 160 | |
| 161 | treatment = DefaultTrafficTreatment.builder() |
| 162 | .setEthDst(instance.mac()) |
| 163 | .setOutput(instance.portNumber()) |
| 164 | .build(); |
| 165 | |
| 166 | flowRule = DefaultFlowRule.builder() |
| 167 | .fromApp(appId) |
| 168 | .withSelector(selector) |
| 169 | .withTreatment(treatment) |
| 170 | .withPriority(CordVtnPipeline.PRIORITY_DEFAULT) |
| 171 | .forDevice(instance.deviceId()) |
Hyunsun Moon | 1e88fef | 2016-08-04 14:00:35 -0700 | [diff] [blame] | 172 | .forTable(CordVtnPipeline.TABLE_DST) |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 173 | .makePermanent() |
| 174 | .build(); |
| 175 | |
| 176 | pipeline.processFlowRule(install, flowRule); |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 177 | } |
| 178 | } |