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.location.resolver;
11 import java.util.Collection;
12 import java.util.Collections;
13 import java.util.HashMap;
14 import java.util.List;
17 import javax.annotation.Nullable;
19 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
20 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
21 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
22 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
23 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
24 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
25 import org.opendaylight.groupbasedpolicy.util.DataStoreHelper;
26 import org.opendaylight.groupbasedpolicy.util.IidFactory;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoint.locations.AddressEndpointLocation;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoint.locations.AddressEndpointLocationBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoint.locations.AddressEndpointLocationKey;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoint.locations.ContainmentEndpointLocation;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoint.locations.ContainmentEndpointLocationBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoint.locations.ContainmentEndpointLocationKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.AbsoluteLocation;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.relative.location.relative.locations.ExternalLocation;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.relative.location.relative.locations.InternalLocation;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.LocationProviders;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.LocationProvider;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.location.provider.ProviderAddressEndpointLocation;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.location.provider.ProviderAddressEndpointLocationKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.location.provider.ProviderContainmentEndpointLocation;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.location.provider.ProviderContainmentEndpointLocationKey;
42 import org.opendaylight.yangtools.concepts.ListenerRegistration;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
47 public class LocationResolver implements ClusteredDataTreeChangeListener<LocationProvider>, AutoCloseable {
49 private static final Logger LOG = LoggerFactory.getLogger(LocationResolver.class);
50 private Map<AddressEndpointLocationKey, Map<Long, AbsoluteLocation>> realLocations;
51 private DataBroker dataBroker;
52 private ListenerRegistration<LocationResolver> listenerRegistation;
54 public LocationResolver(DataBroker dataBroker) {
55 this.dataBroker = dataBroker;
56 this.realLocations = new HashMap<>();
57 this.listenerRegistation = dataBroker.registerDataTreeChangeListener(
58 new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION,
59 InstanceIdentifier.builder(LocationProviders.class).child(LocationProvider.class).build()),
64 public synchronized void onDataTreeChanged(Collection<DataTreeModification<LocationProvider>> changes) {
65 for (DataTreeModification<LocationProvider> change : changes) {
66 WriteTransaction wtx = dataBroker.newWriteOnlyTransaction();
67 switch (change.getRootNode().getModificationType()) {
69 processRemovedLocationProviderData(change.getRootNode().getDataBefore(), wtx);
70 LOG.debug("Data from location provider {} has been removed",
71 change.getRootNode().getDataBefore().getProvider().getValue());
75 if (change.getRootNode().getDataBefore() != null) {
76 processRemovedLocationProviderData(change.getRootNode().getDataBefore(), wtx);
78 processCreatedLocationProviderData(change.getRootNode().getDataAfter(), wtx);
79 LOG.debug("Data from location provider {} has been created",
80 change.getRootNode().getDataAfter().getProvider().getValue());
83 case SUBTREE_MODIFIED: {
84 processRemovedLocationProviderData(change.getRootNode().getDataBefore(), wtx);
85 processCreatedLocationProviderData(change.getRootNode().getDataAfter(), wtx);
86 LOG.debug("Data from location provider {} has been changed",
87 change.getRootNode().getDataAfter().getProvider().getValue());
91 LOG.debug("Writing endpoint location changes to DS");
92 DataStoreHelper.submitToDs(wtx);
96 private void processRemovedLocationProviderData(LocationProvider provider, WriteTransaction wtx) {
97 for (ProviderAddressEndpointLocation addressEndpointLocation : nullToEmpty(
98 provider.getProviderAddressEndpointLocation())) {
99 AddressEndpointLocationKey epKey = createAddressEndpointLocationKey(addressEndpointLocation.getKey());
101 if (provider.getPriority() == null) {
103 LOG.debug("{} provider doesn't provide priority. Using 0 as priority instead.",
104 provider.getProvider().getValue());
106 priority = provider.getPriority();
108 if (realLocations.get(epKey) != null) {
109 realLocations.get(epKey).remove(priority);
111 AbsoluteLocation newAbsoluteLocation = getBestAbsoluteLocation(epKey);
112 if (newAbsoluteLocation == null) {
113 InstanceIdentifier<AbsoluteLocation> iid = IidFactory.absoluteLocationIid(epKey);
114 wtx.delete(LogicalDatastoreType.OPERATIONAL, iid);
116 AddressEndpointLocationBuilder newEP =
117 new AddressEndpointLocationBuilder().setKey(epKey).setAbsoluteLocation(newAbsoluteLocation);
118 InstanceIdentifier<AddressEndpointLocation> iid = IidFactory.addressEndpointLocationIid(newEP.getKey());
119 wtx.merge(LogicalDatastoreType.OPERATIONAL, iid, newEP.build(), true);
121 if (addressEndpointLocation.getRelativeLocations() != null) {
122 for (InternalLocation location : nullToEmpty(
123 addressEndpointLocation.getRelativeLocations().getInternalLocation())) {
124 InstanceIdentifier<InternalLocation> iid = IidFactory.internalLocationIid(epKey, location.getKey());
125 wtx.delete(LogicalDatastoreType.OPERATIONAL, iid);
127 for (ExternalLocation location : nullToEmpty(
128 addressEndpointLocation.getRelativeLocations().getExternalLocation())) {
129 InstanceIdentifier<ExternalLocation> iid = IidFactory.externalLocationIid(epKey, location.getKey());
130 wtx.delete(LogicalDatastoreType.OPERATIONAL, iid);
133 if (newAbsoluteLocation == null && addressEndpointLocation.getRelativeLocations() == null) {
134 InstanceIdentifier<AddressEndpointLocation> iid = IidFactory.addressEndpointLocationIid(epKey);
135 wtx.delete(LogicalDatastoreType.OPERATIONAL, iid);
138 for (ProviderContainmentEndpointLocation containmentEndpoint : nullToEmpty(
139 provider.getProviderContainmentEndpointLocation())) {
140 ContainmentEndpointLocationKey epKey = createContainmentEndpointLocationKey(containmentEndpoint.getKey());
141 if (containmentEndpoint.getRelativeLocations() != null) {
142 for (InternalLocation location : nullToEmpty(
143 containmentEndpoint.getRelativeLocations().getInternalLocation())) {
144 InstanceIdentifier<InternalLocation> iid = IidFactory.internalLocationIid(epKey, location.getKey());
145 wtx.delete(LogicalDatastoreType.OPERATIONAL, iid);
147 for (ExternalLocation location : nullToEmpty(
148 containmentEndpoint.getRelativeLocations().getExternalLocation())) {
149 InstanceIdentifier<ExternalLocation> iid = IidFactory.externalLocationIid(epKey, location.getKey());
150 wtx.delete(LogicalDatastoreType.OPERATIONAL, iid);
156 private void processCreatedLocationProviderData(LocationProvider provider, WriteTransaction wtx) {
157 for (ProviderAddressEndpointLocation addressEndpointLocation : nullToEmpty(
158 provider.getProviderAddressEndpointLocation())) {
159 AddressEndpointLocationKey epKey = createAddressEndpointLocationKey(addressEndpointLocation.getKey());
160 AddressEndpointLocationBuilder newEP = new AddressEndpointLocationBuilder().setKey(epKey);
161 if (addressEndpointLocation.getAbsoluteLocation() != null) {
162 if (realLocations.get(epKey) == null) {
163 realLocations.put(epKey, new HashMap<>());
166 if (provider.getPriority() == null) {
168 LOG.debug("{} provider doesnt provide priority. Using 0 as priority instead.",
169 provider.getProvider().getValue());
171 priority = provider.getPriority();
173 realLocations.get(epKey).put(priority, addressEndpointLocation.getAbsoluteLocation());
175 AbsoluteLocation bestLocation = getBestAbsoluteLocation(epKey);
176 if (bestLocation != null) {
177 newEP.setAbsoluteLocation(bestLocation);
179 if (addressEndpointLocation.getRelativeLocations() != null) {
180 newEP.setRelativeLocations(addressEndpointLocation.getRelativeLocations());
182 InstanceIdentifier<AddressEndpointLocation> iid = IidFactory.addressEndpointLocationIid(newEP.getKey());
183 wtx.merge(LogicalDatastoreType.OPERATIONAL, iid, newEP.build(), true);
185 for (ProviderContainmentEndpointLocation containmentEndpointLocation : nullToEmpty(
186 provider.getProviderContainmentEndpointLocation())) {
187 if (containmentEndpointLocation.getRelativeLocations() != null) {
188 ContainmentEndpointLocationKey key =
189 createContainmentEndpointLocationKey(containmentEndpointLocation.getKey());
190 ContainmentEndpointLocationBuilder newEP = new ContainmentEndpointLocationBuilder().setKey(key);
191 newEP.setRelativeLocations(containmentEndpointLocation.getRelativeLocations());
192 InstanceIdentifier<ContainmentEndpointLocation> iid =
193 IidFactory.containmentEndpointLocationIid(newEP.getKey());
194 wtx.merge(LogicalDatastoreType.OPERATIONAL, iid, newEP.build(), true);
199 private AbsoluteLocation getBestAbsoluteLocation(AddressEndpointLocationKey epKey) {
200 if (realLocations.get(epKey) == null) {
203 long bestPriority = -1;
204 for (long priority : realLocations.get(epKey).keySet()) {
205 bestPriority = bestPriority > priority ? bestPriority : priority;
207 if (bestPriority == -1) {
210 return (realLocations.get(epKey).get(new Long(bestPriority)));
213 private AddressEndpointLocationKey createAddressEndpointLocationKey(ProviderAddressEndpointLocationKey key) {
214 return new AddressEndpointLocationKey(key.getAddress(), key.getAddressType(), key.getContextId(),
215 key.getContextType());
218 private ContainmentEndpointLocationKey createContainmentEndpointLocationKey(
219 ProviderContainmentEndpointLocationKey key) {
220 return new ContainmentEndpointLocationKey(key.getContextId(), key.getContextType());
223 private <T> List<T> nullToEmpty(@Nullable List<T> list) {
224 return list == null ? Collections.emptyList() : list;
228 public void close() {
229 listenerRegistation.close();