efbcab158371f6e2d63d1de59769cec5f243b2e8
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2020 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.onf14.dom.impl.yangspecs;
23
24 import java.util.Arrays;
25 import java.util.Collection;
26 import java.util.List;
27 import java.util.Optional;
28 import org.eclipse.jdt.annotation.NonNull;
29 import org.onap.ccsdk.features.sdnr.wt.devicemanager.onf14.dom.impl.util.Onf14DMDOMUtility;
30 import org.onap.ccsdk.features.sdnr.wt.devicemanager.types.FaultData;
31 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.Capabilities;
32 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfDomAccessor;
33 import org.onap.ccsdk.features.sdnr.wt.websocketmanager.model.WebsocketManagerService;
34 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
35 import org.opendaylight.mdsal.dom.api.DOMNotification;
36 import org.opendaylight.mdsal.dom.api.DOMNotificationListener;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.FaultlogBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.FaultlogEntity;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.SourceType;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
41 import org.opendaylight.yangtools.yang.common.QName;
42 import org.opendaylight.yangtools.yang.common.QNameModule;
43 import org.opendaylight.yangtools.yang.common.Revision;
44 import org.opendaylight.yangtools.yang.common.XMLNamespace;
45 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
46 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.InstanceIdentifierBuilder;
47 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
48 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
49 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
50 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
51 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
52 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55
56 public class Alarms10 extends YangModule {
57
58     private static final Logger LOG = LoggerFactory.getLogger(Alarms10.class);
59
60     private static final String NAMESPACE = "urn:onf:yang:alarms-1-0";
61     private static final List<QNameModule> MODULES =
62             Arrays.asList(QNameModule.create(XMLNamespace.of(NAMESPACE), Revision.of("2022-03-02")),
63                     QNameModule.create(XMLNamespace.of(NAMESPACE), Revision.of("2022-07-29")));
64
65     private final QName ALARM_PAC;
66     private final QName CURRENT_ALARMS;
67     private final QName CURRENT_ALARM_LIST;
68     private final QName CURRENT_ALARM_IDENTIFIER;
69     private final QName ALARM_TYPE_ID;
70     private final QName ALARM_TYPE_QUALIFIER;
71     private final QName RESOURCE;
72     private final QName ALARM_SEVERITY;
73     private final QName ALARM_TIMESTAMP;
74     private final QName ALARM_EVENT_SEQUENCE_NUMBER;
75     private final QName PROBLEM_SEVERITY;
76     private final QName ALARM_AVC_NOTIFICATION;
77     private final QName ALARM_EVENT_NOTIFICATION;
78
79     private final CoreModel14 coreModel14;
80
81     private Alarms10(NetconfDomAccessor netconfDomAccessor, QNameModule module, CoreModel14 coreModel14) {
82         super(netconfDomAccessor, module);
83         this.coreModel14 = coreModel14;
84
85         ALARM_PAC = QName.create(module, "alarm-pac");
86         CURRENT_ALARMS = QName.create(module, "current-alarms");
87         CURRENT_ALARM_LIST = QName.create(module, "current-alarm-list");
88         CURRENT_ALARM_IDENTIFIER = QName.create(module, "current-alarm-identifier");
89         ALARM_TYPE_ID = QName.create(module, "alarm-type-id");
90         ALARM_TYPE_QUALIFIER = QName.create(module, "alarm-type-qualifier");
91         RESOURCE = QName.create(module, "resource");
92         ALARM_SEVERITY = QName.create(module, "alarm-severity");
93         ALARM_TIMESTAMP = QName.create(module, "timestamp");
94         ALARM_EVENT_SEQUENCE_NUMBER = QName.create(module, "alarm-event-sequence-number");
95         PROBLEM_SEVERITY = QName.create(module, "problem-severity");
96         ALARM_AVC_NOTIFICATION = QName.create(module, "attribute-value-changed-notification");
97         ALARM_EVENT_NOTIFICATION = QName.create(module, "alarm-event-notification");
98     }
99
100     public QNameModule getModule() {
101         return module;
102     }
103
104     public FaultlogEntity getFaultlogEntity(ContainerNode cn) {
105         return new FaultlogBuilder().setNodeId(getNodeId().getValue()).setSourceType(SourceType.Netconf)
106                 .setObjectId(Onf14DMDOMUtility.getLeafValueUuid(cn, RESOURCE))
107                 .setProblem(Onf14DMDOMUtility.getLeafValue(cn, ALARM_TYPE_QUALIFIER))
108                 .setTimestamp(Onf14DMDOMUtility.getLeafValueDateAndTime(cn, ALARM_TIMESTAMP))
109                 .setSeverity(Onf14DMDOMUtility.getLeafValueInternalSeverity(cn, PROBLEM_SEVERITY))
110                 .setCounter(Onf14DMDOMUtility.getLeafValueInt(cn, ALARM_EVENT_SEQUENCE_NUMBER)).build();
111     }
112
113     public boolean isAlarmEventNotification(DOMNotification domNotification) {
114         return domNotification.getType().equals(Absolute.of(ALARM_EVENT_NOTIFICATION));
115     }
116
117     public void doRegisterNotificationListener(DOMNotificationListener alarmNotifListener) {
118         QName[] alarmNotifications = {ALARM_AVC_NOTIFICATION, ALARM_EVENT_NOTIFICATION};
119         netconfDomAccessor.doRegisterNotificationListener(alarmNotifListener, alarmNotifications);
120     }
121
122     public void sendNotification(@NonNull WebsocketManagerService websocketService, DOMNotification domNotification,
123             ContainerNode cn) {
124         websocketService.sendNotification(domNotification, getNodeId(), ALARM_TYPE_QUALIFIER,
125                 Onf14DMDOMUtility.getLeafValueDateAndTime(cn, ALARM_TIMESTAMP));
126     }
127
128     public FaultData getCurrentAlarms() {
129
130         YangInstanceIdentifier alarmsPacIID =
131                 YangInstanceIdentifier.builder().node(coreModel14.getControlConstructQName()).build();
132         InstanceIdentifierBuilder alarmsContainerIID = YangInstanceIdentifier.builder(alarmsPacIID).node(ALARM_PAC);
133
134         //        @NonNull
135         //        AugmentationIdentifier alarmsContainerIID =
136         //                YangInstanceIdentifier.AugmentationIdentifier.create(Sets.newHashSet(ALARM_PAC));
137         //
138         //        InstanceIdentifierBuilder augmentedAlarmsIID =
139         //                YangInstanceIdentifier.builder(alarmsPacIID).node(alarmsContainerIID);
140         //
141         //        // reading all the alarms
142         Optional<NormalizedNode> alarms =
143                 this.getNetconfDomAccessor().readDataNode(LogicalDatastoreType.OPERATIONAL, alarmsContainerIID.build());
144
145         FaultData resultList = new FaultData();
146         if (alarms.isPresent()) {
147             ContainerNode alarmsDataNode = (ContainerNode) alarms.get();
148             ContainerNode alarmsContainer = (ContainerNode) alarmsDataNode.childByArg(new NodeIdentifier(ALARM_PAC));
149             ContainerNode currentAlarmsContainer =
150                     (ContainerNode) alarmsContainer.childByArg(new NodeIdentifier(CURRENT_ALARMS));
151             MapNode currentAlarmsList =
152                     (MapNode) currentAlarmsContainer.childByArg(new NodeIdentifier(CURRENT_ALARM_LIST));
153             if (currentAlarmsList != null) {
154                 Collection<MapEntryNode> currentAlarmsCollection = currentAlarmsList.body();
155                 for (MapEntryNode currentAlarm : currentAlarmsCollection) {
156                     resultList.add(getNodeId(),
157                             Onf14DMDOMUtility.getLeafValueInt(currentAlarm, CURRENT_ALARM_IDENTIFIER),
158                             Onf14DMDOMUtility.getLeafValueDateAndTime(currentAlarm, ALARM_TIMESTAMP),
159                             Onf14DMDOMUtility.getLeafValueUuid(currentAlarm, RESOURCE),
160                             Onf14DMDOMUtility.getLeafValue(currentAlarm, ALARM_TYPE_QUALIFIER),
161                             Onf14DMDOMUtility.getLeafValueInternalSeverity(currentAlarm, ALARM_SEVERITY));
162                 }
163             } else {
164                 LOG.debug("DBRead empty CurrentProblemList");
165             }
166         }
167         return resultList;
168
169     }
170
171     public boolean isSupported(Capabilities capabilites) {
172         return netconfDomAccessor.getCapabilites().isSupportingNamespace(NAMESPACE);
173     }
174
175     private NodeId getNodeId() {
176         return netconfDomAccessor.getNodeId();
177     }
178
179     /**
180      * Get specific instance, depending on capabilities
181      *
182      * @param capabilities
183      * @return
184      */
185     public static Optional<Alarms10> getModule(NetconfDomAccessor netconfDomAccessor, CoreModel14 coreModel14) {
186         Capabilities capabilities = netconfDomAccessor.getCapabilites();
187         for (QNameModule module : MODULES) {
188             if (capabilities.isSupportingNamespaceAndRevision(module)) {
189                 return Optional.of(new Alarms10(netconfDomAccessor, module, coreModel14));
190             }
191         }
192         return Optional.empty();
193     }
194 }