dabf7e90dddcdb5547ca0f3627fdd050a955f59d
[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         List<InternalLocation> locs = relLocations.getInternalLocation();
129         if (locs == null) {
130             return false;
131         }
132         return true;
133     }
134
135     public boolean hasRelativeLocation(ContainmentEndpointKey contEpKey) {
136         ContainmentEndpointLocation contEpLoc = contEpLocByContEpKey.get(contEpKey);
137         if (contEpLoc == null) {
138             return false;
139         }
140         RelativeLocations relLocations = contEpLoc.getRelativeLocations();
141         if (relLocations == null) {
142             return false;
143         }
144         List<InternalLocation> locs = relLocations.getInternalLocation();
145         if (locs == null) {
146             return false;
147         }
148         return true;
149     }
150
151     @Override
152     public int hashCode() {
153         final int prime = 31;
154         int result = 1;
155         result = prime * result + ((addrEpLocByAddrEpKey == null) ? 0 : addrEpLocByAddrEpKey.hashCode());
156         result = prime * result + ((contEpLocByContEpKey == null) ? 0 : contEpLocByContEpKey.hashCode());
157         return result;
158     }
159
160     @Override
161     public boolean equals(Object obj) {
162         if (this == obj)
163             return true;
164         if (obj == null)
165             return false;
166         if (getClass() != obj.getClass())
167             return false;
168         EndpointLocationInfo other = (EndpointLocationInfo) obj;
169         if (addrEpLocByAddrEpKey == null) {
170             if (other.addrEpLocByAddrEpKey != null)
171                 return false;
172         } else if (!DtoEquivalenceUtils.equalsAddrEpLocByAddrEpKey(addrEpLocByAddrEpKey, other.addrEpLocByAddrEpKey))
173             return false;
174         if (contEpLocByContEpKey == null) {
175             if (other.contEpLocByContEpKey != null)
176                 return false;
177         } else if (!DtoEquivalenceUtils.equalsContEpLocByContEpKey(contEpLocByContEpKey, other.contEpLocByContEpKey))
178             return false;
179         return true;
180     }
181
182     @Override
183     public String toString() {
184         return "EndpointLocationInfo [adrEpLocByAdrEpKey=" + addrEpLocByAddrEpKey + ", contEpLocBycontEpKey="
185                 + contEpLocByContEpKey + "]";
186     }
187
188 }