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