3 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
5 * This program and the accompanying materials are made available under the
6 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7 * and is available at http://www.eclipse.org/legal/epl-v10.html
10 package org.opendaylight.controller.forwarding.staticrouting;
12 import java.net.Inet4Address;
13 import java.net.Inet6Address;
14 import java.net.InetAddress;
15 import java.nio.ByteBuffer;
18 import org.opendaylight.controller.sal.core.Node;
19 import org.opendaylight.controller.sal.core.NodeConnector;
20 import org.opendaylight.controller.sal.packet.BitBufferHelper;
21 import org.opendaylight.controller.sal.utils.NodeConnectorCreator;
22 import org.opendaylight.controller.sal.utils.NodeCreator;
24 import org.opendaylight.controller.hosttracker.hostAware.HostNodeConnector;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * This class defines a static route object.
31 public class StaticRoute {
32 protected static final Logger logger = LoggerFactory
33 .getLogger(StaticRoute.class);
36 * This Enum defines the possible types for the next hop address.
38 public enum NextHopType {
39 IPADDRESS("nexthop-ip"), SWITCHPORT("nexthop-interface");
40 private NextHopType(String name) {
46 public String toString() {
50 public static NextHopType fromString(String str) {
53 if (str.equals(IPADDRESS.toString()))
55 if (str.equals(SWITCHPORT.toString()))
61 InetAddress networkAddress;
64 InetAddress nextHopAddress;
67 HostNodeConnector host;
70 * Create a static route object with no specific information.
72 public StaticRoute() {
77 * Create a static route object from the StaticRouteConfig.
78 * @param: config: StaticRouteConfig
80 public StaticRoute(StaticRouteConfig config) {
81 networkAddress = config.getStaticRouteIP();
82 mask = StaticRoute.getV4AddressMaskFromDecimal(config
83 .getStaticRouteMask());
84 type = NextHopType.fromString(config.getNextHopType());
85 nextHopAddress = config.getNextHopIP();
86 Map<Long, Short> switchPort = config.getNextHopSwitchPorts();
87 if ((switchPort != null) && (switchPort.size() == 1)) {
88 node = NodeCreator.createOFNode((Long) switchPort.keySet()
90 port = NodeConnectorCreator.createOFNodeConnector(
91 (Short) switchPort.values().toArray()[0], node);
96 * Get the IP address portion of the sub-network of the static route.
97 * @return InetAddress: the IP address portion of the sub-network of the static route
99 public InetAddress getNetworkAddress() {
100 return networkAddress;
104 * Set the IP address portion of the sub-network of the static route.
105 * @param networkAddress The IP address (InetAddress) to be set
107 public void setNetworkAddress(InetAddress networkAddress) {
108 this.networkAddress = networkAddress;
112 * Get the mask of the sub-network of the static route.
113 * @return mask: the mask (InetAddress) of the sub-network of the static route
115 public InetAddress getMask() {
120 * Set the sub-network's mask of the static route.
121 * @param mask The mask (InetAddress) to be set
123 public void setMask(InetAddress mask) {
128 * Get the NextHopeType of the static route.
129 * @return type: NextHopeType
131 public NextHopType getType() {
136 * Set the nextHopType.
137 * @param type The NextHopType to be set
139 public void setType(NextHopType type) {
144 * Get the next hop IP address.
145 * @return: nextHopAddress (InetAddress)
147 public InetAddress getNextHopAddress() {
148 return nextHopAddress;
152 * Set the next hop IP address.
153 * @param nextHopAddress The IP address (InetAddress) to be set
155 public void setNextHopAddress(InetAddress nextHopAddress) {
156 this.nextHopAddress = nextHopAddress;
160 * Get the Node associated with the static route.
163 public Node getNode() {
168 * Set the node associated to the static route.
169 * @param node: The node to be set
171 public void setNode(Node node) {
176 * Set the port associated to the static route.
177 * @param port The port (NodeConnector) to be set
179 public void setPort(NodeConnector port) {
184 * Get the port associated to the static route.
185 * @return port: The port (NodeConnector)
187 public NodeConnector getPort() {
192 * Get the Host associated to static route.
193 * @return host: The host (HostNodeConnector)
195 public HostNodeConnector getHost() {
200 * Set the host associated to the static route.
201 * @param host: (HostNodeConnector) to be set
203 public void setHost(HostNodeConnector host) {
208 public int hashCode() {
209 final int prime = 31;
211 result = prime * result + ((host == null) ? 0 : host.hashCode());
212 result = prime * result + ((mask == null) ? 0 : mask.hashCode());
213 result = prime * result
214 + ((networkAddress == null) ? 0 : networkAddress.hashCode());
215 result = prime * result
216 + ((nextHopAddress == null) ? 0 : nextHopAddress.hashCode());
217 result = prime * result + ((port == null) ? 0 : port.hashCode());
218 result = prime * result + ((node == null) ? 0 : node.hashCode());
219 result = prime * result + ((type == null) ? 0 : type.hashCode());
224 public String toString() {
225 return "StaticRoute [networkAddress=" + networkAddress + ", mask="
226 + mask + ", type=" + type.toString() + ", nextHopAddress="
227 + nextHopAddress + ", swid=" + node + ", port=" + port
228 + ", host=" + host + "]";
232 public boolean equals(Object obj) {
237 if (getClass() != obj.getClass())
239 StaticRoute other = (StaticRoute) obj;
240 if (!networkAddress.equals(other.networkAddress))
242 if (!mask.equals(other.mask))
247 private static InetAddress getV4AddressMaskFromDecimal(int mask) {
249 for (int i = 0; i < mask; i++) {
250 netmask |= (1 << 31 - i);
254 return InetAddress.getByAddress(BitBufferHelper
255 .toByteArray(netmask));
256 } catch (Exception e) {
262 private void applyV4MaskOnByteBuffer(ByteBuffer bb, ByteBuffer bbMask) {
263 for (int i = 0; i < bb.array().length; i++) {
264 bb.put(i, (byte) (bb.get(i) & bbMask.get(i)));
269 * Compute and return the IP address with longest prefix match from the static route based on the
270 * destNetworkAddress. Currently it only take IPv4 address format (Inet4Address)
271 * @param destNetworkAddress: the IP address to be based on
272 * @return: InetAddress: the IPv4 address with the longest prefix matching the static route.
273 * If the destNetworkkAddress is not IPv4 format, it will return null.
275 public InetAddress longestPrefixMatch(InetAddress destNetworkAddress) {
276 if (destNetworkAddress instanceof Inet4Address) {
277 ByteBuffer bbdest = ByteBuffer
278 .wrap(destNetworkAddress.getAddress());
279 ByteBuffer bbself = ByteBuffer.wrap(networkAddress.getAddress());
281 ByteBuffer bbMask = ByteBuffer.wrap(mask.getAddress());
283 applyV4MaskOnByteBuffer(bbdest, bbMask);
284 applyV4MaskOnByteBuffer(bbself, bbMask);
286 if (bbdest.equals(bbself)) {
288 return InetAddress.getByAddress(bbself.array());
289 } catch (Exception e) {
298 * Compare the static route with another static route. It only handles(for now) IPv4Address.
299 * @param s: the other StaticRoute
300 * @return: 0 if they are the same
302 public int compareTo(StaticRoute s) {
305 if ((networkAddress instanceof Inet6Address)
306 || (s.getNetworkAddress() instanceof Inet6Address)) {
311 ByteBuffer bbchallenger = ByteBuffer.wrap(s.getNetworkAddress()
313 ByteBuffer bbself = ByteBuffer.wrap(networkAddress.getAddress());
314 ByteBuffer bbChallengerMask = ByteBuffer.wrap(s.getMask().getAddress());
315 ByteBuffer bbSelfMask = ByteBuffer.wrap(getMask().getAddress());
317 applyV4MaskOnByteBuffer(bbchallenger, bbChallengerMask);
318 applyV4MaskOnByteBuffer(bbself, bbSelfMask);
319 return bbself.compareTo(bbchallenger);