Checkstyle: fix issues and enforce on implementation
[lispflowmapping.git] / mappingservice / implementation / src / main / java / org / opendaylight / lispflowmapping / implementation / util / LispNotificationHelper.java
index 9772303a530e5df8bc1ecba41f63783fc43dcd1e..56519d8a29dd0298d41f567b584f3211a4581e1c 100644 (file)
+/*
+ * Copyright (c) 2014 Contextream, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
 package org.opendaylight.lispflowmapping.implementation.util;
 
-import java.net.Inet4Address;
+import com.google.common.base.Splitter;
+import com.google.common.net.InetAddresses;
 import java.net.InetAddress;
 import java.net.UnknownHostException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import org.opendaylight.lispflowmapping.lisp.type.LispMessage;
+import org.opendaylight.lispflowmapping.lisp.util.LispAddressStringifier;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddress;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.Address;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.ApplicationData;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.DistinguishedName;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv4;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv6;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.KeyValueAddress;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.inet.binary.types.rev160303.IpAddressBinary;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.inet.binary.types.rev160303.IpAddressBinaryBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.inet.binary.types.rev160303.Ipv4AddressBinary;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.inet.binary.types.rev160303.Ipv6AddressBinary;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.augmented.lisp.address.address.Ipv4Binary;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.augmented.lisp.address.address.Ipv6Binary;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.AddMapping;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.SiteId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecord;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapregisternotification.MapRegister;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddress;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddressBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.EidUri;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingOrigin;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.Mapping;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.MappingBuilder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
-import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.mapregisternotification.MapRegister;
-import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.mapregisternotification.MapRegisterBuilder;
-import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.maprequestnotification.MapRequest;
-import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.maprequestnotification.MapRequestBuilder;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Address;
+public final class LispNotificationHelper {
+    protected static final Logger LOG = LoggerFactory.getLogger(LispNotificationHelper.class);
+    private static final Splitter COLON_SPLITTER = Splitter.on(':');
 
-public class LispNotificationHelper {
+    // Utility class, should not be instantiated
+    private LispNotificationHelper() {
+    }
+
+    public static TransportAddress getTransportAddressFromRloc(Rloc rloc) {
+        TransportAddressBuilder tab = new TransportAddressBuilder();
+        Address address = rloc.getAddress();
+        // once we switch to using Ipv4Binary and Ipv6Binary by default for RLOCs, we will only need to keep the below
+        // for backwards compatibility reasons, but the default will be to just return the reference for the binary
+        // object, yey!
+        if (address instanceof Ipv4) {
+            String ipv4 = ((Ipv4) address).getIpv4().getValue();
+            tab.setIpAddress(IpAddressBinaryBuilder.getDefaultInstance(InetAddresses.forString(ipv4).getAddress()));
+            tab.setPort(new PortNumber(LispMessage.PORT_NUM));
+        } else if (address instanceof Ipv6) {
+            String ipv6 = ((Ipv6) address).getIpv6().getValue();
+            tab.setIpAddress(IpAddressBinaryBuilder.getDefaultInstance(InetAddresses.forString(ipv6).getAddress()));
+            tab.setPort(new PortNumber(LispMessage.PORT_NUM));
+        } else if (address instanceof Ipv4Binary) {
+            Ipv4AddressBinary ipv6 = ((Ipv4Binary) address).getIpv4Binary();
+            tab.setIpAddress(new IpAddressBinary(ipv6));
+            tab.setPort(new PortNumber(LispMessage.PORT_NUM));
+        } else if (address instanceof Ipv6Binary) {
+            Ipv6AddressBinary ipv6 = ((Ipv6Binary) address).getIpv6Binary();
+            tab.setIpAddress(new IpAddressBinary(ipv6));
+            tab.setPort(new PortNumber(LispMessage.PORT_NUM));
+        } else if (address instanceof KeyValueAddress) {
+            SimpleAddress sa = ((KeyValueAddress) address).getKeyValueAddress().getValue();
+            if (sa.getDistinguishedNameType() != null) {
+                final Iterator<String> it = COLON_SPLITTER.split(sa.getDistinguishedNameType().getValue()).iterator();
+                String ip = it.next();
+                int port = Integer.valueOf(it.next());
+
+                tab.setIpAddress(IpAddressBinaryBuilder.getDefaultInstance(InetAddresses.forString(ip).getAddress()));
+                tab.setPort(new PortNumber(port));
+            }
+        } else if (address instanceof DistinguishedName) {
+            DistinguishedName dname = (DistinguishedName) address;
+            final Iterator<String> it = COLON_SPLITTER.split(dname.getDistinguishedName().getValue()).iterator();
+            String ip = it.next();
+            int port = Integer.valueOf(it.next());
 
-    public static MapRegister convertMapRegister(org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapRegister mapRegister) {
-        return new MapRegisterBuilder().setAuthenticationData(mapRegister.getAuthenticationData())
-                .setEidToLocatorRecord(mapRegister.getEidToLocatorRecord()).setKeyId(mapRegister.getKeyId()).setNonce(mapRegister.getNonce())
-                .setProxyMapReply(mapRegister.isProxyMapReply()).setWantMapNotify(mapRegister.isWantMapNotify()).build();
+            tab.setIpAddress(IpAddressBinaryBuilder.getDefaultInstance(InetAddresses.forString(ip).getAddress()));
+            tab.setPort(new PortNumber(port));
+        } else if (address instanceof ApplicationData) {
+            ApplicationData appData = (ApplicationData) address;
+            tab.setIpAddress(getIpAddressBinary(appData.getApplicationData().getAddress().getIpAddress()));
+            tab.setPort(new PortNumber(appData.getApplicationData().getLocalPortLow()));
+        }
+        return tab.build();
     }
 
-    public static MapRequest convertMapRequest(org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapRequest mapRequest) {
-        return new MapRequestBuilder().setAuthoritative(mapRequest.isAuthoritative()).setEidRecord(mapRequest.getEidRecord())
-                .setItrRloc(mapRequest.getItrRloc()).setMapDataPresent(mapRequest.isMapDataPresent()).setMapReply(mapRequest.getMapReply())
-                .setNonce(mapRequest.getNonce()).setPitr(mapRequest.isPitr()).setProbe(mapRequest.isProbe()).setSmr(mapRequest.isSmr())
-                .setSmrInvoked(mapRequest.isSmrInvoked()).setSourceEid(mapRequest.getSourceEid()).build();
+    public static InetAddress getAddressByName(String ipAddress) {
+        try {
+            InetAddress address = InetAddress.getByName(ipAddress);
+            return address;
+        } catch (UnknownHostException e) {
+            LOG.debug("Unknown host {}", ipAddress, e);
+            return null;
+        }
     }
 
-    public static IpAddress getIpAddressFromInetAddress(InetAddress inetAddress) {
-        if (inetAddress == null) {
-            inetAddress = InetAddress.getLoopbackAddress();
+    public static List<Mapping> getMapping(AddMapping mapRegisterNotification) {
+        List<Mapping> mappings = new ArrayList<Mapping>();
+        for (int i = 0; i < mapRegisterNotification.getMapRegister().getMappingRecordItem().size(); i++) {
+            MappingRecord record = mapRegisterNotification.getMapRegister().getMappingRecordItem().get(i)
+                    .getMappingRecord();
+            MappingBuilder mb = new MappingBuilder();
+            mb.setEidUri(new EidUri(LispAddressStringifier.getURIString(
+                    record.getEid())));
+            mb.setOrigin(MappingOrigin.Southbound);
+            mb.setSiteId(getSiteId(mapRegisterNotification.getMapRegister()));
+            mb.setMappingRecord(record);
+            mappings.add(mb.build());
         }
-        if (inetAddress instanceof Inet4Address) {
-            return new IpAddress(new Ipv4Address(inetAddress.getHostAddress()));
+        return mappings;
+    }
+
+    public static List<SiteId> getSiteId(MapRegister mapRegister) {
+        if (mapRegister.isXtrSiteIdPresent()) {
+            List<SiteId> siteIds = new ArrayList<SiteId>();
+            SiteId siteId = new SiteId(mapRegister.getSiteId());
+            siteIds.add(siteId);
+            return siteIds;
         } else {
-            return new IpAddress(new Ipv6Address(inetAddress.getHostAddress()));
+            return null;
         }
     }
 
-    public static InetAddress getInetAddressFromIpAddress(IpAddress ipAddress) {
-        InetAddress address = InetAddress.getLoopbackAddress();
-        if (ipAddress == null) {
-            return address;
-        }
-        if (ipAddress.getIpv4Address() != null) {
-            try {
-                address = Inet4Address.getByName(ipAddress.getIpv4Address().getValue());
-            } catch (UnknownHostException e) {
-            }
-        } else if (ipAddress.getIpv6Address() != null) {
-            try {
-                address = Inet4Address.getByName(ipAddress.getIpv6Address().getValue());
-            } catch (UnknownHostException e) {
-            }
+    // We will see if we need to keep this method post full binary-migration, and if yes, will probably move to
+    // LispAddressUtil (and add tests!)
+    private static IpAddressBinary getIpAddressBinary(IpAddress address) {
+        if (address.getIpv4Address() != null) {
+            String ipv4 = address.getIpv4Address().getValue();
+            return IpAddressBinaryBuilder.getDefaultInstance(InetAddresses.forString(ipv4).getAddress());
+        } else if (address.getIpv6Address() != null) {
+            String ipv6 = address.getIpv6Address().getValue();
+            return IpAddressBinaryBuilder.getDefaultInstance(InetAddresses.forString(ipv6).getAddress());
         }
-        return address;
+        return null;
     }
 }