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.DataBroker;
20 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoints.address.endpoints.AddressEndpoint;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoints.address.endpoints.AddressEndpointKey;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.absolute.location.location.type.ExternalLocationCase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.child.endpoints.ChildEndpoint;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.parent.child.endpoints.ParentEndpointChoice;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.parent.child.endpoints.parent.endpoint.choice.ParentContainmentEndpointCase;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.parent.child.endpoints.parent.endpoint.choice.ParentEndpointCase;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.parent.child.endpoints.parent.endpoint.choice.parent.containment.endpoint._case.ParentContainmentEndpoint;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.parent.child.endpoints.parent.endpoint.choice.parent.endpoint._case.ParentEndpoint;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.EndpointGroupId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.policy.rev140421.tenants.tenant.policy.ExternalImplicitGroup;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.policy.configuration.endpoints.AddressEndpointWithLocation;
35 import com.google.common.base.Function;
36 import com.google.common.base.Optional;
37 import com.google.common.util.concurrent.Futures;
38 import com.google.common.util.concurrent.ListenableFuture;
40 public class EndpointUtils {
42 public static AddressEndpointKey createAddressEndpointKey(ChildEndpoint child) {
43 return new AddressEndpointKey(child.getAddress(), child.getAddressType(), child.getContextId(),
44 child.getContextType());
47 public static AddressEndpointKey createAddressEndpointKey(ParentEndpoint parent) {
48 return new AddressEndpointKey(parent.getAddress(), parent.getAddressType(), parent.getContextId(),
49 parent.getContextType());
52 public static @Nonnull List<ParentContainmentEndpoint> getParentContainmentEndpoints(
53 @Nullable ParentEndpointChoice parentEndpointChoice) {
54 if (parentEndpointChoice instanceof ParentContainmentEndpointCase) {
55 ParentContainmentEndpointCase parentCeCase = (ParentContainmentEndpointCase) parentEndpointChoice;
56 List<ParentContainmentEndpoint> parentContainmentEndpoints = parentCeCase.getParentContainmentEndpoint();
57 if (parentContainmentEndpoints != null) {
58 return parentContainmentEndpoints;
61 return Collections.emptyList();
64 public static @Nonnull List<ParentEndpoint> getParentEndpoints(
65 @Nullable ParentEndpointChoice parentEndpointChoice) {
66 if (parentEndpointChoice instanceof ParentEndpointCase) {
67 ParentEndpointCase parentEpCase = (ParentEndpointCase) parentEndpointChoice;
68 List<ParentEndpoint> parentEndpoints = parentEpCase.getParentEndpoint();
69 if (parentEndpoints != null) {
70 return parentEndpoints;
73 return Collections.emptyList();
76 public static boolean isExternalEndpoint(@Nonnull DataBroker dataBroker, @Nonnull AddressEndpoint addrEp) {
77 ReadOnlyTransaction rTx = dataBroker.newReadOnlyTransaction();
78 List<ListenableFuture<Boolean>> results = new ArrayList<>();
79 for (EndpointGroupId epgId : addrEp.getEndpointGroup()) {
80 results.add(Futures.transform(
81 rTx.read(LogicalDatastoreType.CONFIGURATION,
82 IidFactory.externalImplicitGroupIid(addrEp.getTenant(), epgId)),
83 new Function<Optional<ExternalImplicitGroup>, Boolean>() {
86 public Boolean apply(Optional<ExternalImplicitGroup> input) {
87 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();