Convert anyxml nodes lazily
[netconf.git] / netconf / sal-netconf-connector / src / test / java / org / opendaylight / netconf / sal / connect / netconf / NetconfStateSchemasTest.java
1 /*
2  * Copyright (c) 2014, 2015 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.netconf.sal.connect.netconf;
9
10 import static org.hamcrest.CoreMatchers.hasItem;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertThat;
13 import static org.mockito.ArgumentMatchers.any;
14 import static org.mockito.ArgumentMatchers.eq;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.when;
17 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_QNAME;
18 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.toPath;
19 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFailedFluentFuture;
20 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFluentFuture;
21
22 import com.google.common.util.concurrent.FluentFuture;
23 import com.google.common.util.concurrent.ListenableFuture;
24 import java.net.InetSocketAddress;
25 import java.util.Collections;
26 import java.util.Set;
27 import java.util.concurrent.ExecutionException;
28 import java.util.concurrent.Executors;
29 import java.util.concurrent.Future;
30 import java.util.concurrent.TimeUnit;
31 import org.junit.Assert;
32 import org.junit.Before;
33 import org.junit.Ignore;
34 import org.junit.Test;
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 import org.opendaylight.mdsal.dom.api.DOMRpcImplementationNotAvailableException;
38 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
39 import org.opendaylight.mdsal.dom.api.DOMRpcService;
40 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
41 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
42 import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.BaseSchema;
43 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
44 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.NetconfState;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Schemas;
47 import org.opendaylight.yangtools.util.xml.UntrustedXML;
48 import org.opendaylight.yangtools.yang.common.QName;
49 import org.opendaylight.yangtools.yang.common.RpcError;
50 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
51 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
52 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
53 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
54 import org.opendaylight.yangtools.yang.data.codec.xml.XmlParserStream;
55 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
56 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
57 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
58 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
59 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
60 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
61 import org.slf4j.Logger;
62 import org.slf4j.LoggerFactory;
63
64 public class NetconfStateSchemasTest {
65
66     private static final Logger LOG = LoggerFactory.getLogger(NetconfStateSchemasTest.class);
67
68     private static final NetconfSessionPreferences CAPS = NetconfSessionPreferences.fromStrings(Collections.singleton(
69         "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring?module=ietf-netconf-monitoring&revision=2010-10-04"));
70     private final RemoteDeviceId deviceId = new RemoteDeviceId("device", new InetSocketAddress(99));
71     private final int numberOfSchemas = 73;
72     private final int numberOfLegalSchemas = numberOfSchemas - 3;
73     private ContainerNode compositeNodeSchemas;
74
75     @Mock
76     private DOMRpcService rpc;
77
78     private SchemaContext schemaContext;
79
80     @Before
81     public void setUp() throws Exception {
82         MockitoAnnotations.initMocks(this);
83         schemaContext = BaseSchema.BASE_NETCONF_CTX_WITH_NOTIFICATIONS.getSchemaContext();
84         final DataSchemaNode schemasNode =
85                 ((ContainerSchemaNode) schemaContext
86                         .getDataChildByName(NetconfState.QNAME)).getDataChildByName(Schemas.QNAME);
87
88         final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
89         final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
90         final XmlParserStream xmlParser = XmlParserStream.create(writer, schemaContext, schemasNode, false);
91
92         xmlParser.parse(UntrustedXML.createXMLStreamReader(getClass().getResourceAsStream(
93                 "/netconf-state.schemas.payload.xml")));
94         compositeNodeSchemas = (ContainerNode) resultHolder.getResult();
95
96     }
97
98     @Test
99     public void testCreate() throws Exception {
100         final NetconfStateSchemas schemas = NetconfStateSchemas.create(deviceId, compositeNodeSchemas);
101
102         final Set<QName> availableYangSchemasQNames = schemas.getAvailableYangSchemasQNames();
103         assertEquals(numberOfLegalSchemas, availableYangSchemasQNames.size());
104
105         assertThat(availableYangSchemasQNames,
106                 hasItem(QName.create("urn:TBD:params:xml:ns:yang:network-topology", "2013-07-12", "network-topology")));
107     }
108
109     @Ignore
110     @Test
111     public void testCreate2() throws Exception {
112         final ContainerNode netconfState = Builders.containerBuilder()
113                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(NetconfState.QNAME))
114                 .withChild(compositeNodeSchemas)
115                 .build();
116         final ContainerNode data = Builders.containerBuilder()
117                 .withNodeIdentifier(new YangInstanceIdentifier
118                         .NodeIdentifier(NetconfMessageTransformUtil.NETCONF_DATA_QNAME))
119                 .withChild(netconfState)
120                 .build();
121         final ContainerNode rpcReply = Builders.containerBuilder()
122                 .withNodeIdentifier(new YangInstanceIdentifier
123                         .NodeIdentifier(NetconfMessageTransformUtil.NETCONF_RPC_REPLY_QNAME))
124                 .withChild(data)
125                 .build();
126         when(rpc.invokeRpc(eq(toPath(NETCONF_GET_QNAME)), any()))
127             .thenReturn(immediateFluentFuture(new DefaultDOMRpcResult(rpcReply)));
128         final NetconfStateSchemas stateSchemas = NetconfStateSchemas.create(rpc, CAPS, deviceId, schemaContext);
129         final Set<QName> availableYangSchemasQNames = stateSchemas.getAvailableYangSchemasQNames();
130         assertEquals(numberOfLegalSchemas, availableYangSchemasQNames.size());
131
132         assertThat(availableYangSchemasQNames,
133                 hasItem(QName.create("urn:TBD:params:xml:ns:yang:network-topology", "2013-07-12", "network-topology")));
134     }
135
136     @Test
137     public void testCreateMonitoringNotSupported() throws Exception {
138         final NetconfSessionPreferences caps = NetconfSessionPreferences.fromStrings(Collections.emptySet());
139         final NetconfStateSchemas stateSchemas = NetconfStateSchemas.create(rpc, caps, deviceId, schemaContext);
140         final Set<QName> availableYangSchemasQNames = stateSchemas.getAvailableYangSchemasQNames();
141         Assert.assertTrue(availableYangSchemasQNames.isEmpty());
142     }
143
144     @Test
145     public void testCreateFail() throws Exception {
146         when(rpc.invokeRpc(eq(toPath(NETCONF_GET_QNAME)), any())).thenReturn(
147                 immediateFailedFluentFuture(new DOMRpcImplementationNotAvailableException("not available")));
148         final NetconfStateSchemas stateSchemas = NetconfStateSchemas.create(rpc, CAPS, deviceId, schemaContext);
149         final Set<QName> availableYangSchemasQNames = stateSchemas.getAvailableYangSchemasQNames();
150         Assert.assertTrue(availableYangSchemasQNames.isEmpty());
151     }
152
153     @Test
154     public void testCreateRpcError() throws Exception {
155         final RpcError rpcError = RpcResultBuilder.newError(RpcError.ErrorType.RPC, "fail", "fail");
156         when(rpc.invokeRpc(eq(toPath(NETCONF_GET_QNAME)), any()))
157             .thenReturn(immediateFluentFuture(new DefaultDOMRpcResult(rpcError)));
158         final NetconfStateSchemas stateSchemas = NetconfStateSchemas.create(rpc, CAPS, deviceId, schemaContext);
159         final Set<QName> availableYangSchemasQNames = stateSchemas.getAvailableYangSchemasQNames();
160         Assert.assertTrue(availableYangSchemasQNames.isEmpty());
161     }
162
163     @SuppressWarnings({ "checkstyle:IllegalThrows", "checkstyle:avoidHidingCauseException" })
164     @Test(expected = RuntimeException.class)
165     public void testCreateInterrupted() throws Throwable {
166         //NetconfStateSchemas.create calls Thread.currentThread().interrupt(), so it must run in its own thread
167         final Future<?> testFuture = Executors.newSingleThreadExecutor().submit(() -> {
168             final ListenableFuture<DOMRpcResult> interruptedFuture = mock(ListenableFuture.class);
169             try {
170                 when(interruptedFuture.get()).thenThrow(new InterruptedException("interrupted"));
171                 when(rpc.invokeRpc(eq(toPath(NETCONF_GET_QNAME)), any())).thenReturn(
172                         FluentFuture.from(interruptedFuture));
173                 NetconfStateSchemas.create(rpc, CAPS, deviceId, schemaContext);
174             } catch (final InterruptedException | ExecutionException e) {
175                 LOG.info("Operation failed.", e);
176             }
177
178         });
179         try {
180             testFuture.get(3, TimeUnit.SECONDS);
181         } catch (final ExecutionException e) {
182             throw e.getCause();
183         }
184     }
185
186     @Test
187     public void testRemoteYangSchemaEquals() throws Exception {
188         final NetconfStateSchemas.RemoteYangSchema schema1 =
189                 new NetconfStateSchemas.RemoteYangSchema(NetconfState.QNAME);
190         final NetconfStateSchemas.RemoteYangSchema schema2 =
191                 new NetconfStateSchemas.RemoteYangSchema(NetconfState.QNAME);
192         final NetconfStateSchemas.RemoteYangSchema schema3 =
193                 new NetconfStateSchemas.RemoteYangSchema(Schemas.QNAME);
194         Assert.assertEquals(schema1, schema2);
195         Assert.assertEquals(schema2, schema1);
196         Assert.assertNotEquals(schema1, schema3);
197         Assert.assertNotEquals(schema2, schema3);
198
199     }
200 }