blob: 5f34e9f29f39389b3f6388ad821073c4e250bf22 [file] [log] [blame]
Jonathan Hart06e89082016-08-08 17:21:01 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Jonathan Hart06e89082016-08-08 17:21:01 -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
Ray Milkeyb65d7842017-08-03 16:28:24 -070017package org.onosproject.net.neighbour;
Jonathan Hart06e89082016-08-08 17:21:01 -070018
19import org.onosproject.net.Host;
20import org.onosproject.net.host.HostService;
21
22import java.util.Set;
23
Jonathan Hart9bdaaec2016-08-22 13:33:45 -070024import static org.onlab.packet.VlanId.vlanId;
Jonathan Hart06e89082016-08-08 17:21:01 -070025import static org.onosproject.net.HostId.hostId;
26
27/**
Jonathan Hart9bdaaec2016-08-22 13:33:45 -070028 * Default neighbour message handler which implements basic proxying on an
Jonathan Hart06e89082016-08-08 17:21:01 -070029 * L2 network (i.e. ProxyArp behaviour).
30 */
31public class DefaultNeighbourMessageHandler implements NeighbourMessageHandler {
32 @Override
33 public void handleMessage(NeighbourMessageContext context, HostService hostService) {
Jonathan Hart9bdaaec2016-08-22 13:33:45 -070034 switch (context.type()) {
35 case REPLY:
36 Host h = hostService.getHost(hostId(context.packet().getDestinationMAC(),
37 vlanId(context.packet().getVlanID())));
Jonathan Hart06e89082016-08-08 17:21:01 -070038
Jonathan Hart9bdaaec2016-08-22 13:33:45 -070039 if (h == null) {
40 context.flood();
41 } else {
Jonathan Hart2efe0c22016-09-21 12:04:45 -070042 context.forward(h.location());
Jonathan Hart06e89082016-08-08 17:21:01 -070043 }
Jonathan Hart9bdaaec2016-08-22 13:33:45 -070044 break;
45 case REQUEST:
46 // See if we have the target host in the host store
47 Set<Host> hosts = hostService.getHostsByIp(context.target());
48
49 Host dst = null;
50 Host src = hostService.getHost(hostId(context.srcMac(), context.vlan()));
51
52 for (Host host : hosts) {
53 if (host.vlan().equals(context.vlan())) {
54 dst = host;
55 break;
56 }
57 }
58
59 if (src != null && dst != null) {
60 // We know the target host so we can respond
61 context.reply(dst.mac());
62 return;
63 }
64
65 // The request couldn't be resolved.
66 // Flood the request on all ports except the incoming port.
67 context.flood();
68 break;
69 default:
70 break;
Jonathan Hart06e89082016-08-08 17:21:01 -070071 }
72
Jonathan Hart06e89082016-08-08 17:21:01 -070073 }
74}