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 com.google.common.base.Preconditions;
17 import java.io.FileNotFoundException;
18 import java.text.ParseException;
19 import java.util.Date;
21 import javax.ws.rs.core.MultivaluedMap;
22 import javax.ws.rs.core.UriBuilder;
23 import javax.ws.rs.core.UriInfo;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.mockito.Mockito;
27 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
28 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
29 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
30 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
31 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
32 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
33 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
34 import org.opendaylight.netconf.sal.streams.listeners.ListenerAdapter;
35 import org.opendaylight.netconf.sal.streams.listeners.Notificator;
36 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType;
37 import org.opendaylight.yangtools.yang.common.QName;
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.AugmentationSchema;
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 = ControllerContext.getInstance();
70 this.controllerContext.setSchemas(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);
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", prepareDomRpcNode(datastore, scope),
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();
120 revDate = getRevisionFormat().parse("2014-01-14");
122 catch (final ParseException e) {
123 throw new IllegalStateException(e);
125 final Module rpcSalRemoteModule = schema.findModuleByName("sal-remote", revDate);
126 final Set<RpcDefinition> setRpcs = rpcSalRemoteModule.getRpcs();
127 final QName rpcQName = QName.create(rpcSalRemoteModule.getQNameModule(), "create-data-change-event-subscription");
128 final QName rpcInputQName =
129 QName.create("urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote", "2014-01-14", "input");
130 final RpcDefinition rpcDef = Mockito.mock(RpcDefinition.class);
131 ContainerSchemaNode rpcInputSchemaNode = null;
132 for (final RpcDefinition rpc : setRpcs) {
133 if (rpcQName.isEqualWithoutRevision(rpc.getQName())) {
134 rpcInputSchemaNode = SchemaNodeUtils.getRpcDataSchema(rpc, rpcInputQName);
138 assertNotNull("RPC ContainerSchemaNode was not found!", rpcInputSchemaNode);
140 final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> container =
141 Builders.containerBuilder(rpcInputSchemaNode);
143 final QName pathQName =
144 QName.create("urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote", "2014-01-14", "path");
145 final DataSchemaNode pathSchemaNode = rpcInputSchemaNode.getDataChildByName(pathQName);
146 assertTrue(pathSchemaNode instanceof LeafSchemaNode);
147 final LeafNode<Object> pathNode = (Builders.leafBuilder((LeafSchemaNode) pathSchemaNode)
148 .withValue(YangInstanceIdentifier.builder()
149 .node(QName.create("urn:opendaylight:inventory", "2013-08-19", "nodes")).build())).build();
150 container.withChild(pathNode);
152 final AugmentationSchema augmentationSchema = rpcInputSchemaNode.getAvailableAugmentations().iterator().next();
153 Preconditions.checkNotNull(augmentationSchema);
154 final DataContainerNodeBuilder<AugmentationIdentifier, AugmentationNode> augmentationBuilder =
155 Builders.augmentationBuilder(augmentationSchema);
157 final QName dataStoreQName = QName.create("urn:sal:restconf:event:subscription", "2014-7-8", "datastore");
158 final DataSchemaNode dsSchemaNode = augmentationSchema.getDataChildByName(dataStoreQName);
159 assertTrue(dsSchemaNode instanceof LeafSchemaNode);
160 final LeafNode<Object> dsNode = (Builders.leafBuilder((LeafSchemaNode) dsSchemaNode)
161 .withValue(datastore)).build();
162 augmentationBuilder.withChild(dsNode);
164 final QName scopeQName = QName.create("urn:sal:restconf:event:subscription", "2014-7-8", "scope");
165 final DataSchemaNode scopeSchemaNode = augmentationSchema.getDataChildByName(scopeQName);
166 assertTrue(scopeSchemaNode instanceof LeafSchemaNode);
167 final LeafNode<Object> scopeNode = (Builders.leafBuilder((LeafSchemaNode) scopeSchemaNode)
168 .withValue(scope)).build();
169 augmentationBuilder.withChild(scopeNode);
171 final QName outputQName =
172 QName.create("urn:sal:restconf:event:subscription", "2014-7-8", "notification-output-type");
173 final DataSchemaNode outputSchemaNode = augmentationSchema.getDataChildByName(outputQName);
174 assertTrue(outputSchemaNode instanceof LeafSchemaNode);
175 final LeafNode<Object> outputNode =
176 (Builders.leafBuilder((LeafSchemaNode) outputSchemaNode).withValue("XML")).build();
177 augmentationBuilder.withChild(outputNode);
179 container.withChild(augmentationBuilder.build());
181 when(rpcDef.getInput()).thenReturn(rpcInputSchemaNode);
182 when(rpcDef.getPath()).thenReturn(SchemaPath.create(true, rpcQName));
183 when(rpcDef.getQName()).thenReturn(rpcQName);
185 return new NormalizedNodeContext(new InstanceIdentifierContext<RpcDefinition>(null, rpcDef, null, schema),