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.dom.impl.equipment;
 
  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;
 
  48 public class Onf14DomEquipmentManager {
 
  50     private static final Logger log = LoggerFactory.getLogger(Onf14DomEquipmentManager.class);
 
  51     private static final int EQUIPMENTROOTLEVEL = 0;
 
  53     private final NetconfDomAccessor netconfDomAccessor;
 
  54     private final DataProvider databaseService;
 
  55     private final Onf14DomToInternalDataModel onf14Mapper;
 
  56     private final List<String> equipmentUuidList;
 
  58     public Onf14DomEquipmentManager(NetconfDomAccessor netconfDomAccessor, DataProvider databaseService,
 
  59             Onf14DomToInternalDataModel onf14Mapper) {
 
  61         this.netconfDomAccessor = Objects.requireNonNull(netconfDomAccessor);
 
  62         this.databaseService = Objects.requireNonNull(databaseService);
 
  63         this.onf14Mapper = Objects.requireNonNull(onf14Mapper);
 
  65         this.equipmentUuidList = new ArrayList<>();
 
  68     public List<String> getEquipmentUuidList() {
 
  69         return equipmentUuidList;
 
  72     private List<Inventory> collectEquipment(List<Inventory> list, MapEntryNode currentEq, MapEntryNode parentEq,
 
  75         // if the Equipment UUID is already in the list, it was already processed
 
  76         // needed for solving possible circular dependencies
 
  77         if (equipmentUuidList.contains(Onf14DMDOMUtility.getUuidFromEquipment(currentEq))) {
 
  78             log.debug("Not adding equipment with uuid {} because it was aleady added...",
 
  79                     Onf14DMDOMUtility.getUuidFromEquipment(currentEq));
 
  83         // we add this to our internal list, such that we avoid circular dependencies
 
  84         equipmentUuidList.add(Onf14DMDOMUtility.getUuidFromEquipment(currentEq));
 
  85         log.debug("Adding equipment with uuid {} to the database...",
 
  86                 Onf14DMDOMUtility.getUuidFromEquipment(currentEq));
 
  88         // we add our current equipment to the database
 
  89         list.add(onf14Mapper.getInternalEquipment(netconfDomAccessor.getNodeId(), currentEq, parentEq, treeLevel));
 
  91         // we iterate the kids of our current equipment and add them to the database
 
  93         // the actual reference is here:
 
  94         // /core-model:control-construct/equipment/contained-holder/occupying-fru
 
  96         MapNode containedHolderMap = (MapNode) currentEq
 
  97                 .childByArg(new NodeIdentifier(Onf14DevicemanagerQNames.CORE_MODEL_CC_EQPT_CONTAINED_HOLDER));
 
  98         if (containedHolderMap != null) {
 
  99             Collection<MapEntryNode> containedHolderCollection = containedHolderMap.body();
 
 100             for (MapEntryNode holder : containedHolderCollection) {
 
 101                 String occupyingFru = Onf14DMDOMUtility.getLeafValue(holder,
 
 102                         Onf14DevicemanagerQNames.CORE_MODEL_CC_EQPT_OCCUPYING_FRU);
 
 104                 if (occupyingFru != null) {
 
 105                     Optional<NormalizedNode> childEq = readEquipmentInstance(netconfDomAccessor, occupyingFru);
 
 106                     if (childEq.isPresent()) {
 
 107                         // current becomes parent and tree level increases by 1
 
 108                         collectEquipment(list, (MapEntryNode) childEq.get(), currentEq, treeLevel + 1);
 
 119      * Set all equipment data from controlConstruct into database and into this manager.
 
 121      * @param controlConstruct with complete device data
 
 123     public void setEquipmentData(NormalizedNode controlConstruct) {
 
 124         Objects.requireNonNull(controlConstruct);
 
 126         // the top-level-equipment list contains the root objects of the Equipment Model
 
 127         log.debug("Getting list of topLevelEquipment for mountpoint {}", netconfDomAccessor.getNodeId());
 
 128         // adding all root Equipment objects to the DB
 
 129         List<Inventory> inventoryList = new ArrayList<>();
 
 130         for (String uuid : getTopLevelEquipment(controlConstruct)) {
 
 131             Optional<NormalizedNode> equipment = readEquipmentInstance(netconfDomAccessor, uuid);
 
 132             MapEntryNode equipmentEntry = (MapEntryNode) equipment.get();
 
 133             if (equipmentEntry != null) {
 
 134                 collectEquipment(inventoryList, equipmentEntry, null, EQUIPMENTROOTLEVEL);
 
 137         this.databaseService.writeInventory(netconfDomAccessor.getNodeId().getValue(), inventoryList);
 
 141     private List<String> getTopLevelEquipment(NormalizedNode transformedInput) {
 
 142         List<String> topLvlEqptList = new ArrayList<>();
 
 143         Collection<?> topLevelEqptListColl = (Collection<?>) transformedInput.body();
 
 144         Iterator<?> childEntryItr = topLevelEqptListColl.iterator();
 
 145         while (childEntryItr.hasNext()) {
 
 146             LeafSetEntryNode<?> childEntryNode = (LeafSetEntryNode<?>) childEntryItr.next();
 
 147             topLvlEqptList.add((String) childEntryNode.body());
 
 149         return topLvlEqptList;
 
 153      * @param accessData to access device
 
 154      * @param equipmentUuid uuid of equipment to be read
 
 155      * @return Optional Equipment
 
 157     private Optional<NormalizedNode> readEquipmentInstance(NetconfDomAccessor accessData, String equipmentUuid) {
 
 159         log.info("DBRead Get equipment from mountpoint {} for uuid {}", accessData.getNodeId().getValue(),
 
 162         InstanceIdentifierBuilder equipmentIIDBuilder = YangInstanceIdentifier.builder()
 
 163                 .node(Onf14DevicemanagerQNames.CORE_MODEL_CONTROL_CONSTRUCT_CONTAINER)
 
 164                 .node(Onf14DevicemanagerQNames.CORE_MODEL_CC_EQPT)
 
 165                 .nodeWithKey(Onf14DevicemanagerQNames.CORE_MODEL_CC_EQPT,
 
 166                         QName.create(Onf14DevicemanagerQNames.CORE_MODEL_CC_EQPT, "uuid").intern(), equipmentUuid);
 
 168         return accessData.readDataNode(LogicalDatastoreType.CONFIGURATION, equipmentIIDBuilder.build());