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