Updated sal-netconf-connector mountpoint integration
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / test / java / org / opendaylight / controller / sal / connector / netconf / test / MountTest.java
1 package org.opendaylight.controller.sal.connector.netconf.test;
2
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;
7
8 import java.io.IOException;
9 import java.io.InputStream;
10 import java.lang.management.ManagementFactory;
11 import java.net.InetSocketAddress;
12 import java.net.URI;
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;
19
20 import javax.net.ssl.SSLContext;
21 import javax.xml.parsers.ParserConfigurationException;
22
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;
61
62 import com.google.common.base.Optional;
63 import com.google.common.base.Preconditions;
64 import com.google.common.collect.Lists;
65
66 public class MountTest extends AbstractConfigTest {
67
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");
71     
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");
74     
75
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;
82
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");
87
88     private NetconfClient createSession(final InetSocketAddress address, NetconfClientDispatcher dispatcher) throws InterruptedException {
89         final NetconfClient netconfClient = new NetconfClient("test " + address.toString(), address, 5000, dispatcher);
90         return netconfClient;
91     }
92     
93     @Before
94     public void setUp() throws Exception {
95         super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(getModuleFactories().toArray(
96                 new ModuleFactory[0])));
97
98         loadMessages();
99
100         NetconfOperationServiceFactoryListenerImpl factoriesListener = new NetconfOperationServiceFactoryListenerImpl();
101         factoriesListener.onAddNetconfOperationServiceFactory(new NetconfOperationServiceFactoryImpl(getYangStore()));
102
103         commitNot = new DefaultCommitNotificationProducer(ManagementFactory.getPlatformMBeanServer());
104
105         dispatch = createDispatcher(Optional.<SSLContext> absent(), factoriesListener);
106         ChannelFuture s = dispatch.createServer(tcpAddress);
107         s.await();
108
109         dataBroker = new DataBrokerImpl();
110         mountManager = new MountPointManagerImpl();
111         mountManager.setDataBroker(dataBroker);
112         netconfManager = new NetconfDeviceManager();
113
114         netconfManager.setMountService(mountManager);
115         netconfManager.setDataService(dataBroker);
116         netconfManager.start();
117
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);
122         }
123     }
124
125
126     protected List<ModuleFactory> getModuleFactories() {
127         return getModuleFactoriesS();
128     }
129
130     static List<ModuleFactory> getModuleFactoriesS() {
131         return Lists.newArrayList(new TestImplModuleFactory(), new DepTestImplModuleFactory(),
132                 new NetconfTestImplModuleFactory());
133     }
134
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");
140     }
141
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);
147
148         NetconfServerSessionListenerFactory listenerFactory = new NetconfServerSessionListenerFactory(
149                 factoriesListener, commitNot, idProvider);
150
151         return new NetconfServerDispatcher(sslC, serverNegotiatorFactory, listenerFactory);
152     }
153
154     private HardcodedYangStoreService getYangStore() throws YangStoreException, IOException {
155         final Collection<InputStream> yangDependencies = getBasicYangs();
156         return new HardcodedYangStoreService(yangDependencies);
157     }
158
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);
168         }
169         return yangDependencies;
170     }
171
172     @Test
173     public void test() {
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);
181
182         CompositeNode data = mountPoint.readOperationalData(InstanceIdentifier.builder().node(CONFIG_MODULES)
183                 .toInstance());
184         assertNotNull(data);
185         assertEquals(CONFIG_MODULES, data.getNodeType());
186
187         CompositeNode data2 = mountPoint.readOperationalData(InstanceIdentifier.builder().toInstance());
188         assertNotNull(data2);
189
190         InstanceIdentifier fullPath = InstanceIdentifier.builder(mountPointPath).node(CONFIG_MODULES).toInstance();
191
192         CompositeNode data3 = dataBroker.readOperationalData(fullPath);
193         assertNotNull(data3);
194         assertEquals(CONFIG_MODULES, data.getNodeType());
195     }
196
197 }