Add sal-netconf-connector unit tests
[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
9 package org.opendaylight.netconf.sal.connect.netconf;
10
11 import static org.hamcrest.CoreMatchers.hasItem;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertThat;
14 import static org.mockito.Matchers.any;
15 import static org.mockito.Matchers.eq;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.when;
18 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.NETCONF_GET_QNAME;
19 import static org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil.toPath;
20
21 import com.google.common.util.concurrent.CheckedFuture;
22 import com.google.common.util.concurrent.Futures;
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.Test;
34 import org.mockito.Mock;
35 import org.mockito.MockitoAnnotations;
36 import org.opendaylight.controller.config.util.xml.XmlUtil;
37 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
38 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
39 import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementationNotAvailableException;
40 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
41 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
42 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
43 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
44 import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.BaseSchema;
45 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
46 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.NetconfState;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Schemas;
49 import org.opendaylight.yangtools.yang.common.QName;
50 import org.opendaylight.yangtools.yang.common.RpcError;
51 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
52 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
53 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
54 import org.opendaylight.yangtools.yang.data.impl.codec.xml.XmlUtils;
55 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
56 import org.opendaylight.yangtools.yang.data.impl.schema.transform.ToNormalizedNodeParser;
57 import org.opendaylight.yangtools.yang.data.impl.schema.transform.dom.parser.DomToNormalizedNodeParserFactory;
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.w3c.dom.Document;
62 import org.w3c.dom.Element;
63
64 public class NetconfStateSchemasTest {
65
66     private static final NetconfSessionPreferences CAPS = NetconfSessionPreferences.fromStrings(Collections.singleton("urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring?module=ietf-netconf-monitoring&revision=2010-10-04"));
67     private final RemoteDeviceId deviceId = new RemoteDeviceId("device", new InetSocketAddress(99));
68     private ContainerNode compositeNodeSchemas;
69     @Mock
70     private DOMRpcService rpc;
71
72     @Before
73     public void setUp() throws Exception {
74         MockitoAnnotations.initMocks(this);
75         final SchemaContext schemaContext = BaseSchema.BASE_NETCONF_CTX_WITH_NOTIFICATIONS.getSchemaContext();
76         final DataSchemaNode schemasNode =
77                 ((ContainerSchemaNode) schemaContext
78                         .getDataChildByName(NetconfState.QNAME)).getDataChildByName(Schemas.QNAME);
79         final Document schemasXml = XmlUtil.readXmlToDocument(getClass().getResourceAsStream("/netconf-state.schemas.payload.xml"));
80         final ToNormalizedNodeParser<Element, ContainerNode, ContainerSchemaNode> containerNodeParser = DomToNormalizedNodeParserFactory.getInstance(XmlUtils.DEFAULT_XML_CODEC_PROVIDER, schemaContext, false).getContainerNodeParser();
81         compositeNodeSchemas = containerNodeParser.parse(Collections.singleton(schemasXml.getDocumentElement()), (ContainerSchemaNode) schemasNode);
82
83     }
84
85     @Test
86     public void testCreate() throws Exception {
87         final NetconfStateSchemas schemas = NetconfStateSchemas.create(deviceId, compositeNodeSchemas);
88
89         final Set<QName> availableYangSchemasQNames = schemas.getAvailableYangSchemasQNames();
90         assertEquals(73, availableYangSchemasQNames.size());
91
92         assertThat(availableYangSchemasQNames,
93                 hasItem(QName.create("urn:TBD:params:xml:ns:yang:network-topology", "2013-07-12", "network-topology")));
94     }
95
96     @Test
97     public void testCreate2() throws Exception {
98         final ContainerNode netconfState = Builders.containerBuilder()
99                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(NetconfState.QNAME))
100                 .withChild(compositeNodeSchemas)
101                 .build();
102         final ContainerNode data = Builders.containerBuilder()
103                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(NetconfMessageTransformUtil.NETCONF_DATA_QNAME))
104                 .withChild(netconfState)
105                 .build();
106         final ContainerNode rpcReply = Builders.containerBuilder()
107                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(NetconfMessageTransformUtil.NETCONF_RPC_REPLY_QNAME))
108                 .withChild(data)
109                 .build();
110         when(rpc.invokeRpc(eq(toPath(NETCONF_GET_QNAME)), any())).thenReturn(Futures.immediateCheckedFuture(new DefaultDOMRpcResult(rpcReply)));
111         final NetconfStateSchemas stateSchemas = NetconfStateSchemas.create(rpc, CAPS, deviceId);
112         final Set<QName> availableYangSchemasQNames = stateSchemas.getAvailableYangSchemasQNames();
113         assertEquals(73, availableYangSchemasQNames.size());
114
115         assertThat(availableYangSchemasQNames,
116                 hasItem(QName.create("urn:TBD:params:xml:ns:yang:network-topology", "2013-07-12", "network-topology")));
117     }
118
119     @Test
120     public void testCreateMonitoringNotSupported() throws Exception {
121         final NetconfSessionPreferences caps = NetconfSessionPreferences.fromStrings(Collections.emptySet());
122         final NetconfStateSchemas stateSchemas = NetconfStateSchemas.create(rpc, caps, deviceId);
123         final Set<QName> availableYangSchemasQNames = stateSchemas.getAvailableYangSchemasQNames();
124         Assert.assertTrue(availableYangSchemasQNames.isEmpty());
125     }
126
127     @Test
128     public void testCreateFail() throws Exception {
129         final CheckedFuture<DOMRpcResult, DOMRpcException> resultFuture =
130                 Futures.immediateFailedCheckedFuture(new DOMRpcImplementationNotAvailableException("not available"));
131         when(rpc.invokeRpc(eq(toPath(NETCONF_GET_QNAME)), any())).thenReturn(resultFuture);
132         final NetconfStateSchemas stateSchemas = NetconfStateSchemas.create(rpc, CAPS, deviceId);
133         final Set<QName> availableYangSchemasQNames = stateSchemas.getAvailableYangSchemasQNames();
134         Assert.assertTrue(availableYangSchemasQNames.isEmpty());
135     }
136
137     @Test
138     public void testCreateRpcError() throws Exception {
139         final RpcError rpcError = RpcResultBuilder.newError(RpcError.ErrorType.RPC, "fail", "fail");
140         when(rpc.invokeRpc(eq(toPath(NETCONF_GET_QNAME)), any())).thenReturn(Futures.immediateCheckedFuture(new DefaultDOMRpcResult(rpcError)));
141         final NetconfStateSchemas stateSchemas = NetconfStateSchemas.create(rpc, CAPS, deviceId);
142         final Set<QName> availableYangSchemasQNames = stateSchemas.getAvailableYangSchemasQNames();
143         Assert.assertTrue(availableYangSchemasQNames.isEmpty());
144     }
145
146     @Test(expected = RuntimeException.class)
147     public void testCreateInterrupted() throws Throwable {
148         //NetconfStateSchemas.create calls Thread.currentThread().interrupt(), so it must run in its own thread
149         final Future<?> testFuture = Executors.newSingleThreadExecutor().submit(() -> {
150             final ListenableFuture interruptedFuture = mock(ListenableFuture.class);
151             try {
152                 when(interruptedFuture.get()).thenThrow(new InterruptedException("interrupted"));
153                 final CheckedFuture checkedFuture = Futures.makeChecked(interruptedFuture, ReadFailedException.MAPPER);
154                 when(rpc.invokeRpc(eq(toPath(NETCONF_GET_QNAME)), any())).thenReturn(checkedFuture);
155                 NetconfStateSchemas.create(rpc, CAPS, deviceId);
156             } catch (final InterruptedException | ExecutionException e) {
157                 e.printStackTrace();
158             }
159
160         });
161         try {
162             testFuture.get(3, TimeUnit.SECONDS);
163         } catch (final ExecutionException e) {
164             throw e.getCause();
165         }
166     }
167
168     @Test
169     public void testRemoteYangSchemaEquals() throws Exception {
170         final NetconfStateSchemas.RemoteYangSchema schema1 = new NetconfStateSchemas.RemoteYangSchema(NetconfState.QNAME);
171         final NetconfStateSchemas.RemoteYangSchema schema2 = new NetconfStateSchemas.RemoteYangSchema(NetconfState.QNAME);
172         final NetconfStateSchemas.RemoteYangSchema schema3 = new NetconfStateSchemas.RemoteYangSchema(Schemas.QNAME);
173         Assert.assertEquals(schema1, schema2);
174         Assert.assertEquals(schema2, schema1);
175         Assert.assertNotEquals(schema1, schema3);
176         Assert.assertNotEquals(schema2, schema3);
177
178     }
179 }