Bump upstreams
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / util / AddressNormalizationUtil.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.openflowplugin.impl.util;
9
10 import java.net.InetAddress;
11 import java.net.UnknownHostException;
12 import java.util.Locale;
13 import org.eclipse.jdt.annotation.Nullable;
14 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
15 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.IpConversionUtil;
16 import org.opendaylight.openflowplugin.openflow.md.util.InventoryDataServiceUtil;
17 import org.opendaylight.openflowplugin.openflow.md.util.OpenflowPortsUtil;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IetfInetUtil;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DottedQuad;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.opendaylight.ipv6.arbitrary.bitmask.fields.rev160224.Ipv6ArbitraryMask;
27 import org.opendaylight.yangtools.yang.common.Uint32;
28 import org.opendaylight.yangtools.yang.common.Uint8;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * Utility class used for converting OpenFlow port numbers, Ipv4 and Ipv6 addresses to normalized format.
34  */
35 public final class AddressNormalizationUtil {
36     private static final Logger LOG = LoggerFactory.getLogger(AddressNormalizationUtil.class);
37
38     private static final String NO_ETH_MASK = "ff:ff:ff:ff:ff:ff";
39     private static final String PREFIX_SEPARATOR = "/";
40
41     private AddressNormalizationUtil() {
42     }
43
44     /**
45      * Extract port number from URI and convert it to OpenFlow specific textual representation.
46      *
47      * @param port            the OpenFlow port
48      * @param protocolVersion the OpenFLow protocol version
49      * @return normalized uri
50      */
51     public static @Nullable Uri normalizeProtocolAgnosticPort(@Nullable final Uri port, final Uint8 protocolVersion) {
52         if (port == null) {
53             return null;
54         }
55
56         Uint32 portValue = InventoryDataServiceUtil
57                 .portNumberfromNodeConnectorId(OpenflowVersion.get(protocolVersion), port.getValue());
58
59         return portValue == null ? null : OpenflowPortsUtil.getProtocolAgnosticPortUri(protocolVersion, portValue);
60     }
61
62     /**
63      * Normalize Ipv6 address with prefix mask (ex. 1234:5678:9ABC::/76) and apply prefix mask to Ipv6 address.
64      *
65      * @param ipv6Prefix the Ipv6 prefix
66      * @return normalized Ipv6 prefix
67      */
68     public static @Nullable Ipv6Prefix normalizeIpv6Prefix(@Nullable final Ipv6Prefix ipv6Prefix) {
69         if (ipv6Prefix == null) {
70             return null;
71         }
72
73         final byte[] address = IetfInetUtil.ipv6AddressBytes(IpConversionUtil.extractIpv6Address(ipv6Prefix));
74         final byte[] mask =
75                 IpConversionUtil.convertIpv6PrefixToByteArray(IpConversionUtil.extractIpv6Prefix(ipv6Prefix));
76         return normalizeIpv6Address(address, mask);
77     }
78
79     /**
80      * Normalize Ipv6 address and arbitrary mask and apply arbitrary mask to Ipv6 address.
81      *
82      * @param ipv6Address the Ipv4 address
83      * @param ipv4Mask    the Ipv4 mask
84      * @return normalized Ipv6 prefix
85      */
86     public static @Nullable Ipv6Prefix normalizeIpv6Arbitrary(@Nullable final Ipv6Address ipv6Address,
87                                                               @Nullable final Ipv6ArbitraryMask ipv4Mask) {
88         if (ipv6Address == null) {
89             return null;
90         }
91
92         final byte[] address = IetfInetUtil.ipv6AddressBytes(ipv6Address);
93         final byte[] mask = IpConversionUtil.convertIpv6ArbitraryMaskToByteArray(ipv4Mask);
94         return normalizeIpv6Address(address, mask);
95     }
96
97     /**
98      * Normalize ipv 6 address without mask.
99      *
100      * @param ipv6Address the Ipv6 address
101      * @return normalized Ipv6 address
102      */
103     public static @Nullable Ipv6Address normalizeIpv6AddressWithoutMask(@Nullable final Ipv6Address ipv6Address) {
104         final Ipv6Prefix ipv6Prefix = normalizeIpv6Arbitrary(ipv6Address, null);
105         return ipv6Prefix == null ? null : new Ipv6Address(ipv6Prefix.getValue().split(PREFIX_SEPARATOR)[0]);
106     }
107
108     /**
109      * Normalize Ipv4 address with prefix mask (ex. 192.168.0.1/24) and apply prefix mask to Ipv4 address.
110      *
111      * @param ipv4Prefix the Ipv4 prefix
112      * @return normalized Ipv4 prefix
113      */
114     public static @Nullable Ipv4Prefix normalizeIpv4Prefix(@Nullable final Ipv4Prefix ipv4Prefix) {
115         if (ipv4Prefix == null) {
116             return null;
117         }
118
119         final byte[] address = IetfInetUtil.ipv4AddressBytes(IpConversionUtil.extractIpv4Address(ipv4Prefix));
120         final byte[] mask =
121                 IpConversionUtil.convertArbitraryMaskToByteArray(IpConversionUtil.extractIpv4AddressMask(ipv4Prefix));
122         return normalizeIpv4Address(address, mask);
123     }
124
125     /**
126      * Normalize Ipv4 address and arbitrary mask and apply arbitrary mask to Ipv4 address.
127      *
128      * @param ipv4Address the Ipv4 address
129      * @param ipv4Mask    the Ipv4 mask
130      * @return normalized Ipv4 prefix
131      */
132     public static @Nullable Ipv4Prefix normalizeIpv4Arbitrary(@Nullable final Ipv4Address ipv4Address,
133                                                               @Nullable final DottedQuad ipv4Mask) {
134         if (ipv4Address == null) {
135             return null;
136         }
137
138         final byte[] address = IetfInetUtil.ipv4AddressBytes(ipv4Address);
139         final byte[] mask = IpConversionUtil.convertArbitraryMaskToByteArray(ipv4Mask);
140         return normalizeIpv4Address(address, mask);
141     }
142
143     /**
144      * Normalize Ipv4 address and arbitrary mask in byte array format and apply arbitrary mask to Ipv4 address.
145      *
146      * @param address Ipv4 address byte array
147      * @param mask    Ipv4 mask byte array
148      * @return normalized Ipv4 prefix
149      */
150     public static @Nullable Ipv4Prefix normalizeIpv4Address(final byte @Nullable [] address,
151                                                             final byte @Nullable [] mask) {
152         final String addressPrefix = normalizeInetAddressWithMask(normalizeIpAddress(address, mask), mask);
153
154         if (addressPrefix == null) {
155             return null;
156         }
157
158         return new Ipv4Prefix(addressPrefix);
159     }
160
161
162     /**
163      * Normalize Ipv6 address and arbitrary mask in byte array format and apply arbitrary mask to Ipv6 address.
164      *
165      * @param address Ipv6 address byte array
166      * @param mask    Ipv6 mask byte array
167      * @return normalized Ipv6 prefix
168      */
169     public static @Nullable Ipv6Prefix normalizeIpv6Address(final byte @Nullable [] address,
170                                                             final byte @Nullable [] mask) {
171         final String addressPrefix = normalizeInetAddressWithMask(normalizeIpAddress(address, mask), mask);
172
173         if (addressPrefix == null) {
174             return null;
175         }
176
177         return new Ipv6Prefix(addressPrefix);
178     }
179
180     /**
181      * Normalize generic IP address and arbitrary mask in byte array format and apply arbitrary mask to IP address.
182      *
183      * @param address address byte array
184      * @param mask    mask byte array
185      * @return normalized Inet address
186      */
187     public static @Nullable InetAddress normalizeIpAddress(final byte @Nullable [] address,
188                                                            final byte @Nullable [] mask) {
189         if (address == null) {
190             return null;
191         }
192
193         final byte[] result = new byte[address.length];
194
195         for (int i = 0; i < address.length; i++) {
196             result[i] = mask != null ? (byte) (address[i] & mask[i]) : address[i];
197         }
198
199         try {
200             return InetAddress.getByAddress(result);
201         } catch (UnknownHostException e) {
202             LOG.warn("Failed to recognize the host while normalizing IP address from bytes ", e);
203             return null;
204         }
205     }
206
207     /**
208      * Convert arbitrary mask to prefix mask and append it to textual representation of Inet address.
209      *
210      * @param address the address
211      * @param mask    the mask
212      * @return the string
213      */
214     public static @Nullable String normalizeInetAddressWithMask(final @Nullable InetAddress address,
215                                                                 final byte @Nullable [] mask) {
216         if (address == null) {
217             return null;
218         }
219
220         return address.getHostAddress()
221                 + (mask == null ? "" : PREFIX_SEPARATOR + String.valueOf(IpConversionUtil.countBits(mask)));
222     }
223
224     /**
225      * Convert MAC address to it's lower case format.
226      *
227      * @param macAddress the MAC address
228      * @return normalized MAC address
229      */
230     @Nullable
231     public static MacAddress normalizeMacAddress(@Nullable final MacAddress macAddress) {
232         return macAddress == null ? null : new MacAddress(macAddress.getValue().toLowerCase(Locale.ROOT));
233     }
234
235     /**
236      * Convert MAC address mask to it's lower case format and if it is full F mask, return null.
237      *
238      * @param macAddress the MAC address
239      * @return normalized MAC address
240      */
241     @Nullable
242     public static MacAddress normalizeMacAddressMask(@Nullable final MacAddress macAddress) {
243         final MacAddress normalizedMacAddress = normalizeMacAddress(macAddress);
244
245         if (normalizedMacAddress == null) {
246             return null;
247         }
248
249         if (NO_ETH_MASK.equals(normalizedMacAddress.getValue())) {
250             return null;
251         }
252
253         return normalizedMacAddress;
254     }
255 }