Merge "Correct calculation of zip archive size for schemaset upload"
[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-2023 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.ArrayList;
24 import java.util.Collection;
25 import java.util.LinkedHashMap;
26 import java.util.List;
27 import java.util.Map;
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.yangmodels.YangModelCmHandle;
34 import org.onap.cps.ncmp.api.inventory.CompositeState;
35 import org.onap.cps.ncmp.api.inventory.CompositeStateBuilder;
36 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
37 import org.onap.cps.spi.model.DataNode;
38
39 @NoArgsConstructor(access = AccessLevel.PRIVATE)
40 @Slf4j
41 public class YangDataConverter {
42
43     private static final Pattern cmHandleIdInXpathPattern = Pattern.compile("\\[@id='(.*?)']");
44
45     /**
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
49      */
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         setDmiProperties(dmiProperties, ncmpServiceCmHandle);
58         setPublicProperties(publicProperties, ncmpServiceCmHandle);
59         return ncmpServiceCmHandle;
60     }
61
62     /**
63      * This method convert yang model cm handle properties to simple map.
64      * @param properties the yang model cm handle properties
65      * @param propertiesMap the String, String map for the results
66      */
67     public static void asPropertiesMap(final List<YangModelCmHandle.Property> properties,
68                                        final Map<String, String> propertiesMap) {
69         for (final YangModelCmHandle.Property property : properties) {
70             propertiesMap.put(property.getName(), property.getValue());
71         }
72     }
73
74     /**
75      * This method convert cm handle data node to yang model cm handle.
76      * @param cmHandleDataNode the datanode of the cm handle
77      * @param cmHandleId the id of the cm handle
78      * @return yang model cm handle
79      */
80     public static YangModelCmHandle convertCmHandleToYangModel(final DataNode cmHandleDataNode,
81                                                                final String cmHandleId) {
82         final NcmpServiceCmHandle ncmpServiceCmHandle = new NcmpServiceCmHandle();
83         ncmpServiceCmHandle.setCmHandleId(cmHandleId);
84         populateCmHandleDetails(cmHandleDataNode, ncmpServiceCmHandle);
85         return YangModelCmHandle.toYangModelCmHandle(
86                 (String) cmHandleDataNode.getLeaves().get("dmi-service-name"),
87                 (String) cmHandleDataNode.getLeaves().get("dmi-data-service-name"),
88                 (String) cmHandleDataNode.getLeaves().get("dmi-model-service-name"),
89                 ncmpServiceCmHandle
90         );
91     }
92
93     /**
94      * This method convert cm handle data node to yang model cm handle without using NcmpServiceCmHandle.
95      *
96      * @param cmHandleDataNode the datanode of the cm handle
97      * @param cmHandleId       the id of the cm handle
98      * @return yang model cm handle
99      */
100     public static YangModelCmHandle convertCmHandleToYangModelWithoutNcmpServiceCmHandle(
101             final DataNode cmHandleDataNode,
102             final String cmHandleId) {
103         final Map<String, String> dmiProperties = new LinkedHashMap<>();
104         final Map<String, String> publicProperties = new LinkedHashMap<>();
105         final CompositeStateBuilder compositeStateBuilder = new CompositeStateBuilder();
106         CompositeState compositeState = compositeStateBuilder.build();
107         for (final DataNode childDataNode : cmHandleDataNode.getChildDataNodes()) {
108             if (childDataNode.getXpath().contains("/additional-properties[@name=")) {
109                 addProperty(childDataNode, dmiProperties);
110             } else if (childDataNode.getXpath().contains("/public-properties[@name=")) {
111                 addProperty(childDataNode, publicProperties);
112             } else if (childDataNode.getXpath().endsWith("/state")) {
113                 compositeState = compositeStateBuilder.fromDataNode(childDataNode).build();
114             }
115         }
116         return YangModelCmHandle.toYangModelCmHandleWithoutNcmpServiceHandle(
117                 (String) cmHandleDataNode.getLeaves().get("dmi-service-name"),
118                 (String) cmHandleDataNode.getLeaves().get("dmi-data-service-name"),
119                 (String) cmHandleDataNode.getLeaves().get("dmi-model-service-name"),
120                 cmHandleId,
121                 dmiProperties,
122                 publicProperties,
123                 compositeState
124         );
125     }
126
127     /**
128      * This method convert cm handle data nodes to yang model cm handles.
129      * @param cmHandleDataNodes the datanode of the cm handle
130      * @return yang model cm handles
131      */
132     public static Collection<YangModelCmHandle> convertDataNodesToYangModelCmHandles(
133             final Collection<DataNode> cmHandleDataNodes) {
134         final Collection<YangModelCmHandle> yangModelCmHandles = new ArrayList<>(cmHandleDataNodes.size());
135         cmHandleDataNodes.forEach(dataNode -> {
136             final String cmHandleId = extractCmHandleIdFromXpath(dataNode.getXpath());
137             if (cmHandleId != null) {
138                 yangModelCmHandles.add(convertCmHandleToYangModelWithoutNcmpServiceCmHandle(dataNode, cmHandleId));
139             }
140         });
141         return yangModelCmHandles;
142     }
143
144     private static String extractCmHandleIdFromXpath(final String xpath) {
145         final Matcher matcher = cmHandleIdInXpathPattern.matcher(xpath);
146         if (matcher.find()) {
147             return matcher.group(1);
148         } else {
149             log.error("Unexpected xpath {}", xpath);
150         }
151         return null;
152     }
153
154
155     private static void populateCmHandleDetails(final DataNode cmHandleDataNode,
156                                                 final NcmpServiceCmHandle ncmpServiceCmHandle) {
157         final Map<String, String> dmiProperties = new LinkedHashMap<>();
158         final Map<String, String> publicProperties = new LinkedHashMap<>();
159         final CompositeStateBuilder compositeStateBuilder = new CompositeStateBuilder();
160         CompositeState compositeState = compositeStateBuilder.build();
161         for (final DataNode childDataNode: cmHandleDataNode.getChildDataNodes()) {
162             if (childDataNode.getXpath().contains("/additional-properties[@name=")) {
163                 addProperty(childDataNode, dmiProperties);
164             } else if (childDataNode.getXpath().contains("/public-properties[@name=")) {
165                 addProperty(childDataNode, publicProperties);
166             } else if (childDataNode.getXpath().endsWith("/state")) {
167                 compositeState = compositeStateBuilder.fromDataNode(childDataNode).build();
168             }
169         }
170         ncmpServiceCmHandle.setDmiProperties(dmiProperties);
171         ncmpServiceCmHandle.setPublicProperties(publicProperties);
172         ncmpServiceCmHandle.setCompositeState(compositeState);
173     }
174
175     private static void addProperty(final DataNode propertyDataNode, final Map<String, String> propertiesAsMap) {
176         propertiesAsMap.put(String.valueOf(propertyDataNode.getLeaves().get("name")),
177                 String.valueOf(propertyDataNode.getLeaves().get("value")));
178     }
179
180     private static void setDmiProperties(final List<YangModelCmHandle.Property> dmiProperties,
181                                          final NcmpServiceCmHandle ncmpServiceCmHandle) {
182         final Map<String, String> dmiPropertiesMap = new LinkedHashMap<>(dmiProperties.size());
183         asPropertiesMap(dmiProperties, dmiPropertiesMap);
184         ncmpServiceCmHandle.setDmiProperties(dmiPropertiesMap);
185     }
186
187     private static void setPublicProperties(final List<YangModelCmHandle.Property> publicProperties,
188                                             final NcmpServiceCmHandle ncmpServiceCmHandle) {
189         final Map<String, String> publicPropertiesMap = new LinkedHashMap<>();
190         asPropertiesMap(publicProperties, publicPropertiesMap);
191         ncmpServiceCmHandle.setPublicProperties(publicPropertiesMap);
192     }
193 }