1 package org.opendaylight.controller.sal.connector.netconf.test;
3 import static junit.framework.Assert.assertEquals;
4 import static org.junit.Assert.*;
5 import io.netty.channel.ChannelFuture;
6 import io.netty.util.HashedWheelTimer;
8 import java.io.IOException;
9 import java.io.InputStream;
10 import java.lang.management.ManagementFactory;
11 import java.net.InetSocketAddress;
13 import java.util.ArrayList;
14 import java.util.Arrays;
15 import java.util.Collection;
16 import java.util.Date;
17 import java.util.List;
18 import java.util.concurrent.TimeUnit;
20 import javax.net.ssl.SSLContext;
21 import javax.xml.parsers.ParserConfigurationException;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.mockito.Mockito;
26 import org.opendaylight.controller.config.manager.impl.AbstractConfigTest;
27 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
28 import org.opendaylight.controller.config.spi.ModuleFactory;
29 import org.opendaylight.controller.config.yang.store.api.YangStoreException;
30 import org.opendaylight.controller.config.yang.store.impl.HardcodedYangStoreService;
31 import org.opendaylight.controller.config.yang.test.impl.DepTestImplModuleFactory;
32 import org.opendaylight.controller.config.yang.test.impl.NetconfTestImplModuleFactory;
33 import org.opendaylight.controller.config.yang.test.impl.TestImplModuleFactory;
34 import org.opendaylight.controller.netconf.api.NetconfMessage;
35 import org.opendaylight.controller.netconf.client.NetconfClient;
36 import org.opendaylight.controller.netconf.client.NetconfClientDispatcher;
37 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.NetconfOperationServiceFactoryImpl;
38 import org.opendaylight.controller.netconf.impl.DefaultCommitNotificationProducer;
39 import org.opendaylight.controller.netconf.impl.NetconfServerDispatcher;
40 import org.opendaylight.controller.netconf.impl.NetconfServerSessionListenerFactory;
41 import org.opendaylight.controller.netconf.impl.NetconfServerSessionNegotiatorFactory;
42 import org.opendaylight.controller.netconf.impl.SessionIdProvider;
43 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceFactoryListenerImpl;
44 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
45 import org.opendaylight.controller.sal.connect.netconf.InventoryUtils;
46 import org.opendaylight.controller.sal.connect.netconf.NetconfDevice;
47 import org.opendaylight.controller.sal.connect.netconf.NetconfDeviceManager;
48 import org.opendaylight.controller.sal.connect.netconf.NetconfInventoryUtils;
49 import org.opendaylight.controller.sal.core.api.data.DataBrokerService;
50 import org.opendaylight.controller.sal.core.api.data.DataProviderService;
51 import org.opendaylight.controller.sal.core.api.mount.MountProvisionInstance;
52 import org.opendaylight.controller.sal.core.api.mount.MountProvisionService;
53 import org.opendaylight.controller.sal.dom.broker.DataBrokerImpl;
54 import org.opendaylight.controller.sal.dom.broker.MountPointManagerImpl;
55 import org.opendaylight.yangtools.yang.common.QName;
56 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
57 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
58 import org.opendaylight.yangtools.yang.data.api.Node;
59 import org.w3c.dom.Document;
60 import org.xml.sax.SAXException;
62 import com.google.common.base.Optional;
63 import com.google.common.base.Preconditions;
64 import com.google.common.collect.Lists;
66 public class MountTest extends AbstractConfigTest {
68 private static final InetSocketAddress tcpAddress = new InetSocketAddress("127.0.0.1", 12023);
69 private static final InetSocketAddress tlsAddress = new InetSocketAddress("127.0.0.1", 12024);
70 private static final URI NETCONF_MONITORING_NS = URI.create("urn:ietf:params:xml:ns:yang:ietf-netconf-monitoring");
72 private static final QName NETCONF_MONITORING = new QName(NETCONF_MONITORING_NS, new Date(2010,10,04), "ietf-netconf-monitoring");
73 private static final QName NETCONF_MONITORING_STATE = new QName(NETCONF_MONITORING,"netconf-state");
76 private NetconfMessage getConfig, getConfigCandidate, editConfig, closeSession;
77 private DefaultCommitNotificationProducer commitNot;
78 private NetconfServerDispatcher dispatch;
79 private DataProviderService dataBroker;
80 private MountPointManagerImpl mountManager;
81 private NetconfDeviceManager netconfManager;
83 private static QName CONFIG_MODULES = new QName(
84 URI.create("urn:opendaylight:params:xml:ns:yang:controller:config"), null, "modules");
85 private static QName CONFIG_SERVICES = new QName(
86 URI.create("urn:opendaylight:params:xml:ns:yang:controller:config"), null, "modules");
88 private NetconfClient createSession(final InetSocketAddress address, NetconfClientDispatcher dispatcher) throws InterruptedException {
89 final NetconfClient netconfClient = new NetconfClient("test " + address.toString(), address, 5000, dispatcher);
94 public void setUp() throws Exception {
95 super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(getModuleFactories().toArray(
96 new ModuleFactory[0])));
100 NetconfOperationServiceFactoryListenerImpl factoriesListener = new NetconfOperationServiceFactoryListenerImpl();
101 factoriesListener.onAddNetconfOperationServiceFactory(new NetconfOperationServiceFactoryImpl(getYangStore()));
103 commitNot = new DefaultCommitNotificationProducer(ManagementFactory.getPlatformMBeanServer());
105 dispatch = createDispatcher(Optional.<SSLContext> absent(), factoriesListener);
106 ChannelFuture s = dispatch.createServer(tcpAddress);
109 dataBroker = new DataBrokerImpl();
110 mountManager = new MountPointManagerImpl();
111 mountManager.setDataBroker(dataBroker);
112 netconfManager = new NetconfDeviceManager();
114 netconfManager.setMountService(mountManager);
115 netconfManager.setDataService(dataBroker);
116 netconfManager.start();
118 try (NetconfClient netconfClient = createSession(tcpAddress, netconfManager.getDispatcher())) {
119 // send edit_config.xml
120 final Document rpcReply = netconfClient.sendMessage(this.editConfig).getDocument();
121 assertNotNull(rpcReply);
126 protected List<ModuleFactory> getModuleFactories() {
127 return getModuleFactoriesS();
130 static List<ModuleFactory> getModuleFactoriesS() {
131 return Lists.newArrayList(new TestImplModuleFactory(), new DepTestImplModuleFactory(),
132 new NetconfTestImplModuleFactory());
135 private void loadMessages() throws IOException, SAXException, ParserConfigurationException {
136 this.editConfig = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/edit_config.xml");
137 this.getConfig = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/getConfig.xml");
138 this.getConfigCandidate = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/getConfig_candidate.xml");
139 this.closeSession = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/closeSession.xml");
142 private NetconfServerDispatcher createDispatcher(Optional<SSLContext> sslC,
143 NetconfOperationServiceFactoryListenerImpl factoriesListener) {
144 SessionIdProvider idProvider = new SessionIdProvider();
145 NetconfServerSessionNegotiatorFactory serverNegotiatorFactory = new NetconfServerSessionNegotiatorFactory(
146 new HashedWheelTimer(5000, TimeUnit.MILLISECONDS), factoriesListener, idProvider);
148 NetconfServerSessionListenerFactory listenerFactory = new NetconfServerSessionListenerFactory(
149 factoriesListener, commitNot, idProvider);
151 return new NetconfServerDispatcher(sslC, serverNegotiatorFactory, listenerFactory);
154 private HardcodedYangStoreService getYangStore() throws YangStoreException, IOException {
155 final Collection<InputStream> yangDependencies = getBasicYangs();
156 return new HardcodedYangStoreService(yangDependencies);
159 private Collection<InputStream> getBasicYangs() throws IOException {
160 List<String> paths = Arrays.asList("/META-INF/yang/config.yang", "/META-INF/yang/rpc-context.yang",
161 "/META-INF/yang/config-test.yang", "/META-INF/yang/config-test-impl.yang",
162 "/META-INF/yang/ietf-inet-types.yang");
163 final Collection<InputStream> yangDependencies = new ArrayList<>();
164 for (String path : paths) {
165 final InputStream is = Preconditions
166 .checkNotNull(getClass().getResourceAsStream(path), path + " not found");
167 yangDependencies.add(is);
169 return yangDependencies;
174 // MountProvisionInstance mount =
175 // Mockito.mock(MountProvisionInstance.class);
176 InstanceIdentifier path = InstanceIdentifier.builder(InventoryUtils.INVENTORY_PATH)
177 .node(InventoryUtils.INVENTORY_NODE).toInstance();
178 netconfManager.netconfNodeAdded(path, tcpAddress);
179 InstanceIdentifier mountPointPath = path;
180 MountProvisionInstance mountPoint = mountManager.getMountPoint(mountPointPath);
182 CompositeNode data = mountPoint.readOperationalData(InstanceIdentifier.builder().node(CONFIG_MODULES)
185 assertEquals(CONFIG_MODULES, data.getNodeType());
187 CompositeNode data2 = mountPoint.readOperationalData(InstanceIdentifier.builder().toInstance());
188 assertNotNull(data2);
190 InstanceIdentifier fullPath = InstanceIdentifier.builder(mountPointPath).node(CONFIG_MODULES).toInstance();
192 CompositeNode data3 = dataBroker.readOperationalData(fullPath);
193 assertNotNull(data3);
194 assertEquals(CONFIG_MODULES, data.getNodeType());