blob: 0475f07b8f863f14a7e7dfc3e0503d87d4dc8f5a [file] [log] [blame]
Hyunsun Moon60a10672016-06-12 17:39:12 -07001/*
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 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 Moon187bf532017-01-19 10:57:40 +090032import org.opencord.cordvtn.api.core.Instance;
33import org.opencord.cordvtn.api.core.InstanceHandler;
34import org.opencord.cordvtn.api.net.ServiceNetwork;
Hyunsun Moonc031d9b2016-08-04 13:57:22 -070035import org.opencord.cordvtn.impl.CordVtnNodeManager;
Hyunsun Moon60a10672016-06-12 17:39:12 -070036import org.opencord.cordvtn.impl.CordVtnPipeline;
37
Hyunsun Moon187bf532017-01-19 10:57:40 +090038import static org.opencord.cordvtn.api.net.ServiceNetwork.NetworkType.MANAGEMENT_HOST;
39import static org.opencord.cordvtn.api.net.ServiceNetwork.NetworkType.MANAGEMENT_LOCAL;
Hyunsun Moon60a10672016-06-12 17:39:12 -070040
41/**
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070042 * Provides local management network connectivity to the instance. The instance
43 * is only accessible via local management network from the host machine.
Hyunsun Moon60a10672016-06-12 17:39:12 -070044 */
45@Component(immediate = true)
46public class ManagementInstanceHandler extends AbstractInstanceHandler implements InstanceHandler {
47
Hyunsun Moon5401aaa2016-06-12 17:40:34 -070048 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
49 protected CordVtnPipeline pipeline;
50
Hyunsun Moonc031d9b2016-08-04 13:57:22 -070051 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
52 protected CordVtnNodeManager nodeManager;
53
Hyunsun Moon60a10672016-06-12 17:39:12 -070054 @Activate
55 protected void activate() {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070056 netTypes = ImmutableSet.of(MANAGEMENT_LOCAL, MANAGEMENT_HOST);
Hyunsun Moon60a10672016-06-12 17:39:12 -070057 super.activate();
58 }
59
60 @Deactivate
61 protected void deactivate() {
62 super.deactivate();
63 }
64
65 @Override
66 public void instanceDetected(Instance instance) {
Hyunsun Moon187bf532017-01-19 10:57:40 +090067 ServiceNetwork vtnNet = getServiceNetwork(instance);
Hyunsun Moonc031d9b2016-08-04 13:57:22 -070068
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070069 switch (vtnNet.type()) {
Hyunsun Moonc031d9b2016-08-04 13:57:22 -070070 case MANAGEMENT_LOCAL:
71 log.info("LOCAL management instance is detected {}", instance);
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070072 populateLocalManagementRules(instance, true);
Hyunsun Moonc031d9b2016-08-04 13:57:22 -070073 break;
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070074 case MANAGEMENT_HOST:
Hyunsun Moonc031d9b2016-08-04 13:57:22 -070075 log.info("HOSTS management instance is detected {}", instance);
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070076 populateHostsManagementRules(instance, true);
Hyunsun Moonc031d9b2016-08-04 13:57:22 -070077 break;
78 default:
79 break;
80 }
Hyunsun Moon60a10672016-06-12 17:39:12 -070081 }
82
83 @Override
84 public void instanceRemoved(Instance instance) {
Hyunsun Moon187bf532017-01-19 10:57:40 +090085 ServiceNetwork vtnNet = getServiceNetwork(instance);
Hyunsun Moon60a10672016-06-12 17:39:12 -070086
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070087 switch (vtnNet.type()) {
Hyunsun Moonc031d9b2016-08-04 13:57:22 -070088 case MANAGEMENT_LOCAL:
89 log.info("LOCAL management instance removed {}", instance);
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070090 populateLocalManagementRules(instance, false);
Hyunsun Moonc031d9b2016-08-04 13:57:22 -070091 break;
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070092 case MANAGEMENT_HOST:
Hyunsun Moonc031d9b2016-08-04 13:57:22 -070093 log.info("HOSTS management instance removed {}", instance);
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070094 populateHostsManagementRules(instance, false);
Hyunsun Moonc031d9b2016-08-04 13:57:22 -070095 break;
96 default:
97 break;
Hyunsun Moon60a10672016-06-12 17:39:12 -070098 }
99 }
100
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700101 private void populateLocalManagementRules(Instance instance, boolean install) {
Hyunsun Moon60a10672016-06-12 17:39:12 -0700102 TrafficSelector selector = DefaultTrafficSelector.builder()
Hyunsun Moonc031d9b2016-08-04 13:57:22 -0700103 .matchEthType(Ethernet.TYPE_IPV4)
104 .matchIPDst(instance.ipAddress().toIpPrefix())
Hyunsun Moon60a10672016-06-12 17:39:12 -0700105 .build();
106
107 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
Hyunsun Moonc031d9b2016-08-04 13:57:22 -0700108 .setEthDst(instance.mac())
Hyunsun Moon315b9a62016-06-23 14:48:04 -0700109 .setOutput(instance.portNumber())
110 .build();
111
112 FlowRule flowRule = DefaultFlowRule.builder()
113 .fromApp(appId)
114 .withSelector(selector)
115 .withTreatment(treatment)
Hyunsun Moonc031d9b2016-08-04 13:57:22 -0700116 .withPriority(CordVtnPipeline.PRIORITY_DEFAULT)
117 .forDevice(instance.deviceId())
Hyunsun Moon1e88fef2016-08-04 14:00:35 -0700118 .forTable(CordVtnPipeline.TABLE_DST)
Hyunsun Moonc031d9b2016-08-04 13:57:22 -0700119 .makePermanent()
120 .build();
121
122 pipeline.processFlowRule(install, flowRule);
123 }
124
Hyunsun Mooneaf75e62016-09-27 16:40:23 -0700125 private void populateHostsManagementRules(Instance instance, boolean install) {
Hyunsun Moonc031d9b2016-08-04 13:57:22 -0700126 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 Moon315b9a62016-06-23 14:48:04 -0700144 .withPriority(CordVtnPipeline.PRIORITY_MANAGEMENT)
145 .forDevice(instance.deviceId())
Hyunsun Moonc031d9b2016-08-04 13:57:22 -0700146 .forTable(CordVtnPipeline.TABLE_IN_PORT)
Hyunsun Moon60a10672016-06-12 17:39:12 -0700147 .makePermanent()
148 .build();
149
150 pipeline.processFlowRule(install, flowRule);
Hyunsun Moon5401aaa2016-06-12 17:40:34 -0700151
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 Moon1e88fef2016-08-04 14:00:35 -0700168 .forTable(CordVtnPipeline.TABLE_DST)
Hyunsun Moon5401aaa2016-06-12 17:40:34 -0700169 .makePermanent()
170 .build();
171
172 pipeline.processFlowRule(install, flowRule);
Hyunsun Moon60a10672016-06-12 17:39:12 -0700173 }
174}