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;
16 import com.google.common.base.Preconditions;
17 import java.io.FileNotFoundException;
19 import javax.ws.rs.core.MultivaluedMap;
20 import javax.ws.rs.core.UriBuilder;
21 import javax.ws.rs.core.UriInfo;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.mockito.Mockito;
25 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
26 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
27 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
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.RestconfImpl;
31 import org.opendaylight.netconf.sal.streams.listeners.ListenerAdapter;
32 import org.opendaylight.netconf.sal.streams.listeners.Notificator;
33 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
34 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
35 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType;
36 import org.opendaylight.yangtools.yang.common.QName;
37 import org.opendaylight.yangtools.yang.common.Revision;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
40 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.InstanceIdentifierBuilder;
41 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
42 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
43 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
44 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
45 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
46 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
47 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
48 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
49 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
50 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
51 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
52 import org.opendaylight.yangtools.yang.model.api.Module;
53 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
54 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
55 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
56 import org.opendaylight.yangtools.yang.model.util.SchemaNodeUtils;
57 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
59 public class URIParametersParsing {
61 private RestconfImpl restconf;
62 private BrokerFacade mockedBrokerFacade;
63 private ControllerContext controllerContext;
66 public void init() throws FileNotFoundException, ReactorException {
67 this.restconf = RestconfImpl.getInstance();
68 this.mockedBrokerFacade = mock(BrokerFacade.class);
69 this.controllerContext = TestRestconfUtils.newControllerContext(
70 TestUtils.loadSchemaContext("/datastore-and-scope-specification"));
71 this.restconf.setControllerContext(this.controllerContext);
72 this.restconf.setBroker(this.mockedBrokerFacade);
76 public void resolveURIParametersConcreteValues() {
77 resolveURIParameters("OPERATIONAL", "SUBTREE", LogicalDatastoreType.OPERATIONAL, DataChangeScope.SUBTREE);
81 public void resolveURIParametersDefaultValues() {
82 resolveURIParameters(null, null, LogicalDatastoreType.CONFIGURATION, DataChangeScope.BASE);
85 private void resolveURIParameters(final String datastore, final String scope,
86 final LogicalDatastoreType datastoreExpected, final DataChangeScope scopeExpected) {
88 final InstanceIdentifierBuilder iiBuilder = YangInstanceIdentifier.builder();
89 iiBuilder.node(QName.create("", "dummyStreamName"));
91 final String datastoreValue = datastore == null ? "CONFIGURATION" : datastore;
92 final String scopeValue = scope == null ? "BASE" : scope + "";
93 Notificator.createListener(iiBuilder.build(), "dummyStreamName/datastore=" + datastoreValue + "/scope="
94 + scopeValue, NotificationOutputType.XML, controllerContext);
96 final UriInfo mockedUriInfo = mock(UriInfo.class);
97 @SuppressWarnings("unchecked")
98 final MultivaluedMap<String, String> mockedMultivaluedMap = mock(MultivaluedMap.class);
99 when(mockedMultivaluedMap.getFirst(eq("datastore"))).thenReturn(datastoreValue);
100 when(mockedMultivaluedMap.getFirst(eq("scope"))).thenReturn(scopeValue);
102 when(mockedUriInfo.getQueryParameters(eq(false))).thenReturn(mockedMultivaluedMap);
104 final UriBuilder uriBuilder = UriBuilder.fromUri("www.whatever.com");
105 when(mockedUriInfo.getAbsolutePathBuilder()).thenReturn(uriBuilder);
107 this.restconf.invokeRpc("sal-remote:create-data-change-event-subscription",
108 prepareDomRpcNode(datastoreValue, scopeValue), mockedUriInfo);
110 final ListenerAdapter listener =
111 Notificator.getListenerFor("data-change-event-subscription/opendaylight-inventory:nodes/datastore="
112 + datastoreValue + "/scope=" + scopeValue);
113 assertNotNull(listener);
116 private NormalizedNodeContext prepareDomRpcNode(final String datastore, final String scope) {
117 final SchemaContext schema = this.controllerContext.getGlobalSchema();
118 final Module rpcSalRemoteModule = schema.findModule("sal-remote", Revision.of("2014-01-14")).get();
119 final Set<RpcDefinition> setRpcs = rpcSalRemoteModule.getRpcs();
120 final QName rpcQName =
121 QName.create(rpcSalRemoteModule.getQNameModule(), "create-data-change-event-subscription");
122 final QName rpcInputQName =
123 QName.create("urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote", "2014-01-14", "input");
124 final RpcDefinition rpcDef = Mockito.mock(RpcDefinition.class);
125 ContainerSchemaNode rpcInputSchemaNode = null;
126 for (final RpcDefinition rpc : setRpcs) {
127 if (rpcQName.isEqualWithoutRevision(rpc.getQName())) {
128 rpcInputSchemaNode = SchemaNodeUtils.getRpcDataSchema(rpc, rpcInputQName);
132 assertNotNull("RPC ContainerSchemaNode was not found!", rpcInputSchemaNode);
134 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> container =
135 Builders.containerBuilder(rpcInputSchemaNode);
137 final QName pathQName =
138 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()
143 .node(QName.create("urn:opendaylight:inventory", "2013-08-19", "nodes")).build()).build();
144 container.withChild(pathNode);
146 final AugmentationSchemaNode augmentationSchema = rpcInputSchemaNode.getAvailableAugmentations().iterator()
148 Preconditions.checkNotNull(augmentationSchema);
149 final DataContainerNodeBuilder<AugmentationIdentifier, AugmentationNode> augmentationBuilder =
150 Builders.augmentationBuilder(augmentationSchema);
152 final QName dataStoreQName = QName.create("urn:sal:restconf:event:subscription", "2014-07-08", "datastore");
153 final DataSchemaNode dsSchemaNode = augmentationSchema.getDataChildByName(dataStoreQName);
154 assertTrue(dsSchemaNode instanceof LeafSchemaNode);
155 final LeafNode<Object> dsNode = Builders.leafBuilder((LeafSchemaNode) dsSchemaNode)
156 .withValue(datastore).build();
157 augmentationBuilder.withChild(dsNode);
159 final QName scopeQName = QName.create("urn:sal:restconf:event:subscription", "2014-07-08", "scope");
160 final DataSchemaNode scopeSchemaNode = augmentationSchema.getDataChildByName(scopeQName);
161 assertTrue(scopeSchemaNode instanceof LeafSchemaNode);
162 final LeafNode<Object> scopeNode = Builders.leafBuilder((LeafSchemaNode) scopeSchemaNode)
163 .withValue(scope).build();
164 augmentationBuilder.withChild(scopeNode);
166 final QName outputQName =
167 QName.create("urn:sal:restconf:event:subscription", "2014-07-08", "notification-output-type");
168 final DataSchemaNode outputSchemaNode = augmentationSchema.getDataChildByName(outputQName);
169 assertTrue(outputSchemaNode instanceof LeafSchemaNode);
170 final LeafNode<Object> outputNode =
171 Builders.leafBuilder((LeafSchemaNode) outputSchemaNode).withValue("XML").build();
172 augmentationBuilder.withChild(outputNode);
174 container.withChild(augmentationBuilder.build());
176 when(rpcDef.getInput()).thenReturn(rpcInputSchemaNode);
177 when(rpcDef.getPath()).thenReturn(SchemaPath.create(true, rpcQName));
178 when(rpcDef.getQName()).thenReturn(rpcQName);
180 return new NormalizedNodeContext(new InstanceIdentifierContext<>(null, rpcDef, null, schema),