Add the northbound exception handler
[alto.git] / alto-core / standard-northbound-routes / endpointproperty / impl / src / test / java / org / opendaylight / alto / core / northbound / route / endpointproperty / impl / AltoNorthboundRouteEndpointpropertyTest.java
1 package org.opendaylight.alto.core.northbound.route.endpointproperty.impl;
2
3 import com.fasterxml.jackson.databind.JsonNode;
4 import com.fasterxml.jackson.databind.ObjectMapper;
5 import org.junit.Test;
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;
56
57 import javax.annotation.Nonnull;
58 import javax.ws.rs.core.Response;
59
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;
65
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;
73
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";
93
94     final AltoNorthboundRouteEndpointproperty anbre= new AltoNorthboundRouteEndpointproperty();
95
96     @Test
97     public void testPrepareInput() {
98
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);
105
106         doReturn(ctagIID)
107             .when(anbreSpy)
108             .getResourceByPath(eq(PATH), (ReadOnlyTransaction) anyObject());
109         when(session.getSALService(DataBroker.class)).thenReturn(new DataBroker() {
110             @Override public ReadOnlyTransaction newReadOnlyTransaction() {
111                 return null;
112             }
113
114             @Override public ReadWriteTransaction newReadWriteTransaction() {
115                 return null;
116             }
117
118             @Override public WriteTransaction newWriteOnlyTransaction() {
119                 return null;
120             }
121
122             @Override public ListenerRegistration<DataChangeListener> registerDataChangeListener(
123                 LogicalDatastoreType logicalDatastoreType, InstanceIdentifier<?> instanceIdentifier,
124                 DataChangeListener dataChangeListener, DataChangeScope dataChangeScope) {
125                 return null;
126             }
127
128             @Override public BindingTransactionChain createTransactionChain(
129                 TransactionChainListener transactionChainListener) {
130                 return null;
131             }
132
133             @Nonnull @Override
134             public <T extends DataObject, L extends DataTreeChangeListener<T>> ListenerRegistration<L> registerDataTreeChangeListener(
135                 @Nonnull DataTreeIdentifier<T> dataTreeIdentifier, @Nonnull L l) {
136                 return null;
137             }
138         });
139
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<>();
148
149         expectedListProperty.add(new PropertyFilterBuilder()
150             .setProperty(new InputResourceSpecificPropertyBuilder()
151                 .setResourceSpecificProperty(
152                     new SpecificEndpointProperty(RESOURCE_SPECIFIC_PROPERTY))
153                 .build())
154             .build()
155         );
156         expectedListProperty.add(new PropertyFilterBuilder()
157             .setProperty(new InputGlobalPropertyBuilder()
158                 .setGlobalProperty(
159                     new GlobalEndpointProperty(GLOBAL_PROPERTY))
160                 .build())
161             .build()
162         );
163
164         expectedListEndpoint.add(new EndpointFilterBuilder()
165             .setAddress(
166                 new Ipv4Builder()
167                     .setIpv4(new Ipv4Address(THE_FIRST_IPv4_ADDRESS))
168                     .build())
169             .build()
170         );
171         expectedListEndpoint.add(new EndpointFilterBuilder()
172             .setAddress(
173                 new Ipv4Builder()
174                     .setIpv4(new Ipv4Address(THE_SECOND_IPv4_ADDRESS))
175                     .build())
176             .build()
177         );
178
179         assertEquals(expectedListProperty, listProperty);
180         assertEquals(expectedListEndpoint, listEndpoint);
181     }
182
183     @Test
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);
188
189         AltoModelEndpointpropertyService epService = mock(AltoModelEndpointpropertyService.class);
190         Future<RpcResult<QueryOutput>> future = mock(Future.class);
191         RpcResult<QueryOutput> rpcResult =mock(RpcResult.class);
192
193         LinkedList<EndpointProperty> eppist = new LinkedList<>();
194
195         SourceBuilder sourceBuilder = new SourceBuilder();
196         Source THE_FIRST_SOURCE = sourceBuilder.setAddress(
197             new Ipv4Builder()
198                 .setIpv4(new Ipv4Address(THE_FIRST_IPv4_ADDRESS))
199                 .build())
200             .build();
201         Source THE_SECOND_SOURCE = sourceBuilder.setAddress(
202             new Ipv4Builder()
203                 .setIpv4(new Ipv4Address(THE_SECOND_IPv4_ADDRESS))
204                 .build())
205             .build();
206
207         EndpointPropertyBuilder epBuilder = new EndpointPropertyBuilder();
208         List<Properties> THE_FIRST_LIST_PROPERTIES = new LinkedList<>();
209         List<Properties> THE_SECOND_LIST_PROPERTIES = new LinkedList<>();
210
211         PropertiesBuilder propertiesBuilder = new PropertiesBuilder();
212         PropertyContainerBuilder pcBuilder = new PropertyContainerBuilder();
213
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());
222
223         pcBuilder.setProperty(
224             new OutputGlobalPropertyBuilder()
225                 .setGlobalProperty(new GlobalEndpointProperty(DEFAULT_GLOBAL_PROPERTY_NAME))
226                 .build()
227         );
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());
232
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());
240
241         eppist.add(
242             epBuilder
243                 .setSource(THE_FIRST_SOURCE)
244                 .setProperties(THE_FIRST_LIST_PROPERTIES)
245                 .build());
246         eppist.add(
247             epBuilder
248                 .setSource(THE_SECOND_SOURCE)
249                 .setProperties(THE_SECOND_LIST_PROPERTIES).build());
250
251         EndpointPropertyMapBuilder endpointPropertyMapBuilder = new EndpointPropertyMapBuilder();
252         endpointPropertyMapBuilder.setEndpointProperty(eppist);
253
254         EndpointPropertymapDataBuilder endpointPropertymapDataBuilder = new EndpointPropertymapDataBuilder();
255         endpointPropertymapDataBuilder.setEndpointPropertyMap(endpointPropertyMapBuilder.build());
256
257         EndpointpropertyResponseBuilder endpointpropertyResponseBuilder = new EndpointpropertyResponseBuilder();
258         endpointpropertyResponseBuilder.setEndpointpropertyData(endpointPropertymapDataBuilder.build());
259
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() {
268                 return null;
269             }
270
271             @Override public ReadWriteTransaction newReadWriteTransaction() {
272                 return null;
273             }
274
275             @Override public WriteTransaction newWriteOnlyTransaction() {
276                 return null;
277             }
278
279             @Override public ListenerRegistration<DataChangeListener> registerDataChangeListener(
280                 LogicalDatastoreType logicalDatastoreType, InstanceIdentifier<?> instanceIdentifier,
281                 DataChangeListener dataChangeListener, DataChangeScope dataChangeScope) {
282                 return null;
283             }
284
285             @Override public BindingTransactionChain createTransactionChain(
286                 TransactionChainListener transactionChainListener) {
287                 return null;
288             }
289
290             @Nonnull @Override
291             public <T extends DataObject, L extends DataTreeChangeListener<T>> ListenerRegistration<L> registerDataTreeChangeListener(
292                 @Nonnull DataTreeIdentifier<T> dataTreeIdentifier, @Nonnull L l) {
293                 return null;
294             }
295         });
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());
304
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);
312
313         assertEquals(expectedEndpointProperties, endpointProperties);
314     }
315 }