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