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 | |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableSet; |
Hyunsun Moon | 1e88fef | 2016-08-04 14:00:35 -0700 | [diff] [blame] | 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | |
| 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 25 | import org.onosproject.net.flow.DefaultFlowRule; |
| 26 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 27 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 28 | import org.onosproject.net.flow.FlowRule; |
| 29 | import org.onosproject.net.flow.TrafficSelector; |
| 30 | import org.onosproject.net.flow.TrafficTreatment; |
Hyunsun Moon | 1e88fef | 2016-08-04 14:00:35 -0700 | [diff] [blame] | 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 | |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 36 | import static org.opencord.cordvtn.api.ServiceNetwork.ServiceNetworkType.ACCESS_AGENT; |
Hyunsun Moon | 1e88fef | 2016-08-04 14:00:35 -0700 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * Provides network connectivity for access agent instances. |
| 40 | */ |
| 41 | @Component(immediate = true) |
| 42 | public 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 Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 52 | netTypes = ImmutableSet.of(ACCESS_AGENT); |
Hyunsun Moon | 1e88fef | 2016-08-04 14:00:35 -0700 | [diff] [blame] | 53 | 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 Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 64 | populateAccessAgentRules(instance, true); |
Hyunsun Moon | 1e88fef | 2016-08-04 14:00:35 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public void instanceRemoved(Instance instance) { |
| 69 | log.info("Access agent instance removed {}", instance); |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 70 | populateAccessAgentRules(instance, false); |
Hyunsun Moon | 1e88fef | 2016-08-04 14:00:35 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 73 | private void populateAccessAgentRules(Instance instance, boolean install) { |
Hyunsun Moon | 1e88fef | 2016-08-04 14:00:35 -0700 | [diff] [blame] | 74 | 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 | } |