Deprecated Rpcs and RpcErrors helper classes
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / test / java / org / opendaylight / controller / sal / connect / netconf / NetconfDeviceTest.java
1 /*
2  * Copyright (c) 2014 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.controller.sal.connect.netconf;
9
10 import static org.mockito.Matchers.any;
11 import static org.mockito.Matchers.anyString;
12 import static org.mockito.Mockito.doNothing;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.timeout;
15 import static org.mockito.Mockito.verify;
16
17 import java.io.InputStream;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.Collections;
21 import java.util.List;
22 import java.util.Set;
23 import java.util.concurrent.ExecutorService;
24 import java.util.concurrent.Executors;
25
26 import org.junit.Test;
27 import org.mockito.Mockito;
28 import org.opendaylight.controller.netconf.api.NetconfMessage;
29 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
30 import org.opendaylight.controller.sal.connect.api.MessageTransformer;
31 import org.opendaylight.controller.sal.connect.api.RemoteDeviceCommunicator;
32 import org.opendaylight.controller.sal.connect.api.RemoteDeviceHandler;
33 import org.opendaylight.controller.sal.connect.api.SchemaContextProviderFactory;
34 import org.opendaylight.controller.sal.connect.api.SchemaSourceProviderFactory;
35 import org.opendaylight.controller.sal.connect.netconf.listener.NetconfSessionCapabilities;
36 import org.opendaylight.controller.sal.connect.netconf.util.NetconfMessageTransformUtil;
37 import org.opendaylight.controller.sal.connect.util.RemoteDeviceId;
38 import org.opendaylight.controller.sal.core.api.RpcImplementation;
39 import org.opendaylight.yangtools.yang.common.QName;
40 import org.opendaylight.yangtools.yang.common.RpcResult;
41 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
42 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
43 import org.opendaylight.yangtools.yang.model.api.Module;
44 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
45 import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
46 import org.opendaylight.yangtools.yang.model.util.repo.SchemaSourceProvider;
47 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
48
49 import com.google.common.base.Optional;
50 import com.google.common.collect.Lists;
51 import com.google.common.util.concurrent.Futures;
52
53 public class NetconfDeviceTest {
54
55     private static final NetconfMessage netconfMessage;
56     private static final CompositeNode compositeNode;
57
58     static {
59         try {
60             netconfMessage = mockClass(NetconfMessage.class);
61             compositeNode = mockClass(CompositeNode.class);
62         } catch (final Exception e) {
63             throw new RuntimeException(e);
64         }
65     }
66
67     private static final  RpcResult<NetconfMessage> rpcResult = RpcResultBuilder.success(netconfMessage).build();
68     private static final  RpcResult<CompositeNode> rpcResultC = RpcResultBuilder.success(compositeNode).build();
69
70     public static final String TEST_NAMESPACE = "test:namespace";
71     public static final String TEST_MODULE = "test-module";
72     public static final String TEST_REVISION = "2013-07-22";
73
74     @Test
75     public void testNetconfDeviceWithoutMonitoring() throws Exception {
76         final RemoteDeviceHandler<NetconfSessionCapabilities> facade = getFacade();
77         final RemoteDeviceCommunicator<NetconfMessage> listener = getListener();
78
79         final NetconfDevice device = new NetconfDevice(getId(), facade, getExecutor(), getMessageTransformer(), getSchemaContextProviderFactory(), getSourceProviderFactory());
80         device.onRemoteSessionUp(getSessionCaps(false, Collections.<String>emptyList()), listener);
81
82         Mockito.verify(facade, Mockito.timeout(5000)).onDeviceDisconnected();
83     }
84
85     @Test
86     public void testNetconfDeviceReconnect() throws Exception {
87         final RemoteDeviceHandler<NetconfSessionCapabilities> facade = getFacade();
88         final RemoteDeviceCommunicator<NetconfMessage> listener = getListener();
89
90         final SchemaContextProviderFactory schemaContextProviderFactory = getSchemaContextProviderFactory();
91         final SchemaSourceProviderFactory<InputStream> sourceProviderFactory = getSourceProviderFactory();
92         final MessageTransformer<NetconfMessage> messageTransformer = getMessageTransformer();
93
94         final NetconfDevice device = new NetconfDevice(getId(), facade, getExecutor(), messageTransformer, schemaContextProviderFactory, sourceProviderFactory);
95         final NetconfSessionCapabilities sessionCaps = getSessionCaps(true,
96                 Lists.newArrayList(TEST_NAMESPACE + "?module=" + TEST_MODULE + "&amp;revision=" + TEST_REVISION));
97         device.onRemoteSessionUp(sessionCaps, listener);
98
99         verify(sourceProviderFactory, timeout(5000)).createSourceProvider(any(RpcImplementation.class));
100         verify(schemaContextProviderFactory, timeout(5000)).createContextProvider(any(Collection.class), any(SchemaSourceProvider.class));
101         verify(messageTransformer, timeout(5000)).onGlobalContextUpdated(any(SchemaContext.class));
102         verify(facade, timeout(5000)).onDeviceConnected(any(SchemaContextProvider.class), any(NetconfSessionCapabilities.class), any(RpcImplementation.class));
103
104         device.onRemoteSessionDown();
105         verify(facade, timeout(5000)).onDeviceDisconnected();
106
107         device.onRemoteSessionUp(sessionCaps, listener);
108
109         verify(sourceProviderFactory, timeout(5000).times(2)).createSourceProvider(any(RpcImplementation.class));
110         verify(schemaContextProviderFactory, timeout(5000).times(2)).createContextProvider(any(Collection.class), any(SchemaSourceProvider.class));
111         verify(messageTransformer, timeout(5000).times(2)).onGlobalContextUpdated(any(SchemaContext.class));
112         verify(facade, timeout(5000).times(2)).onDeviceConnected(any(SchemaContextProvider.class), any(NetconfSessionCapabilities.class), any(RpcImplementation.class));
113     }
114
115     private SchemaContextProviderFactory getSchemaContextProviderFactory() {
116         final SchemaContextProviderFactory schemaContextProviderFactory = mockClass(SchemaContextProviderFactory.class);
117         doReturn(new SchemaContextProvider() {
118             @Override
119             public SchemaContext getSchemaContext() {
120                 return getSchema();
121             }
122         }).when(schemaContextProviderFactory).createContextProvider(any(Collection.class), any(SchemaSourceProvider.class));
123         return schemaContextProviderFactory;
124     }
125
126     public static SchemaContext getSchema() {
127         final YangParserImpl parser = new YangParserImpl();
128         final List<InputStream> modelsToParse = Lists.newArrayList(
129                 NetconfDeviceTest.class.getResourceAsStream("/schemas/test-module.yang")
130         );
131         final Set<Module> models = parser.parseYangModelsFromStreams(modelsToParse);
132         return parser.resolveSchemaContext(models);
133     }
134
135     private RemoteDeviceHandler<NetconfSessionCapabilities> getFacade() throws Exception {
136         final RemoteDeviceHandler<NetconfSessionCapabilities> remoteDeviceHandler = mockCloseableClass(RemoteDeviceHandler.class);
137         doNothing().when(remoteDeviceHandler).onDeviceConnected(any(SchemaContextProvider.class), any(NetconfSessionCapabilities.class), any(RpcImplementation.class));
138         doNothing().when(remoteDeviceHandler).onDeviceDisconnected();
139         return remoteDeviceHandler;
140     }
141
142     private <T extends AutoCloseable> T mockCloseableClass(final Class<T> remoteDeviceHandlerClass) throws Exception {
143         final T mock = mockClass(remoteDeviceHandlerClass);
144         doNothing().when(mock).close();
145         return mock;
146     }
147
148     public SchemaSourceProviderFactory<InputStream> getSourceProviderFactory() {
149         final SchemaSourceProviderFactory<InputStream> mock = mockClass(SchemaSourceProviderFactory.class);
150
151         final SchemaSourceProvider<InputStream> schemaSourceProvider = mockClass(SchemaSourceProvider.class);
152         doReturn(Optional.<String>absent()).when(schemaSourceProvider).getSchemaSource(anyString(), any(Optional.class));
153
154         doReturn(schemaSourceProvider).when(mock).createSourceProvider(any(RpcImplementation.class));
155         return mock;
156     }
157
158     private static <T> T mockClass(final Class<T> remoteDeviceHandlerClass) {
159         final T mock = Mockito.mock(remoteDeviceHandlerClass);
160         Mockito.doReturn(remoteDeviceHandlerClass.getSimpleName()).when(mock).toString();
161         return mock;
162     }
163
164     public RemoteDeviceId getId() {
165         return new RemoteDeviceId("test-D");
166     }
167
168     public ExecutorService getExecutor() {
169         return Executors.newSingleThreadExecutor();
170     }
171
172     public MessageTransformer<NetconfMessage> getMessageTransformer() throws Exception {
173         final MessageTransformer<NetconfMessage> messageTransformer = mockClass(MessageTransformer.class);
174         doReturn(netconfMessage).when(messageTransformer).toRpcRequest(any(QName.class), any(CompositeNode.class));
175         doReturn(rpcResultC).when(messageTransformer).toRpcResult(any(NetconfMessage.class), any(QName.class));
176         doNothing().when(messageTransformer).onGlobalContextUpdated(any(SchemaContext.class));
177         return messageTransformer;
178     }
179
180     public NetconfSessionCapabilities getSessionCaps(final boolean addMonitor, final Collection<String> additionalCapabilities) {
181         final ArrayList<String> capabilities = Lists.newArrayList(
182                 XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_0,
183                 XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_1);
184
185         if(addMonitor) {
186             capabilities.add(NetconfMessageTransformUtil.IETF_NETCONF_MONITORING.getNamespace().toString());
187         }
188
189         capabilities.addAll(additionalCapabilities);
190
191         return NetconfSessionCapabilities.fromStrings(
192                 capabilities);
193     }
194
195     public RemoteDeviceCommunicator<NetconfMessage> getListener() throws Exception {
196         final RemoteDeviceCommunicator<NetconfMessage> remoteDeviceCommunicator = mockCloseableClass(RemoteDeviceCommunicator.class);
197         doReturn(Futures.immediateFuture(rpcResult)).when(remoteDeviceCommunicator).sendRequest(any(NetconfMessage.class), any(QName.class));
198         return remoteDeviceCommunicator;
199     }
200 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.