blob: 4f027879e33b3f75051c851de31babd220c96e34 [file] [log] [blame]
Jonathan Hartc4f681c2016-09-09 07:14:25 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Jonathan Hartc4f681c2016-09-09 07:14:25 -07003 *
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
17package org.onosproject.proxyarp;
18
19import org.apache.felix.scr.annotations.Activate;
20import org.apache.felix.scr.annotations.Component;
21import org.apache.felix.scr.annotations.Deactivate;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.ReferenceCardinality;
24import org.onosproject.core.ApplicationId;
25import org.onosproject.core.CoreService;
Yi Tsengee9d0612018-02-05 20:31:55 -080026import org.onosproject.net.intf.InterfaceService;
Ray Milkeyb65d7842017-08-03 16:28:24 -070027import org.onosproject.net.neighbour.DefaultNeighbourMessageHandler;
28import org.onosproject.net.neighbour.NeighbourResolutionService;
Jonathan Hartc4f681c2016-09-09 07:14:25 -070029import org.onosproject.net.ConnectPoint;
30import org.onosproject.net.edge.EdgePortEvent;
31import org.onosproject.net.edge.EdgePortListener;
32import org.onosproject.net.edge.EdgePortService;
33
34/**
35 * Implements proxy ARP and NDP functionality by considering the entire network
36 * as a single L2 broadcast domain.
37 * <p>
38 * This application maintains a DefaultNeighbourMessageHandler on all edge ports
39 * in the network, and the handler implements the desired proxying functionality.
40 * </p>
41 */
42@Component(immediate = true)
43public class DefaultProxyArp {
44
45 private static final String APP_NAME = "org.onosproject.proxyarp";
46
47 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
48 protected EdgePortService edgeService;
49
50 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
51 protected NeighbourResolutionService neighbourResolutionService;
52
53 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
54 protected CoreService coreService;
55
Yi Tsengee9d0612018-02-05 20:31:55 -080056 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
57 protected InterfaceService interfaceService;
58
Jonathan Hartc4f681c2016-09-09 07:14:25 -070059 private ApplicationId appId;
60
61 private InternalEdgeListener edgeListener = new InternalEdgeListener();
Yi Tsengf11c9a22018-02-13 02:34:54 +000062 private DefaultNeighbourMessageHandler defaultHandler =
63 new DefaultNeighbourMessageHandler(interfaceService);
Jonathan Hartc4f681c2016-09-09 07:14:25 -070064
65 @Activate
66 protected void activate() {
67 appId = coreService.registerApplication(APP_NAME);
68
69 edgeService.addListener(edgeListener);
70 edgeService.getEdgePoints().forEach(this::addDefault);
Jonathan Hartc4f681c2016-09-09 07:14:25 -070071 }
72
73 @Deactivate
74 protected void deactivate() {
75 edgeService.removeListener(edgeListener);
76 neighbourResolutionService.unregisterNeighbourHandlers(appId);
77 }
78
79 private void addDefault(ConnectPoint port) {
80 neighbourResolutionService.registerNeighbourHandler(port, defaultHandler, appId);
81 }
82
83 private void removeDefault(ConnectPoint port) {
84 neighbourResolutionService.unregisterNeighbourHandler(port, defaultHandler, appId);
85 }
86
87 private class InternalEdgeListener implements EdgePortListener {
88 @Override
89 public void event(EdgePortEvent event) {
90 switch (event.type()) {
91 case EDGE_PORT_ADDED:
92 addDefault(event.subject());
93 break;
94 case EDGE_PORT_REMOVED:
95 removeDefault(event.subject());
96 break;
97 default:
98 break;
99 }
100 }
101 }
102}