blob: d07c00847bf9e1fa516205300947479f178cb17a [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
Ray Milkeyd84f89b2018-08-17 14:54:17 -070019import org.osgi.service.component.annotations.Activate;
20import org.osgi.service.component.annotations.Component;
21import org.osgi.service.component.annotations.Deactivate;
22import org.osgi.service.component.annotations.Reference;
23import org.osgi.service.component.annotations.ReferenceCardinality;
Jonathan Hartc4f681c2016-09-09 07:14:25 -070024import org.onosproject.core.ApplicationId;
25import org.onosproject.core.CoreService;
Ray Milkeyb65d7842017-08-03 16:28:24 -070026import org.onosproject.net.neighbour.DefaultNeighbourMessageHandler;
27import org.onosproject.net.neighbour.NeighbourResolutionService;
Jonathan Hartc4f681c2016-09-09 07:14:25 -070028import org.onosproject.net.ConnectPoint;
29import org.onosproject.net.edge.EdgePortEvent;
30import org.onosproject.net.edge.EdgePortListener;
31import 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)
42public class DefaultProxyArp {
43
44 private static final String APP_NAME = "org.onosproject.proxyarp";
45
Ray Milkeyd84f89b2018-08-17 14:54:17 -070046 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jonathan Hartc4f681c2016-09-09 07:14:25 -070047 protected EdgePortService edgeService;
48
Ray Milkeyd84f89b2018-08-17 14:54:17 -070049 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jonathan Hartc4f681c2016-09-09 07:14:25 -070050 protected NeighbourResolutionService neighbourResolutionService;
51
Ray Milkeyd84f89b2018-08-17 14:54:17 -070052 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Jonathan Hartc4f681c2016-09-09 07:14:25 -070053 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}