92d31ab734f630c5e1d517deb5124c364bfaa9dc
[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     private final int NUMBER_OF_SCHEMAS = 73;
70     private final int NUMBER_OF_LEGAL_SCHEMAS = NUMBER_OF_SCHEMAS - 3;
71
72     @Mock
73     private DOMRpcService rpc;
74
75     @Before
76     public void setUp() throws Exception {
77         MockitoAnnotations.initMocks(this);
78         final SchemaContext schemaContext = BaseSchema.BASE_NETCONF_CTX_WITH_NOTIFICATIONS.getSchemaContext();
79         final DataSchemaNode schemasNode =
80                 ((ContainerSchemaNode) schemaContext
81                         .getDataChildByName(NetconfState.QNAME)).getDataChildByName(Schemas.QNAME);
82         final Document schemasXml = XmlUtil.readXmlToDocument(getClass().getResourceAsStream("/netconf-state.schemas.payload.xml"));
83         final ToNormalizedNodeParser<Element, ContainerNode, ContainerSchemaNode> containerNodeParser = DomToNormalizedNodeParserFactory.getInstance(XmlUtils.DEFAULT_XML_CODEC_PROVIDER, schemaContext, false).getContainerNodeParser();
84         compositeNodeSchemas = containerNodeParser.parse(Collections.singleton(schemasXml.getDocumentElement()), (ContainerSchemaNode) schemasNode);
85
86     }
87
88     @Test
89     public void testCreate() throws Exception {
90         final NetconfStateSchemas schemas = NetconfStateSchemas.create(deviceId, compositeNodeSchemas);
91
92         final Set<QName> availableYangSchemasQNames = schemas.getAvailableYangSchemasQNames();
93         assertEquals(NUMBER_OF_LEGAL_SCHEMAS, availableYangSchemasQNames.size());
94
95         assertThat(availableYangSchemasQNames,
96                 hasItem(QName.create("urn:TBD:params:xml:ns:yang:network-topology", "2013-07-12", "network-topology")));
97     }
98
99     @Test
100     public void testCreate2() throws Exception {
101         final ContainerNode netconfState = Builders.containerBuilder()
102                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(NetconfState.QNAME))
103                 .withChild(compositeNodeSchemas)
104                 .build();
105         final ContainerNode data = Builders.containerBuilder()
106                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(NetconfMessageTransformUtil.NETCONF_DATA_QNAME))
107                 .withChild(netconfState)
108                 .build();
109         final ContainerNode rpcReply = Builders.containerBuilder()
110                 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(NetconfMessageTransformUtil.NETCONF_RPC_REPLY_QNAME))
111                 .withChild(data)
112                 .build();
113         when(rpc.invokeRpc(eq(toPath(NETCONF_GET_QNAME)), any())).thenReturn(Futures.immediateCheckedFuture(new DefaultDOMRpcResult(rpcReply)));
114         final NetconfStateSchemas stateSchemas = NetconfStateSchemas.create(rpc, CAPS, deviceId);
115         final Set<QName> availableYangSchemasQNames = stateSchemas.getAvailableYangSchemasQNames();
116         assertEquals(NUMBER_OF_LEGAL_SCHEMAS, availableYangSchemasQNames.size());
117
118         assertThat(availableYangSchemasQNames,
119                 hasItem(QName.create("urn:TBD:params:xml:ns:yang:network-topology", "2013-07-12", "network-topology")));
120     }
121
122     @Test
123     public void testCreateMonitoringNotSupported() throws Exception {
124         final NetconfSessionPreferences caps = NetconfSessionPreferences.fromStrings(Collections.emptySet());
125         final NetconfStateSchemas stateSchemas = NetconfStateSchemas.create(rpc, caps, deviceId);
126         final Set<QName> availableYangSchemasQNames = stateSchemas.getAvailableYangSchemasQNames();
127         Assert.assertTrue(availableYangSchemasQNames.isEmpty());
128     }
129
130     @Test
131     public void testCreateFail() throws Exception {
132         final CheckedFuture<DOMRpcResult, DOMRpcException> resultFuture =
133                 Futures.immediateFailedCheckedFuture(new DOMRpcImplementationNotAvailableException("not available"));
134         when(rpc.invokeRpc(eq(toPath(NETCONF_GET_QNAME)), any())).thenReturn(resultFuture);
135         final NetconfStateSchemas stateSchemas = NetconfStateSchemas.create(rpc, CAPS, deviceId);
136         final Set<QName> availableYangSchemasQNames = stateSchemas.getAvailableYangSchemasQNames();
137         Assert.assertTrue(availableYangSchemasQNames.isEmpty());
138     }
139
140     @Test
141     public void testCreateRpcError() throws Exception {
142         final RpcError rpcError = RpcResultBuilder.newError(RpcError.ErrorType.RPC, "fail", "fail");
143         when(rpc.invokeRpc(eq(toPath(NETCONF_GET_QNAME)), any())).thenReturn(Futures.immediateCheckedFuture(new DefaultDOMRpcResult(rpcError)));
144         final NetconfStateSchemas stateSchemas = NetconfStateSchemas.create(rpc, CAPS, deviceId);
145         final Set<QName> availableYangSchemasQNames = stateSchemas.getAvailableYangSchemasQNames();
146         Assert.assertTrue(availableYangSchemasQNames.isEmpty());
147     }
148
149     @Test(expected = RuntimeException.class)
150     public void testCreateInterrupted() throws Throwable {
151         //NetconfStateSchemas.create calls Thread.currentThread().interrupt(), so it must run in its own thread
152         final Future<?> testFuture = Executors.newSingleThreadExecutor().submit(() -> {
153             final ListenableFuture interruptedFuture = mock(ListenableFuture.class);
154             try {
155                 when(interruptedFuture.get()).thenThrow(new InterruptedException("interrupted"));
156                 final CheckedFuture checkedFuture = Futures.makeChecked(interruptedFuture, ReadFailedException.MAPPER);
157                 when(rpc.invokeRpc(eq(toPath(NETCONF_GET_QNAME)), any())).thenReturn(checkedFuture);
158                 NetconfStateSchemas.create(rpc, CAPS, deviceId);
159             } catch (final InterruptedException | ExecutionException e) {
160                 e.printStackTrace();
161             }
162
163         });
164         try {
165             testFuture.get(3, TimeUnit.SECONDS);
166         } catch (final ExecutionException e) {
167             throw e.getCause();
168         }
169     }
170
171     @Test
172     public void testRemoteYangSchemaEquals() throws Exception {
173         final NetconfStateSchemas.RemoteYangSchema schema1 = new NetconfStateSchemas.RemoteYangSchema(NetconfState.QNAME);
174         final NetconfStateSchemas.RemoteYangSchema schema2 = new NetconfStateSchemas.RemoteYangSchema(NetconfState.QNAME);
175         final NetconfStateSchemas.RemoteYangSchema schema3 = new NetconfStateSchemas.RemoteYangSchema(Schemas.QNAME);
176         Assert.assertEquals(schema1, schema2);
177         Assert.assertEquals(schema2, schema1);
178         Assert.assertNotEquals(schema1, schema3);
179         Assert.assertNotEquals(schema2, schema3);
180
181     }
182 }