Add sdnr wt devicemanager
[ccsdk/features.git] / sdnr / wt / devicemanager / provider / src / main / java / org / onap / ccsdk / features / sdnr / wt / devicemanager / base / netconf / ONFCoreNetworkElement12Equipment.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.base.netconf;
19
20 import java.util.ArrayList;
21 import java.util.List;
22 import javax.annotation.Nonnull;
23 import javax.annotation.Nullable;
24 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.internalTypes.InventoryInformation;
25 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.netconf.container.Capabilities;
26 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.netconf.container.ValueNameList;
27 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.netconf.util.GenericTransactionUtils;
28 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.netconf.wrapperc.OnfInterfacePac;
29 import org.onap.ccsdk.features.sdnr.wt.devicemanager.base.netconf.wrapperc.WrapperEquipmentPacRev170402;
30 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.database.types.equipment.ExtendedEquipment;
31 import org.onap.ccsdk.features.sdnr.wt.devicemanager.impl.xml.ProblemNotificationXml;
32 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
33 import org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.core.model.rev170320.Equipment;
34 import org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.core.model.rev170320.EquipmentKey;
35 import org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.core.model.rev170320.NetworkElement;
36 import org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.core.model.rev170320.UniversalId;
37 import org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.core.model.rev170320.equipment.g.ContainedHolder;
38 import org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.core.model.rev170320.manufactured.thing.g.EquipmentType;
39 import org.opendaylight.yang.gen.v1.urn.onf.params.xml.ns.yang.core.model.rev170320.manufactured.thing.g.ManufacturerProperties;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 /**
45  * Contains equipment related information of ONFCore Network Element
46  */
47 public class ONFCoreNetworkElement12Equipment {
48
49     private static final Logger LOG = LoggerFactory.getLogger(ONFCoreNetworkElement12Equipment.class);
50
51     private static final UniversalId EQUIPMENTROOT = new UniversalId("network-element");
52     private static final int EQUIPMENTROOTLEVEL = 0;
53
54     private final ONFCOreNetworkElementCoreData coreData;
55     private final OnfInterfacePac equipmentPac;
56
57     private final ValueNameList extensionList;
58     private final List<UniversalId> topLevelEqUuidList;
59     private final List<ProblemNotificationXml> globalProblemList;
60     private final List<ExtendedEquipment> globalEquipmentList;
61
62     public ONFCoreNetworkElement12Equipment(ONFCOreNetworkElementCoreData coreData, Capabilities capabilities) {
63         LOG.debug("Initialize " + ONFCoreNetworkElement12Equipment.class.getName());
64         this.coreData = coreData;
65         if (capabilities.isSupportingNamespaceAndRevision(WrapperEquipmentPacRev170402.QNAME)) {
66             this.equipmentPac = new WrapperEquipmentPacRev170402(coreData);
67             LOG.debug("Equipement pac supported {}", WrapperEquipmentPacRev170402.QNAME);
68         } else {
69             this.equipmentPac = null;
70             LOG.debug("Equipement pac not supported {}", WrapperEquipmentPacRev170402.QNAME);
71         }
72
73         extensionList = new ValueNameList();
74         topLevelEqUuidList = new ArrayList<>();
75         globalProblemList = new ArrayList<>();
76         globalEquipmentList = new ArrayList<>();
77
78         initClassVars();
79     }
80
81     public void addProblemsofNode(List<ProblemNotificationXml> resultList) {
82         resultList.addAll(globalProblemList);
83     }
84
85     public List<ProblemNotificationXml> addProblemsofNodeObject(String uuidString) {
86         List<ProblemNotificationXml> res = new ArrayList<>();
87
88         if (this.equipmentPac != null) {
89             this.equipmentPac.readTheFaults(new UniversalId(uuidString), res);
90         }
91         return res;
92     }
93
94     public @Nonnull InventoryInformation getInventoryInformation(List<String> uuids) {
95         return getInventoryInformation(this.extensionList, uuids);
96     }
97
98     protected void readNetworkElementEquipment() {
99         doSyncNetworkElementEquipmentToClassVars();
100     }
101
102     public String getMountpoint() {
103         return coreData.getMountpoint();
104     }
105
106     public OnfInterfacePac getEquipmentPac() {
107         return equipmentPac;
108     }
109
110     public List<UniversalId> getTopLevelEqUuidList() {
111         return topLevelEqUuidList;
112     }
113
114     public List<ExtendedEquipment> getEquipmentList() {
115         return globalEquipmentList;
116     }
117
118     public List<Equipment> getEquipmentAll() {
119         List<Equipment> equipmentListAll = new ArrayList<>();
120
121         Equipment equipment = readEquipmentAll();
122         equipmentListAll.add(equipment);
123
124         return equipmentListAll;
125     }
126
127
128     /*
129      * --------------------------------------------------------------------------------- private
130      * functions
131      */
132
133     private void initClassVars() {
134         this.globalProblemList.clear();
135         this.globalEquipmentList.clear();
136         this.extensionList.clear();
137         this.topLevelEqUuidList.clear();
138     }
139
140     private void doSyncNetworkElementEquipmentToClassVars() {
141
142         NetworkElement optionalNe = coreData.getOptionalNetworkElement();
143         initClassVars();
144
145         if (optionalNe != null) {
146             // extract Inventory
147             extensionList.put(optionalNe.getExtension());
148
149             if (!extensionList.isEmpty()) {
150
151                 /*
152                  * Loop through network element extension to get "top-level-equipment" <extension>
153                  * <value-name>top-level-equipment</value-name> <value>1.0.BKP,1.0.WCS</value> </extension> "ipv4"
154                  * address
155                  */
156                 extensionList.getAsUniversalIdList("top-level-equipment", topLevelEqUuidList);
157
158                 // If top-level-equipment exists get further information
159                 if (topLevelEqUuidList.isEmpty()) {
160                     LOG.debug("no top level equipment found");
161                 } else {
162                     // Read equipment and problems
163                     for (UniversalId uuid : topLevelEqUuidList) {
164                         recurseReadEquipmentProblems(uuid, EQUIPMENTROOT, EQUIPMENTROOTLEVEL, globalProblemList,
165                                 globalEquipmentList);
166                     }
167                 }
168             } else {
169                 LOG.debug("extension list is null");
170             }
171         }
172     }
173
174     private void recurseReadEquipmentProblems(UniversalId uuid, UniversalId parentUuid, int treeLevel,
175             List<ProblemNotificationXml> problemList, List<ExtendedEquipment> equipmentList) {
176
177         if (uuid != null) {
178
179             Equipment equipment = this.readEquipment(uuid);
180
181             if (equipment != null) {
182                 equipmentList.add(new ExtendedEquipment(parentUuid.getValue(), equipment, treeLevel));
183
184                 if (this.equipmentPac != null) {
185                     this.equipmentPac.readTheFaults(uuid, problemList);
186
187                     List<ContainedHolder> containedHolderListe = equipment.getContainedHolder();
188                     if (containedHolderListe != null) {
189                         for (ContainedHolder containedHolder : containedHolderListe) {
190                             recurseReadEquipmentProblems(containedHolder.getOccupyingFru(), uuid, treeLevel + 1,
191                                     problemList, equipmentList);
192                         }
193                     }
194                 }
195             }
196         }
197     }
198
199     private @Nonnull InventoryInformation getInventoryInformation(ValueNameList extensions, List<String> uuids) {
200
201         InventoryInformation inventoryInformation = new InventoryInformation();
202
203         // uuids
204         inventoryInformation.setInterfaceUuidList(uuids);
205
206         if (!extensions.isEmpty()) {
207
208             inventoryInformation.setDeviceIpv4(extensions.getOrNull("neIpAddress"));
209
210             // If top-level-equipment exists get further information
211             if (topLevelEqUuidList.isEmpty()) {
212                 LOG.debug("no top level equipment found");
213             } else {
214                 //
215                 if (!globalEquipmentList.isEmpty()) {
216                     Equipment e = globalEquipmentList.get(0).getEquipment();
217                     if (e.getManufacturedThing() != null) {
218                         EquipmentType et;
219                         if ((et = e.getManufacturedThing().getEquipmentType()) != null) {
220                             inventoryInformation.setType(et.getTypeName());
221                             inventoryInformation.setModel(et.getModelIdentifier());
222                         }
223                         ManufacturerProperties em;
224                         if ((em = e.getManufacturedThing().getManufacturerProperties()) != null) {
225                             inventoryInformation.setVendor(em.getManufacturerIdentifier());
226                         }
227                     }
228                 }
229             }
230         } else {
231             LOG.debug("extension list is null");
232         }
233
234         LOG.debug("Inventory: {}", inventoryInformation);
235         return inventoryInformation;
236
237     }
238
239
240     /**
241      * Read equipment information
242      *
243      * @param interfacePacUuid uuid as key for Equipment.
244      * @return Equipment or null
245      */
246     private @Nullable Equipment readEquipment(UniversalId interfacePacUuid) {
247
248         final Class<?> clazzPac = Equipment.class;
249
250         LOG.info("DBRead Get equipment for class {} from mountpoint {} for uuid {}", clazzPac.getSimpleName(),
251                 coreData.getMountpoint(), interfacePacUuid.getValue());
252
253         InstanceIdentifier<Equipment> equipmentIID =
254                 InstanceIdentifier.builder(Equipment.class, new EquipmentKey(interfacePacUuid)).build();
255
256         Equipment res = GenericTransactionUtils.readData(coreData.getDataBroker(), LogicalDatastoreType.OPERATIONAL,
257                 equipmentIID);
258
259         return res;
260     }
261
262     /**
263      * Read equipment information
264      *
265      * @param interfacePacUuid uuid as key for Equipment.
266      * @return Equipment or null
267      */
268     private @Nullable Equipment readEquipmentAll() {
269
270         final Class<?> clazzPac = Equipment.class;
271
272         LOG.info("DBRead Get all equipment for class {} from mountpoint {}", clazzPac.getSimpleName(),
273                 coreData.getMountpoint());
274
275         InstanceIdentifier<Equipment> equipmentIID = InstanceIdentifier.builder(Equipment.class).build();
276
277         Equipment res = GenericTransactionUtils.readData(coreData.getDataBroker(), LogicalDatastoreType.OPERATIONAL,
278                 equipmentIID);
279
280         return res;
281     }
282
283     /**
284      * specific toString()
285      */
286     @Override
287     public String toString() {
288         return "ONFCoreNetworkElement12Equipment [coreData=" + coreData + ", equipmentPac=" + equipmentPac
289                 + ", extensions=" + extensionList + ", topLevelEqUuidList=" + topLevelEqUuidList + ", problemList="
290                 + globalProblemList + ", equipmentList=" + globalEquipmentList + "]";
291     }
292
293 }