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