/*
* ============LICENSE_START=======================================================
- * Copyright (C) 2022 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.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;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
+@Slf4j
public class YangDataConverter {
+ private static final Pattern cmHandleIdInXpathPattern = Pattern.compile("\\[@id='(.*?)']");
+
/**
* This method convert yang model cm handle to ncmp service cm handle.
* @param yangModelCmHandle the yang model of the cm handle
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;
/**
* 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
+ ncmpServiceCmHandle,
+ (String) cmHandleDataNode.getLeaves().get("module-set-tag"),
+ (String) cmHandleDataNode.getLeaves().get("alternate-id")
);
}
+ /**
+ * This method convert cm handle data nodes to yang model cm handles.
+ * @param cmHandleDataNodes the datanode of the cm handle
+ * @return yang model cm handles
+ */
+ public static Collection<YangModelCmHandle> convertDataNodesToYangModelCmHandles(
+ final Collection<DataNode> cmHandleDataNodes) {
+ return cmHandleDataNodes.stream().map(YangDataConverter::convertCmHandleToYangModel)
+ .collect(Collectors.toList());
+ }
+
+ /**
+ * 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);
+ matcher.find();
+ return matcher.group(1);
+ }
+
+
private static void populateCmHandleDetails(final DataNode cmHandleDataNode,
final NcmpServiceCmHandle ncmpServiceCmHandle) {
final Map<String, String> dmiProperties = new LinkedHashMap<>();
}
private static void setDmiProperties(final List<YangModelCmHandle.Property> dmiProperties,
- final NcmpServiceCmHandle ncmpServiceCmHandle) {
+ final NcmpServiceCmHandle ncmpServiceCmHandle) {
final Map<String, String> dmiPropertiesMap = new LinkedHashMap<>(dmiProperties.size());
asPropertiesMap(dmiProperties, dmiPropertiesMap);
ncmpServiceCmHandle.setDmiProperties(dmiPropertiesMap);
}
private static void setPublicProperties(final List<YangModelCmHandle.Property> publicProperties,
- final NcmpServiceCmHandle ncmpServiceCmHandle) {
+ final NcmpServiceCmHandle ncmpServiceCmHandle) {
final Map<String, String> publicPropertiesMap = new LinkedHashMap<>();
asPropertiesMap(publicProperties, publicPropertiesMap);
ncmpServiceCmHandle.setPublicProperties(publicPropertiesMap);