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