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