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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
16 * ============LICENSE_END==========================================================================
18 package org.onap.ccsdk.features.sdnr.wt.devicemanager.oran.impl;
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;
42 * Convert data to data-provider model and perform consistency checks.<br>
43 * <b>Component list characteristics:</b><br>
45 * <li>component list is a flat list tree structure specified
46 * <li>via "component.getParent()":
48 * <li>If null we have a root element
49 * <li>if not null it is a child element with generated child level<br>
52 * Example of List:<br>
56 public class ORanToInternalDataModel {
58 private static final Logger log = LoggerFactory.getLogger(ORanToInternalDataModel.class);
60 public static List<Inventory> getInventoryList(NodeId nodeId, Collection<Component> componentList) {
62 List<Inventory> inventoryResultList = new ArrayList<Inventory>();
63 for (Component component : getRootComponents(componentList)) {
64 inventoryResultList = recurseGetInventory(nodeId, component, componentList, 0, inventoryResultList);
66 // Verify if result is complete
67 if (componentList.size() != inventoryResultList.size()) {
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());
73 return inventoryResultList;
76 private static List<Inventory> recurseGetInventory(NodeId nodeId, Component component,
77 Collection<Component> componentList, int treeLevel, List<Inventory> inventoryResultList) {
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());
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);
90 return inventoryResultList;
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
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
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.
121 public static Optional<Inventory> getInternalEquipment(NodeId nodeId, Component component, int treeLevel) {
123 // Make sure that expected data are not null
124 Objects.requireNonNull(nodeId);
125 Objects.requireNonNull(component);
127 // Read manadatory data
130 String nodeIdString = nodeId.getValue();
132 String uuid = component.getName();
134 String idParent = component.getParent();
136 String uuidParent = idParent != null ? idParent : uuid; //<- Passt nicht
138 // do consistency check if all mandatory parameters are there
139 if (treeLevel >= 0 && nodeIdString != null && uuid != null && uuidParent != null) {
143 InventoryBuilder inventoryBuilder = new InventoryBuilder();
145 // General assumed as mandatory
146 inventoryBuilder.setNodeId(nodeIdString);
147 inventoryBuilder.setUuid(uuid);
148 inventoryBuilder.setParentUuid(uuidParent);
149 inventoryBuilder.setTreeLevel(Uint32.valueOf(treeLevel));
151 // -- String list with ids of holders (optional)
152 inventoryBuilder.setContainedHolder(CodeHelpers.nonnull(component.getContainsChild()));
154 // -- Manufacturer related things (optional)
156 String mfgName = component.getMfgName();
157 inventoryBuilder.setManufacturerName(mfgName);
158 inventoryBuilder.setManufacturerIdentifier(mfgName);
160 // Equipment type (optional)
161 inventoryBuilder.setDescription(component.getDescription());
162 inventoryBuilder.setModelIdentifier(component.getModelName());
164 Class<? extends HardwareClass> xmlClass = component.getXmlClass();
165 if (xmlClass != null) {
166 inventoryBuilder.setPartTypeId(xmlClass.getName());
168 inventoryBuilder.setTypeName(component.getName());
169 inventoryBuilder.setVersion(component.getHardwareRev());
171 // Equipment instance (optional)
173 DateAndTime mfgDate = component.getMfgDate();
174 if (mfgDate != null) {
175 inventoryBuilder.setDate(mfgDate.getValue());
177 inventoryBuilder.setSerial(component.getSerialNum());
179 return Optional.of(inventoryBuilder.build());
181 return Optional.empty();
184 public static Optional<Guicutthrough> getGuicutthrough(@Nullable System1 sys) {
186 String name = sys.getName();
188 Uri uri = sys.getWebUi();
190 GuicutthroughBuilder gcBuilder = new GuicutthroughBuilder();
192 gcBuilder.setName(name);
194 gcBuilder.setWeburi(uri.getValue());
195 return Optional.of(gcBuilder.build());
197 log.warn("Uri not set to invoke a Gui cut through session to the device. Please set the Uri in the device");
199 log.warn("Retrieving augmented System details failed. Gui cut through information not available");
200 return Optional.empty();