1b61ea1fef71d2a6d7e31cdc0b88e4b6a6221e4d
[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 java.util.Objects.requireNonNull;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.ArgumentMatchers.eq;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.when;
16
17 import java.io.FileNotFoundException;
18 import javax.ws.rs.core.MultivaluedMap;
19 import javax.ws.rs.core.UriBuilder;
20 import javax.ws.rs.core.UriInfo;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.mockito.Mockito;
24 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
25 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
26 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeContext;
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.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.CreateDataChangeEventSubscriptionInput1.Scope;
34 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType;
35 import org.opendaylight.yangtools.yang.common.QName;
36 import org.opendaylight.yangtools.yang.common.Revision;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.InstanceIdentifierBuilder;
40 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
41 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
42 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
43 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
44 import org.opendaylight.yangtools.yang.data.api.schema.builder.DataContainerNodeBuilder;
45 import org.opendaylight.yangtools.yang.data.impl.schema.SchemaAwareBuilders;
46 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
47 import org.opendaylight.yangtools.yang.model.api.ContainerLike;
48 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
49 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
50 import org.opendaylight.yangtools.yang.model.api.InputSchemaNode;
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.parser.spi.meta.ReactorException;
55
56 public class URIParametersParsing {
57
58     private RestconfImpl restconf;
59     private BrokerFacade mockedBrokerFacade;
60     private ControllerContext controllerContext;
61
62     @Before
63     public void init() throws FileNotFoundException, ReactorException {
64         mockedBrokerFacade = mock(BrokerFacade.class);
65         controllerContext = TestRestconfUtils.newControllerContext(
66                 TestUtils.loadSchemaContext("/datastore-and-scope-specification"));
67         restconf = RestconfImpl.newInstance(mockedBrokerFacade, controllerContext);
68     }
69
70     @Test
71     public void resolveURIParametersConcreteValues() {
72         resolveURIParameters("OPERATIONAL", "SUBTREE", LogicalDatastoreType.OPERATIONAL, Scope.SUBTREE);
73     }
74
75     @Test
76     public void resolveURIParametersDefaultValues() {
77         resolveURIParameters(null, null, LogicalDatastoreType.CONFIGURATION, Scope.BASE);
78     }
79
80     private void resolveURIParameters(final String datastore, final String scope,
81             final LogicalDatastoreType datastoreExpected, final Scope scopeExpected) {
82
83         final InstanceIdentifierBuilder iiBuilder = YangInstanceIdentifier.builder();
84         iiBuilder.node(QName.create("", "dummyStreamName"));
85
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="
89                 + scopeValue, NotificationOutputType.XML, controllerContext);
90
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);
96
97         when(mockedUriInfo.getQueryParameters(eq(false))).thenReturn(mockedMultivaluedMap);
98
99         final UriBuilder uriBuilder = UriBuilder.fromUri("www.whatever.com");
100         when(mockedUriInfo.getAbsolutePathBuilder()).thenReturn(uriBuilder);
101
102         restconf.invokeRpc("sal-remote:create-data-change-event-subscription",
103             prepareDomRpcNode(datastoreValue, scopeValue), mockedUriInfo);
104
105         final ListenerAdapter listener =
106                 Notificator.getListenerFor("data-change-event-subscription/opendaylight-inventory:nodes/datastore="
107                 + datastoreValue + "/scope=" + scopeValue);
108         assertNotNull(listener);
109     }
110
111     private NormalizedNodeContext prepareDomRpcNode(final String datastore, final String scope) {
112         final EffectiveModelContext schema = controllerContext.getGlobalSchema();
113         final Module rpcSalRemoteModule = schema.findModule("sal-remote", Revision.of("2014-01-14")).get();
114         final QName rpcQName =
115                 QName.create(rpcSalRemoteModule.getQNameModule(), "create-data-change-event-subscription");
116         final RpcDefinition rpcDef = Mockito.mock(RpcDefinition.class);
117         ContainerLike rpcInputSchemaNode = null;
118         for (final RpcDefinition rpc : rpcSalRemoteModule.getRpcs()) {
119             if (rpcQName.isEqualWithoutRevision(rpc.getQName())) {
120                 rpcInputSchemaNode = rpc.getInput();
121                 break;
122             }
123         }
124         assertNotNull("RPC ContainerSchemaNode was not found!", rpcInputSchemaNode);
125
126         final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> container =
127                 SchemaAwareBuilders.containerBuilder(rpcInputSchemaNode);
128
129         final QName pathQName =
130                 QName.create("urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote", "2014-01-14", "path");
131         final DataSchemaNode pathSchemaNode = rpcInputSchemaNode.getDataChildByName(pathQName);
132         assertTrue(pathSchemaNode instanceof LeafSchemaNode);
133         final LeafNode<Object> pathNode = SchemaAwareBuilders.leafBuilder((LeafSchemaNode) pathSchemaNode)
134                 .withValue(YangInstanceIdentifier.builder()
135                         .node(QName.create("urn:opendaylight:inventory", "2013-08-19", "nodes")).build()).build();
136         container.withChild(pathNode);
137
138         final AugmentationSchemaNode augmentationSchema = requireNonNull(rpcInputSchemaNode.getAvailableAugmentations()
139                 .iterator().next());
140         final DataContainerNodeBuilder<AugmentationIdentifier, AugmentationNode> augmentationBuilder =
141                 SchemaAwareBuilders.augmentationBuilder(augmentationSchema);
142
143         final QName dataStoreQName = QName.create("urn:sal:restconf:event:subscription", "2014-07-08", "datastore");
144         final DataSchemaNode dsSchemaNode = augmentationSchema.getDataChildByName(dataStoreQName);
145         assertTrue(dsSchemaNode instanceof LeafSchemaNode);
146         final LeafNode<Object> dsNode = SchemaAwareBuilders.leafBuilder((LeafSchemaNode) dsSchemaNode)
147                 .withValue(datastore).build();
148         augmentationBuilder.withChild(dsNode);
149
150         final QName scopeQName = QName.create("urn:sal:restconf:event:subscription", "2014-07-08", "scope");
151         final DataSchemaNode scopeSchemaNode = augmentationSchema.getDataChildByName(scopeQName);
152         assertTrue(scopeSchemaNode instanceof LeafSchemaNode);
153         final LeafNode<Object> scopeNode = SchemaAwareBuilders.leafBuilder((LeafSchemaNode) scopeSchemaNode)
154                 .withValue(scope).build();
155         augmentationBuilder.withChild(scopeNode);
156
157         final QName outputQName =
158                 QName.create("urn:sal:restconf:event:subscription", "2014-07-08", "notification-output-type");
159         final DataSchemaNode outputSchemaNode = augmentationSchema.getDataChildByName(outputQName);
160         assertTrue(outputSchemaNode instanceof LeafSchemaNode);
161         final LeafNode<Object> outputNode =
162             SchemaAwareBuilders.leafBuilder((LeafSchemaNode) outputSchemaNode).withValue("XML").build();
163         augmentationBuilder.withChild(outputNode);
164
165         container.withChild(augmentationBuilder.build());
166
167         when(rpcDef.getInput()).thenReturn((InputSchemaNode) rpcInputSchemaNode);
168         when(rpcDef.getQName()).thenReturn(rpcQName);
169
170         return new NormalizedNodeContext(InstanceIdentifierContext.ofRpcInput(schema, rpcDef, null), container.build());
171     }
172 }