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.client.mdsal.impl;
10 import static org.hamcrest.CoreMatchers.hasItem;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertSame;
14 import static org.junit.Assert.assertThrows;
15 import static org.mockito.ArgumentMatchers.any;
16 import static org.mockito.ArgumentMatchers.eq;
17 import static org.mockito.Mockito.doReturn;
19 import com.google.common.util.concurrent.Futures;
20 import java.net.InetSocketAddress;
21 import java.util.List;
23 import java.util.concurrent.ExecutionException;
24 import java.util.stream.Collectors;
25 import org.junit.BeforeClass;
26 import org.junit.Ignore;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.mockito.Mock;
30 import org.mockito.junit.MockitoJUnitRunner;
31 import org.opendaylight.mdsal.dom.api.DOMRpcImplementationNotAvailableException;
32 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
33 import org.opendaylight.netconf.client.mdsal.AbstractBaseSchemasTest;
34 import org.opendaylight.netconf.client.mdsal.api.NetconfDeviceSchemas;
35 import org.opendaylight.netconf.client.mdsal.api.NetconfRpcService;
36 import org.opendaylight.netconf.client.mdsal.api.NetconfSessionPreferences;
37 import org.opendaylight.netconf.client.mdsal.api.ProvidedSources;
38 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceId;
39 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.base._1._0.rev110601.Get;
40 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.NetconfState;
41 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Schemas;
42 import org.opendaylight.yangtools.util.xml.UntrustedXML;
43 import org.opendaylight.yangtools.yang.common.ErrorTag;
44 import org.opendaylight.yangtools.yang.common.ErrorType;
45 import org.opendaylight.yangtools.yang.common.QName;
46 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
47 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
48 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
49 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
50 import org.opendaylight.yangtools.yang.data.codec.xml.XmlParserStream;
51 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
52 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizationResultHolder;
53 import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes;
54 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
55 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
57 @RunWith(MockitoJUnitRunner.StrictStubs.class)
58 public class NetconfStateSchemasTest extends AbstractBaseSchemasTest {
59 private static final NetconfSessionPreferences CAPS = NetconfSessionPreferences.fromStrings(Set.of(
60 "urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring?module=ietf-netconf-monitoring&revision=2010-10-04"));
61 private static final RemoteDeviceId DEVICE_ID = new RemoteDeviceId("device", new InetSocketAddress(99));
63 private static EffectiveModelContext MODEL_CONTEXT = BASE_SCHEMAS.baseSchemaForCapabilities(CAPS).modelContext();
64 private static ContainerNode SCHEMAS_PAYLOAD;
67 private NetconfRpcService rpc;
70 public static void setUp() throws Exception {
71 final var resultHolder = new NormalizationResultHolder();
72 final var writer = ImmutableNormalizedNodeStreamWriter.from(resultHolder);
73 final var xmlParser = XmlParserStream.create(writer,
74 SchemaInferenceStack.ofDataTreePath(MODEL_CONTEXT, NetconfState.QNAME, Schemas.QNAME).toInference(), false);
76 xmlParser.parse(UntrustedXML.createXMLStreamReader(NetconfStateSchemasTest.class.getResourceAsStream(
77 "/netconf-state.schemas.payload.xml")));
78 SCHEMAS_PAYLOAD = (ContainerNode) resultHolder.getResult().data();
82 public void testTesolveMonitoringSources() throws Exception {
83 final var providedSchemas = NetconfStateSchemasResolverImpl.resolveMonitoringSources(DEVICE_ID, rpc,
86 final var availableYangSchemasQNames = availableYangSchemasQNames(providedSchemas);
88 assertEquals(69, availableYangSchemasQNames.size());
90 assertThat(availableYangSchemasQNames,
91 hasItem(QName.create("urn:TBD:params:xml:ns:yang:network-topology", "2013-07-12", "network-topology")));
95 @Ignore("We cannot handle a container as data -- only anyxml")
96 public void testCreate2() throws Exception {
97 final ContainerNode rpcReply = ImmutableNodes.newContainerBuilder()
98 .withNodeIdentifier(new NodeIdentifier(NetconfMessageTransformUtil.NETCONF_RPC_REPLY_QNAME))
99 .withChild(ImmutableNodes.newContainerBuilder()
100 .withNodeIdentifier(NetconfMessageTransformUtil.NETCONF_DATA_NODEID)
101 .withChild(ImmutableNodes.newContainerBuilder()
102 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(NetconfState.QNAME))
103 .withChild(SCHEMAS_PAYLOAD)
107 doReturn(Futures.immediateFuture(new DefaultDOMRpcResult(rpcReply))).when(rpc)
108 .invokeNetconf(eq(Get.QNAME), any());
109 final var stateSchemas = assertSchemas(CAPS);
110 final var availableYangSchemasQNames = availableYangSchemasQNames(stateSchemas.providedSources());
111 assertEquals(69, availableYangSchemasQNames.size());
113 assertThat(availableYangSchemasQNames,
114 hasItem(QName.create("urn:TBD:params:xml:ns:yang:network-topology", "2013-07-12", "network-topology")));
118 public void testCreateMonitoringNotSupported() throws Exception {
119 final var stateSchemas = assertSchemas(NetconfSessionPreferences.fromStrings(Set.of()));
120 assertEquals(Set.of(), availableYangSchemasQNames(stateSchemas.providedSources()));
124 public void testCreateFail() throws Exception {
125 final var domEx = new DOMRpcImplementationNotAvailableException("not available");
126 doReturn(Futures.immediateFailedFuture(domEx)).when(rpc).invokeNetconf(eq(Get.QNAME), any());
127 assertSame(domEx, assertSchemasFailure());
131 public void testCreateRpcError() throws Exception {
132 final var rpcError = RpcResultBuilder.newError(ErrorType.RPC, new ErrorTag("fail"), "fail");
133 doReturn(Futures.immediateFuture(new DefaultDOMRpcResult(rpcError))).when(rpc)
134 .invokeNetconf(eq(Get.QNAME), any());
136 final var stateSchemas = assertSchemas(CAPS);
137 assertEquals(List.of(), stateSchemas.providedSources());
140 private NetconfDeviceSchemas assertSchemas(final NetconfSessionPreferences prefs) {
142 return Futures.getDone(new NetconfStateSchemasResolverImpl().resolve(DEVICE_ID, prefs, rpc, MODEL_CONTEXT));
143 } catch (ExecutionException e) {
144 throw new AssertionError(e);
148 private Throwable assertSchemasFailure() {
149 final var future = new NetconfStateSchemasResolverImpl().resolve(DEVICE_ID, CAPS, rpc, MODEL_CONTEXT);
150 return assertThrows(ExecutionException.class, () -> Futures.getDone(future)).getCause();
153 private static Set<QName> availableYangSchemasQNames(final List<ProvidedSources<?>> providedSources) {
154 return providedSources.stream()
155 .flatMap(sources -> sources.sources().stream())
156 .collect(Collectors.toSet());