1 package org.opendaylight.alto.core.northbound.route.endpointproperty.impl;
3 import com.fasterxml.jackson.databind.JsonNode;
4 import com.fasterxml.jackson.databind.ObjectMapper;
6 import org.opendaylight.alto.core.northbound.api.utils.rfc7285.RFC7285EndpointPropertyMap;
7 import org.opendaylight.alto.core.northbound.api.utils.rfc7285.RFC7285VersionTag;
8 import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
9 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
10 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
11 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
12 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
13 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
14 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
15 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
16 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
17 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
18 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.alto.core.resourcepool.rev150921.context.resource.ContextTag;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.alto.core.types.rev150921.GlobalEndpointProperty;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.alto.core.types.rev150921.PidName;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.alto.core.types.rev150921.SpecificEndpointProperty;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.alto.service.model.endpointproperty.rev151021.AltoModelEndpointpropertyService;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.alto.service.model.endpointproperty.rev151021.QueryInput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.alto.service.model.endpointproperty.rev151021.QueryOutput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.alto.service.model.endpointproperty.rev151021.QueryOutputBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.alto.service.model.endpointproperty.rev151021.ResourceTypeEndpointproperty;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.alto.service.model.endpointproperty.rev151021.alto.request.endpointproperty.request.EndpointpropertyRequest;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.alto.service.model.endpointproperty.rev151021.alto.response.endpointproperty.response.EndpointpropertyResponseBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.alto.service.model.endpointproperty.rev151021.endpointproperty.request.data.EndpointpropertyParams;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.alto.service.model.endpointproperty.rfc7285.rev151021.endpointproperty.filter.data.endpointproperty.filter.EndpointFilter;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.alto.service.model.endpointproperty.rfc7285.rev151021.endpointproperty.filter.data.endpointproperty.filter.EndpointFilterBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.alto.service.model.endpointproperty.rfc7285.rev151021.endpointproperty.filter.data.endpointproperty.filter.PropertyFilter;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.alto.service.model.endpointproperty.rfc7285.rev151021.endpointproperty.filter.data.endpointproperty.filter.PropertyFilterBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.alto.service.model.endpointproperty.rfc7285.rev151021.endpointpropertymap.response.data.EndpointPropertyMapBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.alto.service.model.endpointproperty.rfc7285.rev151021.endpointpropertymap.response.data.endpoint.property.map.EndpointProperty;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.alto.service.model.endpointproperty.rfc7285.rev151021.endpointpropertymap.response.data.endpoint.property.map.EndpointPropertyBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.alto.service.model.endpointproperty.rfc7285.rev151021.endpointpropertymap.response.data.endpoint.property.map.endpoint.property.Properties;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.alto.service.model.endpointproperty.rfc7285.rev151021.endpointpropertymap.response.data.endpoint.property.map.endpoint.property.PropertiesBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.alto.service.model.endpointproperty.rfc7285.rev151021.endpointpropertymap.response.data.endpoint.property.map.endpoint.property.Source;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.alto.service.model.endpointproperty.rfc7285.rev151021.endpointpropertymap.response.data.endpoint.property.map.endpoint.property.SourceBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.alto.service.model.endpointproperty.rfc7285.rev151021.endpointpropertymap.response.data.endpoint.property.map.endpoint.property.properties.PropertyContainerBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.alto.service.model.endpointproperty.rfc7285.rev151021.query.input.request.endpointproperty.request.endpointproperty.params.filter.EndpointpropertyFilterData;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.alto.service.model.endpointproperty.rfc7285.rev151021.query.input.request.endpointproperty.request.endpointproperty.params.filter.endpointproperty.filter.data.endpointproperty.filter.endpoint.filter.address.Ipv4Builder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.alto.service.model.endpointproperty.rfc7285.rev151021.query.input.request.endpointproperty.request.endpointproperty.params.filter.endpointproperty.filter.data.endpointproperty.filter.property.filter.property.InputGlobalPropertyBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.alto.service.model.endpointproperty.rfc7285.rev151021.query.input.request.endpointproperty.request.endpointproperty.params.filter.endpointproperty.filter.data.endpointproperty.filter.property.filter.property.InputResourceSpecificPropertyBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.alto.service.model.endpointproperty.rfc7285.rev151021.query.output.response.endpointproperty.response.endpointproperty.data.EndpointPropertymapDataBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.alto.service.model.endpointproperty.rfc7285.rev151021.query.output.response.endpointproperty.response.endpointproperty.data.endpoint.propertymap.data.endpoint.property.map.endpoint.property.properties.property.container.property.OutputGlobalPropertyBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.alto.service.model.endpointproperty.rfc7285.rev151021.query.output.response.endpointproperty.response.endpointproperty.data.endpoint.propertymap.data.endpoint.property.map.endpoint.property.properties.property.container.property.OutputResourceSpecificPropertyBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.alto.service.model.endpointproperty.rfc7285.rev151021.query.output.response.endpointproperty.response.endpointproperty.data.endpoint.propertymap.data.endpoint.property.map.endpoint.property.properties.property.value.PidNameBuilder;
52 import org.opendaylight.yangtools.concepts.ListenerRegistration;
53 import org.opendaylight.yangtools.yang.binding.DataObject;
54 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
55 import org.opendaylight.yangtools.yang.common.RpcResult;
57 import javax.annotation.Nonnull;
58 import javax.ws.rs.core.Response;
60 import java.io.IOException;
61 import java.util.LinkedList;
62 import java.util.List;
63 import java.util.concurrent.ExecutionException;
64 import java.util.concurrent.Future;
66 import static org.junit.Assert.assertEquals;
67 import static org.mockito.Mockito.spy;
68 import static org.mockito.Mockito.anyObject;
69 import static org.mockito.Mockito.doReturn;
70 import static org.mockito.Mockito.eq;
71 import static org.mockito.Mockito.mock;
72 import static org.mockito.Mockito.when;
74 public class AltoNorthboundRouteEndpointpropertyTest {
75 static String ENDPOINTPROPERTY_FILTER = "{\"properties\" : [ \"my-default-networkmap.pid\","
76 + " \"priv:ietf-example-prop\" ],"
77 + " \"endpoints\" : [ \"ipv4:192.0.2.34\","
78 + " \"ipv4:203.0.113.129\" ]" + "}";
79 static String PATH = "test-model-endpointproperty";
80 static String RESOURCE_SPECIFIC_PROPERTY = "my-default-networkmap.pid";
81 static String GLOBAL_PROPERTY = "priv:ietf-example-prop";
82 static String THE_FIRST_IPv4_ADDRESS = "192.0.2.34";
83 static String THE_SECOND_IPv4_ADDRESS = "203.0.113.129";
84 static String DEFAULT_NETWORKMAP = "my-default-network-map.pid";
85 static String DEFAULT_GLOBAL_PROPERTY_NAME = "priv:ietf-example-prop";
86 static String TARGET_ENDPOINT_PREOPERTIES =
87 "{\"ipv4:192.0.2.34\": {\"my-default-network-map.pid\": \"PID1\","
88 + " \"priv:ietf-example-prop\": \"1\" },"
89 + "\"ipv4:203.0.113.129\": {\"my-default-network-map.pid\": \"PID3\" }}";
90 static String FIELD_ENDPOINT_PROPERTIES = "endpoint-properties";
91 static String DEFAULT_RID = "test-model-endpointproperty";
92 static String DEFAULT_TAG = "75ed013b3cb58f896e839582504f622838ce670f";
94 final AltoNorthboundRouteEndpointproperty anbre= new AltoNorthboundRouteEndpointproperty();
97 public void testPrepareInput() {
99 AltoNorthboundRouteEndpointproperty anbreSpy = spy(anbre);
100 BindingAwareBroker.ProviderContext session = mock(BindingAwareBroker.ProviderContext.class);
101 InstanceIdentifier<ContextTag> ctagIID = InstanceIdentifier.create(ContextTag.class);
102 JsonNode filterNode = EndpointpropertyRouteChecker.checkJsonSyntax(ENDPOINTPROPERTY_FILTER);
103 JsonNode _properties = filterNode.get(AltoNorthboundRouteEndpointproperty.FIELD_PROPERTIES);
104 JsonNode _endpoints = filterNode.get(AltoNorthboundRouteEndpointproperty.FIELD_ENDPOINTS);
108 .getResourceByPath(eq(PATH), (ReadOnlyTransaction) anyObject());
109 when(session.getSALService(DataBroker.class)).thenReturn(new DataBroker() {
110 @Override public ReadOnlyTransaction newReadOnlyTransaction() {
114 @Override public ReadWriteTransaction newReadWriteTransaction() {
118 @Override public WriteTransaction newWriteOnlyTransaction() {
122 @Override public ListenerRegistration<DataChangeListener> registerDataChangeListener(
123 LogicalDatastoreType logicalDatastoreType, InstanceIdentifier<?> instanceIdentifier,
124 DataChangeListener dataChangeListener, DataChangeScope dataChangeScope) {
128 @Override public BindingTransactionChain createTransactionChain(
129 TransactionChainListener transactionChainListener) {
134 public <T extends DataObject, L extends DataTreeChangeListener<T>> ListenerRegistration<L> registerDataTreeChangeListener(
135 @Nonnull DataTreeIdentifier<T> dataTreeIdentifier, @Nonnull L l) {
140 anbreSpy.onSessionInitiated(session);
141 QueryInput input = anbreSpy.prepareInput(PATH, _properties.elements(), _endpoints.elements());
142 EndpointpropertyRequest request = (EndpointpropertyRequest)input.getRequest();
143 EndpointpropertyParams params = request.getEndpointpropertyParams();
144 List<PropertyFilter> listProperty = ((EndpointpropertyFilterData) params.getFilter()).getEndpointpropertyFilter().getPropertyFilter();
145 List<EndpointFilter> listEndpoint = ((EndpointpropertyFilterData) params.getFilter()).getEndpointpropertyFilter().getEndpointFilter();
146 List<PropertyFilter> expectedListProperty = new LinkedList<>();
147 List<EndpointFilter> expectedListEndpoint = new LinkedList<>();
149 expectedListProperty.add(new PropertyFilterBuilder()
150 .setProperty(new InputResourceSpecificPropertyBuilder()
151 .setResourceSpecificProperty(
152 new SpecificEndpointProperty(RESOURCE_SPECIFIC_PROPERTY))
156 expectedListProperty.add(new PropertyFilterBuilder()
157 .setProperty(new InputGlobalPropertyBuilder()
159 new GlobalEndpointProperty(GLOBAL_PROPERTY))
164 expectedListEndpoint.add(new EndpointFilterBuilder()
167 .setIpv4(new Ipv4Address(THE_FIRST_IPv4_ADDRESS))
171 expectedListEndpoint.add(new EndpointFilterBuilder()
174 .setIpv4(new Ipv4Address(THE_SECOND_IPv4_ADDRESS))
179 assertEquals(expectedListProperty, listProperty);
180 assertEquals(expectedListEndpoint, listEndpoint);
184 public void testBuildOutput() throws IOException, ExecutionException, InterruptedException{
185 AltoNorthboundRouteEndpointproperty anbreSpy = spy(anbre);
186 BindingAwareBroker.ProviderContext session = mock(BindingAwareBroker.ProviderContext.class);
187 InstanceIdentifier<ContextTag> ctagIID = InstanceIdentifier.create(ContextTag.class);
189 AltoModelEndpointpropertyService epService = mock(AltoModelEndpointpropertyService.class);
190 Future<RpcResult<QueryOutput>> future = mock(Future.class);
191 RpcResult<QueryOutput> rpcResult =mock(RpcResult.class);
193 LinkedList<EndpointProperty> eppist = new LinkedList<>();
195 SourceBuilder sourceBuilder = new SourceBuilder();
196 Source THE_FIRST_SOURCE = sourceBuilder.setAddress(
198 .setIpv4(new Ipv4Address(THE_FIRST_IPv4_ADDRESS))
201 Source THE_SECOND_SOURCE = sourceBuilder.setAddress(
203 .setIpv4(new Ipv4Address(THE_SECOND_IPv4_ADDRESS))
207 EndpointPropertyBuilder epBuilder = new EndpointPropertyBuilder();
208 List<Properties> THE_FIRST_LIST_PROPERTIES = new LinkedList<>();
209 List<Properties> THE_SECOND_LIST_PROPERTIES = new LinkedList<>();
211 PropertiesBuilder propertiesBuilder = new PropertiesBuilder();
212 PropertyContainerBuilder pcBuilder = new PropertyContainerBuilder();
214 PidName pidName = new PidName("PID1");
215 PidNameBuilder pidNameBuilder = new PidNameBuilder();
216 pidNameBuilder.setValue(pidName);
217 pcBuilder.setProperty(
218 new OutputResourceSpecificPropertyBuilder()
219 .setResourceSpecificProperty(new SpecificEndpointProperty(DEFAULT_NETWORKMAP)).build());
220 propertiesBuilder.setPropertyContainer(pcBuilder.build()).setPropertyValue(pidNameBuilder.build());
221 THE_FIRST_LIST_PROPERTIES.add(propertiesBuilder.build());
223 pcBuilder.setProperty(
224 new OutputGlobalPropertyBuilder()
225 .setGlobalProperty(new GlobalEndpointProperty(DEFAULT_GLOBAL_PROPERTY_NAME))
228 PidName fackGlobalProperty = new PidName("1");
229 pidNameBuilder.setValue(fackGlobalProperty);
230 propertiesBuilder.setPropertyContainer(pcBuilder.build()).setPropertyValue(pidNameBuilder.build());
231 THE_FIRST_LIST_PROPERTIES.add(propertiesBuilder.build());
233 pidName = new PidName("PID3");
234 pidNameBuilder.setValue(pidName);
235 pcBuilder.setProperty(
236 new OutputResourceSpecificPropertyBuilder().
237 setResourceSpecificProperty(new SpecificEndpointProperty(DEFAULT_NETWORKMAP)).build());
238 propertiesBuilder.setPropertyContainer(pcBuilder.build()).setPropertyValue(pidNameBuilder.build());
239 THE_SECOND_LIST_PROPERTIES.add(propertiesBuilder.build());
243 .setSource(THE_FIRST_SOURCE)
244 .setProperties(THE_FIRST_LIST_PROPERTIES)
248 .setSource(THE_SECOND_SOURCE)
249 .setProperties(THE_SECOND_LIST_PROPERTIES).build());
251 EndpointPropertyMapBuilder endpointPropertyMapBuilder = new EndpointPropertyMapBuilder();
252 endpointPropertyMapBuilder.setEndpointProperty(eppist);
254 EndpointPropertymapDataBuilder endpointPropertymapDataBuilder = new EndpointPropertymapDataBuilder();
255 endpointPropertymapDataBuilder.setEndpointPropertyMap(endpointPropertyMapBuilder.build());
257 EndpointpropertyResponseBuilder endpointpropertyResponseBuilder = new EndpointpropertyResponseBuilder();
258 endpointpropertyResponseBuilder.setEndpointpropertyData(endpointPropertymapDataBuilder.build());
260 QueryOutputBuilder queryOutputBuilder = new QueryOutputBuilder();
261 queryOutputBuilder.setType(ResourceTypeEndpointproperty.class).setResponse(endpointpropertyResponseBuilder.build());
262 when(rpcResult.getResult()).thenReturn(queryOutputBuilder.build());
263 when(future.get()).thenReturn(rpcResult);
264 when(epService.query((QueryInput) anyObject())).thenReturn(future);
265 when(session.getRpcService(AltoModelEndpointpropertyService.class)).thenReturn(epService);
266 when(session.getSALService(DataBroker.class)).thenReturn(new DataBroker() {
267 @Override public ReadOnlyTransaction newReadOnlyTransaction() {
271 @Override public ReadWriteTransaction newReadWriteTransaction() {
275 @Override public WriteTransaction newWriteOnlyTransaction() {
279 @Override public ListenerRegistration<DataChangeListener> registerDataChangeListener(
280 LogicalDatastoreType logicalDatastoreType, InstanceIdentifier<?> instanceIdentifier,
281 DataChangeListener dataChangeListener, DataChangeScope dataChangeScope) {
285 @Override public BindingTransactionChain createTransactionChain(
286 TransactionChainListener transactionChainListener) {
291 public <T extends DataObject, L extends DataTreeChangeListener<T>> ListenerRegistration<L> registerDataTreeChangeListener(
292 @Nonnull DataTreeIdentifier<T> dataTreeIdentifier, @Nonnull L l) {
296 doReturn(ctagIID).when(anbreSpy).getResourceByPath(eq(PATH), (ReadOnlyTransaction) anyObject());
297 RFC7285EndpointPropertyMap.Meta meta = new RFC7285EndpointPropertyMap.Meta();
298 RFC7285VersionTag vtag = new RFC7285VersionTag();
299 vtag.rid = DEFAULT_RID;
300 vtag.tag = DEFAULT_TAG;
301 meta.netmap_tags = new LinkedList<>();
302 meta.netmap_tags.add(vtag);
303 doReturn(meta).when(anbreSpy).buildMeta((InstanceIdentifier<?>) anyObject());
305 anbreSpy.onSessionInitiated(session);
306 Response response = anbreSpy.getEndpointProperty(PATH, ENDPOINTPROPERTY_FILTER);
307 String stringResponse = response.getEntity().toString();
308 ObjectMapper mapper = new ObjectMapper();
309 JsonNode responseNode = mapper.readTree(stringResponse);
310 JsonNode endpointProperties = responseNode.get(FIELD_ENDPOINT_PROPERTIES);
311 JsonNode expectedEndpointProperties = mapper.readTree(TARGET_ENDPOINT_PREOPERTIES);
313 assertEquals(expectedEndpointProperties, endpointProperties);