Support for metadata in HA
[groupbasedpolicy.git] / groupbasedpolicy / src / main / java / org / opendaylight / groupbasedpolicy / renderer / util / EndpointLocationUtils.java
index 89478204908805d54a41a344f88601d598153916..ceb82694a06347a784d5c5ca51b2bba06e4443e8 100644 (file)
@@ -9,6 +9,7 @@
 package org.opendaylight.groupbasedpolicy.renderer.util;
 
 import java.util.List;
+import java.util.stream.Collectors;
 
 import javax.annotation.Nullable;
 
@@ -17,6 +18,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpo
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.absolute.location.LocationType;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.absolute.location.location.type.ExternalLocationCase;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.absolute.location.location.type.InternalLocationCase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.relative.location.RelativeLocations;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 
 import com.google.common.base.Optional;
@@ -28,7 +30,11 @@ public class EndpointLocationUtils {
     // hiding default public constructor
     private EndpointLocationUtils() {}
 
-    public static ImmutableMultimap<InstanceIdentifier<?>, AddressEndpointLocation> resolveEndpointsByAbsoluteNodeLocation(
+    /**
+     * Resolves address endpoint locations  by nodes. Address endpoint should have either an absolute location or a
+     * relative location which reflects multihome endpoints.
+     */
+    public static ImmutableMultimap<InstanceIdentifier<?>, AddressEndpointLocation> resolveEndpointsByExternalNodeLocation(
             @Nullable List<AddressEndpointLocation> addressEndpointLocations) {
         if (addressEndpointLocations == null) {
             return ImmutableMultimap.of();
@@ -38,11 +44,19 @@ public class EndpointLocationUtils {
             Optional<InstanceIdentifier<?>> potentialAbsIntNodeLocation = resolveAbsoluteInternalNodeLocation(epLoc);
             if (potentialAbsIntNodeLocation.isPresent()) {
                 resultBuilder.put(potentialAbsIntNodeLocation.get(), epLoc);
-            } else {
-                Optional<InstanceIdentifier<?>> potentialAbsExtNodeMpLocation =
-                        resolveAbsoluteExternalNodeMountPointLocation(epLoc);
-                if (potentialAbsExtNodeMpLocation.isPresent()) {
-                    resultBuilder.put(potentialAbsExtNodeMpLocation.get(), epLoc);
+                continue;
+            }
+            Optional<InstanceIdentifier<?>> potentialAbsExtNodeMpLocation =
+                    resolveAbsoluteExternalNodeMountPointLocation(epLoc);
+            if (potentialAbsExtNodeMpLocation.isPresent()) {
+                resultBuilder.put(potentialAbsExtNodeMpLocation.get(), epLoc);
+                continue;
+            }
+            Optional<List<InstanceIdentifier<?>>> potentialRelExtNodeMpLocation =
+                    resolveRelativeExternalNodeMountPointLocation(epLoc);
+            if (potentialRelExtNodeMpLocation.isPresent()) {
+                for (InstanceIdentifier<?> iid : potentialRelExtNodeMpLocation.get()) {
+                    resultBuilder.put(iid, epLoc);
                 }
             }
         }
@@ -78,6 +92,19 @@ public class EndpointLocationUtils {
         return Optional.absent();
     }
 
+    public static Optional<List<InstanceIdentifier<?>>> resolveRelativeExternalNodeMountPointLocation(
+            AddressEndpointLocation epLoc) {
+        RelativeLocations relativeLocations = epLoc.getRelativeLocations();
+        if (relativeLocations != null) {
+            List<InstanceIdentifier<?>> mountPoints = relativeLocations.getExternalLocation()
+                .stream()
+                .map(externalLocation -> externalLocation.getExternalNodeMountPoint())
+                .collect(Collectors.toList());
+            return (mountPoints.isEmpty()) ? Optional.absent() : Optional.of(mountPoints);
+        }
+        return Optional.absent();
+    }
+
     public static Optional<InstanceIdentifier<?>> resolveAbsoluteNodeLocation(AddressEndpointLocation epLoc) {
         if (epLoc.getAbsoluteLocation() == null) {
             return Optional.absent();