Add bounds to sphinx requirement
[ccsdk/features.git] / sdnr / wt / devicemanager-o-ran-sc / o-ran / ru-fh / provider / src / main / java / org / onap / ccsdk / features / sdnr / wt / devicemanager / oran / impl / binding / ORanToInternalDataModel.java
1 /*
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt
4  * =================================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6  * =================================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License.
16  * ============LICENSE_END==========================================================================
17  */
18 package org.onap.ccsdk.features.sdnr.wt.devicemanager.oran.impl.binding;
19
20 import java.time.Instant;
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.List;
24 import java.util.Objects;
25 import java.util.Optional;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana.hardware.rev180313.HardwareClass;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.hardware.rev180313.hardware.Component;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DateAndTime;
31 import org.opendaylight.yang.gen.v1.urn.o.ran.fm._1._0.rev190204.Alarm.FaultSeverity;
32 import org.opendaylight.yang.gen.v1.urn.o.ran.fm._1._0.rev190204.AlarmNotif;
33 import org.opendaylight.yang.gen.v1.urn.onap.system.rev201026.System1;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.FaultlogBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.FaultlogEntity;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Guicutthrough;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.GuicutthroughBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Inventory;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.InventoryBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.SeverityType;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.SourceType;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
43 import org.opendaylight.yangtools.yang.binding.CodeHelpers;
44 import org.opendaylight.yangtools.yang.common.Uint32;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 /**
49  * Convert data to data-provider model and perform consistency checks.<br>
50  * <b>Component list characteristics:</b><br>
51  * <ul>
52  * <li>component list is a flat list tree structure specified
53  * <li>via "component.getParent()":
54  * <ul>
55  * <li>If null we have a root element
56  * <li>if not null it is a child element with generated child level<br>
57  * </ul>
58  * </ul>
59  * Example of List:<br>
60  *
61  *
62  */
63 public class ORanToInternalDataModel {
64
65     private static final Logger log = LoggerFactory.getLogger(ORanToInternalDataModel.class);
66
67     public static List<Inventory> getInventoryList(NodeId nodeId, Collection<Component> componentList) {
68
69         List<Inventory> inventoryResultList = new ArrayList<Inventory>();
70         for (Component component : getRootComponents(componentList)) {
71             inventoryResultList = recurseGetInventory(nodeId, component, componentList, 0, inventoryResultList);
72         }
73         // Verify if result is complete
74         if (componentList.size() != inventoryResultList.size()) {
75             log.warn(
76                     "Not all data were written to the Inventory. Potential entries with missing "
77                             + "contained-child. Node Id = {}, Components Found = {}, Entries written to Database = {}",
78                     nodeId.getValue(), componentList.size(), inventoryResultList.size());
79         }
80         return inventoryResultList;
81     }
82
83     private static List<Inventory> recurseGetInventory(NodeId nodeId, Component component,
84             Collection<Component> componentList, int treeLevel, List<Inventory> inventoryResultList) {
85
86         //Add element to list, if conversion successfull
87         Optional<Inventory> oInventory = getInternalEquipment(nodeId, component, treeLevel);
88         if (oInventory.isPresent()) {
89             inventoryResultList.add(oInventory.get());
90         }
91         //Walk trough list of child keys and add to list
92         for (String childUuid : CodeHelpers.nonnull(component.getContainsChild())) {
93             for (Component c : getComponentsByName(childUuid, componentList)) {
94                 inventoryResultList = recurseGetInventory(nodeId, c, componentList, treeLevel + 1, inventoryResultList);
95             }
96         }
97         return inventoryResultList;
98     }
99
100     public static List<Component> getRootComponents(Collection<Component> componentList) {
101         List<Component> resultList = new ArrayList<>();
102         for (Component c : componentList) {
103             if (c.getParent() == null) { // Root elements do not have a parent
104                 resultList.add(c);
105             }
106         }
107         return resultList;
108     }
109
110     private static List<Component> getComponentsByName(String name, Collection<Component> componentList) {
111         List<Component> resultList = new ArrayList<>();
112         for (Component c : componentList) {
113             if (name.equals(c.getName())) { // <-- Component list is flat search for child's of name
114                 resultList.add(c);
115             }
116         }
117         return resultList;
118     }
119
120     /**
121      * Convert equipment into Inventory. Decide if inventory can by created from content or not. Public for test case.
122      *
123      * @param nodeId of node (Similar to mountpointId)
124      * @param component to handle
125      * @param treeLevel of components
126      * @return Inventory if possible to be created.
127      */
128     public static Optional<Inventory> getInternalEquipment(NodeId nodeId, Component component, int treeLevel) {
129
130         // Make sure that expected data are not null
131         Objects.requireNonNull(nodeId);
132         Objects.requireNonNull(component);
133
134         // Read manadatory data
135
136         @Nullable
137         String nodeIdString = nodeId.getValue();
138         @Nullable
139         String uuid = component.getName();
140         @Nullable
141         String idParent = component.getParent();
142         @Nullable
143         String uuidParent = idParent != null ? idParent : uuid; //<- Passt nicht
144
145         // do consistency check if all mandatory parameters are there
146         if (treeLevel >= 0 && nodeIdString != null && uuid != null && uuidParent != null) {
147
148             // Build output data
149
150             InventoryBuilder inventoryBuilder = new InventoryBuilder();
151
152             // General assumed as mandatory
153             inventoryBuilder.setNodeId(nodeIdString);
154             inventoryBuilder.setUuid(uuid);
155             inventoryBuilder.setParentUuid(uuidParent);
156             inventoryBuilder.setTreeLevel(Uint32.valueOf(treeLevel));
157
158             // -- String list with ids of holders (optional)
159             inventoryBuilder.setContainedHolder(CodeHelpers.nonnull(component.getContainsChild()));
160
161             // -- Manufacturer related things (optional)
162             @Nullable
163             String mfgName = component.getMfgName();
164             inventoryBuilder.setManufacturerName(mfgName);
165             inventoryBuilder.setManufacturerIdentifier(mfgName);
166
167             // Equipment type (optional)
168             inventoryBuilder.setDescription(component.getDescription());
169             inventoryBuilder.setModelIdentifier(component.getModelName());
170             @Nullable
171             Class<? extends HardwareClass> xmlClass = component.getXmlClass();
172             if (xmlClass != null) {
173                 inventoryBuilder.setPartTypeId(xmlClass.getName());
174             }
175             inventoryBuilder.setTypeName(component.getName());
176             inventoryBuilder.setVersion(component.getHardwareRev());
177
178             // Equipment instance (optional)
179             @Nullable
180             DateAndTime mfgDate = component.getMfgDate();
181             if (mfgDate != null) {
182                 inventoryBuilder.setDate(mfgDate.getValue());
183             }
184             inventoryBuilder.setSerial(component.getSerialNum());
185
186             return Optional.of(inventoryBuilder.build());
187         }
188         return Optional.empty();
189     }
190
191     /**
192      * If system data is available convert
193      *
194      * @param sys
195      * @return
196      */
197     public static Optional<Guicutthrough> getGuicutthrough(@Nullable System1 sys) {
198         if (sys != null) {
199             String name = sys.getName();
200             @Nullable
201             Uri uri = sys.getWebUi();
202             if (uri != null) {
203                 GuicutthroughBuilder gcBuilder = new GuicutthroughBuilder();
204                 if (name != null) {
205                     gcBuilder.setName(name);
206                 }
207                 gcBuilder.setWeburi(uri.getValue());
208                 return Optional.of(gcBuilder.build());
209             }
210             log.warn("Uri not set to invoke a Gui cut through session to the device. Please set the Uri in the device");
211         }
212         log.warn("Retrieving augmented System details failed. Gui cut through information not available");
213         return Optional.empty();
214     }
215
216     /**
217      * Convert netconf time into Instant
218      *
219      * @param eventTime with netconf time
220      * @return Instant with converted time. If not convertable provide Instant.Min
221      */
222     public static Instant getInstantTime(@Nullable DateAndTime eventTime) {
223         return eventTime != null ? Instant.parse(eventTime.getValue()) : Instant.MIN;
224     }
225
226     /**
227      * Convert fault notification into data-provider FaultLogEntity
228      *
229      * @param notification with O-RAN notification
230      * @param nodeId of node to handle
231      * @param counter to be integrated into data
232      * @return FaultlogEntity with data
233      */
234     public static FaultlogEntity getFaultLog(AlarmNotif notification, NodeId nodeId, Integer counter) {
235         FaultlogBuilder faultAlarm = new FaultlogBuilder();
236         faultAlarm.setNodeId(nodeId.getValue());
237         faultAlarm.setObjectId(notification.getFaultSource());
238         faultAlarm.setProblem(notification.getFaultText());
239         faultAlarm.setSeverity(getSeverityType(notification.getFaultSeverity(), notification.getIsCleared()));
240         faultAlarm.setCounter(counter);
241         faultAlarm.setId(String.valueOf(notification.getFaultId()));
242         faultAlarm.setSourceType(SourceType.Netconf);
243         faultAlarm.setTimestamp(notification.getEventTime());
244         return faultAlarm.build();
245     }
246
247     /**
248      * Convert O-RAN specific severity into data-provider severity
249      *
250      * @param faultSeverity O-RAN severity
251      * @param isCleared clear indicator
252      * @return data-provider severity type
253      * @throws IllegalArgumentException if conversion not possible.
254      */
255     public static SeverityType getSeverityType(@Nullable FaultSeverity faultSeverity, @Nullable Boolean isCleared)
256             throws IllegalArgumentException {
257         if (isCleared != null && isCleared) {
258             return SeverityType.NonAlarmed;
259         }
260         if (faultSeverity != null) {
261             switch (faultSeverity) {
262                 case CRITICAL:
263                     return SeverityType.Critical;
264                 case MAJOR:
265                     return SeverityType.Major;
266                 case MINOR:
267                     return SeverityType.Minor;
268                 case WARNING:
269                     return SeverityType.Warning;
270             }
271         }
272         throw new IllegalArgumentException("Unknown Alarm state represent as Critical. isCleared=" + isCleared
273                 + " faultSeverity=" + faultSeverity);
274     }
275
276 }