1f84db41ccc20239dcd2f5d29086d13f5a74bb42
[ccsdk/features.git] /
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;
19
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.List;
23 import java.util.Objects;
24 import java.util.Optional;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.iana.hardware.rev180313.HardwareClass;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.hardware.rev180313.hardware.Component;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.DateAndTime;
30 import org.opendaylight.yang.gen.v1.urn.onap.system.rev201026.System1;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Guicutthrough;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.GuicutthroughBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Inventory;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.InventoryBuilder;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
36 import org.opendaylight.yangtools.yang.binding.CodeHelpers;
37 import org.opendaylight.yangtools.yang.common.Uint32;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 /**
42  * Convert data to data-provider model and perform consistency checks.<br>
43  * <b>Component list characteristics:</b><br>
44  * <ul>
45  * <li>component list is a flat list tree structure specified
46  * <li>via "component.getParent()":
47  * <ul>
48  * <li>If null we have a root element
49  * <li>if not null it is a child element with generated child level<br>
50  * </ul>
51  * </ul>
52  * Example of List:<br>
53  *
54  *
55  */
56 public class ORanToInternalDataModel {
57
58     private static final Logger log = LoggerFactory.getLogger(ORanToInternalDataModel.class);
59
60     public static List<Inventory> getInventoryList(NodeId nodeId, Collection<Component> componentList) {
61
62         List<Inventory> inventoryResultList = new ArrayList<Inventory>();
63         for (Component component : getRootComponents(componentList)) {
64             inventoryResultList = recurseGetInventory(nodeId, component, componentList, 0, inventoryResultList);
65         }
66         // Verify if result is complete
67         if (componentList.size() != inventoryResultList.size()) {
68             log.warn(
69                     "Not all data were written to the Inventory. Potential entries with missing "
70                             + "contained-child. Node Id = {}, Components Found = {}, Entries written to Database = {}",
71                     nodeId.getValue(), componentList.size(), inventoryResultList.size());
72         }
73         return inventoryResultList;
74     }
75
76     private static List<Inventory> recurseGetInventory(NodeId nodeId, Component component,
77             Collection<Component> componentList, int treeLevel, List<Inventory> inventoryResultList) {
78
79         //Add element to list, if conversion successfull
80         Optional<Inventory> oInventory = getInternalEquipment(nodeId, component, treeLevel);
81         if (oInventory.isPresent()) {
82             inventoryResultList.add(oInventory.get());
83         }
84         //Walk trough list of child keys and add to list
85         for (String childUuid : CodeHelpers.nonnull(component.getContainsChild())) {
86             for (Component c : getComponentsByName(childUuid, componentList)) {
87                 inventoryResultList = recurseGetInventory(nodeId, c, componentList, treeLevel + 1, inventoryResultList);
88             }
89         }
90         return inventoryResultList;
91     }
92
93     public static List<Component> getRootComponents(Collection<Component> componentList) {
94         List<Component> resultList = new ArrayList<>();
95         for (Component c : componentList) {
96             if (c.getParent() == null) { // Root elements do not have a parent
97                 resultList.add(c);
98             }
99         }
100         return resultList;
101     }
102
103     private static List<Component> getComponentsByName(String name, Collection<Component> componentList) {
104         List<Component> resultList = new ArrayList<>();
105         for (Component c : componentList) {
106             if (name.equals(c.getName())) { // <-- Component list is flat search for child's of name
107                 resultList.add(c);
108             }
109         }
110         return resultList;
111     }
112
113     /**
114      * Convert equipment into Inventory. Decide if inventory can by created from content or not.
115      * Public for test case.
116      * @param nodeId of node (Similar to mountpointId)
117      * @param component to handle
118      * @param treeLevel of components
119      * @return Inventory if possible to be created.
120      */
121     public static Optional<Inventory> getInternalEquipment(NodeId nodeId, Component component, int treeLevel) {
122
123         // Make sure that expected data are not null
124         Objects.requireNonNull(nodeId);
125         Objects.requireNonNull(component);
126
127         // Read manadatory data
128
129         @Nullable
130         String nodeIdString = nodeId.getValue();
131         @Nullable
132         String uuid = component.getName();
133         @Nullable
134         String idParent = component.getParent();
135         @Nullable
136         String uuidParent = idParent != null ? idParent : uuid; //<- Passt nicht
137
138         // do consistency check if all mandatory parameters are there
139         if (treeLevel >= 0 && nodeIdString != null && uuid != null && uuidParent != null) {
140
141             // Build output data
142
143             InventoryBuilder inventoryBuilder = new InventoryBuilder();
144
145             // General assumed as mandatory
146             inventoryBuilder.setNodeId(nodeIdString);
147             inventoryBuilder.setUuid(uuid);
148             inventoryBuilder.setParentUuid(uuidParent);
149             inventoryBuilder.setTreeLevel(Uint32.valueOf(treeLevel));
150
151             // -- String list with ids of holders (optional)
152             inventoryBuilder.setContainedHolder(CodeHelpers.nonnull(component.getContainsChild()));
153
154             // -- Manufacturer related things (optional)
155             @Nullable
156             String mfgName = component.getMfgName();
157             inventoryBuilder.setManufacturerName(mfgName);
158             inventoryBuilder.setManufacturerIdentifier(mfgName);
159
160             // Equipment type (optional)
161             inventoryBuilder.setDescription(component.getDescription());
162             inventoryBuilder.setModelIdentifier(component.getModelName());
163             @Nullable
164             Class<? extends HardwareClass> xmlClass = component.getXmlClass();
165             if (xmlClass != null) {
166                 inventoryBuilder.setPartTypeId(xmlClass.getName());
167             }
168             inventoryBuilder.setTypeName(component.getName());
169             inventoryBuilder.setVersion(component.getHardwareRev());
170
171             // Equipment instance (optional)
172             @Nullable
173             DateAndTime mfgDate = component.getMfgDate();
174             if (mfgDate != null) {
175                 inventoryBuilder.setDate(mfgDate.getValue());
176             }
177             inventoryBuilder.setSerial(component.getSerialNum());
178
179             return Optional.of(inventoryBuilder.build());
180         }
181         return Optional.empty();
182     }
183
184     public static Optional<Guicutthrough> getGuicutthrough(@Nullable System1 sys) {
185         if (sys != null) {
186             String name = sys.getName();
187             @Nullable
188             Uri uri = sys.getWebUi();
189             if (uri != null) {
190                 GuicutthroughBuilder gcBuilder = new GuicutthroughBuilder();
191                 if (name != null) {
192                     gcBuilder.setName(name);
193                 }
194                 gcBuilder.setWeburi(uri.getValue());
195                 return Optional.of(gcBuilder.build());
196             }
197             log.warn("Uri not set to invoke a Gui cut through session to the device. Please set the Uri in the device");
198         }
199         log.warn("Retrieving augmented System details failed. Gui cut through information not available");
200         return Optional.empty();
201     }
202
203 }