2 * Copyright (c) 2017 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
8 package org.opendaylight.netvirt.fibmanager.api;
10 import static java.util.Comparator.comparing;
11 import static java.util.stream.Collectors.toList;
13 import java.math.BigInteger;
14 import java.net.InetAddress;
15 import java.net.UnknownHostException;
16 import java.util.ArrayList;
17 import java.util.Collections;
18 import java.util.List;
19 import java.util.Optional;
20 import java.util.concurrent.ExecutionException;
21 import java.util.stream.Collectors;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.opendaylight.mdsal.binding.api.DataBroker;
24 import org.opendaylight.mdsal.binding.api.ReadTransaction;
25 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.FibEntries;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.fibentries.VrfTables;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.fibentries.VrfTablesKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.vrfentries.VrfEntry;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.vrfentries.VrfEntryBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.vrfentries.VrfEntryKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.vrfentrybase.RoutePaths;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.vrfentrybase.RoutePathsBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.fibmanager.rev150330.vrfentrybase.RoutePathsKey;
35 import org.opendaylight.yangtools.yang.binding.DataObject;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
38 import org.opendaylight.yangtools.yang.common.Uint32;
39 import org.opendaylight.yangtools.yang.common.Uint64;
41 public final class FibHelper {
46 public static RoutePaths buildRoutePath(String nextHop, Uint32 label) {
47 RoutePathsBuilder builder = new RoutePathsBuilder()
48 .withKey(new RoutePathsKey(nextHop))
49 .setNexthopAddress(nextHop);
51 builder.setLabel(label);
53 return builder.build();
56 public static VrfEntryBuilder getVrfEntryBuilder(String prefix, RouteOrigin origin, @Nullable String parentVpnRd) {
57 return new VrfEntryBuilder().withKey(new VrfEntryKey(prefix)).setDestPrefix(prefix)
58 .setOrigin(origin.getValue()).setParentVpnRd(parentVpnRd);
61 public static VrfEntryBuilder getVrfEntryBuilder(String prefix, List<RoutePaths> routePaths,
62 RouteOrigin origin, @Nullable String parentVpnRd) {
63 return new VrfEntryBuilder().withKey(new VrfEntryKey(prefix)).setDestPrefix(prefix)
64 .setRoutePaths(routePaths).setOrigin(origin.getValue()).setParentVpnRd(parentVpnRd);
67 public static VrfEntryBuilder getVrfEntryBuilder(String prefix, Uint32 label, String nextHop, RouteOrigin origin,
68 @Nullable String parentVpnRd) {
69 if (nextHop != null) {
70 RoutePaths routePath = buildRoutePath(nextHop, label);
71 return getVrfEntryBuilder(prefix, Collections.singletonList(routePath), origin, parentVpnRd);
73 return getVrfEntryBuilder(prefix, origin, parentVpnRd);
77 public static VrfEntryBuilder getVrfEntryBuilder(VrfEntry vrfEntry, Uint32 label,
78 List<String> nextHopList, RouteOrigin origin, @Nullable String parentvpnRd) {
79 List<RoutePaths> routePaths =
80 nextHopList.stream().map(nextHop -> buildRoutePath(nextHop, label))
82 return getVrfEntryBuilder(vrfEntry.getDestPrefix(), routePaths, origin, parentvpnRd);
85 public static InstanceIdentifier<RoutePaths> buildRoutePathId(String rd, String prefix, String nextHop) {
86 InstanceIdentifierBuilder<RoutePaths> idBuilder =
87 InstanceIdentifier.builder(FibEntries.class)
88 .child(VrfTables.class, new VrfTablesKey(rd))
89 .child(VrfEntry.class, new VrfEntryKey(prefix))
90 .child(RoutePaths.class, new RoutePathsKey(nextHop));
91 return idBuilder.build();
94 public static boolean isControllerManagedNonInterVpnLinkRoute(RouteOrigin routeOrigin) {
95 return routeOrigin == RouteOrigin.STATIC
96 || routeOrigin == RouteOrigin.CONNECTED
97 || routeOrigin == RouteOrigin.LOCAL;
100 public static boolean isControllerManagedVpnInterfaceRoute(RouteOrigin routeOrigin) {
101 return routeOrigin == RouteOrigin.STATIC
102 || routeOrigin == RouteOrigin.LOCAL;
105 public static boolean isControllerManagedNonSelfImportedRoute(RouteOrigin routeOrigin) {
106 return routeOrigin != RouteOrigin.SELF_IMPORTED;
109 public static void sortIpAddress(List<RoutePaths> routePathList) {
110 if (routePathList != null) {
111 routePathList.sort(comparing(RoutePaths::getNexthopAddress));
115 public static List<String> getNextHopListFromRoutePaths(final VrfEntry vrfEntry) {
116 List<RoutePaths> routePaths = vrfEntry.getRoutePaths();
117 if (routePaths == null || routePaths.isEmpty()) {
118 return new ArrayList<>();
120 return routePaths.stream()
121 .map(RoutePaths::getNexthopAddress)
122 .collect(Collectors.toList());
125 public static Optional<VrfEntry> getVrfEntry(DataBroker broker, String rd, String ipPrefix) {
126 InstanceIdentifier<VrfEntry> vrfEntryId = InstanceIdentifier.builder(FibEntries.class)
127 .child(VrfTables.class, new VrfTablesKey(rd))
128 .child(VrfEntry.class, new VrfEntryKey(ipPrefix)).build();
129 return read(broker, LogicalDatastoreType.CONFIGURATION, vrfEntryId);
132 private static <T extends DataObject> Optional<T> read(DataBroker broker,
133 LogicalDatastoreType datastoreType, InstanceIdentifier<T> path) {
134 try (ReadTransaction tx = broker.newReadOnlyTransaction()) {
135 return tx.read(datastoreType, path).get();
136 } catch (InterruptedException | ExecutionException e) {
137 throw new RuntimeException(e);
141 /** get true if this prefix is an IPv4 version, false otherwise.
142 * @param prefix the prefix as (x.x.x.x/nn) to find if it is in IP version 4
143 * @return true if it is an IPv4 or false if it is not.
145 public static boolean isIpv4Prefix(String prefix) {
146 if (prefix == null || prefix.length() < 7) {
150 String ip = getIpFromPrefix(prefix);
151 java.net.Inet4Address.getByName(ip);
152 } catch (SecurityException | UnknownHostException | ClassCastException e) {
158 /** get true if this prefix is an IPv6 version, false otherwise.
159 * @param prefix the prefix as ( x:x:x::/nn) to find if it is in IP version 6
160 * @return true if it is an IPv4 or false if it is not.
162 public static boolean isIpv6Prefix(String prefix) {
163 if (prefix == null || prefix.length() < 2) {
167 String ip = getIpFromPrefix(prefix);
168 java.net.Inet6Address.getByName(ip);
169 } catch (SecurityException | UnknownHostException | ClassCastException e) {
175 /**get String format IP from prefix as x.x.....x/nn.
176 * @param prefix the prefix as IPv4 or IPv6 as x.....x/nn
177 * @return prefix if "/" is unfindable or the IP only as x.x...x from x.x......x/nn
180 public static String getIpFromPrefix(String prefix) {
181 if (prefix == null || prefix.length() < 2) {
185 String[] prefixValues = prefix.split("/");
186 if (prefixValues.length > 0) {
187 rep = prefixValues[0];
192 /**Return true if this prefix or subnet is belonging the specified subnetwork.
193 * @param prefixToTest the prefix which could be in the subnet
194 * @param subnet the subnet that have to contain the prefixToTest to return true
195 * @return true if the param subnet contained the prefixToTest false otherwise
197 public static boolean isBelongingPrefix(String prefixToTest, String subnet) {
198 return doesPrefixBelongToSubnet(prefixToTest, subnet, true);
201 /**Return true if this prefix or subnet is belonging the specified subnetwork.
202 * @param prefixToTest the prefix which could be in the subnet
203 * @param subnet the subnet that have to contain the prefixToTest to return true
204 * @param exactMatch boolean set to true if exact match is expected
205 * @return true if the param subnet contained the prefixToTest false otherwise
207 public static boolean doesPrefixBelongToSubnet(String prefixToTest, String subnet, boolean exactMatch) {
208 if (prefixToTest == null || prefixToTest.length() < 7 || subnet == null || subnet.length() < 7) {
211 if (isIpv4Prefix(prefixToTest) && isIpv4Prefix(subnet)
212 || isIpv6Prefix(prefixToTest) && isIpv6Prefix(subnet)) {
215 if (isIpv6Prefix(prefixToTest)) {
219 String ipPref = getIpFromPrefix(prefixToTest);
220 String ipSub = getIpFromPrefix(subnet);
221 String maskSubString = subnet.substring(subnet.indexOf("/") + 1);
222 String maskPrefString;
223 if (prefixToTest.contains("/")) {
224 maskPrefString = prefixToTest.substring(prefixToTest.indexOf("/") + 1);
225 } else if (ipVersion == 4) {
226 maskPrefString = "32";
228 maskPrefString = "128";
232 int maskPref = Integer.parseInt(maskPrefString);
233 int maskSub = Integer.parseInt(maskSubString);
234 if (exactMatch && maskPref != maskSub) {
235 /*because the mask must be exactly the same between them, the return type is false. This behavior could
236 * be changed to ignored it in including a boolean options to force or not the same mask control*/
239 BigInteger maskSubBig = getMaskNetwork(ipVersion, maskSub);
240 byte[] byteIpSub = InetAddress.getByName(ipSub).getAddress();
241 byte[] byteIpPref = InetAddress.getByName(ipPref).getAddress();
242 BigInteger netFromIpSub = packBigInteger(byteIpSub).and(maskSubBig);
243 BigInteger netFromIpPref = packBigInteger(byteIpPref).and(maskSubBig);
244 return netFromIpSub.compareTo(netFromIpPref) == 0;
245 } catch (NumberFormatException | UnknownHostException e) {
252 /**This method converts a byte[] to a BigInteger value.
253 * @param bytes the byte[] of IP
254 * @return (big) integer value of IP_bytes
256 public static BigInteger packBigInteger(byte[] bytes) {
257 BigInteger val = BigInteger.valueOf(0);
258 for (byte b : bytes) {
259 val = val.shiftLeft(8);
260 val = val.add(BigInteger.valueOf(b & 0xff));
265 /**get the bits cache mask of net for a ip version type.
266 * @param ipVersion version of ip must be 4 or 6
267 * @param mask the lengh of the mask of net as 24 from this representation 10.1.1.0/24 or 64 for 2001::1/64
268 * @return the bit mask of net ex: x.x.x.x/24 return a BigInteger == 0xFFFFFFotherwise null if any error
271 public static BigInteger getMaskNetwork(int ipVersion, int mask) {
273 if (ipVersion == 6) {
274 lenghBitsIp = 128 - 1;
275 } else if (ipVersion == 4) {
276 lenghBitsIp = 32 - 1;
278 return null;//ip version is unknown
280 BigInteger bb = BigInteger.ZERO;
281 for (int i = 0 ; i < mask; i++) {
282 bb = bb.setBit(lenghBitsIp - i);
287 public static String getJobKeyForDcGwLoadBalancingGroup(Uint64 dpnId) {
288 return new StringBuilder().append("NextHopManager").append(dpnId).toString();