blob: 8a818b8f3c08cce4ff9fed238503b7ffa3c24759 [file] [log] [blame]
Hyunsun Moon1e88fef2016-08-04 14:00:35 -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 */
16package org.opencord.cordvtn.impl.handler;
17
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070018import com.google.common.collect.ImmutableSet;
Hyunsun Moon1e88fef2016-08-04 14:00:35 -070019import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22
23import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
25import org.onosproject.net.flow.DefaultFlowRule;
26import org.onosproject.net.flow.DefaultTrafficSelector;
27import org.onosproject.net.flow.DefaultTrafficTreatment;
28import org.onosproject.net.flow.FlowRule;
29import org.onosproject.net.flow.TrafficSelector;
30import org.onosproject.net.flow.TrafficTreatment;
Hyunsun Moon1e88fef2016-08-04 14:00:35 -070031import org.opencord.cordvtn.api.Instance;
32import org.opencord.cordvtn.api.InstanceHandler;
33import org.opencord.cordvtn.impl.CordVtnNodeManager;
34import org.opencord.cordvtn.impl.CordVtnPipeline;
35
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070036import static org.opencord.cordvtn.api.ServiceNetwork.ServiceNetworkType.ACCESS_AGENT;
Hyunsun Moon1e88fef2016-08-04 14:00:35 -070037
38/**
39 * Provides network connectivity for access agent instances.
40 */
41@Component(immediate = true)
42public class AccessAgentInstanceHandler extends AbstractInstanceHandler implements InstanceHandler {
43
44 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
45 protected CordVtnPipeline pipeline;
46
47 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
48 protected CordVtnNodeManager nodeManager;
49
50 @Activate
51 protected void activate() {
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070052 netTypes = ImmutableSet.of(ACCESS_AGENT);
Hyunsun Moon1e88fef2016-08-04 14:00:35 -070053 super.activate();
54 }
55
56 @Deactivate
57 protected void deactivate() {
58 super.deactivate();
59 }
60
61 @Override
62 public void instanceDetected(Instance instance) {
63 log.info("Access agent instance detected {}", instance);
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070064 populateAccessAgentRules(instance, true);
Hyunsun Moon1e88fef2016-08-04 14:00:35 -070065 }
66
67 @Override
68 public void instanceRemoved(Instance instance) {
69 log.info("Access agent instance removed {}", instance);
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070070 populateAccessAgentRules(instance, false);
Hyunsun Moon1e88fef2016-08-04 14:00:35 -070071 }
72
Hyunsun Mooneaf75e62016-09-27 16:40:23 -070073 private void populateAccessAgentRules(Instance instance, boolean install) {
Hyunsun Moon1e88fef2016-08-04 14:00:35 -070074 TrafficSelector selector = DefaultTrafficSelector.builder()
75 .matchEthDst(instance.mac())
76 .build();
77
78 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
79 .setOutput(instance.portNumber())
80 .build();
81
82 FlowRule flowRule = DefaultFlowRule.builder()
83 .fromApp(appId)
84 .withSelector(selector)
85 .withTreatment(treatment)
86 .withPriority(CordVtnPipeline.PRIORITY_DEFAULT)
87 .forDevice(instance.deviceId())
88 .forTable(CordVtnPipeline.TABLE_DST)
89 .makePermanent()
90 .build();
91
92 pipeline.processFlowRule(install, flowRule);
93
94 selector = DefaultTrafficSelector.builder()
95 .matchInPort(instance.portNumber())
96 .build();
97
98 treatment = DefaultTrafficTreatment.builder()
99 .setOutput(nodeManager.dataPort(instance.deviceId()))
100 .build();
101
102 flowRule = DefaultFlowRule.builder()
103 .fromApp(appId)
104 .withSelector(selector)
105 .withTreatment(treatment)
106 .withPriority(CordVtnPipeline.PRIORITY_DEFAULT)
107 .forDevice(instance.deviceId())
108 .forTable(CordVtnPipeline.TABLE_IN_PORT)
109 .makePermanent()
110 .build();
111
112 pipeline.processFlowRule(install, flowRule);
113 }
114}