update deprecated transform and addCallback methods
[groupbasedpolicy.git] / groupbasedpolicy / src / main / java / org / opendaylight / groupbasedpolicy / util / EndpointUtils.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.groupbasedpolicy.util;
10
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.List;
14 import java.util.concurrent.ExecutionException;
15
16 import javax.annotation.Nonnull;
17 import javax.annotation.Nullable;
18
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.renderer.rev151103.renderers.renderer.renderer.policy.configuration.endpoints.AddressEndpointWithLocation;
32
33 import com.google.common.base.Optional;
34 import com.google.common.util.concurrent.Futures;
35 import com.google.common.util.concurrent.ListenableFuture;
36 import com.google.common.util.concurrent.MoreExecutors;
37
38 public class EndpointUtils {
39
40     public static AddressEndpointKey createAddressEndpointKey(ChildEndpoint child) {
41         return new AddressEndpointKey(child.getAddress(), child.getAddressType(), child.getContextId(),
42                 child.getContextType());
43     }
44
45     public static AddressEndpointKey createAddressEndpointKey(ParentEndpoint parent) {
46         return new AddressEndpointKey(parent.getAddress(), parent.getAddressType(), parent.getContextId(),
47                 parent.getContextType());
48     }
49
50     public static @Nonnull List<ParentContainmentEndpoint> getParentContainmentEndpoints(
51             @Nullable ParentEndpointChoice parentEndpointChoice) {
52         if (parentEndpointChoice instanceof ParentContainmentEndpointCase) {
53             ParentContainmentEndpointCase parentCeCase = (ParentContainmentEndpointCase) parentEndpointChoice;
54             List<ParentContainmentEndpoint> parentContainmentEndpoints = parentCeCase.getParentContainmentEndpoint();
55             if (parentContainmentEndpoints != null) {
56                 return parentContainmentEndpoints;
57             }
58         }
59         return Collections.emptyList();
60     }
61
62     public static @Nonnull List<ParentEndpoint> getParentEndpoints(
63             @Nullable ParentEndpointChoice parentEndpointChoice) {
64         if (parentEndpointChoice != null && parentEndpointChoice instanceof ParentEndpointCase) {
65             ParentEndpointCase parentEpCase = (ParentEndpointCase) parentEndpointChoice;
66             List<ParentEndpoint> parentEndpoints = parentEpCase.getParentEndpoint();
67             if (parentEndpoints != null) {
68                 return parentEndpoints;
69             }
70         }
71         return Collections.emptyList();
72     }
73
74     public static boolean isExternalEndpoint(@Nonnull ReadTransaction rTx, @Nonnull AddressEndpoint addrEp) {
75         List<ListenableFuture<Boolean>> results = new ArrayList<>();
76         if(addrEp.getEndpointGroup() == null) {
77             return false;
78         }
79         for (EndpointGroupId epgId : addrEp.getEndpointGroup()) {
80             results.add(Futures.transform( rTx.read(LogicalDatastoreType.CONFIGURATION,
81                         IidFactory.externalImplicitGroupIid(addrEp.getTenant(), epgId)), Optional::isPresent,
82                 MoreExecutors.directExecutor()));
83         }
84         try {
85             List<Boolean> list = Futures.allAsList(results).get();
86             return list.stream().anyMatch(Boolean::booleanValue);
87         } catch (InterruptedException | ExecutionException e) {
88             return false;
89         }
90     }
91
92     public static Optional<ExternalLocationCase> getExternalLocationFrom(AddressEndpointWithLocation input) {
93         if (input.getAbsoluteLocation() != null && input.getAbsoluteLocation().getLocationType() != null
94                 && input.getAbsoluteLocation().getLocationType() instanceof ExternalLocationCase) {
95             return Optional.of((ExternalLocationCase) input.getAbsoluteLocation().getLocationType());
96         }
97         return Optional.absent();
98     }
99 }