2 * ============LICENSE_START=======================================================
3 * ONAP : ccsdk features
4 * ================================================================================
5 * Copyright (C) 2020 highstreet technologies GmbH Intellectual Property.
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.ccsdk.features.sdnr.wt.devicemanager.onf14.impl.equipment;
24 import java.util.ArrayList;
25 import java.util.Iterator;
26 import java.util.List;
28 import java.util.Objects;
29 import java.util.Optional;
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;
54 public class Onf14DomEquipmentManager {
58 private static final Logger log = LoggerFactory.getLogger(Onf14DomEquipmentManager.class);
59 private static final int EQUIPMENTROOTLEVEL = 0;
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<>();
71 public Onf14DomEquipmentManager(NetconfDomAccessor netconfDomAccessor, DataProvider databaseService,
72 Onf14ToInternalDataModel onf14Mapper) {
74 this.netconfDomAccessor = Objects.requireNonNull(netconfDomAccessor);
75 this.databaseService = Objects.requireNonNull(databaseService);
76 this.onf14Mapper = Objects.requireNonNull(onf14Mapper);
78 // end of constructors
80 // getters and setters
81 public List<UniversalId> getEquipmentUuidList() {
82 return equipmentUuidList;
84 // end of getters and setters
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);
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());
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());
106 // we add our current equipment to the database
107 databaseService.writeInventory(
108 onf14Mapper.getInternalEquipment(netconfDomAccessor.getNodeId(), currentEq, parentEq, treeLevel));
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())) {
114 UniversalId occupyingFru = holder.getOccupyingFru();
115 if (occupyingFru != null) {
116 equipmentKey = new EquipmentKey(occupyingFru);
117 addEquipmentToDb(equipmentMap.get(equipmentKey), currentEq, treeLevel + 1, equipmentMap, equipmentKey);
121 // end of private methods
125 * Set all equipment data from controlConstruct into database and into this manager.
127 * @param controlConstruct with complete device data
129 public void setEquipmentData(ControlConstruct controlConstruct) {
130 Objects.requireNonNull(controlConstruct);
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);
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);
147 * Read one equipment from device
149 * @param accessData to access device
150 * @param equipmentUuid uuid of equipment to be read
151 * @return Optional Equipment
153 public Optional<Equipment> readEquipmentInstance(NetconfDomAccessor accessData, UniversalId equipmentUuid) {
155 final Class<?> clazzPac = Equipment.class;
157 log.info("DBRead Get equipment for class {} from mountpoint {} for uuid {}", clazzPac.getSimpleName(),
158 accessData.getNodeId().getValue(), equipmentUuid.getValue());
160 InstanceIdentifierBuilder equipmentIIDBuilder =
161 YangInstanceIdentifier.builder().node(ControlConstruct.QNAME).node(Equipment.QNAME).nodeWithKey(
162 Equipment.QNAME, QName.create(Equipment.QNAME, "uuid").intern(), equipmentUuid.getValue());
164 return accessData.readData(LogicalDatastoreType.CONFIGURATION, equipmentIIDBuilder.build(), Equipment.class);
168 * Read one equipment list from device
170 * @param accessData to access device
171 * @param equipmentUuid uuid of equipment to be read
172 * @return Optional Equipment
174 public Optional<ControlConstruct> readEquipmentList(NetconfDomAccessor accessData, UniversalId equipmentUuid) {
176 log.info("DBRead Get equipment-list for mountpoint {} for uuid {}", accessData.getNodeId().getValue(),
177 equipmentUuid.getValue());
179 YangInstanceIdentifier equipmentIIDBuilder = YangInstanceIdentifier.builder()
180 .node(ControlConstruct.QNAME)
181 .node(Equipment.QNAME)
182 .node(NodeIdentifierWithPredicates.of(Equipment.QNAME))
185 return accessData.readData(LogicalDatastoreType.CONFIGURATION, equipmentIIDBuilder,
186 ControlConstruct.class);
190 * Read one equipment list from device
192 * @param accessData to access device
193 * @param equipmentUuid uuid of equipment to be read
194 * @return Optional Equipment
196 public void readTopLevelEquipment(NetconfDomAccessor accessData) {
198 log.info("DBRead Get top-level-equipment for mountpoint {}", accessData.getNodeId().getValue());
200 InstanceIdentifierBuilder equipmentIIDBuilder = YangInstanceIdentifier.builder().node(ControlConstruct.QNAME)
201 .node(QName.create(ControlConstruct.QNAME, "top-level-equipment"));
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());
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;
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());
224 // end of public methods
227 // end of static methods
230 // end of private classes