f4bf7c76da76d71462e51649859c29ae26784bd1
[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.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;
33
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;
38
39 public class EndpointUtils {
40
41     public static AddressEndpointKey createAddressEndpointKey(ChildEndpoint child) {
42         return new AddressEndpointKey(child.getAddress(), child.getAddressType(), child.getContextId(),
43                 child.getContextType());
44     }
45
46     public static AddressEndpointKey createAddressEndpointKey(ParentEndpoint parent) {
47         return new AddressEndpointKey(parent.getAddress(), parent.getAddressType(), parent.getContextId(),
48                 parent.getContextType());
49     }
50
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;
58             }
59         }
60         return Collections.emptyList();
61     }
62
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;
70             }
71         }
72         return Collections.emptyList();
73     }
74
75     public static boolean isExternalEndpoint(@Nonnull ReadTransaction rTx, @Nonnull AddressEndpoint addrEp) {
76         List<ListenableFuture<Boolean>> results = new ArrayList<>();
77         if(addrEp.getEndpointGroup() == null) {
78             return false;
79         }
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>() {
85
86                         @Override
87                         public Boolean apply(Optional<ExternalImplicitGroup> input) {
88                             return input.isPresent();
89                         }
90                     }));
91         }
92         try {
93             List<Boolean> list = Futures.allAsList(results).get();
94             return list.stream().anyMatch(Boolean::booleanValue);
95         } catch (InterruptedException | ExecutionException e) {
96             return false;
97         }
98     }
99
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());
104         }
105         return Optional.absent();
106     }
107 }