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 static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
14 import static org.junit.Assert.assertTrue;
16 import java.util.Collection;
17 import java.util.Collections;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
22 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
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.test.CustomDataBrokerTest;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.EndpointLocations;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoint.locations.AddressEndpointLocationKey;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.endpoint.locations.ContainmentEndpointLocationKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.AbsoluteLocation;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.AbsoluteLocationBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.absolute.location.absolute.location.location.type.InternalLocationCaseBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.relative.location.RelativeLocations;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.relative.location.RelativeLocationsBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.base_endpoint.rev160427.has.relative.location.relative.locations.InternalLocationBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.common.rev140421.ContextId;
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.ProviderName;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.LocationProvider;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.LocationProviderBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.LocationProviderKey;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.location.provider.ProviderAddressEndpointLocation;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.location.provider.ProviderAddressEndpointLocationBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.location.provider.ProviderAddressEndpointLocationKey;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint_location_provider.rev160419.location.providers.location.provider.ProviderContainmentEndpointLocationBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.forwarding.rev160427.AddressType;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.forwarding.rev160427.ContextType;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
54 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
56 import com.google.common.base.Optional;
57 import com.google.common.collect.ImmutableList;
59 public class LocationResolverTest extends CustomDataBrokerTest {
61 private final String PROVIDER_NAME = "location-provider";
62 private final String ADDRESS = "192.168.50.20/24";
63 private final String NODE_1 = "node1";
64 private final String NODE_2 = "node2";
65 private final String NODE_CONNNECTOR = "connector";
66 private final ContextId contextId = new ContextId("context");
68 private InstanceIdentifier<Node> nodeIid1 =
69 InstanceIdentifier.builder(Nodes.class).child(Node.class, new NodeKey(new NodeId(NODE_1))).build();
70 private InstanceIdentifier<Node> nodeIid2 =
71 InstanceIdentifier.builder(Nodes.class).child(Node.class, new NodeKey(new NodeId(NODE_2))).build();
72 private InstanceIdentifier<NodeConnector> connectorIid = InstanceIdentifier.builder(Nodes.class)
73 .child(Node.class, new NodeKey(new NodeId(NODE_1)))
74 .child(NodeConnector.class, new NodeConnectorKey(new NodeConnectorId(NODE_CONNNECTOR)))
76 private DataBroker dataBroker;
77 private LocationResolver resolver;
81 dataBroker = getDataBroker();
82 resolver = new LocationResolver(dataBroker);
86 public Collection<Class<?>> getClassesFromModules() {
87 return ImmutableList.<Class<?>>of(LocationProvider.class, Nodes.class, EndpointLocations.class);
91 public void testOnDataTreeChanged_write() throws Exception {
92 AbsoluteLocation absoluteLocation =
93 new AbsoluteLocationBuilder().setLocationType(new InternalLocationCaseBuilder()
94 .setInternalNode(nodeIid1).setInternalNodeConnector(connectorIid).build()).build();
95 RelativeLocations relativeLocations = new RelativeLocationsBuilder()
96 .setInternalLocation(Collections.singletonList(new InternalLocationBuilder().setInternalNode(nodeIid1)
97 .setInternalNodeConnector(connectorIid)
100 LocationProvider provider = new LocationProviderBuilder().setProvider(new ProviderName(PROVIDER_NAME))
101 .setProviderAddressEndpointLocation(
102 Collections.singletonList(new ProviderAddressEndpointLocationBuilder()
103 .setKey(new ProviderAddressEndpointLocationKey(ADDRESS, AddressType.class, contextId,
105 .setAbsoluteLocation(absoluteLocation)
106 .setRelativeLocations(relativeLocations)
108 .setProviderContainmentEndpointLocation(
109 Collections.singletonList(new ProviderContainmentEndpointLocationBuilder().setContextId(contextId)
110 .setContextType(ContextType.class)
111 .setRelativeLocations(relativeLocations)
114 InstanceIdentifier<LocationProvider> iid = InstanceIdentifier.builder(LocationProviders.class)
115 .child(LocationProvider.class, provider.getKey())
117 WriteTransaction wtx = dataBroker.newWriteOnlyTransaction();
118 wtx.put(LogicalDatastoreType.CONFIGURATION, iid, provider);
121 ReadOnlyTransaction rtx = dataBroker.newReadOnlyTransaction();
122 InstanceIdentifier<EndpointLocations> readIid = InstanceIdentifier.builder(EndpointLocations.class).build();
123 Optional<EndpointLocations> read = rtx.read(LogicalDatastoreType.OPERATIONAL, readIid).get();
124 assertTrue(read.isPresent());
125 EndpointLocations readLocations = read.get();
126 assertNotNull(readLocations.getAddressEndpointLocation());
127 assertEquals(1, readLocations.getAddressEndpointLocation().size());
128 assertEquals(new AddressEndpointLocationKey(ADDRESS, AddressType.class, contextId, ContextType.class),
129 readLocations.getAddressEndpointLocation().get(0).getKey());
130 assertEquals(absoluteLocation, readLocations.getAddressEndpointLocation().get(0).getAbsoluteLocation());
131 assertNotNull(readLocations.getContainmentEndpointLocation());
132 assertEquals(1, readLocations.getContainmentEndpointLocation().size());
133 assertEquals(new ContainmentEndpointLocationKey(contextId, ContextType.class),
134 readLocations.getContainmentEndpointLocation().get(0).getKey());
135 assertEquals(relativeLocations, readLocations.getContainmentEndpointLocation().get(0).getRelativeLocations());
139 public void testOnDataTreeChanged_overWrite() throws Exception {
140 testOnDataTreeChanged_write();
141 AbsoluteLocation absoluteLocation =
142 new AbsoluteLocationBuilder().setLocationType(new InternalLocationCaseBuilder()
143 .setInternalNode(nodeIid2).setInternalNodeConnector(connectorIid).build()).build();
144 RelativeLocations relativeLocations = new RelativeLocationsBuilder()
145 .setInternalLocation(Collections.singletonList(new InternalLocationBuilder().setInternalNode(nodeIid2)
146 .setInternalNodeConnector(connectorIid)
149 LocationProvider provider = new LocationProviderBuilder().setProvider(new ProviderName(PROVIDER_NAME))
150 .setProviderAddressEndpointLocation(
151 Collections.singletonList(new ProviderAddressEndpointLocationBuilder()
152 .setKey(new ProviderAddressEndpointLocationKey(ADDRESS, AddressType.class, contextId,
154 .setAbsoluteLocation(absoluteLocation)
155 .setRelativeLocations(relativeLocations)
157 .setProviderContainmentEndpointLocation(
158 Collections.singletonList(new ProviderContainmentEndpointLocationBuilder().setContextId(contextId)
159 .setContextType(ContextType.class)
160 .setRelativeLocations(relativeLocations)
163 InstanceIdentifier<LocationProvider> iid = InstanceIdentifier.builder(LocationProviders.class)
164 .child(LocationProvider.class, provider.getKey())
166 WriteTransaction wtx = dataBroker.newWriteOnlyTransaction();
167 wtx.put(LogicalDatastoreType.CONFIGURATION, iid, provider);
170 ReadOnlyTransaction rtx = dataBroker.newReadOnlyTransaction();
171 InstanceIdentifier<EndpointLocations> readIid = InstanceIdentifier.builder(EndpointLocations.class).build();
172 Optional<EndpointLocations> read = rtx.read(LogicalDatastoreType.OPERATIONAL, readIid).get();
173 assertTrue(read.isPresent());
174 EndpointLocations readLocations = read.get();
175 assertNotNull(readLocations.getAddressEndpointLocation());
176 assertEquals(1, readLocations.getAddressEndpointLocation().size());
177 assertEquals(new AddressEndpointLocationKey(ADDRESS, AddressType.class, contextId, ContextType.class),
178 readLocations.getAddressEndpointLocation().get(0).getKey());
179 assertEquals(absoluteLocation, readLocations.getAddressEndpointLocation().get(0).getAbsoluteLocation());
180 assertNotNull(readLocations.getContainmentEndpointLocation());
181 assertEquals(1, readLocations.getContainmentEndpointLocation().size());
182 assertEquals(new ContainmentEndpointLocationKey(contextId, ContextType.class),
183 readLocations.getContainmentEndpointLocation().get(0).getKey());
184 assertEquals(relativeLocations, readLocations.getContainmentEndpointLocation().get(0).getRelativeLocations());
188 public void testOnDataTreeChanged_delete() throws Exception {
189 testOnDataTreeChanged_write();
190 InstanceIdentifier<LocationProvider> iid = InstanceIdentifier.builder(LocationProviders.class)
191 .child(LocationProvider.class, new LocationProviderKey(new ProviderName(PROVIDER_NAME)))
193 WriteTransaction wtx = dataBroker.newWriteOnlyTransaction();
194 wtx.delete(LogicalDatastoreType.CONFIGURATION, iid);
197 ReadOnlyTransaction rtx = dataBroker.newReadOnlyTransaction();
198 InstanceIdentifier<EndpointLocations> readIid = InstanceIdentifier.builder(EndpointLocations.class).build();
199 Optional<EndpointLocations> read = rtx.read(LogicalDatastoreType.OPERATIONAL, readIid).get();
200 assertTrue(read.isPresent());
201 EndpointLocations readLocations = read.get();
202 assertEquals(1, readLocations.getAddressEndpointLocation().size());
203 assertNull(readLocations.getAddressEndpointLocation().get(0).getAbsoluteLocation());
204 assertTrue(readLocations.getAddressEndpointLocation()
206 .getRelativeLocations()
207 .getInternalLocation()
209 assertNull(readLocations.getAddressEndpointLocation().get(0).getRelativeLocations().getExternalLocation());
210 assertEquals(1, readLocations.getContainmentEndpointLocation().size());
211 assertTrue(readLocations.getAddressEndpointLocation()
213 .getRelativeLocations()
214 .getInternalLocation()
216 assertNull(readLocations.getAddressEndpointLocation().get(0).getRelativeLocations().getExternalLocation());
220 public void testOnDataTreeChanged_modify() throws Exception {
221 testOnDataTreeChanged_write();
222 AbsoluteLocation absoluteLocation =
223 new AbsoluteLocationBuilder().setLocationType(new InternalLocationCaseBuilder()
224 .setInternalNode(nodeIid2).setInternalNodeConnector(connectorIid).build()).build();
225 InstanceIdentifier<AbsoluteLocation> iid = InstanceIdentifier.builder(LocationProviders.class)
226 .child(LocationProvider.class, new LocationProviderKey(new ProviderName(PROVIDER_NAME)))
227 .child(ProviderAddressEndpointLocation.class,
228 new ProviderAddressEndpointLocationKey(ADDRESS, AddressType.class, contextId, ContextType.class))
229 .child(AbsoluteLocation.class)
231 WriteTransaction wtx = dataBroker.newWriteOnlyTransaction();
232 wtx.put(LogicalDatastoreType.CONFIGURATION, iid, absoluteLocation);
235 ReadOnlyTransaction rtx = dataBroker.newReadOnlyTransaction();
236 InstanceIdentifier<EndpointLocations> readIid = InstanceIdentifier.builder(EndpointLocations.class).build();
237 Optional<EndpointLocations> read = rtx.read(LogicalDatastoreType.OPERATIONAL, readIid).get();
238 assertTrue(read.isPresent());
239 EndpointLocations readLocations = read.get();
240 assertNotNull(readLocations.getAddressEndpointLocation());
241 assertEquals(1, readLocations.getAddressEndpointLocation().size());
242 assertEquals(new AddressEndpointLocationKey(ADDRESS, AddressType.class, contextId, ContextType.class),
243 readLocations.getAddressEndpointLocation().get(0).getKey());
244 assertEquals(absoluteLocation, readLocations.getAddressEndpointLocation().get(0).getAbsoluteLocation());