2 * Copyright (c) 2014 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
8 package org.opendaylight.controller.sal.restconf.impl.test;
10 import static org.junit.Assert.assertNotNull;
11 import static org.junit.Assert.assertTrue;
12 import static org.mockito.Matchers.eq;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.when;
15 import static org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil.getRevisionFormat;
16 import java.io.FileNotFoundException;
17 import java.text.ParseException;
18 import java.util.Date;
20 import javax.ws.rs.core.MultivaluedMap;
21 import javax.ws.rs.core.UriBuilder;
22 import javax.ws.rs.core.UriInfo;
23 import org.junit.Before;
24 import org.junit.Ignore;
25 import org.junit.Test;
26 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
27 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
28 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
29 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
30 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
31 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
32 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
33 import org.opendaylight.netconf.sal.streams.listeners.ListenerAdapter;
34 import org.opendaylight.netconf.sal.streams.listeners.Notificator;
35 import org.opendaylight.yangtools.yang.common.QName;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.InstanceIdentifierBuilder;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
39 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
40 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
41 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
42 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
43 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
44 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
45 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
46 import org.opendaylight.yangtools.yang.model.api.Module;
47 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
48 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
49 import org.opendaylight.yangtools.yang.model.util.SchemaNodeUtils;
50 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
52 public class URIParametersParsing {
54 private RestconfImpl restconf;
55 private BrokerFacade mockedBrokerFacade;
56 private ControllerContext controllerContext;
59 public void init() throws FileNotFoundException, ReactorException {
60 this.restconf = RestconfImpl.getInstance();
61 this.mockedBrokerFacade = mock(BrokerFacade.class);
62 this.controllerContext = ControllerContext.getInstance();
63 this.controllerContext.setSchemas(TestUtils.loadSchemaContext("/datastore-and-scope-specification"));
64 this.restconf.setControllerContext(this.controllerContext);
65 this.restconf.setBroker(this.mockedBrokerFacade);
69 @Ignore // URI parsing test - not able to catch a motivation + bad mocking response now - it needs to change Controller RPC table holder approach
70 public void resolveURIParametersConcreteValues() {
71 resolveURIParameters("OPERATIONAL", "SUBTREE", LogicalDatastoreType.OPERATIONAL, DataChangeScope.SUBTREE);
75 @Ignore // URI parsing test - not able to catch a motivation + bad mocking response now - it needs to change Controller RPC table holder approach
76 public void resolveURIParametersDefaultValues() {
77 resolveURIParameters(null, null, LogicalDatastoreType.CONFIGURATION, DataChangeScope.BASE);
80 private void resolveURIParameters(final String datastore, final String scope,
81 final LogicalDatastoreType datastoreExpected, final DataChangeScope scopeExpected) {
83 final InstanceIdentifierBuilder iiBuilder = YangInstanceIdentifier.builder();
84 iiBuilder.node(QName.create("dummyStreamName"));
86 final String datastoreValue = datastore == null ? "CONFIGURATION" : datastore;
87 final String scopeValue = scope == null ? "BASE" : scope + "";
88 Notificator.createListener(iiBuilder.build(), "dummyStreamName/datastore=" + datastoreValue + "/scope="
91 final UriInfo mockedUriInfo = mock(UriInfo.class);
92 @SuppressWarnings("unchecked")
93 final MultivaluedMap<String, String> mockedMultivaluedMap = mock(MultivaluedMap.class);
94 when(mockedMultivaluedMap.getFirst(eq("datastore"))).thenReturn(datastoreValue);
95 when(mockedMultivaluedMap.getFirst(eq("scope"))).thenReturn(scopeValue);
97 when(mockedUriInfo.getQueryParameters(eq(false))).thenReturn(mockedMultivaluedMap);
99 final UriBuilder uriBuilder = UriBuilder.fromUri("www.whatever.com");
100 when(mockedUriInfo.getAbsolutePathBuilder()).thenReturn(uriBuilder);
102 // when(mockedBrokerFacade.invokeRpc(any(SchemaPath.class), any(NormalizedNode.class)))
103 // .thenReturn(Futures.<DOMRpcResult, DOMRpcException> immediateCheckedFuture(new DefaultDOMRpcResult(Builders.containerBuilder().build())));
105 this.restconf.invokeRpc("sal-remote:create-data-change-event-subscription", prepareDomRpcNode(datastore, scope),
108 final ListenerAdapter listener = Notificator.getListenerFor("opendaylight-inventory:nodes/datastore="
109 + datastoreValue + "/scope=" + scopeValue);
110 assertNotNull(listener);
114 private NormalizedNodeContext prepareDomRpcNode(final String datastore, final String scope) {
115 final SchemaContext schema = this.controllerContext.getGlobalSchema();
118 revDate = getRevisionFormat().parse("2014-01-14");
120 catch (final ParseException e) {
121 throw new IllegalStateException(e);
123 final Module rpcSalRemoteModule = schema.findModuleByName("sal-remote", revDate);
124 final Set<RpcDefinition> setRpcs = rpcSalRemoteModule.getRpcs();
125 final QName rpcQName = QName.create(rpcSalRemoteModule.getQNameModule(), "create-data-change-event-subscription");
126 final QName rpcInputQName = QName.create("urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote","2014-01-14","input");
127 ContainerSchemaNode rpcInputSchemaNode = null;
128 for (final RpcDefinition rpc : setRpcs) {
129 if (rpcQName.isEqualWithoutRevision(rpc.getQName())) {
130 rpcInputSchemaNode = SchemaNodeUtils.getRpcDataSchema(rpc, rpcInputQName);
134 assertNotNull("RPC ContainerSchemaNode was not found!", rpcInputSchemaNode);
136 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> container = Builders.containerBuilder(rpcInputSchemaNode);
138 final QName pathQName = QName.create("urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote", "2014-01-14", "path");
139 final DataSchemaNode pathSchemaNode = rpcInputSchemaNode.getDataChildByName(pathQName);
140 assertTrue(pathSchemaNode instanceof LeafSchemaNode);
141 final LeafNode<Object> pathNode = (Builders.leafBuilder((LeafSchemaNode) pathSchemaNode)
142 .withValue(YangInstanceIdentifier.builder().node(QName.create("urn:opendaylight:inventory", "2013-08-19", "nodes")).build())).build();
143 container.withChild(pathNode);
145 final QName dataStoreQName = QName.create("urn:sal:restconf:event:subscription", "2014-7-8", "datastore");
146 final DataSchemaNode dsSchemaNode = rpcInputSchemaNode.getDataChildByName(dataStoreQName);
147 assertTrue(dsSchemaNode instanceof LeafSchemaNode);
148 final LeafNode<Object> dsNode = (Builders.leafBuilder((LeafSchemaNode) dsSchemaNode)
149 .withValue(datastore)).build();
150 container.withChild(dsNode);
152 final QName scopeQName = QName.create("urn:sal:restconf:event:subscription", "2014-7-8", "scope");
153 final DataSchemaNode scopeSchemaNode = rpcInputSchemaNode.getDataChildByName(scopeQName);
154 assertTrue(scopeSchemaNode instanceof LeafSchemaNode);
155 final LeafNode<Object> scopeNode = (Builders.leafBuilder((LeafSchemaNode) scopeSchemaNode)
156 .withValue(scope)).build();
157 container.withChild(scopeNode);
159 return new NormalizedNodeContext(new InstanceIdentifierContext<>(null, rpcInputSchemaNode, null, schema), container.build());