2 * Copyright (c) 2016 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
9 package org.opendaylight.groupbasedpolicy.util;
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.List;
14 import java.util.concurrent.ExecutionException;
16 import javax.annotation.Nonnull;
17 import javax.annotation.Nullable;
19 import org.opendaylight.controller.md.sal.binding.api.ReadTransaction;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoints.address.endpoints.AddressEndpoint;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoints.address.endpoints.AddressEndpointKey;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.absolute.location.location.type.ExternalLocationCase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.child.endpoints.ChildEndpoint;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.parent.child.endpoints.ParentEndpointChoice;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.parent.child.endpoints.parent.endpoint.choice.ParentContainmentEndpointCase;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.parent.child.endpoints.parent.endpoint.choice.ParentEndpointCase;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.parent.child.endpoints.parent.endpoint.choice.parent.containment.endpoint._case.ParentContainmentEndpoint;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.parent.child.endpoints.parent.endpoint.choice.parent.endpoint._case.ParentEndpoint;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.ExternalImplicitGroup;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.policy.configuration.endpoints.AddressEndpointWithLocation;
34 import com.google.common.base.Function;
35 import com.google.common.base.Optional;
36 import com.google.common.util.concurrent.Futures;
37 import com.google.common.util.concurrent.ListenableFuture;
39 public class EndpointUtils {
41 public static AddressEndpointKey createAddressEndpointKey(ChildEndpoint child) {
42 return new AddressEndpointKey(child.getAddress(), child.getAddressType(), child.getContextId(),
43 child.getContextType());
46 public static AddressEndpointKey createAddressEndpointKey(ParentEndpoint parent) {
47 return new AddressEndpointKey(parent.getAddress(), parent.getAddressType(), parent.getContextId(),
48 parent.getContextType());
51 public static @Nonnull List<ParentContainmentEndpoint> getParentContainmentEndpoints(
52 @Nullable ParentEndpointChoice parentEndpointChoice) {
53 if (parentEndpointChoice instanceof ParentContainmentEndpointCase) {
54 ParentContainmentEndpointCase parentCeCase = (ParentContainmentEndpointCase) parentEndpointChoice;
55 List<ParentContainmentEndpoint> parentContainmentEndpoints = parentCeCase.getParentContainmentEndpoint();
56 if (parentContainmentEndpoints != null) {
57 return parentContainmentEndpoints;
60 return Collections.emptyList();
63 public static @Nonnull List<ParentEndpoint> getParentEndpoints(
64 @Nullable ParentEndpointChoice parentEndpointChoice) {
65 if (parentEndpointChoice != null && parentEndpointChoice instanceof ParentEndpointCase) {
66 ParentEndpointCase parentEpCase = (ParentEndpointCase) parentEndpointChoice;
67 List<ParentEndpoint> parentEndpoints = parentEpCase.getParentEndpoint();
68 if (parentEndpoints != null) {
69 return parentEndpoints;
72 return Collections.emptyList();
75 public static boolean isExternalEndpoint(@Nonnull ReadTransaction rTx, @Nonnull AddressEndpoint addrEp) {
76 List<ListenableFuture<Boolean>> results = new ArrayList<>();
77 if(addrEp.getEndpointGroup() == null) {
80 for (EndpointGroupId epgId : addrEp.getEndpointGroup()) {
81 results.add(Futures.transform(
82 rTx.read(LogicalDatastoreType.CONFIGURATION,
83 IidFactory.externalImplicitGroupIid(addrEp.getTenant(), epgId)),
84 new Function<Optional<ExternalImplicitGroup>, Boolean>() {
87 public Boolean apply(Optional<ExternalImplicitGroup> input) {
88 return input.isPresent();
93 List<Boolean> list = Futures.allAsList(results).get();
94 return list.stream().anyMatch(Boolean::booleanValue);
95 } catch (InterruptedException | ExecutionException e) {
100 public static Optional<ExternalLocationCase> getExternalLocationFrom(AddressEndpointWithLocation input) {
101 if (input.getAbsoluteLocation() != null && input.getAbsoluteLocation().getLocationType() != null
102 && input.getAbsoluteLocation().getLocationType() instanceof ExternalLocationCase) {
103 return Optional.of((ExternalLocationCase) input.getAbsoluteLocation().getLocationType());
105 return Optional.absent();