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.renderer;
11 import java.util.List;
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;
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;
35 public class EndpointLocationInfo {
37 private final ImmutableMultimap<InstanceIdentifier<?>, AddressEndpointLocation> endpointsByAbsNodeLocation;
38 private final ImmutableMap<AddressEndpointKey, AddressEndpointLocation> addrEpLocByAddrEpKey;
39 private final ImmutableMap<ContainmentEndpointKey, ContainmentEndpointLocation> contEpLocByContEpKey;
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();
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);
53 addrEpLocByAddrEpKey = adrEpLocByAdrEpKeyBuilder.build();
55 List<ContainmentEndpointLocation> containmentEndpointLocations = epLocations.getContainmentEndpointLocation();
56 if (containmentEndpointLocations == null) {
57 contEpLocByContEpKey = ImmutableMap.of();
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);
64 contEpLocByContEpKey = contEpLocBycontEpKeyBuilder.build();
68 public Optional<AddressEndpointLocation> getAdressEndpointLocation(AddressEndpointKey epKey) {
69 return Optional.fromNullable(addrEpLocByAddrEpKey.get(epKey));
72 public Optional<ContainmentEndpointLocation> getContainmentEndpointLocation(ContainmentEndpointKey contEpKey) {
73 return Optional.fromNullable(contEpLocByContEpKey.get(contEpKey));
76 private AddressEndpointKey toAdrEpKey(AddressEndpointLocationKey adrEpLocKey) {
77 return new AddressEndpointKey(adrEpLocKey.getAddress(), adrEpLocKey.getAddressType(),
78 adrEpLocKey.getContextId(), adrEpLocKey.getContextType());
81 private ContainmentEndpointKey toContEpKey(ContainmentEndpointLocationKey contEpLocKey) {
82 return new ContainmentEndpointKey(contEpLocKey.getContextId(), contEpLocKey.getContextType());
85 public Set<InstanceIdentifier<?>> getAllAbsoluteNodeLocations() {
86 return endpointsByAbsNodeLocation.keySet();
89 public ImmutableSet<AddressEndpointKey> getAddressEpsWithAbsoluteNodeLocation(
90 InstanceIdentifier<?> realNodeLocation) {
91 return FluentIterable.from(endpointsByAbsNodeLocation.get(realNodeLocation))
92 .transform(new Function<AddressEndpointLocation, AddressEndpointKey>() {
95 public AddressEndpointKey apply(AddressEndpointLocation epLoc) {
96 return new AddressEndpointKey(epLoc.getAddress(), epLoc.getAddressType(), epLoc.getContextId(),
97 epLoc.getContextType());
103 public boolean hasAbsoluteLocation(AddressEndpointKey adrEpKey) {
104 AddressEndpointLocation adrEpLoc = addrEpLocByAddrEpKey.get(adrEpKey);
105 if (adrEpLoc == null) {
108 AbsoluteLocation absLocation = adrEpLoc.getAbsoluteLocation();
109 if (absLocation == null) {
112 LocationType locationType = absLocation.getLocationType();
113 if (locationType == null) {
119 public boolean hasRelativeLocation(AddressEndpointKey adrEpKey) {
120 AddressEndpointLocation adrEpLoc = addrEpLocByAddrEpKey.get(adrEpKey);
121 if (adrEpLoc == null) {
124 RelativeLocations relLocations = adrEpLoc.getRelativeLocations();
125 if (relLocations == null) {
128 List<InternalLocation> locs = relLocations.getInternalLocation();
135 public boolean hasRelativeLocation(ContainmentEndpointKey contEpKey) {
136 ContainmentEndpointLocation contEpLoc = contEpLocByContEpKey.get(contEpKey);
137 if (contEpLoc == null) {
140 RelativeLocations relLocations = contEpLoc.getRelativeLocations();
141 if (relLocations == null) {
144 List<InternalLocation> locs = relLocations.getInternalLocation();
152 public int hashCode() {
153 final int prime = 31;
155 result = prime * result + ((addrEpLocByAddrEpKey == null) ? 0 : addrEpLocByAddrEpKey.hashCode());
156 result = prime * result + ((contEpLocByContEpKey == null) ? 0 : contEpLocByContEpKey.hashCode());
161 public boolean equals(Object obj) {
166 if (getClass() != obj.getClass())
168 EndpointLocationInfo other = (EndpointLocationInfo) obj;
169 if (addrEpLocByAddrEpKey == null) {
170 if (other.addrEpLocByAddrEpKey != null)
172 } else if (!DtoEquivalenceUtils.equalsAddrEpLocByAddrEpKey(addrEpLocByAddrEpKey, other.addrEpLocByAddrEpKey))
174 if (contEpLocByContEpKey == null) {
175 if (other.contEpLocByContEpKey != null)
177 } else if (!DtoEquivalenceUtils.equalsContEpLocByContEpKey(contEpLocByContEpKey, other.contEpLocByContEpKey))
183 public String toString() {
184 return "EndpointLocationInfo [adrEpLocByAdrEpKey=" + addrEpLocByAddrEpKey + ", contEpLocBycontEpKey="
185 + contEpLocByContEpKey + "]";