Jonathan Hart | c4f681c | 2016-09-09 07:14:25 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | ce2a03d | 2017-08-03 19:21:03 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Jonathan Hart | c4f681c | 2016-09-09 07:14:25 -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 | |
| 17 | package org.onosproject.proxyarp; |
| 18 | |
| 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 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 24 | import org.onosproject.core.ApplicationId; |
| 25 | import org.onosproject.core.CoreService; |
| 26 | import org.onosproject.incubator.net.neighbour.DefaultNeighbourMessageHandler; |
| 27 | import org.onosproject.incubator.net.neighbour.NeighbourResolutionService; |
| 28 | import org.onosproject.net.ConnectPoint; |
| 29 | import org.onosproject.net.edge.EdgePortEvent; |
| 30 | import org.onosproject.net.edge.EdgePortListener; |
| 31 | import org.onosproject.net.edge.EdgePortService; |
| 32 | |
| 33 | /** |
| 34 | * Implements proxy ARP and NDP functionality by considering the entire network |
| 35 | * as a single L2 broadcast domain. |
| 36 | * <p> |
| 37 | * This application maintains a DefaultNeighbourMessageHandler on all edge ports |
| 38 | * in the network, and the handler implements the desired proxying functionality. |
| 39 | * </p> |
| 40 | */ |
| 41 | @Component(immediate = true) |
| 42 | public class DefaultProxyArp { |
| 43 | |
| 44 | private static final String APP_NAME = "org.onosproject.proxyarp"; |
| 45 | |
| 46 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 47 | protected EdgePortService edgeService; |
| 48 | |
| 49 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 50 | protected NeighbourResolutionService neighbourResolutionService; |
| 51 | |
| 52 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 53 | protected CoreService coreService; |
| 54 | |
| 55 | private ApplicationId appId; |
| 56 | |
| 57 | private InternalEdgeListener edgeListener = new InternalEdgeListener(); |
| 58 | private DefaultNeighbourMessageHandler defaultHandler = new DefaultNeighbourMessageHandler(); |
| 59 | |
| 60 | @Activate |
| 61 | protected void activate() { |
| 62 | appId = coreService.registerApplication(APP_NAME); |
| 63 | |
| 64 | edgeService.addListener(edgeListener); |
| 65 | edgeService.getEdgePoints().forEach(this::addDefault); |
| 66 | } |
| 67 | |
| 68 | @Deactivate |
| 69 | protected void deactivate() { |
| 70 | edgeService.removeListener(edgeListener); |
| 71 | neighbourResolutionService.unregisterNeighbourHandlers(appId); |
| 72 | } |
| 73 | |
| 74 | private void addDefault(ConnectPoint port) { |
| 75 | neighbourResolutionService.registerNeighbourHandler(port, defaultHandler, appId); |
| 76 | } |
| 77 | |
| 78 | private void removeDefault(ConnectPoint port) { |
| 79 | neighbourResolutionService.unregisterNeighbourHandler(port, defaultHandler, appId); |
| 80 | } |
| 81 | |
| 82 | private class InternalEdgeListener implements EdgePortListener { |
| 83 | @Override |
| 84 | public void event(EdgePortEvent event) { |
| 85 | switch (event.type()) { |
| 86 | case EDGE_PORT_ADDED: |
| 87 | addDefault(event.subject()); |
| 88 | break; |
| 89 | case EDGE_PORT_REMOVED: |
| 90 | removeDefault(event.subject()); |
| 91 | break; |
| 92 | default: |
| 93 | break; |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | } |