4f3bb1faf02fefc2235a378462b6c5d1b2ce8ace
[groupbasedpolicy.git] / neutron-mapper / src / main / java / org / opendaylight / groupbasedpolicy / neutron / mapper / util / Utils.java
1 /*
2  * Copyright (c) 2014 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.groupbasedpolicy.neutron.mapper.util;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
12 import com.google.common.base.Preconditions;
13 import com.google.common.base.Strings;
14 import com.google.common.net.InetAddresses;
15
16 import java.net.Inet4Address;
17 import java.net.InetAddress;
18
19 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.groupbasedpolicy.util.IidFactory;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.NatAddress;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.NatAddressBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoints.address.endpoints.AddressEndpointKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ContextId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.forwarding.l2_l3.rev170511.IpPrefixType;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.forwarding.l2_l3.rev170511.L3Context;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.floatingips.attributes.floatingips.Floatingip;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 public class Utils {
39
40     private static final String MASK_32 = "/32";
41     private static final Logger LOG = LoggerFactory.getLogger(Utils.class);
42
43     private Utils() {
44         throw new UnsupportedOperationException("Cannot create an instance.");
45     }
46
47     /**
48      * This implementation does not use nameservice lookups (e.g. no DNS).
49      *
50      * @param cidr - format must be valid for regex in {@link Ipv4Prefix} or {@link Ipv6Prefix}
51      * @return the {@link IpPrefix} having the given cidr string representation
52      * @throws IllegalArgumentException - if the argument is not a valid CIDR string
53      */
54     public static IpPrefix createIpPrefix(String cidr) {
55         checkArgument(!Strings.isNullOrEmpty(cidr), "Cannot be null or empty.");
56         String[] ipAndPrefix = cidr.split("/");
57         checkArgument(ipAndPrefix.length == 2, "Bad format.");
58         InetAddress ip = InetAddresses.forString(ipAndPrefix[0]);
59         if (ip instanceof Inet4Address) {
60             return new IpPrefix(new Ipv4Prefix(cidr));
61         }
62         return new IpPrefix(new Ipv6Prefix(cidr));
63     }
64
65     /**
66      * This implementation does not use nameservice lookups (e.g. no DNS).
67      *
68      * @param ipAddress - format must be valid for regex in {@link Ipv4Address} or
69      *        {@link Ipv6Address}
70      * @return the {@link IpAddress} having the given ipAddress string representation
71      * @throws IllegalArgumentException - if the argument is not a valid IP address string
72      */
73     public static IpAddress createIpAddress(String ipAddress) {
74         checkArgument(!Strings.isNullOrEmpty(ipAddress), "Cannot be null or empty.");
75         InetAddress ip = InetAddresses.forString(ipAddress);
76         if (ip instanceof Inet4Address) {
77             return new IpAddress(new Ipv4Address(ipAddress));
78         }
79         return new IpAddress(new Ipv6Address(ipAddress));
80     }
81
82     public static String getStringIpPrefix(IpPrefix ipPrefix) {
83         Preconditions.checkNotNull(ipPrefix);
84         if (ipPrefix.getIpv4Prefix() != null) {
85             String ipPrefixIpv4 = ipPrefix.getIpv4Prefix().getValue();
86             return ipPrefixIpv4.replace('/', '_');
87         }
88         String ipPrefixIpv6 = ipPrefix.getIpv6Prefix().getValue();
89         return ipPrefixIpv6.replace('/', '_').replace(':', '.');
90     }
91
92     public static String getStringIpAddress(IpAddress ipAddress) {
93         if (ipAddress.getIpv4Address() != null) {
94             return ipAddress.getIpv4Address().getValue();
95         }
96         return ipAddress.getIpv6Address().getValue();
97     }
98
99     public static String normalizeUuid(String string) {
100         return string.replaceFirst("([0-9a-fA-F]{8})([0-9a-fA-F]{4})([0-9a-fA-F]{4})([0-9a-fA-F]{4})([0-9a-fA-F]+)",
101                 "$1-$2-$3-$4-$5");
102     }
103
104     //TODO move to FloatingIpAware when deprecated API is removed
105     public static void syncNat(ReadWriteTransaction rwTx, Floatingip oldFloatingIp, Floatingip newFloatingIp) {
106         IpAddress oldEpIp = oldFloatingIp.getFixedIpAddress();
107         IpAddress newEpIp = newFloatingIp.getFixedIpAddress();
108         IpAddress epNatIp = newFloatingIp.getFloatingIpAddress();
109         if (oldEpIp != null && oldFloatingIp.getRouterId() != null) {
110             removeNat(rwTx, oldFloatingIp);
111         }
112         if (epNatIp != null && newEpIp != null) {
113             NatAddress natAddr = new NatAddressBuilder().setNatAddress(epNatIp).build();
114             AddressEndpointKey addrEpKey = new AddressEndpointKey(newEpIp.getIpv4Address().getValue() + MASK_32,
115                     IpPrefixType.class, new ContextId(newFloatingIp.getRouterId().getValue()), L3Context.class);
116             rwTx.put(LogicalDatastoreType.OPERATIONAL,
117                     IidFactory.addressEndpointIid(addrEpKey).augmentation(NatAddress.class), natAddr, true);
118             LOG.debug("Adding NatAddress to endpoint {}", addrEpKey);
119         }
120     }
121
122     public static void removeNat(ReadWriteTransaction rwTx, Floatingip removedFloatingIp) {
123         if (removedFloatingIp.getFixedIpAddress() == null || removedFloatingIp.getRouterId() == null) {
124             // NAT augmentation should have been already removed
125             return;
126         }
127         AddressEndpointKey addrEpKey =
128                 new AddressEndpointKey(removedFloatingIp.getFixedIpAddress().getIpv4Address().getValue() + MASK_32,
129                         IpPrefixType.class, new ContextId(removedFloatingIp.getRouterId().getValue()), L3Context.class);
130         rwTx.delete(LogicalDatastoreType.OPERATIONAL,
131                 IidFactory.addressEndpointIid(addrEpKey).augmentation(NatAddress.class));
132         LOG.debug("Removing NatAddress from endpoint {}", addrEpKey);
133     }
134 }