blob: e53b506d5b8ec69b7550b8694d8e3d26a92db8d1 [file] [log] [blame]
Hyunsun Moon60a10672016-06-12 17:39:12 -07001/*
Brian O'Connor80dff972017-08-03 22:46:30 -07002 * Copyright 2016-present Open Networking Foundation
Hyunsun Moon60a10672016-06-12 17:39:12 -07003 *
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 Moon5401aaa2016-06-12 17:40:34 -070016package org.opencord.cordvtn.impl.handler;
Hyunsun Moon60a10672016-06-12 17:39:12 -070017
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070018import com.google.common.collect.ImmutableSet;
Hyunsun Moon60a10672016-06-12 17:39:12 -070019import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
Hyunsun Moon5401aaa2016-06-12 17:40:34 -070022import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
Hyunsun Moon60a10672016-06-12 17:39:12 -070024import org.onlab.packet.Ethernet;
25import org.onosproject.net.PortNumber;
26import org.onosproject.net.flow.DefaultFlowRule;
27import org.onosproject.net.flow.DefaultTrafficSelector;
28import org.onosproject.net.flow.DefaultTrafficTreatment;
29import org.onosproject.net.flow.FlowRule;
30import org.onosproject.net.flow.TrafficSelector;
31import org.onosproject.net.flow.TrafficTreatment;
Hyunsun Moon2c3f0ee2017-04-06 16:47:21 +090032import org.opencord.cordvtn.api.core.CordVtnPipeline;
Hyunsun Moon187bf532017-01-19 10:57:40 +090033import org.opencord.cordvtn.api.core.Instance;
34import org.opencord.cordvtn.api.core.InstanceHandler;
Hyunsun Moon2c3f0ee2017-04-06 16:47:21 +090035import org.opencord.cordvtn.api.core.ServiceNetworkService;
Hyunsun Moon187bf532017-01-19 10:57:40 +090036import org.opencord.cordvtn.api.net.ServiceNetwork;
Hyunsun Moon2c3f0ee2017-04-06 16:47:21 +090037import org.opencord.cordvtn.api.node.CordVtnNodeService;
Hyunsun Moon60a10672016-06-12 17:39:12 -070038
Hyunsun Moon187bf532017-01-19 10:57:40 +090039import static org.opencord.cordvtn.api.net.ServiceNetwork.NetworkType.MANAGEMENT_HOST;
40import static org.opencord.cordvtn.api.net.ServiceNetwork.NetworkType.MANAGEMENT_LOCAL;
Hyunsun Moon60a10672016-06-12 17:39:12 -070041
42/**
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070043 * Provides local management network connectivity to the instance. The instance
44 * is only accessible via local management network from the host machine.
Hyunsun Moon60a10672016-06-12 17:39:12 -070045 */
46@Component(immediate = true)
47public class ManagementInstanceHandler extends AbstractInstanceHandler implements InstanceHandler {
48
Hyunsun Moon5401aaa2016-06-12 17:40:34 -070049 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Hyunsun Moon2c3f0ee2017-04-06 16:47:21 +090050 protected ServiceNetworkService snetService;
Hyunsun Moon5401aaa2016-06-12 17:40:34 -070051
Hyunsun Moonc031d9b2016-08-04 13:57:22 -070052 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Hyunsun Moon2c3f0ee2017-04-06 16:47:21 +090053 protected CordVtnNodeService nodeService;
54
55 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
56 protected CordVtnPipeline pipeline;
Hyunsun Moonc031d9b2016-08-04 13:57:22 -070057
Hyunsun Moon60a10672016-06-12 17:39:12 -070058 @Activate
59 protected void activate() {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070060 netTypes = ImmutableSet.of(MANAGEMENT_LOCAL, MANAGEMENT_HOST);
Hyunsun Moon60a10672016-06-12 17:39:12 -070061 super.activate();
62 }
63
64 @Deactivate
65 protected void deactivate() {
66 super.deactivate();
67 }
68
69 @Override
70 public void instanceDetected(Instance instance) {
Hyunsun Moon187bf532017-01-19 10:57:40 +090071 ServiceNetwork vtnNet = getServiceNetwork(instance);
Hyunsun Moonc031d9b2016-08-04 13:57:22 -070072
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070073 switch (vtnNet.type()) {
Hyunsun Moonc031d9b2016-08-04 13:57:22 -070074 case MANAGEMENT_LOCAL:
75 log.info("LOCAL management instance is detected {}", instance);
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070076 populateLocalManagementRules(instance, true);
Hyunsun Moonc031d9b2016-08-04 13:57:22 -070077 break;
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070078 case MANAGEMENT_HOST:
Hyunsun Moonc031d9b2016-08-04 13:57:22 -070079 log.info("HOSTS management instance is detected {}", instance);
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070080 populateHostsManagementRules(instance, true);
Hyunsun Moonc031d9b2016-08-04 13:57:22 -070081 break;
82 default:
83 break;
84 }
Hyunsun Moon60a10672016-06-12 17:39:12 -070085 }
86
87 @Override
88 public void instanceRemoved(Instance instance) {
Hyunsun Moon187bf532017-01-19 10:57:40 +090089 ServiceNetwork vtnNet = getServiceNetwork(instance);
Hyunsun Moon60a10672016-06-12 17:39:12 -070090
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070091 switch (vtnNet.type()) {
Hyunsun Moonc031d9b2016-08-04 13:57:22 -070092 case MANAGEMENT_LOCAL:
93 log.info("LOCAL management instance removed {}", instance);
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070094 populateLocalManagementRules(instance, false);
Hyunsun Moonc031d9b2016-08-04 13:57:22 -070095 break;
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070096 case MANAGEMENT_HOST:
Hyunsun Moonc031d9b2016-08-04 13:57:22 -070097 log.info("HOSTS management instance removed {}", instance);
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070098 populateHostsManagementRules(instance, false);
Hyunsun Moonc031d9b2016-08-04 13:57:22 -070099 break;
100 default:
101 break;
Hyunsun Moon60a10672016-06-12 17:39:12 -0700102 }
103 }
104
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700105 private void populateLocalManagementRules(Instance instance, boolean install) {
Hyunsun Moon60a10672016-06-12 17:39:12 -0700106 TrafficSelector selector = DefaultTrafficSelector.builder()
Hyunsun Moonc031d9b2016-08-04 13:57:22 -0700107 .matchEthType(Ethernet.TYPE_IPV4)
108 .matchIPDst(instance.ipAddress().toIpPrefix())
Hyunsun Moon60a10672016-06-12 17:39:12 -0700109 .build();
110
111 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
Hyunsun Moonc031d9b2016-08-04 13:57:22 -0700112 .setEthDst(instance.mac())
Hyunsun Moon315b9a62016-06-23 14:48:04 -0700113 .setOutput(instance.portNumber())
114 .build();
115
116 FlowRule flowRule = DefaultFlowRule.builder()
117 .fromApp(appId)
118 .withSelector(selector)
119 .withTreatment(treatment)
Hyunsun Moonc031d9b2016-08-04 13:57:22 -0700120 .withPriority(CordVtnPipeline.PRIORITY_DEFAULT)
121 .forDevice(instance.deviceId())
Hyunsun Moon1e88fef2016-08-04 14:00:35 -0700122 .forTable(CordVtnPipeline.TABLE_DST)
Hyunsun Moonc031d9b2016-08-04 13:57:22 -0700123 .makePermanent()
124 .build();
125
126 pipeline.processFlowRule(install, flowRule);
127 }
128
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700129 private void populateHostsManagementRules(Instance instance, boolean install) {
Hyunsun Moon2c3f0ee2017-04-06 16:47:21 +0900130 PortNumber hostMgmtPort = hostManagementPort(instance.deviceId());
Hyunsun Moonc031d9b2016-08-04 13:57:22 -0700131 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 Moon315b9a62016-06-23 14:48:04 -0700148 .withPriority(CordVtnPipeline.PRIORITY_MANAGEMENT)
149 .forDevice(instance.deviceId())
Hyunsun Moonc031d9b2016-08-04 13:57:22 -0700150 .forTable(CordVtnPipeline.TABLE_IN_PORT)
Hyunsun Moon60a10672016-06-12 17:39:12 -0700151 .makePermanent()
152 .build();
153
154 pipeline.processFlowRule(install, flowRule);
Hyunsun Moon5401aaa2016-06-12 17:40:34 -0700155
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 Moon1e88fef2016-08-04 14:00:35 -0700172 .forTable(CordVtnPipeline.TABLE_DST)
Hyunsun Moon5401aaa2016-06-12 17:40:34 -0700173 .makePermanent()
174 .build();
175
176 pipeline.processFlowRule(install, flowRule);
Hyunsun Moon60a10672016-06-12 17:39:12 -0700177 }
178}