Update MRI projects for Aluminium
[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 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.restconf.impl.BrokerFacade;
27 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
28 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
29 import org.opendaylight.netconf.sal.streams.listeners.ListenerAdapter;
30 import org.opendaylight.netconf.sal.streams.listeners.Notificator;
31 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
32 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
33 import org.opendaylight.restconf.common.util.DataChangeScope;
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.impl.schema.Builders;
45 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
46 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
47 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
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.LeafSchemaNode;
51 import org.opendaylight.yangtools.yang.model.api.Module;
52 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
53 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
54 import org.opendaylight.yangtools.yang.model.util.SchemaNodeUtils;
55 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
56
57 public class URIParametersParsing {
58
59     private RestconfImpl restconf;
60     private BrokerFacade mockedBrokerFacade;
61     private ControllerContext controllerContext;
62
63     @Before
64     public void init() throws FileNotFoundException, ReactorException {
65         this.mockedBrokerFacade = mock(BrokerFacade.class);
66         this.controllerContext = TestRestconfUtils.newControllerContext(
67                 TestUtils.loadSchemaContext("/datastore-and-scope-specification"));
68         this.restconf = RestconfImpl.newInstance(mockedBrokerFacade, controllerContext);
69     }
70
71     @Test
72     public void resolveURIParametersConcreteValues() {
73         resolveURIParameters("OPERATIONAL", "SUBTREE", LogicalDatastoreType.OPERATIONAL, DataChangeScope.SUBTREE);
74     }
75
76     @Test
77     public void resolveURIParametersDefaultValues() {
78         resolveURIParameters(null, null, LogicalDatastoreType.CONFIGURATION, DataChangeScope.BASE);
79     }
80
81     private void resolveURIParameters(final String datastore, final String scope,
82             final LogicalDatastoreType datastoreExpected, final DataChangeScope scopeExpected) {
83
84         final InstanceIdentifierBuilder iiBuilder = YangInstanceIdentifier.builder();
85         iiBuilder.node(QName.create("", "dummyStreamName"));
86
87         final String datastoreValue = datastore == null ? "CONFIGURATION" : datastore;
88         final String scopeValue = scope == null ? "BASE" : scope + "";
89         Notificator.createListener(iiBuilder.build(), "dummyStreamName/datastore=" + datastoreValue + "/scope="
90                 + scopeValue, NotificationOutputType.XML, controllerContext);
91
92         final UriInfo mockedUriInfo = mock(UriInfo.class);
93         @SuppressWarnings("unchecked")
94         final MultivaluedMap<String, String> mockedMultivaluedMap = mock(MultivaluedMap.class);
95         when(mockedMultivaluedMap.getFirst(eq("datastore"))).thenReturn(datastoreValue);
96         when(mockedMultivaluedMap.getFirst(eq("scope"))).thenReturn(scopeValue);
97
98         when(mockedUriInfo.getQueryParameters(eq(false))).thenReturn(mockedMultivaluedMap);
99
100         final UriBuilder uriBuilder = UriBuilder.fromUri("www.whatever.com");
101         when(mockedUriInfo.getAbsolutePathBuilder()).thenReturn(uriBuilder);
102
103         this.restconf.invokeRpc("sal-remote:create-data-change-event-subscription",
104             prepareDomRpcNode(datastoreValue, scopeValue), mockedUriInfo);
105
106         final ListenerAdapter listener =
107                 Notificator.getListenerFor("data-change-event-subscription/opendaylight-inventory:nodes/datastore="
108                 + datastoreValue + "/scope=" + scopeValue);
109         assertNotNull(listener);
110     }
111
112     private NormalizedNodeContext prepareDomRpcNode(final String datastore, final String scope) {
113         final EffectiveModelContext schema = this.controllerContext.getGlobalSchema();
114         final Module rpcSalRemoteModule = schema.findModule("sal-remote", Revision.of("2014-01-14")).get();
115         final QName rpcQName =
116                 QName.create(rpcSalRemoteModule.getQNameModule(), "create-data-change-event-subscription");
117         final QName rpcInputQName =
118                 QName.create("urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote", "2014-01-14", "input");
119         final RpcDefinition rpcDef = Mockito.mock(RpcDefinition.class);
120         ContainerSchemaNode rpcInputSchemaNode = null;
121         for (final RpcDefinition rpc : rpcSalRemoteModule.getRpcs()) {
122             if (rpcQName.isEqualWithoutRevision(rpc.getQName())) {
123                 rpcInputSchemaNode = SchemaNodeUtils.getRpcDataSchema(rpc, rpcInputQName);
124                 break;
125             }
126         }
127         assertNotNull("RPC ContainerSchemaNode was not found!", rpcInputSchemaNode);
128
129         final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> container =
130                 Builders.containerBuilder(rpcInputSchemaNode);
131
132         final QName pathQName =
133                 QName.create("urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote", "2014-01-14", "path");
134         final DataSchemaNode pathSchemaNode = rpcInputSchemaNode.getDataChildByName(pathQName);
135         assertTrue(pathSchemaNode instanceof LeafSchemaNode);
136         final LeafNode<Object> pathNode = Builders.leafBuilder((LeafSchemaNode) pathSchemaNode)
137                 .withValue(YangInstanceIdentifier.builder()
138                         .node(QName.create("urn:opendaylight:inventory", "2013-08-19", "nodes")).build()).build();
139         container.withChild(pathNode);
140
141         final AugmentationSchemaNode augmentationSchema = rpcInputSchemaNode.getAvailableAugmentations().iterator()
142                 .next();
143         Preconditions.checkNotNull(augmentationSchema);
144         final DataContainerNodeBuilder<AugmentationIdentifier, AugmentationNode> augmentationBuilder =
145                 Builders.augmentationBuilder(augmentationSchema);
146
147         final QName dataStoreQName = QName.create("urn:sal:restconf:event:subscription", "2014-07-08", "datastore");
148         final DataSchemaNode dsSchemaNode = augmentationSchema.getDataChildByName(dataStoreQName);
149         assertTrue(dsSchemaNode instanceof LeafSchemaNode);
150         final LeafNode<Object> dsNode = Builders.leafBuilder((LeafSchemaNode) dsSchemaNode)
151                 .withValue(datastore).build();
152         augmentationBuilder.withChild(dsNode);
153
154         final QName scopeQName = QName.create("urn:sal:restconf:event:subscription", "2014-07-08", "scope");
155         final DataSchemaNode scopeSchemaNode = augmentationSchema.getDataChildByName(scopeQName);
156         assertTrue(scopeSchemaNode instanceof LeafSchemaNode);
157         final LeafNode<Object> scopeNode = Builders.leafBuilder((LeafSchemaNode) scopeSchemaNode)
158                 .withValue(scope).build();
159         augmentationBuilder.withChild(scopeNode);
160
161         final QName outputQName =
162                 QName.create("urn:sal:restconf:event:subscription", "2014-07-08", "notification-output-type");
163         final DataSchemaNode outputSchemaNode = augmentationSchema.getDataChildByName(outputQName);
164         assertTrue(outputSchemaNode instanceof LeafSchemaNode);
165         final LeafNode<Object> outputNode =
166                 Builders.leafBuilder((LeafSchemaNode) outputSchemaNode).withValue("XML").build();
167         augmentationBuilder.withChild(outputNode);
168
169         container.withChild(augmentationBuilder.build());
170
171         when(rpcDef.getInput()).thenReturn(rpcInputSchemaNode);
172         when(rpcDef.getPath()).thenReturn(SchemaPath.create(true, rpcQName));
173         when(rpcDef.getQName()).thenReturn(rpcQName);
174
175         return new NormalizedNodeContext(new InstanceIdentifierContext<>(null, rpcDef, null, schema),
176                 container.build());
177     }
178 }