Route Constrain policies
[bgpcep.git] / bgp / rib-spi / src / main / java / org / opendaylight / protocol / bgp / rib / spi / RouterIds.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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.protocol.bgp.rib.spi;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.cache.CacheBuilder;
12 import com.google.common.cache.CacheLoader;
13 import com.google.common.cache.LoadingCache;
14 import com.google.common.net.InetAddresses;
15 import com.google.common.primitives.UnsignedInteger;
16 import javax.annotation.Nonnull;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.PeerId;
20
21 public final class RouterIds {
22     private static final LoadingCache<String, UnsignedInteger> ROUTER_IDS =
23             CacheBuilder.newBuilder().weakValues().build(new CacheLoader<String, UnsignedInteger>() {
24                 @Override
25                 public UnsignedInteger load(final String key) {
26                     return UnsignedInteger.fromIntBits(InetAddresses.coerceToInteger(InetAddresses.forString(key)));
27                 }
28             });
29     private static final String BGP_PREFIX = "bgp://";
30     private static final LoadingCache<PeerId, UnsignedInteger> BGP_ROUTER_IDS =
31             CacheBuilder.newBuilder().weakValues().build(new CacheLoader<PeerId, UnsignedInteger>() {
32                 @Override
33                 public UnsignedInteger load(final PeerId key) {
34                     return routerIdForAddress(key.getValue().substring(BGP_PREFIX.length()));
35                 }
36             });
37
38     private RouterIds() {
39         throw new UnsupportedOperationException();
40     }
41
42     /**
43      * Get a router ID in unsigned integer format from an Ipv4Address. This implementation uses an internal
44      * cache, so the objects can be expected to perform quickly when compared with equals and similar.
45      *
46      * @param address Router ID as a dotted-quad
47      * @return Router ID as an {@link UnsignedInteger}
48      */
49     public static UnsignedInteger routerIdForAddress(@Nonnull final String address) {
50         return ROUTER_IDS.getUnchecked(address);
51     }
52
53     public static UnsignedInteger routerIdForPeerId(@Nonnull final PeerId peerId) {
54         Preconditions.checkArgument(peerId.getValue().startsWith(BGP_PREFIX),
55                 "Unhandled peer ID %s", peerId);
56         return BGP_ROUTER_IDS.getUnchecked(peerId);
57     }
58
59     public static PeerId createPeerId(@Nonnull final IpAddress address) {
60         if (address.getIpv4Address() != null) {
61             return createPeerId(address.getIpv4Address());
62         }
63         return new PeerId(BGP_PREFIX + address.getIpv6Address().getValue());
64     }
65
66     public static PeerId createPeerId(@Nonnull final Ipv4Address address) {
67         return new PeerId(BGP_PREFIX + address.getValue());
68     }
69
70     public static PeerId createPeerId(@Nonnull final UnsignedInteger intAddress) {
71         final String inet4Address = InetAddresses.fromInteger(intAddress.intValue()).getHostAddress();
72         return new PeerId(BGP_PREFIX.concat(inet4Address));
73     }
74
75     public static Ipv4Address inetFromPeerId(@Nonnull final PeerId peerId) {
76         final String inet4Address = peerId.getValue().replace(BGP_PREFIX, "");
77         return new Ipv4Address(inet4Address);
78     }
79 }