cb03a7f68af47fe980a6344008de7aa4b2d7a440
[netconf.git] / restconf / restconf-nb-bierman02 / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / URIParametersParsing.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.sal.restconf.impl.test;
9
10 import static org.junit.Assert.assertNotNull;
11 import static org.junit.Assert.assertTrue;
12 import static org.mockito.ArgumentMatchers.eq;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.when;
15
16 import com.google.common.base.Preconditions;
17 import java.io.FileNotFoundException;
18 import java.util.Set;
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.LogicalDatastoreType;
26 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
27 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
28 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
29 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
30 import org.opendaylight.netconf.sal.streams.listeners.ListenerAdapter;
31 import org.opendaylight.netconf.sal.streams.listeners.Notificator;
32 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
33 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
34 import org.opendaylight.restconf.common.util.DataChangeScope;
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;
58
59 public class URIParametersParsing {
60
61     private RestconfImpl restconf;
62     private BrokerFacade mockedBrokerFacade;
63     private ControllerContext controllerContext;
64
65     @Before
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);
71     }
72
73     @Test
74     public void resolveURIParametersConcreteValues() {
75         resolveURIParameters("OPERATIONAL", "SUBTREE", LogicalDatastoreType.OPERATIONAL, DataChangeScope.SUBTREE);
76     }
77
78     @Test
79     public void resolveURIParametersDefaultValues() {
80         resolveURIParameters(null, null, LogicalDatastoreType.CONFIGURATION, DataChangeScope.BASE);
81     }
82
83     private void resolveURIParameters(final String datastore, final String scope,
84             final LogicalDatastoreType datastoreExpected, final DataChangeScope scopeExpected) {
85
86         final InstanceIdentifierBuilder iiBuilder = YangInstanceIdentifier.builder();
87         iiBuilder.node(QName.create("", "dummyStreamName"));
88
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);
93
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);
99
100         when(mockedUriInfo.getQueryParameters(eq(false))).thenReturn(mockedMultivaluedMap);
101
102         final UriBuilder uriBuilder = UriBuilder.fromUri("www.whatever.com");
103         when(mockedUriInfo.getAbsolutePathBuilder()).thenReturn(uriBuilder);
104
105         this.restconf.invokeRpc("sal-remote:create-data-change-event-subscription",
106             prepareDomRpcNode(datastoreValue, scopeValue), mockedUriInfo);
107
108         final ListenerAdapter listener =
109                 Notificator.getListenerFor("data-change-event-subscription/opendaylight-inventory:nodes/datastore="
110                 + datastoreValue + "/scope=" + scopeValue);
111         assertNotNull(listener);
112     }
113
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);
127                 break;
128             }
129         }
130         assertNotNull("RPC ContainerSchemaNode was not found!", rpcInputSchemaNode);
131
132         final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> container =
133                 Builders.containerBuilder(rpcInputSchemaNode);
134
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);
143
144         final AugmentationSchemaNode augmentationSchema = rpcInputSchemaNode.getAvailableAugmentations().iterator()
145                 .next();
146         Preconditions.checkNotNull(augmentationSchema);
147         final DataContainerNodeBuilder<AugmentationIdentifier, AugmentationNode> augmentationBuilder =
148                 Builders.augmentationBuilder(augmentationSchema);
149
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);
156
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);
163
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);
171
172         container.withChild(augmentationBuilder.build());
173
174         when(rpcDef.getInput()).thenReturn(rpcInputSchemaNode);
175         when(rpcDef.getPath()).thenReturn(SchemaPath.create(true, rpcQName));
176         when(rpcDef.getQName()).thenReturn(rpcQName);
177
178         return new NormalizedNodeContext(new InstanceIdentifierContext<>(null, rpcDef, null, schema),
179                 container.build());
180     }
181 }