2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022 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.LinkedHashMap;
24 import java.util.List;
26 import lombok.AccessLevel;
27 import lombok.NoArgsConstructor;
28 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
29 import org.onap.cps.ncmp.api.inventory.CompositeState;
30 import org.onap.cps.ncmp.api.inventory.CompositeStateBuilder;
31 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
32 import org.onap.cps.spi.model.DataNode;
34 @NoArgsConstructor(access = AccessLevel.PRIVATE)
35 public class YangDataConverter {
38 * This method convert yang model cm handle to ncmp service cm handle.
39 * @param yangModelCmHandle the yang model of the cm handle
40 * @return ncmp service cm handle
42 public static NcmpServiceCmHandle convertYangModelCmHandleToNcmpServiceCmHandle(
43 final YangModelCmHandle yangModelCmHandle) {
44 final NcmpServiceCmHandle ncmpServiceCmHandle = new NcmpServiceCmHandle();
45 final List<YangModelCmHandle.Property> dmiProperties = yangModelCmHandle.getDmiProperties();
46 final List<YangModelCmHandle.Property> publicProperties = yangModelCmHandle.getPublicProperties();
47 ncmpServiceCmHandle.setCmHandleId(yangModelCmHandle.getId());
48 ncmpServiceCmHandle.setCompositeState(yangModelCmHandle.getCompositeState());
49 setDmiProperties(dmiProperties, ncmpServiceCmHandle);
50 setPublicProperties(publicProperties, ncmpServiceCmHandle);
51 return ncmpServiceCmHandle;
55 * This method convert yang model cm handle properties to simple map.
56 * @param properties the yang model cm handle properties
57 * @param propertiesMap the String, String map for the results
59 public static void asPropertiesMap(final List<YangModelCmHandle.Property> properties,
60 final Map<String, String> propertiesMap) {
61 for (final YangModelCmHandle.Property property : properties) {
62 propertiesMap.put(property.getName(), property.getValue());
67 * This method convert cm handle data node to yang model cm handle.
68 * @param cmHandleDataNode the datanode of the cm handle
69 * @param cmHandleId the id of the cm handle
70 * @return yang model cm handle
72 public static YangModelCmHandle convertCmHandleToYangModel(final DataNode cmHandleDataNode,
73 final String cmHandleId) {
74 final NcmpServiceCmHandle ncmpServiceCmHandle = new NcmpServiceCmHandle();
75 ncmpServiceCmHandle.setCmHandleId(cmHandleId);
76 populateCmHandleDetails(cmHandleDataNode, ncmpServiceCmHandle);
77 return YangModelCmHandle.toYangModelCmHandle(
78 (String) cmHandleDataNode.getLeaves().get("dmi-service-name"),
79 (String) cmHandleDataNode.getLeaves().get("dmi-data-service-name"),
80 (String) cmHandleDataNode.getLeaves().get("dmi-model-service-name"),
85 private static void populateCmHandleDetails(final DataNode cmHandleDataNode,
86 final NcmpServiceCmHandle ncmpServiceCmHandle) {
87 final Map<String, String> dmiProperties = new LinkedHashMap<>();
88 final Map<String, String> publicProperties = new LinkedHashMap<>();
89 final CompositeStateBuilder compositeStateBuilder = new CompositeStateBuilder();
90 CompositeState compositeState = compositeStateBuilder.build();
91 for (final DataNode childDataNode: cmHandleDataNode.getChildDataNodes()) {
92 if (childDataNode.getXpath().contains("/additional-properties[@name=")) {
93 addProperty(childDataNode, dmiProperties);
94 } else if (childDataNode.getXpath().contains("/public-properties[@name=")) {
95 addProperty(childDataNode, publicProperties);
96 } else if (childDataNode.getXpath().endsWith("/state")) {
97 compositeState = compositeStateBuilder.fromDataNode(childDataNode).build();
100 ncmpServiceCmHandle.setDmiProperties(dmiProperties);
101 ncmpServiceCmHandle.setPublicProperties(publicProperties);
102 ncmpServiceCmHandle.setCompositeState(compositeState);
105 private static void addProperty(final DataNode propertyDataNode, final Map<String, String> propertiesAsMap) {
106 propertiesAsMap.put(String.valueOf(propertyDataNode.getLeaves().get("name")),
107 String.valueOf(propertyDataNode.getLeaves().get("value")));
110 private static void setDmiProperties(final List<YangModelCmHandle.Property> dmiProperties,
111 final NcmpServiceCmHandle ncmpServiceCmHandle) {
112 final Map<String, String> dmiPropertiesMap = new LinkedHashMap<>(dmiProperties.size());
113 asPropertiesMap(dmiProperties, dmiPropertiesMap);
114 ncmpServiceCmHandle.setDmiProperties(dmiPropertiesMap);
117 private static void setPublicProperties(final List<YangModelCmHandle.Property> publicProperties,
118 final NcmpServiceCmHandle ncmpServiceCmHandle) {
119 final Map<String, String> publicPropertiesMap = new LinkedHashMap<>();
120 asPropertiesMap(publicProperties, publicPropertiesMap);
121 ncmpServiceCmHandle.setPublicProperties(publicPropertiesMap);