2 * Copyright (c) 2014 Cisco Systems, Inc. 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.groupbasedpolicy.neutron.mapper.util;
10 import static com.google.common.base.Preconditions.checkArgument;
12 import java.net.Inet4Address;
13 import java.net.InetAddress;
14 import java.util.concurrent.ExecutionException;
16 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
17 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
18 import org.opendaylight.groupbasedpolicy.neutron.gbp.util.NeutronGbpIidFactory;
19 import org.opendaylight.groupbasedpolicy.util.DataStoreHelper;
20 import org.opendaylight.groupbasedpolicy.util.IidFactory;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.NatAddress;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.NatAddressBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoints.address.endpoints.AddressEndpointKey;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.L3ContextId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.UniqueId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.neutron.gbp.mapper.rev150513.mappings.gbp.by.neutron.mappings.base.endpoints.by.ports.BaseEndpointByPort;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.floatingips.attributes.floatingips.Floatingip;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36 import com.google.common.base.Optional;
37 import com.google.common.base.Preconditions;
38 import com.google.common.base.Strings;
39 import com.google.common.net.InetAddresses;
44 throw new UnsupportedOperationException("Cannot create an instance.");
48 * This implementation does not use nameservice lookups (e.g. no DNS).
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
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));
62 return new IpPrefix(new Ipv6Prefix(cidr));
66 * This implementation does not use nameservice lookups (e.g. no DNS).
68 * @param ipAddress - format must be valid for regex in {@link Ipv4Address} or
70 * @return the {@link IpAddress} having the given ipAddress string representation
71 * @throws IllegalArgumentException - if the argument is not a valid IP address string
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));
79 return new IpAddress(new Ipv6Address(ipAddress));
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('/', '_');
88 String ipPrefixIpv6 = ipPrefix.getIpv6Prefix().getValue();
89 return ipPrefixIpv6.replace('/', '_').replace(':', '.');
92 public static String getStringIpAddress(IpAddress ipAddress) {
93 if (ipAddress.getIpv4Address() != null) {
94 return ipAddress.getIpv4Address().getValue();
96 return ipAddress.getIpv6Address().getValue();
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]+)",
104 //TODO move to FloatingIpAware when deprecated API is removed
105 public static void syncNat(ReadWriteTransaction rwTx, Floatingip oldFloatingIp, Floatingip newFloatingIp)
106 throws InterruptedException, ExecutionException {
107 IpAddress oldEpIp = oldFloatingIp.getFixedIpAddress();
108 IpAddress newEpIp = newFloatingIp.getFixedIpAddress();
109 IpAddress epNatIp = newFloatingIp.getFloatingIpAddress();
110 if (epNatIp != null && newEpIp != null) {
111 InstanceIdentifier<BaseEndpointByPort> baseEpByPortId = NeutronGbpIidFactory.baseEndpointByPortIid(new UniqueId(
112 newFloatingIp.getPortId().getValue()));
113 Optional<BaseEndpointByPort> optional = rwTx.read(LogicalDatastoreType.OPERATIONAL, baseEpByPortId).get();
114 if (!optional.isPresent()) {
117 NatAddress nat = new NatAddressBuilder().setNatAddress(epNatIp).build();
118 AddressEndpointKey addrEpKey = new AddressEndpointKey(optional.get().getAddress(), optional.get()
119 .getAddressType(), optional.get().getContextId(), optional.get().getContextType());
120 rwTx.put(LogicalDatastoreType.OPERATIONAL,
121 IidFactory.addressEndpointIid(addrEpKey).augmentation(NatAddress.class), nat, true);
123 if (oldEpIp != null) {
124 InstanceIdentifier<BaseEndpointByPort> baseEpByPortId = NeutronGbpIidFactory.baseEndpointByPortIid(new UniqueId(
125 oldFloatingIp.getPortId().getValue()));
126 Optional<BaseEndpointByPort> optional = rwTx.read(LogicalDatastoreType.OPERATIONAL, baseEpByPortId).get();
127 if (!optional.isPresent()) {
130 AddressEndpointKey addrEpKey = new AddressEndpointKey(optional.get().getAddress(), optional.get()
131 .getAddressType(), optional.get().getContextId(), optional.get().getContextType());
132 DataStoreHelper.removeIfExists(LogicalDatastoreType.OPERATIONAL, IidFactory.addressEndpointIid(addrEpKey)
133 .augmentation(NatAddress.class), rwTx);