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<>();
 
  69     public List<String> getEquipmentUuidList() {
 
  70         return equipmentUuidList;
 
  74      * Set all equipment data from controlConstruct into database and into this manager.
 
  76      * @param controlConstruct with complete device data
 
  78     public void setEquipmentData(NormalizedNode controlConstruct) {
 
  79         Objects.requireNonNull(controlConstruct);
 
  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);
 
  92         this.databaseService.writeInventory(netconfDomAccessor.getNodeId().getValue(), inventoryList);
 
  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());
 
 104         return topLvlEqptList;
 
 108      * @param accessData to access device
 
 109      * @param equipmentUuid uuid of equipment to be read
 
 110      * @return Optional Equipment
 
 112     private Optional<NormalizedNode> readEquipmentInstance(NetconfDomAccessor accessData, String equipmentUuid) {
 
 114         log.debug("DBRead Get equipment from mountpoint {} for uuid {}", accessData.getNodeId().getValue(),
 
 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);
 
 123         return accessData.readDataNode(LogicalDatastoreType.CONFIGURATION, equipmentIIDBuilder.build());
 
 126     private List<Inventory> collectEquipment(List<Inventory> list, MapEntryNode currentEq, MapEntryNode parentEq,
 
 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));
 
 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));
 
 142         // we add our current equipment to the database
 
 143         list.add(onf14Mapper.getInternalEquipment(netconfDomAccessor.getNodeId(), currentEq, parentEq, treeLevel));
 
 145         // we iterate the kids of our current equipment and add them to the database
 
 147         // the actual reference is here:
 
 148         // /core-model:control-construct/equipment/contained-holder/occupying-fru
 
 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);
 
 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);