| /* |
| * Copyright 2016-present Open Networking Laboratory |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| package org.opencord.cordvtn.impl.handler; |
| |
| import com.google.common.collect.ImmutableSet; |
| import org.apache.felix.scr.annotations.Activate; |
| import org.apache.felix.scr.annotations.Component; |
| import org.apache.felix.scr.annotations.Deactivate; |
| |
| import org.apache.felix.scr.annotations.Reference; |
| import org.apache.felix.scr.annotations.ReferenceCardinality; |
| import org.onosproject.net.flow.DefaultFlowRule; |
| import org.onosproject.net.flow.DefaultTrafficSelector; |
| import org.onosproject.net.flow.DefaultTrafficTreatment; |
| import org.onosproject.net.flow.FlowRule; |
| import org.onosproject.net.flow.TrafficSelector; |
| import org.onosproject.net.flow.TrafficTreatment; |
| import org.opencord.cordvtn.api.instance.Instance; |
| import org.opencord.cordvtn.api.instance.InstanceHandler; |
| import org.opencord.cordvtn.impl.CordVtnNodeManager; |
| import org.opencord.cordvtn.impl.CordVtnPipeline; |
| |
| import static org.opencord.cordvtn.api.net.ServiceNetwork.ServiceNetworkType.ACCESS_AGENT; |
| |
| /** |
| * Provides network connectivity for access agent instances. |
| */ |
| @Component(immediate = true) |
| public class AccessAgentInstanceHandler extends AbstractInstanceHandler implements InstanceHandler { |
| |
| @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| protected CordVtnPipeline pipeline; |
| |
| @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| protected CordVtnNodeManager nodeManager; |
| |
| @Activate |
| protected void activate() { |
| netTypes = ImmutableSet.of(ACCESS_AGENT); |
| super.activate(); |
| } |
| |
| @Deactivate |
| protected void deactivate() { |
| super.deactivate(); |
| } |
| |
| @Override |
| public void instanceDetected(Instance instance) { |
| log.info("Access agent instance detected {}", instance); |
| populateAccessAgentRules(instance, true); |
| } |
| |
| @Override |
| public void instanceRemoved(Instance instance) { |
| log.info("Access agent instance removed {}", instance); |
| populateAccessAgentRules(instance, false); |
| } |
| |
| private void populateAccessAgentRules(Instance instance, boolean install) { |
| TrafficSelector selector = DefaultTrafficSelector.builder() |
| .matchEthDst(instance.mac()) |
| .build(); |
| |
| TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| .setOutput(instance.portNumber()) |
| .build(); |
| |
| FlowRule flowRule = DefaultFlowRule.builder() |
| .fromApp(appId) |
| .withSelector(selector) |
| .withTreatment(treatment) |
| .withPriority(CordVtnPipeline.PRIORITY_DEFAULT) |
| .forDevice(instance.deviceId()) |
| .forTable(CordVtnPipeline.TABLE_DST) |
| .makePermanent() |
| .build(); |
| |
| pipeline.processFlowRule(install, flowRule); |
| |
| selector = DefaultTrafficSelector.builder() |
| .matchInPort(instance.portNumber()) |
| .build(); |
| |
| treatment = DefaultTrafficTreatment.builder() |
| .setOutput(nodeManager.dataPort(instance.deviceId())) |
| .build(); |
| |
| flowRule = DefaultFlowRule.builder() |
| .fromApp(appId) |
| .withSelector(selector) |
| .withTreatment(treatment) |
| .withPriority(CordVtnPipeline.PRIORITY_DEFAULT) |
| .forDevice(instance.deviceId()) |
| .forTable(CordVtnPipeline.TABLE_IN_PORT) |
| .makePermanent() |
| .build(); |
| |
| pipeline.processFlowRule(install, flowRule); |
| } |
| } |