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