a1ec2fc9e458095395810b14252216e4f2bf963f
[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.dataprovider;
23
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.List;
27 import java.util.Objects;
28 import java.util.Optional;
29 import org.eclipse.jdt.annotation.NonNull;
30 import org.eclipse.jdt.annotation.Nullable;
31 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.types.NetconfTimeStampImpl;
32 import org.onap.ccsdk.features.sdnr.wt.devicemanager.oran.util.ORanDMDOMUtility;
33 import org.onap.ccsdk.features.sdnr.wt.devicemanager.oran.util.ORanDeviceManagerQNames;
34 import org.onap.ccsdk.features.sdnr.wt.devicemanager.oran.yangspecs.ORANFM;
35 import org.onap.ccsdk.features.sdnr.wt.devicemanager.oran.yangspecs.OnapSystem;
36 import org.opendaylight.mdsal.dom.api.DOMNotification;
37 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.FaultlogBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.FaultlogEntity;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Guicutthrough;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.GuicutthroughBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Inventory;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.InventoryBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.SeverityType;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.SourceType;
46 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
47 import org.opendaylight.yangtools.yang.binding.CodeHelpers;
48 import org.opendaylight.yangtools.yang.common.Uint32;
49 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
50 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
51 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
52 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
53 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
54 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
55 import org.opendaylight.yangtools.yang.data.api.schema.UnkeyedListEntryNode;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
58
59 public class ORanDOMToInternalDataModel {
60
61     private static final Logger LOG = LoggerFactory.getLogger(ORanDOMToInternalDataModel.class);
62
63     public static List<Inventory> getInventoryList(NodeId nodeId, NormalizedNode hwData) {
64
65         List<Inventory> inventoryResultList = new ArrayList<Inventory>();
66         ContainerNode hwContainer = (ContainerNode) hwData;
67         MapNode componentMap =
68                 (MapNode) hwContainer.getChildByArg(new NodeIdentifier(ORanDeviceManagerQNames.IETF_HW_COMPONENT_LIST));
69         Collection<MapEntryNode> componentMapEntries = componentMap.body();
70
71         for (MapEntryNode componentMapEntryNode : getRootComponents(componentMapEntries)) {
72             inventoryResultList =
73                     recurseGetInventory(nodeId, componentMapEntryNode, componentMapEntries, 0, inventoryResultList);
74         }
75         // Verify if result is complete
76         if (componentMapEntries.size() != inventoryResultList.size()) {
77             LOG.warn(
78                     "Not all data were written to the Inventory. Potential entries with missing "
79                             + "contained-child. Node Id = {}, Components Found = {}, Entries written to Database = {}",
80                     nodeId.getValue(), componentMapEntries.size(), inventoryResultList.size());
81         }
82         return inventoryResultList;
83     }
84
85     private static List<Inventory> recurseGetInventory(NodeId nodeId, MapEntryNode component,
86             Collection<MapEntryNode> componentList, int treeLevel, List<Inventory> inventoryResultList) {
87         //Add element to list, if conversion successfull
88         Optional<Inventory> oInventory = getInternalEquipment(nodeId, component, treeLevel);
89         if (oInventory.isPresent()) {
90             inventoryResultList.add(oInventory.get());
91         }
92         //Walk through list of child keys and add to list
93         for (String childUuid : CodeHelpers.nonnull(new ArrayList<>(ORanDMDOMUtility.getLeafListValue(component,
94                 ORanDeviceManagerQNames.IETF_HW_COMPONENT_LIST_CONTAINS_CHILD)))) {
95             for (MapEntryNode c : getComponentsByName(childUuid, componentList)) {
96                 inventoryResultList = recurseGetInventory(nodeId, c, componentList, treeLevel + 1, inventoryResultList);
97             }
98         }
99         return inventoryResultList;
100     }
101
102     public static List<MapEntryNode> getRootComponents(Collection<MapEntryNode> componentMapEntries) {
103         List<MapEntryNode> resultList = new ArrayList<>();
104         for (MapEntryNode componentMapEntryNode : componentMapEntries) {
105             if (ORanDMDOMUtility.getLeafValue(componentMapEntryNode,
106                     ORanDeviceManagerQNames.IETF_HW_COMPONENT_LIST_PARENT) == null) { // Root elements do not have a parent
107                 resultList.add(componentMapEntryNode);
108             }
109         }
110         return resultList;
111     }
112
113     private static List<MapEntryNode> getComponentsByName(String name, Collection<MapEntryNode> componentList) {
114         List<MapEntryNode> resultList = new ArrayList<>();
115         for (MapEntryNode c : componentList) {
116             if (name.equals(ORanDMDOMUtility.getKeyValue(c))) { // <-- Component list is flat search for child's of name
117                 resultList.add(c);
118             }
119         }
120         return resultList;
121     }
122
123     public static Optional<Inventory> getInternalEquipment(NodeId nodeId, MapEntryNode component, int treeLevel) {
124
125         // Make sure that expected data are not null
126         Objects.requireNonNull(nodeId);
127         Objects.requireNonNull(component);
128
129         // Read mandatory data
130
131         @Nullable
132         String nodeIdString = nodeId.getValue();
133         @Nullable
134         String uuid = ORanDMDOMUtility.getKeyValue(component);
135         @Nullable
136         String idParent =
137                 ORanDMDOMUtility.getLeafValue(component, ORanDeviceManagerQNames.IETF_HW_COMPONENT_LIST_PARENT);
138         @Nullable
139         String uuidParent = idParent != null ? idParent : uuid; //<- Passt nicht
140
141         // do consistency check if all mandatory parameters are there
142         if (treeLevel >= 0 && nodeIdString != null && uuid != null && uuidParent != null) {
143             // Build output data
144
145             InventoryBuilder inventoryBuilder = new InventoryBuilder();
146
147             // General assumed as mandatory
148             inventoryBuilder.setNodeId(nodeIdString);
149             inventoryBuilder.setUuid(uuid);
150             inventoryBuilder.setParentUuid(uuidParent);
151             inventoryBuilder.setTreeLevel(Uint32.valueOf(treeLevel));
152
153             // -- String list with ids of holders (optional)
154             inventoryBuilder.setContainedHolder(ORanDMDOMUtility.getLeafListValue(component,
155                     ORanDeviceManagerQNames.IETF_HW_COMPONENT_LIST_CONTAINS_CHILD));
156
157             // -- Manufacturer related things (optional)
158             @Nullable
159             String mfgName =
160                     ORanDMDOMUtility.getLeafValue(component, ORanDeviceManagerQNames.IETF_HW_COMPONENT_LIST_MFG_NAME);
161             inventoryBuilder.setManufacturerName(mfgName);
162             inventoryBuilder.setManufacturerIdentifier(mfgName);
163
164             // Equipment type (optional)
165             inventoryBuilder.setDescription(
166                     ORanDMDOMUtility.getLeafValue(component, ORanDeviceManagerQNames.IETF_HW_COMPONENT_LIST_DESC));
167             inventoryBuilder.setModelIdentifier(ORanDMDOMUtility.getLeafValue(component,
168                     ORanDeviceManagerQNames.IETF_HW_COMPONENT_LIST_MODEL_NAME));
169
170             inventoryBuilder.setPartTypeId(
171                     ORanDMDOMUtility.getLeafValue(component, ORanDeviceManagerQNames.IETF_HW_COMPONENT_LIST_CLASS));
172
173             inventoryBuilder.setTypeName(ORanDMDOMUtility.getKeyValue(component));
174             inventoryBuilder.setVersion(
175                     ORanDMDOMUtility.getLeafValue(component, ORanDeviceManagerQNames.IETF_HW_COMPONENT_LIST_HW_REV));
176
177             // Equipment instance (optional)
178             @Nullable
179             String mfgDate =
180                     ORanDMDOMUtility.getLeafValue(component, ORanDeviceManagerQNames.IETF_HW_COMPONENT_LIST_MFG_DATE);
181             if (mfgDate != null) {
182                 inventoryBuilder.setDate(mfgDate);
183             }
184             inventoryBuilder.setSerial(
185                     ORanDMDOMUtility.getLeafValue(component, ORanDeviceManagerQNames.IETF_HW_COMPONENT_LIST_SER_NUM));
186
187             return Optional.of(inventoryBuilder.build());
188         }
189         return Optional.empty();
190     }
191
192     /**
193      * If system data is available convert
194      *
195      * @param sys
196      * @return
197      */
198     public static Optional<Guicutthrough> getGuicutthrough(@Nullable AugmentationNode onapSysAugData,
199             @NonNull OnapSystem onapSys) {
200
201         if (onapSysAugData != null) {
202             String name = ORanDMDOMUtility.getLeafValue(onapSysAugData, onapSys.getName());
203             @Nullable
204             Uri uri = new Uri(ORanDMDOMUtility.getLeafValue(onapSysAugData, onapSys.getWebUi()));
205             if (uri.getValue() != null) {
206                 GuicutthroughBuilder gcBuilder = new GuicutthroughBuilder();
207                 if (name != null) {
208                     gcBuilder.setName(name);
209                 }
210                 gcBuilder.setWeburi(uri.getValue());
211                 return Optional.of(gcBuilder.build());
212             }
213             LOG.warn("Uri not set to invoke a Gui cut through session to the device. Please set the Uri in the device");
214         }
215         LOG.warn("Retrieving augmented System details failed. Gui cut through information not available");
216         return Optional.empty();
217     }
218
219     /**
220      * Convert fault notification into data-provider FaultLogEntity
221      *
222      * @param notification with O-RAN notification
223      * @param oranfm
224      * @param nodeId of node to handle
225      * @param counter to be integrated into data
226      * @return FaultlogEntity with data
227      */
228     public static FaultlogEntity getFaultLog(DOMNotification notification, @NonNull ORANFM oranfm, NodeId nodeId) {
229         ContainerNode cn = notification.getBody();
230         FaultlogBuilder faultAlarm = new FaultlogBuilder();
231         faultAlarm.setNodeId(nodeId.getValue());
232         faultAlarm.setObjectId(ORanDMDOMUtility.getLeafValue(cn, oranfm.getFaultSourceQName()));
233         faultAlarm.setProblem(ORanDMDOMUtility.getLeafValue(cn, oranfm.getFaultTextQName()));
234         faultAlarm.setSeverity(getSeverityType(ORanDMDOMUtility.getLeafValue(cn, oranfm.getFaultSeverityQName()),
235                 ORanDMDOMUtility.getLeafValue(cn, oranfm.getFaultIsClearedQName()).equals("true")));
236         faultAlarm.setCounter(Integer.parseInt(ORanDMDOMUtility.getLeafValue(cn, oranfm.getFaultIdQName())));
237         faultAlarm.setId(ORanDMDOMUtility.getLeafValue(cn, oranfm.getFaultIdQName()));
238         faultAlarm.setSourceType(SourceType.Netconf);
239         faultAlarm.setTimestamp(NetconfTimeStampImpl.getConverter()
240                 .getTimeStamp(ORanDMDOMUtility.getLeafValue(cn, oranfm.getFaultEventTimeQName())));
241         return faultAlarm.build();
242     }
243
244     public static FaultlogEntity getFaultLog(UnkeyedListEntryNode activeAlarmEntry, ORANFM oranfm, NodeId nodeId) {
245         FaultlogBuilder faultAlarm = new FaultlogBuilder();
246         faultAlarm.setNodeId(nodeId.getValue());
247         faultAlarm.setObjectId(ORanDMDOMUtility.getLeafValue(activeAlarmEntry, oranfm.getFaultSourceQName()));
248         faultAlarm.setProblem(ORanDMDOMUtility.getLeafValue(activeAlarmEntry, oranfm.getFaultTextQName()));
249         faultAlarm.setSeverity(getSeverityType(
250                 ORanDMDOMUtility.getLeafValue(activeAlarmEntry, oranfm.getFaultSeverityQName()),
251                 ORanDMDOMUtility.getLeafValue(activeAlarmEntry, oranfm.getFaultIsClearedQName()).equals("true")));
252         faultAlarm.setCounter(
253                 Integer.parseInt(ORanDMDOMUtility.getLeafValue(activeAlarmEntry, oranfm.getFaultIdQName())));
254         faultAlarm.setId(ORanDMDOMUtility.getLeafValue(activeAlarmEntry, oranfm.getFaultIdQName()));
255         faultAlarm.setSourceType(SourceType.Netconf);
256         faultAlarm.setTimestamp(NetconfTimeStampImpl.getConverter()
257                 .getTimeStamp(ORanDMDOMUtility.getLeafValue(activeAlarmEntry, oranfm.getFaultEventTimeQName())));
258         return faultAlarm.build();
259     }
260
261     /**
262      * Convert O-RAN specific severity into data-provider severity
263      *
264      * @param faultSeverity O-RAN severity
265      * @param isCleared clear indicator
266      * @return data-provider severity type
267      * @throws IllegalArgumentException if conversion not possible.
268      */
269     private static SeverityType getSeverityType(@Nullable String faultSeverity, @Nullable Boolean isCleared)
270             throws IllegalArgumentException {
271         if (isCleared != null && isCleared) {
272             return SeverityType.NonAlarmed;
273         }
274         if (faultSeverity != null) {
275             switch (faultSeverity) {
276                 case "CRITICAL":
277                     return SeverityType.Critical;
278                 case "MAJOR":
279                     return SeverityType.Major;
280                 case "MINOR":
281                     return SeverityType.Minor;
282                 case "WARNING":
283                     return SeverityType.Warning;
284             }
285         }
286         throw new IllegalArgumentException("Unknown Alarm state represent as Critical. isCleared=" + isCleared
287                 + " faultSeverity=" + faultSeverity);
288     }
289
290 }