96983b1ba43f3a4cc34f9f25c5a8427e4f40a9b0
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2021 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.devicemanager.oran.impl.dom;
23
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
28 import java.time.Instant;
29 import org.junit.BeforeClass;
30 import org.junit.Test;
31 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.DataProvider;
32 import org.onap.ccsdk.features.sdnr.wt.devicemanager.oran.notification.ORanDOMChangeNotificationListener;
33 import org.onap.ccsdk.features.sdnr.wt.devicemanager.oran.util.ORanDeviceManagerQNames;
34 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.VESCollectorCfgService;
35 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.VESCollectorService;
36 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfDomAccessor;
37 import org.opendaylight.mdsal.dom.api.DOMEvent;
38 import org.opendaylight.mdsal.dom.api.DOMNotification;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.EventlogEntity;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
41 import org.opendaylight.yangtools.yang.common.Uint32;
42 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
43 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
44 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
45 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
46 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50
51 public class TestORanDOMNotification {
52
53     private static final Logger log = LoggerFactory.getLogger(TestORanDOMNotification.class);
54     static ContainerNode cn = null;
55     static NodeId nodeId = new NodeId("nSky");
56     private static NetconfDomAccessor domAccessor;
57     private static VESCollectorService vesCollectorService;
58     private static DataProvider databaseService;
59     private static VESCollectorCfgService vesCfgService;
60     // Use the below procedure for creating QName if binding generated classes are not available
61     /*String ietf_netconf_notif_ns = "urn:ietf:params:xml:ns:yang:ietf-netconf-notifications";
62     String ietf_netconf_notif_ns_date = "2012-02-06";
63     QName username = QName.create(ietf_netconf_notif_ns, ietf_netconf_notif_ns_date, "username");*/
64
65
66     @BeforeClass
67     public static void prepare() {
68         domAccessor = mock(NetconfDomAccessor.class);
69         vesCollectorService = mock(VESCollectorService.class);
70         databaseService = mock(DataProvider.class);
71         vesCfgService = mock(VESCollectorCfgService.class);
72         when(vesCollectorService.getConfig()).thenReturn(vesCfgService);
73         when(vesCfgService.isVESCollectorEnabled()).thenReturn(true);
74         when(domAccessor.getNodeId()).thenReturn(nodeId);
75     }
76
77     /*
78     ImmutableContainerNode{
79     identifier=(urn:ietf:params:xml:ns:yang:ietf-netconf-notifications?revision=2012-02-06)netconf-config-change,
80     value=[
81         ImmutableContainerNode{
82             identifier=(urn:ietf:params:xml:ns:yang:ietf-netconf-notifications?revision=2012-02-06)changed-by,
83             value=[
84                 ImmutableChoiceNode{
85                     identifier=(urn:ietf:params:xml:ns:yang:ietf-netconf-notifications?revision=2012-02-06)server-or-user,
86                     value=[
87                         ImmutableLeafNode{
88                             identifier=(urn:ietf:params:xml:ns:yang:ietf-netconf-notifications?revision=2012-02-06)username,
89                             value=root
90                         },
91                         ImmutableLeafNode{
92                             identifier=(urn:ietf:params:xml:ns:yang:ietf-netconf-notifications?revision=2012-02-06)session-id,
93                             value=2
94                         }
95                     ]
96                 }
97             ]
98         },
99         ImmutableUnkeyedListNode{
100             identifier=(urn:ietf:params:xml:ns:yang:ietf-netconf-notifications?revision=2012-02-06)edit,
101             value=[
102                 ImmutableUnkeyedListEntryNode{
103                     identifier=(urn:ietf:params:xml:ns:yang:ietf-netconf-notifications?revision=2012-02-06)edit,
104                     value=[
105                         ImmutableLeafNode{
106                             identifier=(urn:ietf:params:xml:ns:yang:ietf-netconf-notifications?revision=2012-02-06)operation,
107                             value=replace
108                         },
109                         ImmutableLeafNode{
110                             identifier=(urn:ietf:params:xml:ns:yang:ietf-netconf-notifications?revision=2012-02-06)target, value=/(urn:ietf:params:xml:ns:yang:ietf-hardware?revision=2018-03-13)hardware/component/component[{(urn:ietf:params:xml:ns:yang:ietf-hardware?revision=2018-03-13)name=chassis-fan3}]/alias
111                         }
112                     ]
113                 }
114             ]
115         },
116         ImmutableLeafNode{
117             identifier=(urn:ietf:params:xml:ns:yang:ietf-netconf-notifications?revision=2012-02-06)datastore,
118             value=running
119         }
120     ]
121     }
122      */
123     private static ContainerNode createDOMNotificationBody() {
124         return Builders.containerBuilder().withNodeIdentifier(NodeIdentifier.create(ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_NETCONF_CONFIG_CHANGE))
125                 .withChild(
126                         Builders.containerBuilder().withNodeIdentifier(NodeIdentifier.create(ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_CHANGEDBY))
127                                 .withChild(Builders.choiceBuilder()
128                                         .withNodeIdentifier(NodeIdentifier.create(ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_SERVERORUSER))
129                                         .withChild(ImmutableNodes.leafNode(ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_USERNAME, "root"))
130                                         .withChild(ImmutableNodes.leafNode(ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_SESSIONID, Uint32.valueOf(2))).build())
131                                 .build())
132                 .withChild(Builders.unkeyedListBuilder().withNodeIdentifier(NodeIdentifier.create(ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_EDITNODE))
133                         .withChild(Builders.unkeyedListEntryBuilder()
134                                 .withNodeIdentifier(NodeIdentifier.create(ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_EDITNODE))
135                                 .withChild(ImmutableNodes.leafNode(ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_OPERATION, "replace"))
136                                 .withChild(ImmutableNodes.leafNode(ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_TARGET,
137                                         "/(urn:ietf:params:xml:ns:yang:ietf-hardware?revision=2018-03-13)hardware/component[{(urn:ietf:params:xml:ns:yang:ietf-hardware?revision=2018-03-13)name=chassis-fan3}]/alias"))
138                                 .build())
139                         .build())
140                 .withChild(ImmutableNodes.leafNode(ORanDeviceManagerQNames.IETF_NETCONF_NOTIFICATIONS_DATASTORE, "running")).build();
141     }
142
143     @Test
144     public void test() {
145         ContainerNode cn = createDOMNotificationBody();
146         NetconfDeviceNotification ndn = new NetconfDeviceNotification(cn, Instant.now());
147         ORanDOMChangeNotificationListener changeListener = new ORanDOMChangeNotificationListener(domAccessor, vesCollectorService, databaseService);
148         changeListener.onNotification(ndn);
149         verify(databaseService).writeEventLog(any(EventlogEntity.class));
150     }
151
152     public static class NetconfDeviceNotification implements DOMNotification, DOMEvent {
153         private final ContainerNode content;
154         private final Absolute schemaPath;
155         private final Instant eventTime;
156
157         NetconfDeviceNotification(final ContainerNode content, final Instant eventTime) {
158             this.content = content;
159             this.eventTime = eventTime;
160             this.schemaPath = Absolute.of(content.getIdentifier().getNodeType());
161         }
162
163         NetconfDeviceNotification(final ContainerNode content, final Absolute schemaPath, final Instant eventTime) {
164             this.content = content;
165             this.eventTime = eventTime;
166             this.schemaPath = schemaPath;
167         }
168
169         @Override
170         public Absolute getType() {
171             return schemaPath;
172         }
173
174         @Override
175         public ContainerNode getBody() {
176             return content;
177         }
178
179         @Override
180         public Instant getEventInstant() {
181             return eventTime;
182         }
183     }
184
185 }