blob: 307a7836c3c40895372268217c77431e93d1600f [file] [log] [blame]
Jonathan Hart06e89082016-08-08 17:21:01 -07001/*
2 * Copyright 2016-present Open Networking Laboratory
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
17package org.onosproject.incubator.net.neighbour;
18
19import com.google.common.annotations.Beta;
20import org.onlab.packet.Ethernet;
21import org.onlab.packet.IpAddress;
22import org.onlab.packet.MacAddress;
23import org.onlab.packet.VlanId;
24import org.onosproject.incubator.net.intf.Interface;
25import org.onosproject.net.ConnectPoint;
26
27import static com.google.common.base.Preconditions.checkState;
28
29/**
30 * Context of an incoming neighbor message (e.g. ARP, NDP).
31 *
32 * <p>This includes information about the message accessible through a
33 * protocol-agnostic interface, as well as mechanisms to perform an action in
34 * response to the incoming message.</p>
35 */
36@Beta
37public class NeighbourMessageContext {
38
39 private final NeighbourProtocol protocol;
40 private final NeighbourMessageType type;
41
42 private final IpAddress target;
43 private final IpAddress sender;
44
45 private final Ethernet eth;
46 private final ConnectPoint inPort;
47
48 private final NeighbourMessageActions actions;
49
50 /**
51 * Creates a new neighbour message context.
52 *
53 * @param actions actions
54 * @param eth ethernet frame
55 * @param inPort incoming port
56 * @param protocol message protocol
57 * @param type message type
58 * @param target target IP address
59 * @param sender sender IP address
60 */
61 public NeighbourMessageContext(NeighbourMessageActions actions,
62 Ethernet eth, ConnectPoint inPort,
63 NeighbourProtocol protocol, NeighbourMessageType type,
64 IpAddress target, IpAddress sender) {
65 this.actions = actions;
66 this.eth = eth;
67 this.inPort = inPort;
68 this.protocol = protocol;
69 this.type = type;
70 this.target = target;
71 this.sender = sender;
72 }
73
74 /**
75 * Gets the port where the packet came in to the network.
76 *
77 * @return connect point
78 */
79 public ConnectPoint inPort() {
80 return inPort;
81 }
82
83 /**
84 * Gets the full parsed representation of the packet.
85 *
86 * @return ethernet header
87 */
88 public Ethernet packet() {
89 return eth;
90 }
91
92 /**
93 * Gets the protocol of the packet.
94 *
95 * @return protocol
96 */
97 public NeighbourProtocol protocol() {
98 return protocol;
99 }
100
101 /**
102 * Gets the message type of the packet.
103 *
104 * @return message type
105 */
106 public NeighbourMessageType type() {
107 return type;
108 }
109
110 /**
111 * Gets the vlan of the packet, if any.
112 *
113 * @return vlan
114 */
115 public VlanId vlan() {
116 return VlanId.vlanId(eth.getVlanID());
117 }
118
119 /**
120 * Gets the source MAC address of the message.
121 *
122 * @return source MAC address
123 */
124 public MacAddress srcMac() {
125 return MacAddress.valueOf(eth.getSourceMACAddress());
126 }
127
128 /**
129 * Gets the target IP address of the message.
130 *
131 * @return target IP address
132 */
133 public IpAddress target() {
134 return target;
135 }
136
137 /**
138 * Gets the source IP address of the message.
139 *
140 * @return source IP address
141 */
142 public IpAddress sender() {
143 return sender;
144 }
145
146 /**
147 * Proxies the message to a given output port.
148 *
149 * @param outPort output port
150 */
151 public void proxy(ConnectPoint outPort) {
152 actions.proxy(this, outPort);
153 }
154
155 /**
156 * Proxies the message to a given interface.
157 *
158 * @param outIntf output interface
159 */
160 public void proxy(Interface outIntf) {
161 actions.proxy(this, outIntf);
162 }
163
164 /**
165 * Replies to the request message with a given MAC address.
166 *
167 * @param targetMac target MAC address
168 */
169 public void reply(MacAddress targetMac) {
170 checkState(type == NeighbourMessageType.REQUEST, "can only reply to requests");
171
172 actions.reply(this, targetMac);
173 }
174
175 /**
176 * Floods the incoming message out all ports except the input port.
177 */
178 public void flood() {
179 actions.flood(this);
180 }
181
182 /**
183 * Drops the incoming message.
184 */
185 public void drop() {
186 actions.drop(this);
187 }
188
189}