Support for PM (Performance Management)
[ccsdk/features.git] / sdnr / wt / devicemanager-onap / onf14 / provider / src / main / java / org / onap / ccsdk / features / sdnr / wt / devicemanager / onf14 / dom / impl / equipment / Onf14DomEquipmentManager.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  *
21  */
22 package org.onap.ccsdk.features.sdnr.wt.devicemanager.onf14.dom.impl.equipment;
23
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.Objects;
29 import java.util.Optional;
30 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.DataProvider;
31 import org.onap.ccsdk.features.sdnr.wt.devicemanager.onf14.dom.impl.dataprovider.Onf14DomToInternalDataModel;
32 import org.onap.ccsdk.features.sdnr.wt.devicemanager.onf14.dom.impl.util.Onf14DMDOMUtility;
33 import org.onap.ccsdk.features.sdnr.wt.devicemanager.onf14.dom.impl.util.Onf14DevicemanagerQNames;
34 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfDomAccessor;
35 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.Inventory;
37 import org.opendaylight.yangtools.yang.common.QName;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.InstanceIdentifierBuilder;
40 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
41 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
42 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
43 import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
44 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 public class Onf14DomEquipmentManager {
49
50     private static final Logger log = LoggerFactory.getLogger(Onf14DomEquipmentManager.class);
51     private static final int EQUIPMENTROOTLEVEL = 0;
52
53     private final NetconfDomAccessor netconfDomAccessor;
54     private final DataProvider databaseService;
55     private final Onf14DomToInternalDataModel onf14Mapper;
56     private final List<String> equipmentUuidList;
57
58     public Onf14DomEquipmentManager(NetconfDomAccessor netconfDomAccessor, DataProvider databaseService,
59             Onf14DomToInternalDataModel onf14Mapper) {
60         super();
61         this.netconfDomAccessor = Objects.requireNonNull(netconfDomAccessor);
62         this.databaseService = Objects.requireNonNull(databaseService);
63         this.onf14Mapper = Objects.requireNonNull(onf14Mapper);
64
65         this.equipmentUuidList = new ArrayList<>();
66     }
67
68     // public methods
69     public List<String> getEquipmentUuidList() {
70         return equipmentUuidList;
71     }
72
73     /**
74      * Set all equipment data from controlConstruct into database and into this manager.
75      *
76      * @param controlConstruct with complete device data
77      */
78     public void setEquipmentData(NormalizedNode controlConstruct) {
79         Objects.requireNonNull(controlConstruct);
80
81         // the top-level-equipment list contains the root objects of the Equipment Model
82         log.debug("Iterating through the list of topLevelEquipment for mountpoint {}", netconfDomAccessor.getNodeId());
83         // adding all root Equipment objects to the DB
84         List<Inventory> inventoryList = new ArrayList<>();
85         for (String uuid : getTopLevelEquipment(controlConstruct)) {
86             Optional<NormalizedNode> equipment = readEquipmentInstance(netconfDomAccessor, uuid);
87             MapEntryNode equipmentEntry = (MapEntryNode) equipment.get();
88             if (equipmentEntry != null) {
89                 collectEquipment(inventoryList, equipmentEntry, null, EQUIPMENTROOTLEVEL);
90             }
91         }
92         this.databaseService.writeInventory(netconfDomAccessor.getNodeId().getValue(), inventoryList);
93
94     }
95
96     private List<String> getTopLevelEquipment(NormalizedNode transformedInput) {
97         List<String> topLvlEqptList = new ArrayList<>();
98         Collection<?> topLevelEqptListColl = (Collection<?>) transformedInput.body();
99         Iterator<?> childEntryItr = topLevelEqptListColl.iterator();
100         while (childEntryItr.hasNext()) {
101             LeafSetEntryNode<?> childEntryNode = (LeafSetEntryNode<?>) childEntryItr.next();
102             topLvlEqptList.add((String) childEntryNode.body());
103         }
104         return topLvlEqptList;
105     }
106
107     /**
108      * @param accessData to access device
109      * @param equipmentUuid uuid of equipment to be read
110      * @return Optional Equipment
111      */
112     private Optional<NormalizedNode> readEquipmentInstance(NetconfDomAccessor accessData, String equipmentUuid) {
113
114         log.debug("DBRead Get equipment from mountpoint {} for uuid {}", accessData.getNodeId().getValue(),
115                 equipmentUuid);
116
117         InstanceIdentifierBuilder equipmentIIDBuilder = YangInstanceIdentifier.builder()
118                 .node(Onf14DevicemanagerQNames.CORE_MODEL_CONTROL_CONSTRUCT_CONTAINER)
119                 .node(Onf14DevicemanagerQNames.CORE_MODEL_CC_EQPT)
120                 .nodeWithKey(Onf14DevicemanagerQNames.CORE_MODEL_CC_EQPT,
121                         QName.create(Onf14DevicemanagerQNames.CORE_MODEL_CC_EQPT, "uuid").intern(), equipmentUuid);
122
123         return accessData.readDataNode(LogicalDatastoreType.CONFIGURATION, equipmentIIDBuilder.build());
124     }
125
126     private List<Inventory> collectEquipment(List<Inventory> list, MapEntryNode currentEq, MapEntryNode parentEq,
127             long treeLevel) {
128
129         // if the Equipment UUID is already in the list, it was already processed
130         // needed for solving possible circular dependencies
131         if (equipmentUuidList.contains(Onf14DMDOMUtility.getUuidFromEquipment(currentEq))) {
132             log.debug("Not adding equipment with uuid {} because it was aleady added...",
133                     Onf14DMDOMUtility.getUuidFromEquipment(currentEq));
134             return list;
135         }
136
137         // we add this to our internal list, such that we avoid circular dependencies
138         equipmentUuidList.add(Onf14DMDOMUtility.getUuidFromEquipment(currentEq));
139         log.debug("Adding equipment with uuid {} to the database...",
140                 Onf14DMDOMUtility.getUuidFromEquipment(currentEq));
141
142         // we add our current equipment to the database
143         list.add(onf14Mapper.getInternalEquipment(netconfDomAccessor.getNodeId(), currentEq, parentEq, treeLevel));
144
145         // we iterate the kids of our current equipment and add them to the database
146         // recursively
147         // the actual reference is here:
148         // /core-model:control-construct/equipment/contained-holder/occupying-fru
149
150         MapNode containedHolderMap = (MapNode) currentEq
151                 .childByArg(new NodeIdentifier(Onf14DevicemanagerQNames.CORE_MODEL_CC_EQPT_CONTAINED_HOLDER));
152         if (containedHolderMap != null) {
153             Collection<MapEntryNode> containedHolderCollection = containedHolderMap.body();
154             for (MapEntryNode holder : containedHolderCollection) {
155                 String occupyingFru = Onf14DMDOMUtility.getLeafValue(holder,
156                         Onf14DevicemanagerQNames.CORE_MODEL_CC_EQPT_OCCUPYING_FRU);
157
158                 if (occupyingFru != null) {
159                     Optional<NormalizedNode> childEq = readEquipmentInstance(netconfDomAccessor, occupyingFru);
160                     if (childEq.isPresent()) {
161                         // current becomes parent and tree level increases by 1
162                         collectEquipment(list, (MapEntryNode) childEq.get(), currentEq, treeLevel + 1);
163                     }
164                 }
165             }
166         }
167
168         return list;
169     }
170
171 }