72ffdf6bcc3483ff40de9c4ac05522eb8173982b
[ccsdk/features.git] /
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.impl.equipment;
23
24 import java.util.ArrayList;
25 import java.util.Iterator;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Objects;
29 import java.util.Optional;
30
31 import org.eclipse.jdt.annotation.NonNull;
32 import org.eclipse.jdt.annotation.Nullable;
33 import org.onap.ccsdk.features.sdnr.wt.common.YangHelper;
34 import org.onap.ccsdk.features.sdnr.wt.dataprovider.model.DataProvider;
35 import org.onap.ccsdk.features.sdnr.wt.devicemanager.onf14.impl.dataprovider.Onf14ToInternalDataModel;
36 import org.onap.ccsdk.features.sdnr.wt.netconfnodestateservice.NetconfDomAccessor;
37 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
38 import org.opendaylight.yang.gen.v1.urn.onf.yang.core.model._1._4.rev191127.ControlConstruct;
39 import org.opendaylight.yang.gen.v1.urn.onf.yang.core.model._1._4.rev191127.UniversalId;
40 import org.opendaylight.yang.gen.v1.urn.onf.yang.core.model._1._4.rev191127.control.construct.Equipment;
41 import org.opendaylight.yang.gen.v1.urn.onf.yang.core.model._1._4.rev191127.control.construct.EquipmentKey;
42 import org.opendaylight.yang.gen.v1.urn.onf.yang.core.model._1._4.rev191127.equipment.ContainedHolder;
43 import org.opendaylight.yangtools.util.UnmodifiableCollection;
44 import org.opendaylight.yangtools.yang.binding.CodeHelpers;
45 import org.opendaylight.yangtools.yang.common.QName;
46 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
47 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.InstanceIdentifierBuilder;
48 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
49 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
50 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53
54 public class Onf14DomEquipmentManager {
55
56
57     // constants
58     private static final Logger log = LoggerFactory.getLogger(Onf14DomEquipmentManager.class);
59     private static final int EQUIPMENTROOTLEVEL = 0;
60     // end of constants
61
62     // variables
63     private final NetconfDomAccessor netconfDomAccessor;
64     private final DataProvider databaseService;
65     private final Onf14ToInternalDataModel onf14Mapper;
66     // for storing the Equipment UUIDs that are inserted in the DB
67     private final List<UniversalId> equipmentUuidList = new ArrayList<>();
68     // end of variables
69
70     // constructors
71     public Onf14DomEquipmentManager(NetconfDomAccessor netconfDomAccessor, DataProvider databaseService,
72             Onf14ToInternalDataModel onf14Mapper) {
73         super();
74         this.netconfDomAccessor = Objects.requireNonNull(netconfDomAccessor);
75         this.databaseService = Objects.requireNonNull(databaseService);
76         this.onf14Mapper = Objects.requireNonNull(onf14Mapper);
77     }
78     // end of constructors
79
80     // getters and setters
81     public List<UniversalId> getEquipmentUuidList() {
82         return equipmentUuidList;
83     }
84     // end of getters and setters
85
86     // private methods
87     private void addEquipmentToDb(Equipment currentEq, Equipment parentEq, long treeLevel,
88             Map<EquipmentKey, Equipment> equipmentMap, EquipmentKey equipmentKey) {
89         if (currentEq == null) {
90             log.info("Ignore empty equipment with key {}", equipmentKey);
91             return;
92         }
93
94         // if the Equipment UUID is already in the list, it was already processed
95         // needed for solving possible circular dependencies
96         if (equipmentUuidList.contains(currentEq.getUuid())) {
97             log.debug("Not adding equipment with uuid {} because it was aleady added...",
98                     currentEq.getUuid().getValue());
99             return;
100         }
101
102         // we add this to our internal list, such that we avoid circular dependencies
103         equipmentUuidList.add(currentEq.getUuid());
104         log.debug("Adding equipment with uuid {} to the database...", currentEq.getUuid().getValue());
105
106         // we add our current equipment to the database
107         databaseService.writeInventory(
108                 onf14Mapper.getInternalEquipment(netconfDomAccessor.getNodeId(), currentEq, parentEq, treeLevel));
109
110         // we iterate the kids of our current equipment and add them to the database recursively
111         // the actual reference is here: /core-model:control-construct/equipment/contained-holder/occupying-fru
112         for (ContainedHolder holder : YangHelper.getCollection(currentEq.nonnullContainedHolder())) {
113             @Nullable
114             UniversalId occupyingFru = holder.getOccupyingFru();
115             if (occupyingFru != null) {
116                 equipmentKey = new EquipmentKey(occupyingFru);
117                 addEquipmentToDb(equipmentMap.get(equipmentKey), currentEq, treeLevel + 1, equipmentMap, equipmentKey);
118             }
119         }
120     }
121     // end of private methods
122
123     // public methods
124     /**
125      * Set all equipment data from controlConstruct into database and into this manager.
126      *
127      * @param controlConstruct with complete device data
128      */
129     public void setEquipmentData(ControlConstruct controlConstruct) {
130         Objects.requireNonNull(controlConstruct);
131
132         // the top-level-equipment list contains the root objects of the Equipment Model
133         log.debug("Getting list of topLevelEquipment for mountpoint {}", netconfDomAccessor.getNodeId());
134         // adding all root Equipment objects to the DB
135         for (UniversalId uuid : CodeHelpers.nonnull(controlConstruct.getTopLevelEquipment())) {
136             log.debug("Got back topLevelEquipment with uuid {}", uuid.getValue());
137             EquipmentKey equipmentKey = new EquipmentKey(uuid);
138
139             // adding all root Equipment objects to the DB
140             Map<EquipmentKey, Equipment> equipmentMap = controlConstruct.nonnullEquipment();
141             // recursively adding the root equipment and all its children into the DB
142             addEquipmentToDb(equipmentMap.get(equipmentKey), null, EQUIPMENTROOTLEVEL, equipmentMap, equipmentKey);
143         }
144     }
145
146     /**
147      * Read one equipment from device
148      *
149      * @param accessData to access device
150      * @param equipmentUuid uuid of equipment to be read
151      * @return Optional Equipment
152      */
153     public Optional<Equipment> readEquipmentInstance(NetconfDomAccessor accessData, UniversalId equipmentUuid) {
154
155         final Class<?> clazzPac = Equipment.class;
156
157         log.info("DBRead Get equipment for class {} from mountpoint {} for uuid {}", clazzPac.getSimpleName(),
158                 accessData.getNodeId().getValue(), equipmentUuid.getValue());
159
160         InstanceIdentifierBuilder equipmentIIDBuilder =
161                 YangInstanceIdentifier.builder().node(ControlConstruct.QNAME).node(Equipment.QNAME).nodeWithKey(
162                         Equipment.QNAME, QName.create(Equipment.QNAME, "uuid").intern(), equipmentUuid.getValue());
163
164         return accessData.readData(LogicalDatastoreType.CONFIGURATION, equipmentIIDBuilder.build(), Equipment.class);
165     }
166
167     /**
168      * Read one equipment list from device
169      *
170      * @param accessData to access device
171      * @param equipmentUuid uuid of equipment to be read
172      * @return Optional Equipment
173      */
174     public Optional<ControlConstruct> readEquipmentList(NetconfDomAccessor accessData, UniversalId equipmentUuid) {
175
176         log.info("DBRead Get equipment-list for mountpoint {} for uuid {}", accessData.getNodeId().getValue(),
177                 equipmentUuid.getValue());
178
179         YangInstanceIdentifier equipmentIIDBuilder = YangInstanceIdentifier.builder()
180                         .node(ControlConstruct.QNAME)
181                         .node(Equipment.QNAME)
182                         .node(NodeIdentifierWithPredicates.of(Equipment.QNAME))
183                         .build();
184
185         return accessData.readData(LogicalDatastoreType.CONFIGURATION, equipmentIIDBuilder,
186                 ControlConstruct.class);
187     }
188
189     /**
190      * Read one equipment list from device
191      *
192      * @param accessData to access device
193      * @param equipmentUuid uuid of equipment to be read
194      * @return Optional Equipment
195      */
196     public void readTopLevelEquipment(NetconfDomAccessor accessData) {
197
198         log.info("DBRead Get top-level-equipment for mountpoint {}", accessData.getNodeId().getValue());
199
200         InstanceIdentifierBuilder equipmentIIDBuilder = YangInstanceIdentifier.builder().node(ControlConstruct.QNAME)
201                 .node(QName.create(ControlConstruct.QNAME, "top-level-equipment"));
202
203         Optional<NormalizedNode<?, ?>> oData =
204                 accessData.readDataNode(LogicalDatastoreType.CONFIGURATION, equipmentIIDBuilder.build());
205         NormalizedNode<?, ?> data = oData.get();
206         Object value = data.getValue();
207         log.info("DataNode: {} {}", data.getNodeType(), data.getIdentifier());
208         if (value != null) {
209             log.info("DataNode value: {} {}", value.getClass().getName(), value);
210             if (value instanceof UnmodifiableCollection) {
211                 @SuppressWarnings("unchecked")
212                 UnmodifiableCollection<LeafSetEntryNode<String>> topLevelEquipmentCollection = (UnmodifiableCollection<LeafSetEntryNode<String>>) value;
213                 @NonNull
214                 Iterator<LeafSetEntryNode<String>> it = topLevelEquipmentCollection.iterator();
215                 while (it.hasNext()) {
216                     LeafSetEntryNode<String> topLevelEquipmentUuid = it.next();
217                     if (topLevelEquipmentUuid != null) {
218                         log.info("LeafSetEntryNode: {} {} {}", topLevelEquipmentUuid.getValue(), topLevelEquipmentUuid.getNodeType() ,topLevelEquipmentUuid.getValue().getClass().getName());
219                     }
220                 }
221             }
222         }
223     }
224     // end of public methods
225
226     // static methods
227     // end of static methods
228
229     // private classes
230     // end of private classes
231 }