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