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