Merge "BUG 2676 : Introduce API for accessing akka dispatchers"
[controller.git] / opendaylight / netconf / netconf-it / src / test / java / org / opendaylight / controller / netconf / it / AbstractNetconfConfigTest.java
1 /*
2  * Copyright (c) 2013 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.netconf.it;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.mockito.Matchers.any;
13 import static org.mockito.Matchers.anySetOf;
14 import static org.mockito.Matchers.anyString;
15 import static org.mockito.Mockito.doNothing;
16 import static org.mockito.Mockito.mock;
17
18 import io.netty.channel.Channel;
19 import io.netty.channel.ChannelFuture;
20 import io.netty.channel.EventLoopGroup;
21 import io.netty.channel.local.LocalAddress;
22 import io.netty.channel.nio.NioEventLoopGroup;
23 import io.netty.util.HashedWheelTimer;
24 import io.netty.util.concurrent.GlobalEventExecutor;
25 import java.io.ByteArrayInputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.net.InetSocketAddress;
29 import java.net.SocketAddress;
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.Collection;
33 import java.util.Collections;
34 import java.util.HashSet;
35 import java.util.List;
36 import java.util.concurrent.TimeUnit;
37 import org.apache.commons.io.IOUtils;
38 import org.junit.After;
39 import org.junit.Before;
40 import org.opendaylight.controller.config.manager.impl.AbstractConfigTest;
41 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
42 import org.opendaylight.controller.config.spi.ModuleFactory;
43 import org.opendaylight.controller.config.yang.test.impl.DepTestImplModuleFactory;
44 import org.opendaylight.controller.config.yang.test.impl.IdentityTestModuleFactory;
45 import org.opendaylight.controller.config.yang.test.impl.MultipleDependenciesModuleFactory;
46 import org.opendaylight.controller.config.yang.test.impl.NetconfTestImplModuleFactory;
47 import org.opendaylight.controller.config.yang.test.impl.TestImplModuleFactory;
48 import org.opendaylight.controller.netconf.api.NetconfMessage;
49 import org.opendaylight.controller.netconf.api.monitoring.NetconfMonitoringService;
50 import org.opendaylight.controller.netconf.client.NetconfClientDispatcherImpl;
51 import org.opendaylight.controller.netconf.client.SimpleNetconfClientSessionListener;
52 import org.opendaylight.controller.netconf.client.conf.NetconfClientConfiguration;
53 import org.opendaylight.controller.netconf.client.conf.NetconfClientConfigurationBuilder;
54 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.NetconfOperationServiceFactoryImpl;
55 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.YangStoreService;
56 import org.opendaylight.controller.netconf.impl.DefaultCommitNotificationProducer;
57 import org.opendaylight.controller.netconf.impl.NetconfServerDispatcherImpl;
58 import org.opendaylight.controller.netconf.impl.NetconfServerSessionNegotiatorFactory;
59 import org.opendaylight.controller.netconf.impl.SessionIdProvider;
60 import org.opendaylight.controller.netconf.impl.osgi.AggregatedNetconfOperationServiceFactory;
61 import org.opendaylight.controller.netconf.impl.osgi.NetconfMonitoringServiceImpl;
62 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceFactory;
63 import org.opendaylight.controller.netconf.monitoring.osgi.NetconfMonitoringActivator;
64 import org.opendaylight.controller.netconf.monitoring.osgi.NetconfMonitoringOperationService;
65 import org.opendaylight.controller.netconf.notifications.BaseNetconfNotificationListener;
66 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
67 import org.opendaylight.protocol.framework.NeverReconnectStrategy;
68 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.notifications.rev120206.NetconfCapabilityChange;
69 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
70 import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
71 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
72 import org.w3c.dom.Element;
73
74 public abstract class AbstractNetconfConfigTest extends AbstractConfigTest {
75
76     public static final String LOOPBACK_ADDRESS = "127.0.0.1";
77     public static final int SERVER_CONNECTION_TIMEOUT_MILLIS = 5000;
78     private static final int RESOURCE_TIMEOUT_MINUTES = 2;
79
80     static ModuleFactory[] FACTORIES = {new TestImplModuleFactory(),
81                                         new DepTestImplModuleFactory(),
82                                         new NetconfTestImplModuleFactory(),
83                                         new IdentityTestModuleFactory(),
84                                         new MultipleDependenciesModuleFactory() };
85
86     private EventLoopGroup nettyThreadgroup;
87     private HashedWheelTimer hashedWheelTimer;
88
89     private NetconfClientDispatcherImpl clientDispatcher;
90     private Channel serverTcpChannel;
91
92     private NetconfMessage getConfig;
93     private NetconfMessage get;
94
95     /**
96      * @Before in subclasses is called after this method.
97      */
98     @Before
99     public void setUpAbstractNetconfConfigTest() throws Exception {
100         super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(mockedContext, FACTORIES));
101
102         nettyThreadgroup = new NioEventLoopGroup();
103         hashedWheelTimer = new HashedWheelTimer();
104
105         loadMessages();
106
107         setUpTestInitial();
108
109         final AggregatedNetconfOperationServiceFactory factoriesListener = new AggregatedNetconfOperationServiceFactory();
110         final NetconfMonitoringService netconfMonitoringService = getNetconfMonitoringService(factoriesListener);
111         factoriesListener.onAddNetconfOperationServiceFactory(new NetconfOperationServiceFactoryImpl(getYangStore()));
112         factoriesListener.onAddNetconfOperationServiceFactory(new NetconfMonitoringActivator.NetconfMonitoringOperationServiceFactory(new NetconfMonitoringOperationService(netconfMonitoringService)));
113
114         for (final NetconfOperationServiceFactory netconfOperationServiceFactory : getAdditionalServiceFactories(factoriesListener)) {
115             factoriesListener.onAddNetconfOperationServiceFactory(netconfOperationServiceFactory);
116         }
117
118         serverTcpChannel = startNetconfTcpServer(factoriesListener, netconfMonitoringService);
119         clientDispatcher = new NetconfClientDispatcherImpl(getNettyThreadgroup(), getNettyThreadgroup(), getHashedWheelTimer());
120     }
121
122     /**
123      * Called before setUp method is executed, so test classes can set up resources before setUpAbstractNetconfConfigTest method is called.
124      */
125     protected void setUpTestInitial() throws Exception {}
126
127     private void loadMessages() throws Exception {
128         this.getConfig = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/getConfig.xml");
129         this.get = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/get.xml");
130     }
131
132     public NetconfMessage getGetConfig() {
133         return getConfig;
134     }
135
136     public NetconfMessage getGet() {
137         return get;
138     }
139
140     private Channel startNetconfTcpServer(final AggregatedNetconfOperationServiceFactory listener, final NetconfMonitoringService monitoring) throws Exception {
141         final NetconfServerDispatcherImpl dispatch = createDispatcher(listener, monitoring, getNotificationProducer());
142
143         final ChannelFuture s;
144         if(getTcpServerAddress() instanceof LocalAddress) {
145             s = dispatch.createLocalServer(((LocalAddress) getTcpServerAddress()));
146         } else {
147             s = dispatch.createServer(((InetSocketAddress) getTcpServerAddress()));
148         }
149         s.await(RESOURCE_TIMEOUT_MINUTES, TimeUnit.MINUTES);
150         return s.channel();
151     }
152
153     protected DefaultCommitNotificationProducer getNotificationProducer() {
154         final DefaultCommitNotificationProducer notificationProducer = mock(DefaultCommitNotificationProducer.class);
155         doNothing().when(notificationProducer).close();
156         doNothing().when(notificationProducer).sendCommitNotification(anyString(), any(Element.class), anySetOf(String.class));
157         return notificationProducer;
158     }
159
160     protected Iterable<NetconfOperationServiceFactory> getAdditionalServiceFactories(final AggregatedNetconfOperationServiceFactory factoriesListener) throws Exception {
161         return Collections.emptySet();
162     }
163
164     protected NetconfMonitoringService getNetconfMonitoringService(final AggregatedNetconfOperationServiceFactory factoriesListener) throws Exception {
165         return new NetconfMonitoringServiceImpl(factoriesListener);
166     }
167
168     protected abstract SocketAddress getTcpServerAddress();
169
170     public NetconfClientDispatcherImpl getClientDispatcher() {
171         return clientDispatcher;
172     }
173
174     private HardcodedYangStoreService getYangStore() throws IOException {
175         final Collection<InputStream> yangDependencies = getBasicYangs();
176         return new HardcodedYangStoreService(yangDependencies);
177     }
178
179     static Collection<InputStream> getBasicYangs() throws IOException {
180
181         final List<String> paths = Arrays.asList(
182                 "/META-INF/yang/config.yang",
183                 "/META-INF/yang/rpc-context.yang",
184                 "/META-INF/yang/config-test.yang",
185                 "/META-INF/yang/config-test-impl.yang",
186                 "/META-INF/yang/test-types.yang",
187                 "/META-INF/yang/test-groups.yang",
188                 "/META-INF/yang/ietf-inet-types.yang");
189
190         final Collection<InputStream> yangDependencies = new ArrayList<>();
191         final List<String> failedToFind = new ArrayList<>();
192         for (final String path : paths) {
193             final InputStream resourceAsStream = NetconfITTest.class.getResourceAsStream(path);
194             if (resourceAsStream == null) {
195                 failedToFind.add(path);
196             } else {
197                 yangDependencies.add(resourceAsStream);
198             }
199         }
200         assertEquals("Some yang files were not found", Collections.<String>emptyList(), failedToFind);
201         return yangDependencies;
202     }
203
204     protected NetconfServerDispatcherImpl createDispatcher(
205             final AggregatedNetconfOperationServiceFactory factoriesListener, final NetconfMonitoringService sessionMonitoringService,
206             final DefaultCommitNotificationProducer commitNotifier) {
207         final SessionIdProvider idProvider = new SessionIdProvider();
208
209         final NetconfServerSessionNegotiatorFactory serverNegotiatorFactory = new NetconfServerSessionNegotiatorFactory(
210                 hashedWheelTimer, factoriesListener, idProvider, SERVER_CONNECTION_TIMEOUT_MILLIS, commitNotifier, sessionMonitoringService);
211
212         final NetconfServerDispatcherImpl.ServerChannelInitializer serverChannelInitializer = new NetconfServerDispatcherImpl.ServerChannelInitializer(
213                 serverNegotiatorFactory);
214         return new NetconfServerDispatcherImpl(serverChannelInitializer, nettyThreadgroup, nettyThreadgroup);
215     }
216
217     protected HashedWheelTimer getHashedWheelTimer() {
218         return hashedWheelTimer;
219     }
220
221     protected EventLoopGroup getNettyThreadgroup() {
222         return nettyThreadgroup;
223     }
224
225     /**
226      * @After in subclasses is be called before this.
227      */
228     @After
229     public void cleanUpNetconf() throws Exception {
230         serverTcpChannel.close().await(RESOURCE_TIMEOUT_MINUTES, TimeUnit.MINUTES);
231         hashedWheelTimer.stop();
232         nettyThreadgroup.shutdownGracefully().await(RESOURCE_TIMEOUT_MINUTES, TimeUnit.MINUTES);
233     }
234
235     public NetconfClientConfiguration getClientConfiguration(final InetSocketAddress tcpAddress, final int timeout) {
236         final NetconfClientConfigurationBuilder b = NetconfClientConfigurationBuilder.create();
237         b.withAddress(tcpAddress);
238         b.withSessionListener(new SimpleNetconfClientSessionListener());
239         b.withReconnectStrategy(new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, timeout));
240         b.withConnectionTimeoutMillis(timeout);
241         return b.build();
242     }
243
244     public static final class HardcodedYangStoreService extends YangStoreService {
245         public HardcodedYangStoreService(final Collection<? extends InputStream> inputStreams) throws IOException {
246             super(new SchemaContextProvider() {
247                 @Override
248                 public SchemaContext getSchemaContext() {
249                     return getSchema(inputStreams);
250                 }
251             }, new BaseNetconfNotificationListener() {
252                 @Override
253                 public void onCapabilityChanged(final NetconfCapabilityChange capabilityChange) {
254                     // NOOP
255                 }
256             });
257         }
258
259         private static SchemaContext getSchema(final Collection<? extends InputStream> inputStreams) {
260             final ArrayList<InputStream> byteArrayInputStreams = new ArrayList<>();
261             for (final InputStream inputStream : inputStreams) {
262                 assertNotNull(inputStream);
263                 final byte[] content;
264                 try {
265                     content = IOUtils.toByteArray(inputStream);
266                 } catch (IOException e) {
267                     throw new IllegalStateException("Cannot read " + inputStream, e);
268                 }
269                 final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(content);
270                 byteArrayInputStreams.add(byteArrayInputStream);
271             }
272
273             for (final InputStream inputStream : byteArrayInputStreams) {
274                 try {
275                     inputStream.reset();
276                 } catch (final IOException e) {
277                     throw new RuntimeException(e);
278                 }
279             }
280
281             final YangParserImpl yangParser = new YangParserImpl();
282             return yangParser.resolveSchemaContext(new HashSet<>(yangParser.parseYangModelsFromStreamsMapped(byteArrayInputStreams).values()));
283         }
284     }
285 }