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