95ff48a9c29c44764152ccc000c44856bd763546
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / utils / YangDataConverter.java
1 /*
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.ncmp.api.impl.utils;
22
23 import java.util.LinkedHashMap;
24 import java.util.List;
25 import java.util.Map;
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;
33
34 @NoArgsConstructor(access = AccessLevel.PRIVATE)
35 public class YangDataConverter {
36
37     /**
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
41      */
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;
52     }
53
54     /**
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
58      */
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());
63         }
64     }
65
66     /**
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
71      */
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"),
81             ncmpServiceCmHandle
82         );
83     }
84
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();
98             }
99         }
100         ncmpServiceCmHandle.setDmiProperties(dmiProperties);
101         ncmpServiceCmHandle.setPublicProperties(publicProperties);
102         ncmpServiceCmHandle.setCompositeState(compositeState);
103     }
104
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")));
108     }
109
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);
115     }
116
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);
122     }
123 }