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