Hyunsun Moon | 1e88fef | 2016-08-04 14:00:35 -0700 | [diff] [blame^] | 1 | /* |
| 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 | */ |
| 16 | package org.opencord.cordvtn.impl.handler; |
| 17 | |
| 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
| 21 | |
| 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 24 | import org.onosproject.net.flow.DefaultFlowRule; |
| 25 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 26 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 27 | import org.onosproject.net.flow.FlowRule; |
| 28 | import org.onosproject.net.flow.TrafficSelector; |
| 29 | import org.onosproject.net.flow.TrafficTreatment; |
| 30 | import org.opencord.cordvtn.impl.AbstractInstanceHandler; |
| 31 | import org.opencord.cordvtn.api.Instance; |
| 32 | import org.opencord.cordvtn.api.InstanceHandler; |
| 33 | import org.opencord.cordvtn.impl.CordVtnNodeManager; |
| 34 | import org.opencord.cordvtn.impl.CordVtnPipeline; |
| 35 | |
| 36 | import java.util.Optional; |
| 37 | |
| 38 | import static org.onosproject.xosclient.api.VtnServiceApi.ServiceType.ACCESS_AGENT; |
| 39 | |
| 40 | /** |
| 41 | * Provides network connectivity for access agent instances. |
| 42 | */ |
| 43 | @Component(immediate = true) |
| 44 | public 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 | } |