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; |
Hyunsun Moon | acbc8ef | 2016-06-21 17:58:45 -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 | |
| 38 | import java.util.Optional; |
| 39 | |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 40 | import static org.onosproject.xosclient.api.VtnServiceApi.ServiceType.MANAGEMENT; |
| 41 | |
| 42 | /** |
| 43 | * Provides network connectivity for management network connected instances. |
| 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 | acbc8ef | 2016-06-21 17:58:45 -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() { |
| 56 | serviceType = Optional.of(MANAGEMENT); |
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 | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 67 | VtnService service = getVtnService(instance.serviceId()); |
| 68 | if (service == null) { |
| 69 | log.warn("Failed to get VtnService for {}", instance); |
| 70 | return; |
| 71 | } |
Hyunsun Moon | acbc8ef | 2016-06-21 17:58:45 -0700 | [diff] [blame] | 72 | |
| 73 | switch (service.networkType()) { |
| 74 | case MANAGEMENT_LOCAL: |
| 75 | log.info("LOCAL management instance detected {}", instance); |
| 76 | localManagementRules(instance, true); |
| 77 | break; |
| 78 | case MANAGEMENT_HOSTS: |
| 79 | log.info("HOSTS management instance detected {}", instance); |
| 80 | hostsManagementRules(instance, true); |
| 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 | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 89 | VtnService service = getVtnService(instance.serviceId()); |
| 90 | if (service == null) { |
| 91 | log.warn("Failed to get VtnService for {}", instance); |
| 92 | return; |
| 93 | } |
| 94 | |
Hyunsun Moon | acbc8ef | 2016-06-21 17:58:45 -0700 | [diff] [blame] | 95 | switch (service.networkType()) { |
| 96 | case MANAGEMENT_LOCAL: |
| 97 | log.info("LOCAL management instance removed {}", instance); |
| 98 | localManagementRules(instance, false); |
| 99 | break; |
| 100 | case MANAGEMENT_HOSTS: |
| 101 | log.info("HOSTS management instance removed {}", instance); |
| 102 | hostsManagementRules(instance, false); |
| 103 | break; |
| 104 | default: |
| 105 | break; |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
Hyunsun Moon | acbc8ef | 2016-06-21 17:58:45 -0700 | [diff] [blame] | 109 | private void localManagementRules(Instance instance, boolean install) { |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 110 | TrafficSelector selector = DefaultTrafficSelector.builder() |
Hyunsun Moon | acbc8ef | 2016-06-21 17:58:45 -0700 | [diff] [blame] | 111 | .matchEthType(Ethernet.TYPE_IPV4) |
| 112 | .matchIPDst(instance.ipAddress().toIpPrefix()) |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 113 | .build(); |
| 114 | |
| 115 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
Hyunsun Moon | acbc8ef | 2016-06-21 17:58:45 -0700 | [diff] [blame] | 116 | .setEthDst(instance.mac()) |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 117 | .setOutput(instance.portNumber()) |
| 118 | .build(); |
| 119 | |
| 120 | FlowRule flowRule = DefaultFlowRule.builder() |
| 121 | .fromApp(appId) |
| 122 | .withSelector(selector) |
| 123 | .withTreatment(treatment) |
Hyunsun Moon | acbc8ef | 2016-06-21 17:58:45 -0700 | [diff] [blame] | 124 | .withPriority(CordVtnPipeline.PRIORITY_DEFAULT) |
| 125 | .forDevice(instance.deviceId()) |
Hyunsun Moon | 3a7bf9e | 2016-06-22 17:35:35 -0700 | [diff] [blame] | 126 | .forTable(CordVtnPipeline.TABLE_DST) |
Hyunsun Moon | acbc8ef | 2016-06-21 17:58:45 -0700 | [diff] [blame] | 127 | .makePermanent() |
| 128 | .build(); |
| 129 | |
| 130 | pipeline.processFlowRule(install, flowRule); |
| 131 | } |
| 132 | |
| 133 | private void hostsManagementRules(Instance instance, boolean install) { |
| 134 | PortNumber hostMgmtPort = nodeManager.hostManagementPort(instance.deviceId()); |
| 135 | if (hostMgmtPort == null) { |
| 136 | log.warn("Can not find host management port in {}", instance.deviceId()); |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 141 | .matchInPort(instance.portNumber()) |
| 142 | .build(); |
| 143 | |
| 144 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 145 | .setOutput(hostMgmtPort) |
| 146 | .build(); |
| 147 | |
| 148 | FlowRule flowRule = DefaultFlowRule.builder() |
| 149 | .fromApp(appId) |
| 150 | .withSelector(selector) |
| 151 | .withTreatment(treatment) |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 152 | .withPriority(CordVtnPipeline.PRIORITY_MANAGEMENT) |
| 153 | .forDevice(instance.deviceId()) |
Hyunsun Moon | acbc8ef | 2016-06-21 17:58:45 -0700 | [diff] [blame] | 154 | .forTable(CordVtnPipeline.TABLE_IN_PORT) |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 155 | .makePermanent() |
| 156 | .build(); |
| 157 | |
| 158 | pipeline.processFlowRule(install, flowRule); |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 159 | |
| 160 | selector = DefaultTrafficSelector.builder() |
| 161 | .matchEthType(Ethernet.TYPE_IPV4) |
| 162 | .matchIPDst(instance.ipAddress().toIpPrefix()) |
| 163 | .build(); |
| 164 | |
| 165 | treatment = DefaultTrafficTreatment.builder() |
| 166 | .setEthDst(instance.mac()) |
| 167 | .setOutput(instance.portNumber()) |
| 168 | .build(); |
| 169 | |
| 170 | flowRule = DefaultFlowRule.builder() |
| 171 | .fromApp(appId) |
| 172 | .withSelector(selector) |
| 173 | .withTreatment(treatment) |
| 174 | .withPriority(CordVtnPipeline.PRIORITY_DEFAULT) |
| 175 | .forDevice(instance.deviceId()) |
Hyunsun Moon | 3a7bf9e | 2016-06-22 17:35:35 -0700 | [diff] [blame] | 176 | .forTable(CordVtnPipeline.TABLE_DST) |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 177 | .makePermanent() |
| 178 | .build(); |
| 179 | |
| 180 | pipeline.processFlowRule(install, flowRule); |
Hyunsun Moon | 60a1067 | 2016-06-12 17:39:12 -0700 | [diff] [blame] | 181 | } |
| 182 | } |