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