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