blob: 00768dcfce97ec0efb2bf1aac9e793db14cd8465 [file] [log] [blame]
Thomas Vachuskab2c47a72015-08-05 14:22:54 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuskab2c47a72015-08-05 14:22:54 -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 */
16package org.onosproject.store.proxyarp.impl;
17
18import com.google.common.collect.Maps;
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.apache.felix.scr.annotations.Service;
25import org.onlab.util.KryoNamespace;
26import org.onosproject.cluster.ClusterService;
27import org.onosproject.cluster.NodeId;
28import org.onosproject.mastership.MastershipService;
29import org.onosproject.net.ConnectPoint;
30import org.onosproject.net.Host;
31import org.onosproject.net.HostId;
32import org.onosproject.net.host.HostEvent;
33import org.onosproject.net.host.HostListener;
34import org.onosproject.net.host.HostService;
35import org.onosproject.net.proxyarp.ProxyArpStore;
36import org.onosproject.net.proxyarp.ProxyArpStoreDelegate;
37import org.onosproject.store.cluster.messaging.ClusterCommunicationService;
38import org.onosproject.store.cluster.messaging.MessageSubject;
39import org.onosproject.store.serializers.KryoNamespaces;
HIGUCHI Yutae7290652016-05-18 11:29:01 -070040import org.onosproject.store.serializers.StoreSerializer;
Thomas Vachuskab2c47a72015-08-05 14:22:54 -070041import org.slf4j.Logger;
42import org.slf4j.LoggerFactory;
43
44import java.nio.ByteBuffer;
45import java.util.Map;
46import java.util.concurrent.ExecutorService;
47
48import static org.onlab.util.BoundedThreadPool.newFixedThreadPool;
49import static org.onlab.util.Tools.groupedThreads;
50
51/**
52 * Implementation of proxy ARP distribution mechanism.
Jonathan Hartc4f681c2016-09-09 07:14:25 -070053 *
54 * @deprecated in Hummingbird release. This is no longer necessary as there are
55 * other solutions for the problem this was solving.
Thomas Vachuskab2c47a72015-08-05 14:22:54 -070056 */
Jonathan Hartc4f681c2016-09-09 07:14:25 -070057@Deprecated
Thomas Vachuskab2c47a72015-08-05 14:22:54 -070058@Component(immediate = true)
59@Service
60public class DistributedProxyArpStore implements ProxyArpStore {
61
62 private Logger log = LoggerFactory.getLogger(getClass());
63
64 private static final MessageSubject ARP_RESPONSE_MESSAGE =
65 new MessageSubject("onos-arp-response");
66
HIGUCHI Yutae7290652016-05-18 11:29:01 -070067 protected final StoreSerializer serializer = StoreSerializer.using(
68 KryoNamespace.newBuilder()
Thomas Vachuskab2c47a72015-08-05 14:22:54 -070069 .register(KryoNamespaces.API)
HIGUCHI Yutae7290652016-05-18 11:29:01 -070070 .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID)
Thomas Vachuskab2c47a72015-08-05 14:22:54 -070071 .register(ArpResponseMessage.class)
72 .register(ByteBuffer.class)
HIGUCHI Yutae7290652016-05-18 11:29:01 -070073 .build("ProxyArpStore"));
Thomas Vachuskab2c47a72015-08-05 14:22:54 -070074
75 private ProxyArpStoreDelegate delegate;
76
77 private Map<HostId, ArpResponseMessage> pendingMessages = Maps.newConcurrentMap();
78
79 private ExecutorService executor =
HIGUCHI Yutad9e01052016-04-14 09:31:42 -070080 newFixedThreadPool(4, groupedThreads("onos/arp", "sender-%d", log));
Thomas Vachuskab2c47a72015-08-05 14:22:54 -070081
82 private NodeId localNodeId;
83
84 private HostListener hostListener = new InternalHostListener();
85
86 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
87 protected MastershipService mastershipService;
88
89 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
90 protected ClusterService clusterService;
91
92 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
93 protected ClusterCommunicationService commService;
94
95 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
96 protected HostService hostService;
97
98
99 @Activate
100 protected void activate() {
101 localNodeId = clusterService.getLocalNode().id();
102 hostService.addListener(hostListener);
103 commService.addSubscriber(ARP_RESPONSE_MESSAGE, serializer::decode,
104 this::processArpResponse, executor);
105 log.info("Started");
106 }
107
108 @Deactivate
109 protected void deactivate() {
110 commService.removeSubscriber(ARP_RESPONSE_MESSAGE);
111 hostService.removeListener(hostListener);
112 log.info("Stopped");
113 }
114
115 @Override
116 public void forward(ConnectPoint outPort, Host subject, ByteBuffer packet) {
alshabib8a4a6002015-11-25 14:31:16 -0800117 /*NodeId nodeId = mastershipService.getMasterFor(outPort.deviceId());
Thomas Vachuskab2c47a72015-08-05 14:22:54 -0700118 if (nodeId.equals(localNodeId)) {
119 if (delegate != null) {
120 delegate.emitResponse(outPort, packet);
121 }
122 } else {
123 log.info("Forwarding ARP response from {} to {}", subject.id(), outPort);
124 commService.unicast(new ArpResponseMessage(outPort, subject, packet.array()),
125 ARP_RESPONSE_MESSAGE, serializer::encode, nodeId);
alshabib8a4a6002015-11-25 14:31:16 -0800126 }*/
127 //FIXME: Code above may be unnecessary and therefore cluster messaging
128 // and pendingMessages could be pruned as well.
129 delegate.emitResponse(outPort, packet);
Thomas Vachuskab2c47a72015-08-05 14:22:54 -0700130 }
131
132 @Override
133 public void setDelegate(ProxyArpStoreDelegate delegate) {
134 this.delegate = delegate;
135 }
136
137 // Processes the incoming ARP response message.
138 private void processArpResponse(ArpResponseMessage msg) {
139 pendingMessages.put(msg.subject.id(), msg);
140 if (hostService.getHost(msg.subject.id()) != null) {
141 checkPendingArps(msg.subject.id());
142 }
143 // FIXME: figure out pruning so stuff does not build up
144 }
145
146 // Checks for pending ARP response message for the specified host.
147 // If one exists, emit response via delegate.
148 private void checkPendingArps(HostId id) {
149 ArpResponseMessage msg = pendingMessages.remove(id);
150 if (msg != null && delegate != null) {
151 log.info("Emitting ARP response from {} to {}", id, msg.outPort);
152 delegate.emitResponse(msg.outPort, ByteBuffer.wrap(msg.packet));
153 }
154 }
155
156 // Message carrying an ARP response.
157 private static class ArpResponseMessage {
158 private ConnectPoint outPort;
159 private Host subject;
160 private byte[] packet;
161
162 public ArpResponseMessage(ConnectPoint outPort, Host subject, byte[] packet) {
163 this.outPort = outPort;
164 this.subject = subject;
165 this.packet = packet;
166 }
167
168 private ArpResponseMessage() {
169 }
170 }
171
172 private class InternalHostListener implements HostListener {
173 @Override
174 public void event(HostEvent event) {
175 checkPendingArps(event.subject().id());
176 }
177 }
178}