blob: b2de2c31a4a2f3f9bb5738bc00cbb14270189f71 [file] [log] [blame]
Hyunsun Moon3a7bf9e2016-06-22 17:35: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
18import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Deactivate;
21
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
24import org.onosproject.net.flow.DefaultFlowRule;
25import org.onosproject.net.flow.DefaultTrafficSelector;
26import org.onosproject.net.flow.DefaultTrafficTreatment;
27import org.onosproject.net.flow.FlowRule;
28import org.onosproject.net.flow.TrafficSelector;
29import org.onosproject.net.flow.TrafficTreatment;
30import org.opencord.cordvtn.impl.AbstractInstanceHandler;
31import org.opencord.cordvtn.api.Instance;
32import org.opencord.cordvtn.api.InstanceHandler;
33import org.opencord.cordvtn.impl.CordVtnNodeManager;
34import org.opencord.cordvtn.impl.CordVtnPipeline;
35
36import java.util.Optional;
37
38import static org.onosproject.xosclient.api.VtnServiceApi.ServiceType.ACCESS_AGENT;
39
40/**
41 * Provides network connectivity for access agent instances.
42 */
43@Component(immediate = true)
44public class AccessAgentInstanceHandler extends AbstractInstanceHandler implements InstanceHandler {
45
46 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
47 protected CordVtnPipeline pipeline;
48
49 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
50 protected CordVtnNodeManager nodeManager;
51
52 @Activate
53 protected void activate() {
54 serviceType = Optional.of(ACCESS_AGENT);
55 super.activate();
56 }
57
58 @Deactivate
59 protected void deactivate() {
60 super.deactivate();
61 }
62
63 @Override
64 public void instanceDetected(Instance instance) {
65 log.info("Access agent instance detected {}", instance);
66 accessAgentRules(instance, true);
67 }
68
69 @Override
70 public void instanceRemoved(Instance instance) {
71 log.info("Access agent instance removed {}", instance);
72 accessAgentRules(instance, false);
73 }
74
75 private void accessAgentRules(Instance instance, boolean install) {
76 TrafficSelector selector = DefaultTrafficSelector.builder()
77 .matchEthDst(instance.mac())
78 .build();
79
80 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
81 .setOutput(instance.portNumber())
82 .build();
83
84 FlowRule flowRule = DefaultFlowRule.builder()
85 .fromApp(appId)
86 .withSelector(selector)
87 .withTreatment(treatment)
88 .withPriority(CordVtnPipeline.PRIORITY_DEFAULT)
89 .forDevice(instance.deviceId())
90 .forTable(CordVtnPipeline.TABLE_DST)
91 .makePermanent()
92 .build();
93
94 pipeline.processFlowRule(install, flowRule);
95
96 selector = DefaultTrafficSelector.builder()
97 .matchInPort(instance.portNumber())
98 .build();
99
100 treatment = DefaultTrafficTreatment.builder()
101 .setOutput(nodeManager.dataPort(instance.deviceId()))
102 .build();
103
104 flowRule = DefaultFlowRule.builder()
105 .fromApp(appId)
106 .withSelector(selector)
107 .withTreatment(treatment)
108 .withPriority(CordVtnPipeline.PRIORITY_DEFAULT)
109 .forDevice(instance.deviceId())
110 .forTable(CordVtnPipeline.TABLE_IN_PORT)
111 .makePermanent()
112 .build();
113
114 pipeline.processFlowRule(install, flowRule);
115 }
116}