2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022-2024 Nordix Foundation
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.ncmp.api.impl.utils;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.LinkedHashMap;
26 import java.util.List;
28 import java.util.regex.Matcher;
29 import java.util.regex.Pattern;
30 import lombok.AccessLevel;
31 import lombok.NoArgsConstructor;
32 import lombok.extern.slf4j.Slf4j;
33 import org.onap.cps.ncmp.api.impl.inventory.CompositeState;
34 import org.onap.cps.ncmp.api.impl.inventory.CompositeStateBuilder;
35 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
36 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
37 import org.onap.cps.spi.model.DataNode;
39 @NoArgsConstructor(access = AccessLevel.PRIVATE)
41 public class YangDataConverter {
43 private static final Pattern cmHandleIdInXpathPattern = Pattern.compile("\\[@id='(.*?)']");
46 * This method convert yang model cm handle to ncmp service cm handle.
47 * @param yangModelCmHandle the yang model of the cm handle
48 * @return ncmp service cm handle
50 public static NcmpServiceCmHandle convertYangModelCmHandleToNcmpServiceCmHandle(
51 final YangModelCmHandle yangModelCmHandle) {
52 final NcmpServiceCmHandle ncmpServiceCmHandle = new NcmpServiceCmHandle();
53 final List<YangModelCmHandle.Property> dmiProperties = yangModelCmHandle.getDmiProperties();
54 final List<YangModelCmHandle.Property> publicProperties = yangModelCmHandle.getPublicProperties();
55 ncmpServiceCmHandle.setCmHandleId(yangModelCmHandle.getId());
56 ncmpServiceCmHandle.setCompositeState(yangModelCmHandle.getCompositeState());
57 ncmpServiceCmHandle.setModuleSetTag(yangModelCmHandle.getModuleSetTag());
58 ncmpServiceCmHandle.setAlternateId(yangModelCmHandle.getAlternateId());
59 setDmiProperties(dmiProperties, ncmpServiceCmHandle);
60 setPublicProperties(publicProperties, ncmpServiceCmHandle);
61 return ncmpServiceCmHandle;
65 * This method convert yang model cm handle properties to simple map.
66 * @param properties the yang model cm handle properties
67 * @param propertiesMap the String, String map for the results
69 public static void asPropertiesMap(final List<YangModelCmHandle.Property> properties,
70 final Map<String, String> propertiesMap) {
71 for (final YangModelCmHandle.Property property : properties) {
72 propertiesMap.put(property.getName(), property.getValue());
77 * This method convert cm handle data node to yang model cm handle.
78 * @param cmHandleDataNode the datanode of the cm handle
79 * @param cmHandleId the id of the cm handle
80 * @return yang model cm handle
82 public static YangModelCmHandle convertCmHandleToYangModel(final DataNode cmHandleDataNode,
83 final String cmHandleId) {
84 final NcmpServiceCmHandle ncmpServiceCmHandle = new NcmpServiceCmHandle();
85 ncmpServiceCmHandle.setCmHandleId(cmHandleId);
86 populateCmHandleDetails(cmHandleDataNode, ncmpServiceCmHandle);
87 return YangModelCmHandle.toYangModelCmHandle(
88 (String) cmHandleDataNode.getLeaves().get("dmi-service-name"),
89 (String) cmHandleDataNode.getLeaves().get("dmi-data-service-name"),
90 (String) cmHandleDataNode.getLeaves().get("dmi-model-service-name"),
92 (String) cmHandleDataNode.getLeaves().get("module-set-tag"),
93 (String) cmHandleDataNode.getLeaves().get("alternate-id")
98 * This method convert cm handle data nodes to yang model cm handles.
99 * @param cmHandleDataNodes the datanode of the cm handle
100 * @return yang model cm handles
102 public static Collection<YangModelCmHandle> convertDataNodesToYangModelCmHandles(
103 final Collection<DataNode> cmHandleDataNodes) {
104 final Collection<YangModelCmHandle> yangModelCmHandles = new ArrayList<>(cmHandleDataNodes.size());
105 cmHandleDataNodes.forEach(dataNode -> {
106 final String cmHandleId = extractCmHandleIdFromXpath(dataNode.getXpath());
107 yangModelCmHandles.add(convertCmHandleToYangModel(dataNode, cmHandleId));
109 return yangModelCmHandles;
113 * This method extract cm handle id from xpath of data node.
114 * @param xpath for data node of the cm handle
115 * @return cm handle Id
117 public static String extractCmHandleIdFromXpath(final String xpath) {
118 final Matcher matcher = cmHandleIdInXpathPattern.matcher(xpath);
120 return matcher.group(1);
124 private static void populateCmHandleDetails(final DataNode cmHandleDataNode,
125 final NcmpServiceCmHandle ncmpServiceCmHandle) {
126 final Map<String, String> dmiProperties = new LinkedHashMap<>();
127 final Map<String, String> publicProperties = new LinkedHashMap<>();
128 final CompositeStateBuilder compositeStateBuilder = new CompositeStateBuilder();
129 CompositeState compositeState = compositeStateBuilder.build();
130 for (final DataNode childDataNode: cmHandleDataNode.getChildDataNodes()) {
131 if (childDataNode.getXpath().contains("/additional-properties[@name=")) {
132 addProperty(childDataNode, dmiProperties);
133 } else if (childDataNode.getXpath().contains("/public-properties[@name=")) {
134 addProperty(childDataNode, publicProperties);
135 } else if (childDataNode.getXpath().endsWith("/state")) {
136 compositeState = compositeStateBuilder.fromDataNode(childDataNode).build();
139 ncmpServiceCmHandle.setDmiProperties(dmiProperties);
140 ncmpServiceCmHandle.setPublicProperties(publicProperties);
141 ncmpServiceCmHandle.setCompositeState(compositeState);
144 private static void addProperty(final DataNode propertyDataNode, final Map<String, String> propertiesAsMap) {
145 propertiesAsMap.put(String.valueOf(propertyDataNode.getLeaves().get("name")),
146 String.valueOf(propertyDataNode.getLeaves().get("value")));
149 private static void setDmiProperties(final List<YangModelCmHandle.Property> dmiProperties,
150 final NcmpServiceCmHandle ncmpServiceCmHandle) {
151 final Map<String, String> dmiPropertiesMap = new LinkedHashMap<>(dmiProperties.size());
152 asPropertiesMap(dmiProperties, dmiPropertiesMap);
153 ncmpServiceCmHandle.setDmiProperties(dmiPropertiesMap);
156 private static void setPublicProperties(final List<YangModelCmHandle.Property> publicProperties,
157 final NcmpServiceCmHandle ncmpServiceCmHandle) {
158 final Map<String, String> publicPropertiesMap = new LinkedHashMap<>();
159 asPropertiesMap(publicProperties, publicPropertiesMap);
160 ncmpServiceCmHandle.setPublicProperties(publicPropertiesMap);