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.mockedBrokerFacade = mock(BrokerFacade.class);
68 this.controllerContext = TestRestconfUtils.newControllerContext(
69 TestUtils.loadSchemaContext("/datastore-and-scope-specification"));
70 this.restconf = RestconfImpl.newInstance(mockedBrokerFacade, controllerContext);
74 public void resolveURIParametersConcreteValues() {
75 resolveURIParameters("OPERATIONAL", "SUBTREE", LogicalDatastoreType.OPERATIONAL, DataChangeScope.SUBTREE);
79 public void resolveURIParametersDefaultValues() {
80 resolveURIParameters(null, null, LogicalDatastoreType.CONFIGURATION, DataChangeScope.BASE);
83 private void resolveURIParameters(final String datastore, final String scope,
84 final LogicalDatastoreType datastoreExpected, final DataChangeScope scopeExpected) {
86 final InstanceIdentifierBuilder iiBuilder = YangInstanceIdentifier.builder();
87 iiBuilder.node(QName.create("", "dummyStreamName"));
89 final String datastoreValue = datastore == null ? "CONFIGURATION" : datastore;
90 final String scopeValue = scope == null ? "BASE" : scope + "";
91 Notificator.createListener(iiBuilder.build(), "dummyStreamName/datastore=" + datastoreValue + "/scope="
92 + scopeValue, NotificationOutputType.XML, controllerContext);
94 final UriInfo mockedUriInfo = mock(UriInfo.class);
95 @SuppressWarnings("unchecked")
96 final MultivaluedMap<String, String> mockedMultivaluedMap = mock(MultivaluedMap.class);
97 when(mockedMultivaluedMap.getFirst(eq("datastore"))).thenReturn(datastoreValue);
98 when(mockedMultivaluedMap.getFirst(eq("scope"))).thenReturn(scopeValue);
100 when(mockedUriInfo.getQueryParameters(eq(false))).thenReturn(mockedMultivaluedMap);
102 final UriBuilder uriBuilder = UriBuilder.fromUri("www.whatever.com");
103 when(mockedUriInfo.getAbsolutePathBuilder()).thenReturn(uriBuilder);
105 this.restconf.invokeRpc("sal-remote:create-data-change-event-subscription",
106 prepareDomRpcNode(datastoreValue, scopeValue), mockedUriInfo);
108 final ListenerAdapter listener =
109 Notificator.getListenerFor("data-change-event-subscription/opendaylight-inventory:nodes/datastore="
110 + datastoreValue + "/scope=" + scopeValue);
111 assertNotNull(listener);
114 private NormalizedNodeContext prepareDomRpcNode(final String datastore, final String scope) {
115 final SchemaContext schema = this.controllerContext.getGlobalSchema();
116 final Module rpcSalRemoteModule = schema.findModule("sal-remote", Revision.of("2014-01-14")).get();
117 final Set<RpcDefinition> setRpcs = rpcSalRemoteModule.getRpcs();
118 final QName rpcQName =
119 QName.create(rpcSalRemoteModule.getQNameModule(), "create-data-change-event-subscription");
120 final QName rpcInputQName =
121 QName.create("urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote", "2014-01-14", "input");
122 final RpcDefinition rpcDef = Mockito.mock(RpcDefinition.class);
123 ContainerSchemaNode rpcInputSchemaNode = null;
124 for (final RpcDefinition rpc : setRpcs) {
125 if (rpcQName.isEqualWithoutRevision(rpc.getQName())) {
126 rpcInputSchemaNode = SchemaNodeUtils.getRpcDataSchema(rpc, rpcInputQName);
130 assertNotNull("RPC ContainerSchemaNode was not found!", rpcInputSchemaNode);
132 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> container =
133 Builders.containerBuilder(rpcInputSchemaNode);
135 final QName pathQName =
136 QName.create("urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote", "2014-01-14", "path");
137 final DataSchemaNode pathSchemaNode = rpcInputSchemaNode.getDataChildByName(pathQName);
138 assertTrue(pathSchemaNode instanceof LeafSchemaNode);
139 final LeafNode<Object> pathNode = Builders.leafBuilder((LeafSchemaNode) pathSchemaNode)
140 .withValue(YangInstanceIdentifier.builder()
141 .node(QName.create("urn:opendaylight:inventory", "2013-08-19", "nodes")).build()).build();
142 container.withChild(pathNode);
144 final AugmentationSchemaNode augmentationSchema = rpcInputSchemaNode.getAvailableAugmentations().iterator()
146 Preconditions.checkNotNull(augmentationSchema);
147 final DataContainerNodeBuilder<AugmentationIdentifier, AugmentationNode> augmentationBuilder =
148 Builders.augmentationBuilder(augmentationSchema);
150 final QName dataStoreQName = QName.create("urn:sal:restconf:event:subscription", "2014-07-08", "datastore");
151 final DataSchemaNode dsSchemaNode = augmentationSchema.getDataChildByName(dataStoreQName);
152 assertTrue(dsSchemaNode instanceof LeafSchemaNode);
153 final LeafNode<Object> dsNode = Builders.leafBuilder((LeafSchemaNode) dsSchemaNode)
154 .withValue(datastore).build();
155 augmentationBuilder.withChild(dsNode);
157 final QName scopeQName = QName.create("urn:sal:restconf:event:subscription", "2014-07-08", "scope");
158 final DataSchemaNode scopeSchemaNode = augmentationSchema.getDataChildByName(scopeQName);
159 assertTrue(scopeSchemaNode instanceof LeafSchemaNode);
160 final LeafNode<Object> scopeNode = Builders.leafBuilder((LeafSchemaNode) scopeSchemaNode)
161 .withValue(scope).build();
162 augmentationBuilder.withChild(scopeNode);
164 final QName outputQName =
165 QName.create("urn:sal:restconf:event:subscription", "2014-07-08", "notification-output-type");
166 final DataSchemaNode outputSchemaNode = augmentationSchema.getDataChildByName(outputQName);
167 assertTrue(outputSchemaNode instanceof LeafSchemaNode);
168 final LeafNode<Object> outputNode =
169 Builders.leafBuilder((LeafSchemaNode) outputSchemaNode).withValue("XML").build();
170 augmentationBuilder.withChild(outputNode);
172 container.withChild(augmentationBuilder.build());
174 when(rpcDef.getInput()).thenReturn(rpcInputSchemaNode);
175 when(rpcDef.getPath()).thenReturn(SchemaPath.create(true, rpcQName));
176 when(rpcDef.getQName()).thenReturn(rpcQName);
178 return new NormalizedNodeContext(new InstanceIdentifierContext<>(null, rpcDef, null, schema),