Hyunsun Moon | 1e88fef | 2016-08-04 14:00:35 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 80dff97 | 2017-08-03 22:46:30 -0700 | [diff] [blame^] | 2 | * Copyright 2016-present Open Networking Foundation |
Hyunsun Moon | 1e88fef | 2016-08-04 14:00:35 -0700 | [diff] [blame] | 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 | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 31 | import org.opencord.cordvtn.api.core.CordVtnPipeline; |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 32 | import org.opencord.cordvtn.api.core.Instance; |
| 33 | import org.opencord.cordvtn.api.core.InstanceHandler; |
Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 34 | import org.opencord.cordvtn.api.core.ServiceNetworkService; |
| 35 | import org.opencord.cordvtn.api.node.CordVtnNodeService; |
Hyunsun Moon | 1e88fef | 2016-08-04 14:00:35 -0700 | [diff] [blame] | 36 | |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 37 | import static org.opencord.cordvtn.api.net.ServiceNetwork.NetworkType.ACCESS_AGENT; |
Hyunsun Moon | 1e88fef | 2016-08-04 14:00:35 -0700 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * Provides network connectivity for access agent instances. |
| 41 | */ |
| 42 | @Component(immediate = true) |
| 43 | public class AccessAgentInstanceHandler extends AbstractInstanceHandler implements InstanceHandler { |
| 44 | |
| 45 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 46 | protected ServiceNetworkService snetService; |
Hyunsun Moon | 1e88fef | 2016-08-04 14:00:35 -0700 | [diff] [blame] | 47 | |
| 48 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 49 | protected CordVtnNodeService nodeService; |
| 50 | |
| 51 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 52 | protected CordVtnPipeline pipeline; |
Hyunsun Moon | 1e88fef | 2016-08-04 14:00:35 -0700 | [diff] [blame] | 53 | |
| 54 | @Activate |
| 55 | protected void activate() { |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 56 | netTypes = ImmutableSet.of(ACCESS_AGENT); |
Hyunsun Moon | 1e88fef | 2016-08-04 14:00:35 -0700 | [diff] [blame] | 57 | super.activate(); |
| 58 | } |
| 59 | |
| 60 | @Deactivate |
| 61 | protected void deactivate() { |
| 62 | super.deactivate(); |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public void instanceDetected(Instance instance) { |
| 67 | log.info("Access agent instance detected {}", instance); |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 68 | populateAccessAgentRules(instance, true); |
Hyunsun Moon | 1e88fef | 2016-08-04 14:00:35 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public void instanceRemoved(Instance instance) { |
| 73 | log.info("Access agent instance removed {}", instance); |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 74 | populateAccessAgentRules(instance, false); |
Hyunsun Moon | 1e88fef | 2016-08-04 14:00:35 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 77 | private void populateAccessAgentRules(Instance instance, boolean install) { |
Hyunsun Moon | 1e88fef | 2016-08-04 14:00:35 -0700 | [diff] [blame] | 78 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 79 | .matchEthDst(instance.mac()) |
| 80 | .build(); |
| 81 | |
| 82 | TrafficTreatment treatment = DefaultTrafficTreatment.builder() |
| 83 | .setOutput(instance.portNumber()) |
| 84 | .build(); |
| 85 | |
| 86 | FlowRule flowRule = DefaultFlowRule.builder() |
| 87 | .fromApp(appId) |
| 88 | .withSelector(selector) |
| 89 | .withTreatment(treatment) |
| 90 | .withPriority(CordVtnPipeline.PRIORITY_DEFAULT) |
| 91 | .forDevice(instance.deviceId()) |
| 92 | .forTable(CordVtnPipeline.TABLE_DST) |
| 93 | .makePermanent() |
| 94 | .build(); |
| 95 | |
| 96 | pipeline.processFlowRule(install, flowRule); |
| 97 | |
| 98 | selector = DefaultTrafficSelector.builder() |
| 99 | .matchInPort(instance.portNumber()) |
| 100 | .build(); |
| 101 | |
| 102 | treatment = DefaultTrafficTreatment.builder() |
Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 103 | .setOutput(dataPort(instance.deviceId())) |
Hyunsun Moon | 1e88fef | 2016-08-04 14:00:35 -0700 | [diff] [blame] | 104 | .build(); |
| 105 | |
| 106 | flowRule = DefaultFlowRule.builder() |
| 107 | .fromApp(appId) |
| 108 | .withSelector(selector) |
| 109 | .withTreatment(treatment) |
| 110 | .withPriority(CordVtnPipeline.PRIORITY_DEFAULT) |
| 111 | .forDevice(instance.deviceId()) |
| 112 | .forTable(CordVtnPipeline.TABLE_IN_PORT) |
| 113 | .makePermanent() |
| 114 | .build(); |
| 115 | |
| 116 | pipeline.processFlowRule(install, flowRule); |
| 117 | } |
| 118 | } |