1ec9cde2c925c889b3cc0b9c6712d766b9dc9da8
[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.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;
57
58 public class TestNetconfAccessorImpl extends Mockito {
59
60     @Test
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);
69
70         NetconfAccessorImpl netconfAccessor =
71                 new NetconfAccessorImpl(nodeId, testNode, netconfCommunicatorManager, domContext);
72
73         Assert.assertNotNull(netconfAccessor);
74
75         NetconfAccessorImpl netconfAcessor2 = new NetconfAccessorImpl(netconfAccessor);
76
77         Assert.assertNotNull(netconfAcessor2);
78     }
79
80     @Test
81     public void testBindingNotifications() {
82         NetconfAccessorImpl netconfAccessor = TestNetconfHelper.getNetconfAcessorImpl();
83
84         DataBroker dataBroker = mock(DataBroker.class);
85
86         NotificationsService notificationService = mock(NotificationsService.class);
87
88         RpcConsumerRegistry rpcComerRegistry = mock(RpcConsumerRegistry.class);
89         when(rpcComerRegistry.getRpcService(NotificationsService.class)).thenReturn(notificationService);
90
91         MountPoint mountPoint = mock(MountPoint.class);
92         when(mountPoint.getService(RpcConsumerRegistry.class)).thenReturn(Optional.of(rpcComerRegistry));
93
94         //Start here
95         NetconfBindingNotificationsImpl test =
96                 new NetconfBindingNotificationsImpl(netconfAccessor, dataBroker, mountPoint);
97
98         String streamName = "NETCONF";
99         test.registerNotificationsStream(streamName);
100
101         //Capture parameters and assert them
102         ArgumentCaptor<CreateSubscriptionInput> captor = ArgumentCaptor.forClass(CreateSubscriptionInput.class);
103         verify(notificationService).createSubscription(captor.capture());
104
105         assertEquals("StreamName", streamName, captor.getValue().getStream().getValue());
106     }
107
108     @Test
109     public void testNotificationList() {
110
111         NetconfAccessorImpl netconfAccessor = TestNetconfHelper.getNetconfAcessorImpl();
112
113         DataBroker dataBroker = mock(DataBroker.class);
114
115         NotificationsService notificationService = mock(NotificationsService.class);
116
117         RpcConsumerRegistry rpcComerRegistry = mock(RpcConsumerRegistry.class);
118         when(rpcComerRegistry.getRpcService(NotificationsService.class)).thenReturn(notificationService);
119
120         MountPoint mountPoint = mock(MountPoint.class);
121         when(mountPoint.getService(RpcConsumerRegistry.class)).thenReturn(Optional.of(rpcComerRegistry));
122
123
124         //Start here
125         NetconfBindingNotificationsImpl test =
126                 new NetconfBindingNotificationsImpl(netconfAccessor, dataBroker, mountPoint);
127
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));
132
133         //Capture parameters and assert them
134         ArgumentCaptor<CreateSubscriptionInput> captor = ArgumentCaptor.forClass(CreateSubscriptionInput.class);
135         verify(notificationService).createSubscription(captor.capture());
136
137         assertEquals("StreamName", streamName, captor.getValue().getStream().getValue());
138
139     }
140
141     @Test
142     public void testNetconfDomNotification() {
143
144         NetconfAccessorImpl netconfAccessor = TestNetconfHelper.getNetconfAcessorImpl();
145         DOMDataBroker domDataBroker = mock(DOMDataBroker.class);
146         DOMMountPoint domMountPoint = mock(DOMMountPoint.class);
147         DOMNotificationService domNotificationService = mock(DOMNotificationService.class);
148
149         YangInstanceIdentifier mountpointPath = YangInstanceIdentifier.builder()
150                 .node(NetworkTopology.QNAME)
151                 .build();
152         when(domMountPoint.getIdentifier()).thenReturn(mountpointPath);
153         when(domMountPoint.getService(DOMNotificationService.class)).thenReturn(Optional.of(domNotificationService));
154
155         DomContext domContext = mock(DomContext.class);
156
157         NetconfDomAccessorImpl netconfDomAccessor =
158                 new NetconfDomAccessorImpl(netconfAccessor, domDataBroker, domMountPoint, domContext);
159
160         Collection<SchemaPath> types = Arrays.asList(SchemaPath.create(false, NetworkTopology.QNAME));
161
162         DOMNotificationListener listener = (notification) -> System.out.println("Notification: "+notification);
163         ListenerRegistration<DOMNotificationListener> res =
164                 netconfDomAccessor.doRegisterNotificationListener(listener, types);
165
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());
171
172         assertEquals("Listener", listener, captor1.getValue());
173         assertEquals("SchemaPath", types, captor2.getValue());
174     }
175
176 }