2 * ============LICENSE_START=======================================================
3 * ONAP : ccsdk features
4 * ================================================================================
5 * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.test;
24 import static org.junit.Assert.assertEquals;
25 import java.util.Arrays;
26 import java.util.Collection;
27 import java.util.Optional;
28 import org.junit.Assert;
29 import org.junit.Test;
30 import org.mockito.ArgumentCaptor;
31 import org.mockito.Mockito;
32 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfNotifications;
33 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.impl.access.NetconfAccessorImpl;
34 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.impl.access.NetconfCommunicatorManager;
35 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.impl.access.binding.NetconfBindingNotificationsImpl;
36 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.impl.access.dom.DomContext;
37 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.impl.access.dom.NetconfDomAccessorImpl;
38 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.test.example.TestNetconfHelper;
39 import org.opendaylight.mdsal.binding.api.DataBroker;
40 import org.opendaylight.mdsal.binding.api.MountPoint;
41 import org.opendaylight.mdsal.binding.api.RpcConsumerRegistry;
42 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
43 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
44 import org.opendaylight.mdsal.dom.api.DOMNotificationListener;
45 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.CreateSubscriptionInput;
47 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.NotificationsService;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.StreamNameType;
49 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.Stream;
50 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.StreamBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
52 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
53 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
54 import org.opendaylight.yangtools.concepts.ListenerRegistration;
55 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
56 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
58 public class TestNetconfAccessorImpl extends Mockito {
61 public void testConstruct() {
62 NetconfCommunicatorManager netconfCommunicatorManager = mock(NetconfCommunicatorManager.class);
63 DomContext domContext = mock(DomContext.class);
64 String nodeIdString = "Test";
65 String capabilityStringForNetworkElement = "network-element";
66 NodeId nodeId = new NodeId(nodeIdString);
67 NetconfNode testNode = TestNetconfHelper.getTestNode(nodeId, capabilityStringForNetworkElement)
68 .augmentation(NetconfNode.class);
70 NetconfAccessorImpl netconfAccessor =
71 new NetconfAccessorImpl(nodeId, testNode, netconfCommunicatorManager, domContext);
73 Assert.assertNotNull(netconfAccessor);
75 NetconfAccessorImpl netconfAcessor2 = new NetconfAccessorImpl(netconfAccessor);
77 Assert.assertNotNull(netconfAcessor2);
81 public void testBindingNotifications() {
82 NetconfAccessorImpl netconfAccessor = TestNetconfHelper.getNetconfAcessorImpl();
84 DataBroker dataBroker = mock(DataBroker.class);
86 NotificationsService notificationService = mock(NotificationsService.class);
88 RpcConsumerRegistry rpcComerRegistry = mock(RpcConsumerRegistry.class);
89 when(rpcComerRegistry.getRpcService(NotificationsService.class)).thenReturn(notificationService);
91 MountPoint mountPoint = mock(MountPoint.class);
92 when(mountPoint.getService(RpcConsumerRegistry.class)).thenReturn(Optional.of(rpcComerRegistry));
95 NetconfBindingNotificationsImpl test =
96 new NetconfBindingNotificationsImpl(netconfAccessor, dataBroker, mountPoint);
98 String streamName = "NETCONF";
99 test.registerNotificationsStream(streamName);
101 //Capture parameters and assert them
102 ArgumentCaptor<CreateSubscriptionInput> captor = ArgumentCaptor.forClass(CreateSubscriptionInput.class);
103 verify(notificationService).createSubscription(captor.capture());
105 assertEquals("StreamName", streamName, captor.getValue().getStream().getValue());
109 public void testNotificationList() {
111 NetconfAccessorImpl netconfAccessor = TestNetconfHelper.getNetconfAcessorImpl();
113 DataBroker dataBroker = mock(DataBroker.class);
115 NotificationsService notificationService = mock(NotificationsService.class);
117 RpcConsumerRegistry rpcComerRegistry = mock(RpcConsumerRegistry.class);
118 when(rpcComerRegistry.getRpcService(NotificationsService.class)).thenReturn(notificationService);
120 MountPoint mountPoint = mock(MountPoint.class);
121 when(mountPoint.getService(RpcConsumerRegistry.class)).thenReturn(Optional.of(rpcComerRegistry));
125 NetconfBindingNotificationsImpl test =
126 new NetconfBindingNotificationsImpl(netconfAccessor, dataBroker, mountPoint);
128 String streamName = NetconfNotifications.DefaultNotificationsStream+"ChangeIt";
129 StreamNameType streamNameType = new StreamNameType(streamName);
130 Stream stream = new StreamBuilder().setName(streamNameType).build();
131 test.registerNotificationsStream(Arrays.asList(stream));
133 //Capture parameters and assert them
134 ArgumentCaptor<CreateSubscriptionInput> captor = ArgumentCaptor.forClass(CreateSubscriptionInput.class);
135 verify(notificationService).createSubscription(captor.capture());
137 assertEquals("StreamName", streamName, captor.getValue().getStream().getValue());
142 public void testNetconfDomNotification() {
144 NetconfAccessorImpl netconfAccessor = TestNetconfHelper.getNetconfAcessorImpl();
145 DOMDataBroker domDataBroker = mock(DOMDataBroker.class);
146 DOMMountPoint domMountPoint = mock(DOMMountPoint.class);
147 DOMNotificationService domNotificationService = mock(DOMNotificationService.class);
149 YangInstanceIdentifier mountpointPath = YangInstanceIdentifier.builder()
150 .node(NetworkTopology.QNAME)
152 when(domMountPoint.getIdentifier()).thenReturn(mountpointPath);
153 when(domMountPoint.getService(DOMNotificationService.class)).thenReturn(Optional.of(domNotificationService));
155 DomContext domContext = mock(DomContext.class);
157 NetconfDomAccessorImpl netconfDomAccessor =
158 new NetconfDomAccessorImpl(netconfAccessor, domDataBroker, domMountPoint, domContext);
160 Collection<SchemaPath> types = Arrays.asList(SchemaPath.create(false, NetworkTopology.QNAME));
162 DOMNotificationListener listener = (notification) -> System.out.println("Notification: "+notification);
163 ListenerRegistration<DOMNotificationListener> res =
164 netconfDomAccessor.doRegisterNotificationListener(listener, types);
166 //Capture parameters and assert them
167 ArgumentCaptor<DOMNotificationListener> captor1 = ArgumentCaptor.forClass(DOMNotificationListener.class);
168 @SuppressWarnings("unchecked")
169 ArgumentCaptor<Collection<SchemaPath>> captor2 = ArgumentCaptor.forClass(Collection.class);
170 verify(domNotificationService).registerNotificationListener(captor1.capture(), captor2.capture());
172 assertEquals("Listener", listener, captor1.getValue());
173 assertEquals("SchemaPath", types, captor2.getValue());