2 * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.netconf.sal.connect.netconf;
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;
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;
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;
64 public class NetconfStateSchemasTest {
66 private static final Logger LOG = LoggerFactory.getLogger(NetconfStateSchemasTest.class);
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;
76 private DOMRpcService rpc;
78 private SchemaContext schemaContext;
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);
88 final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
89 final NormalizedNodeStreamWriter writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
90 final XmlParserStream xmlParser = XmlParserStream.create(writer, schemaContext, schemasNode, false);
92 xmlParser.parse(UntrustedXML.createXMLStreamReader(getClass().getResourceAsStream(
93 "/netconf-state.schemas.payload.xml")));
94 compositeNodeSchemas = (ContainerNode) resultHolder.getResult();
99 public void testCreate() throws Exception {
100 final NetconfStateSchemas schemas = NetconfStateSchemas.create(deviceId, compositeNodeSchemas);
102 final Set<QName> availableYangSchemasQNames = schemas.getAvailableYangSchemasQNames();
103 assertEquals(numberOfLegalSchemas, availableYangSchemasQNames.size());
105 assertThat(availableYangSchemasQNames,
106 hasItem(QName.create("urn:TBD:params:xml:ns:yang:network-topology", "2013-07-12", "network-topology")));
111 public void testCreate2() throws Exception {
112 final ContainerNode netconfState = Builders.containerBuilder()
113 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(NetconfState.QNAME))
114 .withChild(compositeNodeSchemas)
116 final ContainerNode data = Builders.containerBuilder()
117 .withNodeIdentifier(new YangInstanceIdentifier
118 .NodeIdentifier(NetconfMessageTransformUtil.NETCONF_DATA_QNAME))
119 .withChild(netconfState)
121 final ContainerNode rpcReply = Builders.containerBuilder()
122 .withNodeIdentifier(new YangInstanceIdentifier
123 .NodeIdentifier(NetconfMessageTransformUtil.NETCONF_RPC_REPLY_QNAME))
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());
132 assertThat(availableYangSchemasQNames,
133 hasItem(QName.create("urn:TBD:params:xml:ns:yang:network-topology", "2013-07-12", "network-topology")));
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());
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());
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());
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);
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);
180 testFuture.get(3, TimeUnit.SECONDS);
181 } catch (final ExecutionException e) {
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);