Refactor YangDataConverter.convertCmHandleToYangModel
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / utils / YangDataConverter.java
index f8e0659..3954142 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  ============LICENSE_START=======================================================
- *  Copyright (C) 2022-2023 Nordix Foundation
+ *  Copyright (C) 2022-2024 Nordix Foundation
  *  ================================================================================
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
 
 package org.onap.cps.ncmp.api.impl.utils;
 
-import java.util.ArrayList;
 import java.util.Collection;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
+import java.util.stream.Collectors;
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
+import org.onap.cps.ncmp.api.impl.inventory.CompositeState;
+import org.onap.cps.ncmp.api.impl.inventory.CompositeStateBuilder;
 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
-import org.onap.cps.ncmp.api.inventory.CompositeState;
-import org.onap.cps.ncmp.api.inventory.CompositeStateBuilder;
 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
 import org.onap.cps.spi.model.DataNode;
 
@@ -54,6 +54,8 @@ public class YangDataConverter {
         final List<YangModelCmHandle.Property> publicProperties = yangModelCmHandle.getPublicProperties();
         ncmpServiceCmHandle.setCmHandleId(yangModelCmHandle.getId());
         ncmpServiceCmHandle.setCompositeState(yangModelCmHandle.getCompositeState());
+        ncmpServiceCmHandle.setModuleSetTag(yangModelCmHandle.getModuleSetTag());
+        ncmpServiceCmHandle.setAlternateId(yangModelCmHandle.getAlternateId());
         setDmiProperties(dmiProperties, ncmpServiceCmHandle);
         setPublicProperties(publicProperties, ncmpServiceCmHandle);
         return ncmpServiceCmHandle;
@@ -74,53 +76,20 @@ public class YangDataConverter {
     /**
      * This method convert cm handle data node to yang model cm handle.
      * @param cmHandleDataNode the datanode of the cm handle
-     * @param cmHandleId the id of the cm handle
      * @return yang model cm handle
      */
-    public static YangModelCmHandle convertCmHandleToYangModel(final DataNode cmHandleDataNode,
-                                                               final String cmHandleId) {
+    public static YangModelCmHandle convertCmHandleToYangModel(final DataNode cmHandleDataNode) {
         final NcmpServiceCmHandle ncmpServiceCmHandle = new NcmpServiceCmHandle();
+        final String cmHandleId = cmHandleDataNode.getLeaves().get("id").toString();
         ncmpServiceCmHandle.setCmHandleId(cmHandleId);
         populateCmHandleDetails(cmHandleDataNode, ncmpServiceCmHandle);
         return YangModelCmHandle.toYangModelCmHandle(
                 (String) cmHandleDataNode.getLeaves().get("dmi-service-name"),
                 (String) cmHandleDataNode.getLeaves().get("dmi-data-service-name"),
                 (String) cmHandleDataNode.getLeaves().get("dmi-model-service-name"),
-                ncmpServiceCmHandle
-        );
-    }
-
-    /**
-     * This method convert cm handle data node to yang model cm handle without using NcmpServiceCmHandle.
-     *
-     * @param cmHandleDataNode the datanode of the cm handle
-     * @param cmHandleId       the id of the cm handle
-     * @return yang model cm handle
-     */
-    public static YangModelCmHandle convertCmHandleToYangModelWithoutNcmpServiceCmHandle(
-            final DataNode cmHandleDataNode,
-            final String cmHandleId) {
-        final Map<String, String> dmiProperties = new LinkedHashMap<>();
-        final Map<String, String> publicProperties = new LinkedHashMap<>();
-        final CompositeStateBuilder compositeStateBuilder = new CompositeStateBuilder();
-        CompositeState compositeState = compositeStateBuilder.build();
-        for (final DataNode childDataNode : cmHandleDataNode.getChildDataNodes()) {
-            if (childDataNode.getXpath().contains("/additional-properties[@name=")) {
-                addProperty(childDataNode, dmiProperties);
-            } else if (childDataNode.getXpath().contains("/public-properties[@name=")) {
-                addProperty(childDataNode, publicProperties);
-            } else if (childDataNode.getXpath().endsWith("/state")) {
-                compositeState = compositeStateBuilder.fromDataNode(childDataNode).build();
-            }
-        }
-        return YangModelCmHandle.toYangModelCmHandleWithoutNcmpServiceHandle(
-                (String) cmHandleDataNode.getLeaves().get("dmi-service-name"),
-                (String) cmHandleDataNode.getLeaves().get("dmi-data-service-name"),
-                (String) cmHandleDataNode.getLeaves().get("dmi-model-service-name"),
-                cmHandleId,
-                dmiProperties,
-                publicProperties,
-                compositeState
+                ncmpServiceCmHandle,
+                (String) cmHandleDataNode.getLeaves().get("module-set-tag"),
+                (String) cmHandleDataNode.getLeaves().get("alternate-id")
         );
     }
 
@@ -131,24 +100,19 @@ public class YangDataConverter {
      */
     public static Collection<YangModelCmHandle> convertDataNodesToYangModelCmHandles(
             final Collection<DataNode> cmHandleDataNodes) {
-        final Collection<YangModelCmHandle> yangModelCmHandles = new ArrayList<>(cmHandleDataNodes.size());
-        cmHandleDataNodes.forEach(dataNode -> {
-            final String cmHandleId = extractCmHandleIdFromXpath(dataNode.getXpath());
-            if (cmHandleId != null) {
-                yangModelCmHandles.add(convertCmHandleToYangModelWithoutNcmpServiceCmHandle(dataNode, cmHandleId));
-            }
-        });
-        return yangModelCmHandles;
+        return cmHandleDataNodes.stream().map(YangDataConverter::convertCmHandleToYangModel)
+                .collect(Collectors.toList());
     }
 
-    private static String extractCmHandleIdFromXpath(final String xpath) {
+    /**
+     * This method extract cm handle id from xpath of data node.
+     * @param xpath for data node of the cm handle
+     * @return cm handle Id
+     */
+    public static String extractCmHandleIdFromXpath(final String xpath) {
         final Matcher matcher = cmHandleIdInXpathPattern.matcher(xpath);
-        if (matcher.find()) {
-            return matcher.group(1);
-        } else {
-            log.error("Unexpected xpath {}", xpath);
-        }
-        return null;
+        matcher.find();
+        return matcher.group(1);
     }