10c3b2697a4d194958e1c0c2f3c17745a2f14e14
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
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
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  *
21  */
22 package org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.test;
23
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.eclipse.jdt.annotation.NonNull;
29 import org.junit.Assert;
30 import org.junit.Test;
31 import org.mockito.ArgumentCaptor;
32 import org.mockito.ArgumentMatchers;
33 import org.mockito.Mockito;
34 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfAccessor;
35 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.impl.NetconfNodeStateServiceImpl;
36 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.impl.access.NetconfAccessorImpl;
37 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.impl.access.NetconfCommunicatorManager;
38 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.impl.access.binding.NetconfBindingAccessorImpl;
39 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.impl.access.dom.DomContext;
40 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.impl.access.dom.NetconfDomAccessorImpl;
41 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.test.example.TestNetconfHelper;
42 import org.opendaylight.mdsal.binding.api.DataBroker;
43 import org.opendaylight.mdsal.binding.api.MountPoint;
44 import org.opendaylight.mdsal.binding.api.RpcConsumerRegistry;
45 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
46 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
47 import org.opendaylight.mdsal.dom.api.DOMNotificationListener;
48 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
49 import org.opendaylight.mdsal.dom.api.DOMRpcService;
50 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.CreateSubscriptionInput;
51 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.NotificationsService;
52 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netconf.notification._1._0.rev080714.StreamNameType;
53 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.Stream;
54 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.netmod.notification.rev080714.netconf.streams.StreamBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.netconf.node.topology.rev150114.NetconfNode;
56 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
57 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
58 import org.opendaylight.yangtools.concepts.ListenerRegistration;
59 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
60 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
61
62 public class TestNetconfAccessorImpl extends Mockito {
63
64     @Test
65     public void testConstruct() {
66         NetconfCommunicatorManager netconfCommunicatorManager = mock(NetconfCommunicatorManager.class);
67         DomContext domContext = mock(DomContext.class);
68         String nodeIdString = "Test";
69         String capabilityStringForNetworkElement = "network-element";
70         NodeId nodeId = new NodeId(nodeIdString);
71         NetconfNode testNode = TestNetconfHelper.getTestNode(nodeId, capabilityStringForNetworkElement)
72                 .augmentation(NetconfNode.class);
73
74         NetconfNodeStateServiceImpl netconfNodeStateService = mock(NetconfNodeStateServiceImpl.class);
75         NetconfAccessorImpl netconfAccessor = new NetconfAccessorImpl(nodeId, testNode, netconfCommunicatorManager,
76                 domContext, netconfNodeStateService);
77
78         Assert.assertNotNull(netconfAccessor);
79
80         NetconfAccessorImpl netconfAcessor2 = new NetconfAccessorImpl(netconfAccessor);
81
82         Assert.assertNotNull(netconfAcessor2);
83     }
84
85     @Test
86     public void testBindingNotifications() {
87         NetconfAccessorImpl netconfAccessor = TestNetconfHelper.getNetconfAcessorImpl();
88
89         DataBroker dataBroker = mock(DataBroker.class);
90
91         NotificationsService notificationService = mock(NotificationsService.class);
92
93         RpcConsumerRegistry rpcComerRegistry = mock(RpcConsumerRegistry.class);
94         when(rpcComerRegistry.getRpcService(NotificationsService.class)).thenReturn(notificationService);
95
96         MountPoint mountPoint = mock(MountPoint.class);
97         when(mountPoint.getService(RpcConsumerRegistry.class)).thenReturn(Optional.of(rpcComerRegistry));
98
99         //Start here
100         NetconfBindingAccessorImpl test =
101                 new NetconfBindingAccessorImpl(netconfAccessor, dataBroker, mountPoint);
102
103         String streamName = "NETCONF";
104         test.registerNotificationsStream(streamName);
105
106         //Capture parameters and assert them
107         ArgumentCaptor<CreateSubscriptionInput> captor = ArgumentCaptor.forClass(CreateSubscriptionInput.class);
108         verify(notificationService).createSubscription(captor.capture());
109
110         assertEquals("StreamName", streamName, captor.getValue().getStream().getValue());
111     }
112
113     @Test
114     public void testNotificationList() {
115
116         NetconfAccessorImpl netconfAccessor = TestNetconfHelper.getNetconfAcessorImpl();
117
118         DataBroker dataBroker = mock(DataBroker.class);
119
120         NotificationsService notificationService = mock(NotificationsService.class);
121
122         RpcConsumerRegistry rpcComerRegistry = mock(RpcConsumerRegistry.class);
123         when(rpcComerRegistry.getRpcService(NotificationsService.class)).thenReturn(notificationService);
124
125         MountPoint mountPoint = mock(MountPoint.class);
126         when(mountPoint.getService(RpcConsumerRegistry.class)).thenReturn(Optional.of(rpcComerRegistry));
127
128
129         //Start here
130         NetconfBindingAccessorImpl test =
131                 new NetconfBindingAccessorImpl(netconfAccessor, dataBroker, mountPoint);
132
133         String streamName = NetconfAccessor.DefaultNotificationsStream + "ChangeIt";
134         StreamNameType streamNameType = new StreamNameType(streamName);
135         Stream stream = new StreamBuilder().setName(streamNameType).build();
136         test.registerNotificationsStream(Arrays.asList(stream));
137
138         //Capture parameters and assert them
139         ArgumentCaptor<CreateSubscriptionInput> captor = ArgumentCaptor.forClass(CreateSubscriptionInput.class);
140         verify(notificationService).createSubscription(captor.capture());
141
142         assertEquals("StreamName", streamName, captor.getValue().getStream().getValue());
143
144     }
145
146     @Test
147     public void testNetconfDomNotification() {
148
149         DOMDataBroker domDataBroker = mock(DOMDataBroker.class);
150         DomContext domContext = mock(DomContext.class);
151         DOMRpcService domRpcService = mock(DOMRpcService.class);
152         NetconfAccessorImpl netconfAccessor = TestNetconfHelper.getNetconfAcessorImpl();
153         DOMNotificationService domNotificationService = mock(DOMNotificationService.class);
154         DOMMountPoint domMountPoint = mock(DOMMountPoint.class);
155
156         when(domNotificationService.registerNotificationListener(any(DOMNotificationListener.class),
157                 ArgumentMatchers.<Collection<SchemaPath>>any()))
158                         .thenReturn(new ListenerRegistration<DOMNotificationListener>() {
159                             @Override
160                             public @NonNull DOMNotificationListener getInstance() {
161                                 return null;
162                             }
163
164                             @Override
165                             public void close() {}
166                         });
167
168         YangInstanceIdentifier mountpointPath = YangInstanceIdentifier.builder().node(NetworkTopology.QNAME).build();
169         when(domMountPoint.getIdentifier()).thenReturn(mountpointPath);
170         when(domMountPoint.getService(DOMNotificationService.class)).thenReturn(Optional.of(domNotificationService));
171         when(domMountPoint.getService(DOMRpcService.class)).thenReturn(Optional.of(domRpcService));
172
173         /* Remark: Throws WARN java.lang.UnsupportedOperationException
174          *  "[main] WARN org.opendaylight.netconf.util.NetconfUtil -
175          *  Unable to set namespace context, falling back to setPrefix()
176          *  during initialization."
177          */
178         NetconfDomAccessorImpl netconfDomAccessor =
179                 new NetconfDomAccessorImpl(netconfAccessor, domDataBroker, domMountPoint, domContext);
180
181         Collection<SchemaPath> types = Arrays.asList(SchemaPath.create(false, NetworkTopology.QNAME));
182         DOMNotificationListener listener = (notification) -> System.out.println("Notification: " + notification);
183         ListenerRegistration<DOMNotificationListener> res =
184                 netconfDomAccessor.doRegisterNotificationListener(listener, types);
185
186         //Capture parameters and assert them
187         ArgumentCaptor<DOMNotificationListener> captor1 = ArgumentCaptor.forClass(DOMNotificationListener.class);
188         @SuppressWarnings("unchecked")
189         ArgumentCaptor<Collection<SchemaPath>> captor2 = ArgumentCaptor.forClass(Collection.class);
190         verify(domNotificationService).registerNotificationListener(captor1.capture(), captor2.capture());
191
192         assertEquals("Listener", listener, captor1.getValue());
193         assertEquals("SchemaPath", types, captor2.getValue());
194         res.close();
195     }
196
197 }