Use DOM API for ORAN Devicemanager
[ccsdk/features.git] / sdnr / wt / devicemanager-o-ran-sc / o-ran / ru-fh / provider / src / test / java / org / onap / ccsdk / features / sdnr / wt / devicemanager / oran / impl / dom / TestORanDOMFaultNotificationListener.java
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.verify;
26 import static org.mockito.Mockito.when;
27 import com.google.common.io.Files;
28 import java.io.File;
29 import java.io.IOException;
30 import java.nio.charset.StandardCharsets;
31 import java.time.Instant;
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.Mock;
37 import org.mockito.junit.MockitoJUnitRunner;
38 import org.onap.ccsdk.features.sdnr.wt.common.configuration.ConfigurationFileRepresentation;
39 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.DataProvider;
40 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.DeviceManagerServiceProvider;
41 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.FaultService;
42 import org.onap.ccsdk.features.sdnr.wt.devicemanager.service.VESCollectorService;
43 import org.onap.ccsdk.features.sdnr.wt.devicemanager.vescollectorconnector.impl.VESCollectorServiceImpl;
44 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfDomAccessor;
45 import org.onap.ccsdk.features.sdnr.wt.websocketmanager.model.WebsocketManagerService;
46 import org.opendaylight.mdsal.dom.api.DOMEvent;
47 import org.opendaylight.mdsal.dom.api.DOMNotification;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.FaultlogEntity;
49 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
50 import org.opendaylight.yangtools.yang.common.QName;
51 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
52 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
53 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
54 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
55 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
58
59 @RunWith(MockitoJUnitRunner.class)
60 public class TestORanDOMFaultNotificationListener {
61     private static final Logger LOG = LoggerFactory.getLogger(TestORanDOMFaultNotificationListener.class);
62     private static final String TESTFILENAME = "configFile.txt";
63
64     // @formatter:off
65     private static final String TESTCONFIG_CONTENT = "[VESCollector]\n"
66             + "VES_COLLECTOR_ENABLED=true\n"
67             + "VES_COLLECTOR_TLS_ENABLED=true\n"
68             + "VES_COLLECTOR_TRUST_ALL_CERTS=true\n"
69             + "VES_COLLECTOR_USERNAME=sample1\n"
70             + "VES_COLLECTOR_PASSWORD=sample1\n"
71             + "VES_COLLECTOR_IP=[2001:db8:1:1::1]\n"
72             + "VES_COLLECTOR_PORT=8443\n"
73             + "VES_COLLECTOR_VERSION=v7\n"
74             + "REPORTING_ENTITY_NAME=ONAP SDN-R\n"
75             + "EVENTLOG_MSG_DETAIL=SHORT\n"
76             + "";
77     // @formatter:on
78
79     @Mock
80     NetconfDomAccessor domAccessor;
81     @Mock
82     DataProvider dataProvider;
83     @Mock
84     FaultService faultService;
85     @Mock
86     DeviceManagerServiceProvider serviceProvider;
87     @Mock
88     WebsocketManagerService websocketManagerService;
89     @Mock
90     DataProvider databaseService;
91     VESCollectorService vesCollectorService;
92
93     @After
94     @Before
95     public void afterAndBefore() {
96         File f = new File(TESTFILENAME);
97         if (f.exists()) {
98             LOG.info("Remove {}", f.getAbsolutePath());
99             f.delete();
100         }
101     }
102
103     @Test
104     public void test() throws IOException {
105         Files.asCharSink(new File(TESTFILENAME), StandardCharsets.UTF_8).write(TESTCONFIG_CONTENT);
106         vesCollectorService = new VESCollectorServiceImpl(new ConfigurationFileRepresentation(TESTFILENAME));
107         when(domAccessor.getNodeId()).thenReturn(new NodeId("nSky"));
108         ORanDOMFaultNotificationListener faultListener = new ORanDOMFaultNotificationListener(domAccessor,
109                 vesCollectorService, faultService, websocketManagerService, databaseService);
110         NetconfDeviceNotification ndn = new NetconfDeviceNotification(createORANDOMFault(), Instant.now());
111         faultListener.onNotification(ndn);
112
113         verify(faultService).faultNotification(any(FaultlogEntity.class));
114     }
115
116     public static ContainerNode createORANDOMFault() {
117         final QName fault_id = QName.create(ORanDeviceManagerQNames.ORAN_FM_ALARM_NOTIF, "fault-id");
118         final QName fault_source = QName.create(ORanDeviceManagerQNames.ORAN_FM_ALARM_NOTIF, "fault-source");
119         final QName fault_severity = QName.create(ORanDeviceManagerQNames.ORAN_FM_ALARM_NOTIF, "fault-severity");
120         final QName is_cleared = QName.create(ORanDeviceManagerQNames.ORAN_FM_ALARM_NOTIF, "is-cleared");
121         final QName fault_text = QName.create(ORanDeviceManagerQNames.ORAN_FM_ALARM_NOTIF, "fault-text");
122         return Builders.containerBuilder().withNodeIdentifier(NodeIdentifier.create(ORanDeviceManagerQNames.ORAN_FM_ALARM_NOTIF))
123                 .withChild(ImmutableNodes.leafNode(fault_id, "47"))
124                 .withChild(ImmutableNodes.leafNode(fault_source, "Slot-2-Port-B"))
125                 .withChild(ImmutableNodes.leafNode(fault_severity, "MAJOR"))
126                 .withChild(ImmutableNodes.leafNode(is_cleared, "true"))
127                 .withChild(ImmutableNodes.leafNode(fault_text, "CPRI Port Down")).build();
128     }
129
130
131     public static class NetconfDeviceNotification implements DOMNotification, DOMEvent {
132         private final ContainerNode content;
133         private final Absolute schemaPath;
134         private final Instant eventTime;
135
136         NetconfDeviceNotification(final ContainerNode content, final Instant eventTime) {
137             this.content = content;
138             this.eventTime = eventTime;
139             this.schemaPath = Absolute.of(content.getIdentifier().getNodeType());
140         }
141
142         NetconfDeviceNotification(final ContainerNode content, final Absolute schemaPath, final Instant eventTime) {
143             this.content = content;
144             this.eventTime = eventTime;
145             this.schemaPath = schemaPath;
146         }
147
148         @Override
149         public Absolute getType() {
150             return schemaPath;
151         }
152
153         @Override
154         public ContainerNode getBody() {
155             return content;
156         }
157
158         @Override
159         public Instant getEventInstant() {
160             return eventTime;
161         }
162     }
163 }