Bug 8228 - metadata service fix made cleaner
[groupbasedpolicy.git] / groupbasedpolicy / src / main / java / org / opendaylight / groupbasedpolicy / renderer / EndpointLocationInfo.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.renderer;
10
11 import java.util.List;
12 import java.util.Set;
13
14 import org.opendaylight.groupbasedpolicy.renderer.util.EndpointLocationUtils;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.EndpointLocations;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoint.locations.AddressEndpointLocation;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoint.locations.AddressEndpointLocationKey;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoint.locations.ContainmentEndpointLocation;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoint.locations.ContainmentEndpointLocationKey;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoints.address.endpoints.AddressEndpointKey;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoints.containment.endpoints.ContainmentEndpointKey;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.AbsoluteLocation;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.absolute.location.LocationType;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.relative.location.RelativeLocations;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.relative.location.relative.locations.InternalLocation;
26 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
27
28 import com.google.common.base.Function;
29 import com.google.common.base.Optional;
30 import com.google.common.collect.FluentIterable;
31 import com.google.common.collect.ImmutableMap;
32 import com.google.common.collect.ImmutableMultimap;
33 import com.google.common.collect.ImmutableSet;
34
35 public class EndpointLocationInfo {
36
37     private final ImmutableMultimap<InstanceIdentifier<?>, AddressEndpointLocation> endpointsByAbsNodeLocation;
38     private final ImmutableMap<AddressEndpointKey, AddressEndpointLocation> addrEpLocByAddrEpKey;
39     private final ImmutableMap<ContainmentEndpointKey, ContainmentEndpointLocation> contEpLocByContEpKey;
40
41     public EndpointLocationInfo(EndpointLocations epLocations) {
42         List<AddressEndpointLocation> addressEndpointLocations = epLocations.getAddressEndpointLocation();
43         endpointsByAbsNodeLocation =
44                 EndpointLocationUtils.resolveEndpointsByAbsoluteNodeLocation(addressEndpointLocations);
45         if (addressEndpointLocations == null) {
46             addrEpLocByAddrEpKey = ImmutableMap.of();
47         } else {
48             com.google.common.collect.ImmutableMap.Builder<AddressEndpointKey, AddressEndpointLocation> adrEpLocByAdrEpKeyBuilder =
49                     ImmutableMap.builder();
50             for (AddressEndpointLocation adrEpLoc : addressEndpointLocations) {
51                 adrEpLocByAdrEpKeyBuilder.put(toAdrEpKey(adrEpLoc.getKey()), adrEpLoc);
52             }
53             addrEpLocByAddrEpKey = adrEpLocByAdrEpKeyBuilder.build();
54         }
55         List<ContainmentEndpointLocation> containmentEndpointLocations = epLocations.getContainmentEndpointLocation();
56         if (containmentEndpointLocations == null) {
57             contEpLocByContEpKey = ImmutableMap.of();
58         } else {
59             com.google.common.collect.ImmutableMap.Builder<ContainmentEndpointKey, ContainmentEndpointLocation> contEpLocBycontEpKeyBuilder =
60                     ImmutableMap.builder();
61             for (ContainmentEndpointLocation contEpLoc : containmentEndpointLocations) {
62                 contEpLocBycontEpKeyBuilder.put(toContEpKey(contEpLoc.getKey()), contEpLoc);
63             }
64             contEpLocByContEpKey = contEpLocBycontEpKeyBuilder.build();
65         }
66     }
67
68     public Optional<AddressEndpointLocation> getAdressEndpointLocation(AddressEndpointKey epKey) {
69         return Optional.fromNullable(addrEpLocByAddrEpKey.get(epKey));
70     }
71
72     public Optional<ContainmentEndpointLocation> getContainmentEndpointLocation(ContainmentEndpointKey contEpKey) {
73         return Optional.fromNullable(contEpLocByContEpKey.get(contEpKey));
74     }
75
76     private AddressEndpointKey toAdrEpKey(AddressEndpointLocationKey adrEpLocKey) {
77         return new AddressEndpointKey(adrEpLocKey.getAddress(), adrEpLocKey.getAddressType(),
78                 adrEpLocKey.getContextId(), adrEpLocKey.getContextType());
79     }
80
81     private ContainmentEndpointKey toContEpKey(ContainmentEndpointLocationKey contEpLocKey) {
82         return new ContainmentEndpointKey(contEpLocKey.getContextId(), contEpLocKey.getContextType());
83     }
84
85     public Set<InstanceIdentifier<?>> getAllAbsoluteNodeLocations() {
86         return endpointsByAbsNodeLocation.keySet();
87     }
88
89     public ImmutableSet<AddressEndpointKey> getAddressEpsWithAbsoluteNodeLocation(
90             InstanceIdentifier<?> realNodeLocation) {
91         return FluentIterable.from(endpointsByAbsNodeLocation.get(realNodeLocation))
92             .transform(new Function<AddressEndpointLocation, AddressEndpointKey>() {
93
94                 @Override
95                 public AddressEndpointKey apply(AddressEndpointLocation epLoc) {
96                     return new AddressEndpointKey(epLoc.getAddress(), epLoc.getAddressType(), epLoc.getContextId(),
97                             epLoc.getContextType());
98                 }
99             })
100             .toSet();
101     }
102
103     public boolean hasAbsoluteLocation(AddressEndpointKey adrEpKey) {
104         AddressEndpointLocation adrEpLoc = addrEpLocByAddrEpKey.get(adrEpKey);
105         if (adrEpLoc == null) {
106             return false;
107         }
108         AbsoluteLocation absLocation = adrEpLoc.getAbsoluteLocation();
109         if (absLocation == null) {
110             return false;
111         }
112         LocationType locationType = absLocation.getLocationType();
113         if (locationType == null) {
114             return false;
115         }
116         return true;
117     }
118
119     public boolean hasRelativeLocation(AddressEndpointKey adrEpKey) {
120         AddressEndpointLocation adrEpLoc = addrEpLocByAddrEpKey.get(adrEpKey);
121         if (adrEpLoc == null) {
122             return false;
123         }
124         RelativeLocations relLocations = adrEpLoc.getRelativeLocations();
125         if (relLocations == null) {
126             return false;
127         }
128         if (relLocations.getInternalLocation() == null && relLocations.getExternalLocation() == null) {
129             return false;
130         }
131         return true;
132     }
133
134     public boolean hasRelativeLocation(ContainmentEndpointKey contEpKey) {
135         ContainmentEndpointLocation contEpLoc = contEpLocByContEpKey.get(contEpKey);
136         if (contEpLoc == null) {
137             return false;
138         }
139         RelativeLocations relLocations = contEpLoc.getRelativeLocations();
140         if (relLocations == null) {
141             return false;
142         }
143         List<InternalLocation> locs = relLocations.getInternalLocation();
144         if (locs == null) {
145             return false;
146         }
147         return true;
148     }
149
150     @Override
151     public int hashCode() {
152         final int prime = 31;
153         int result = 1;
154         result = prime * result + ((addrEpLocByAddrEpKey == null) ? 0 : addrEpLocByAddrEpKey.hashCode());
155         result = prime * result + ((contEpLocByContEpKey == null) ? 0 : contEpLocByContEpKey.hashCode());
156         return result;
157     }
158
159     @Override
160     public boolean equals(Object obj) {
161         if (this == obj)
162             return true;
163         if (obj == null)
164             return false;
165         if (getClass() != obj.getClass())
166             return false;
167         EndpointLocationInfo other = (EndpointLocationInfo) obj;
168         if (addrEpLocByAddrEpKey == null) {
169             if (other.addrEpLocByAddrEpKey != null)
170                 return false;
171         } else if (!DtoEquivalenceUtils.equalsAddrEpLocByAddrEpKey(addrEpLocByAddrEpKey, other.addrEpLocByAddrEpKey))
172             return false;
173         if (contEpLocByContEpKey == null) {
174             if (other.contEpLocByContEpKey != null)
175                 return false;
176         } else if (!DtoEquivalenceUtils.equalsContEpLocByContEpKey(contEpLocByContEpKey, other.contEpLocByContEpKey))
177             return false;
178         return true;
179     }
180
181     @Override
182     public String toString() {
183         return "EndpointLocationInfo [adrEpLocByAdrEpKey=" + addrEpLocByAddrEpKey + ", contEpLocBycontEpKey="
184                 + contEpLocByContEpKey + "]";
185     }
186
187 }