2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2025-2026 OpenInfra Foundation Europe
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.impl.data.policyexecutor;
23 import static java.util.Collections.emptyList;
24 import static org.onap.cps.ncmp.api.data.models.OperationType.CREATE;
25 import static org.onap.cps.ncmp.api.data.models.OperationType.UPDATE;
27 import java.util.HashMap;
28 import java.util.List;
30 import lombok.RequiredArgsConstructor;
31 import lombok.extern.slf4j.Slf4j;
32 import org.onap.cps.ncmp.api.data.models.OperationType;
33 import org.onap.cps.ncmp.api.exceptions.ProvMnSException;
34 import org.onap.cps.ncmp.impl.provmns.ParameterHelper;
35 import org.onap.cps.ncmp.impl.provmns.RequestParameters;
36 import org.onap.cps.ncmp.impl.provmns.model.PatchItem;
37 import org.onap.cps.utils.JsonObjectMapper;
38 import org.springframework.http.HttpStatus;
39 import org.springframework.stereotype.Service;
43 @RequiredArgsConstructor
44 public class OperationDetailsFactory {
46 private static final String ATTRIBUTE_NAME_SEPARATOR = "/";
47 private static final String REGEX_FOR_LEADING_AND_TRAILING_SEPARATORS = "(^/)|(/$)";
49 private final JsonObjectMapper jsonObjectMapper;
52 * Create OperationDetails object from ProvMnS request details.
54 * @param requestParameters request parameters including uri-ldn-first-part, className and id
55 * @param patchItem provided request payload
56 * @return OperationDetails object
58 public OperationDetails buildOperationDetails(final RequestParameters requestParameters,
59 final PatchItem patchItem) {
60 final OperationDetails operationDetails;
61 switch (patchItem.getOp()) {
63 operationDetails = buildOperationDetails(CREATE, requestParameters, patchItem.getValue());
66 if (patchItem.getPath().contains("#/attributes")) {
67 operationDetails = buildOperationDetailsForPatchItemWithHash(requestParameters, patchItem);
69 operationDetails = buildOperationDetailsForPatchItem(requestParameters, patchItem);
73 operationDetails = buildOperationDetailsForDelete(requestParameters.fdn());
76 throw new ProvMnSException("PATCH", HttpStatus.UNPROCESSABLE_ENTITY,
77 "Unsupported Patch Operation Type: " + patchItem.getOp().getValue(), patchItem.getOp().getValue());
79 return operationDetails;
83 * Build a OperationDetails object from ProvMnS request details.
85 * @param operationType Type of operation create, update.
86 * @param requestParameters request parameters including uri-ldn-first-part, className and id
87 * @param resourceAsObject provided request payload
88 * @return OperationDetails object
90 public OperationDetails buildOperationDetails(final OperationType operationType,
91 final RequestParameters requestParameters,
92 final Object resourceAsObject) {
93 final ResourceObjectDetails resourceObjectDetails = createResourceObjectDetails(resourceAsObject,
95 final String parentFdn = ParameterHelper.extractParentFdn(requestParameters.fdn());
96 final List<ClassInstance> classInstances
97 = List.of(new ClassInstance(resourceObjectDetails.id(), resourceObjectDetails.attributes()));
98 return new OperationDetails(operationType, parentFdn, resourceObjectDetails.objectClass(), classInstances);
102 * Build a OperationDetails object from ProvMnS request details for delete.
104 * @param fdn fdn to be deleted
105 * @return OperationDetails object
107 public OperationDetails buildOperationDetailsForDelete(final String fdn) {
108 final String parentFdn = ParameterHelper.extractParentFdn(fdn);
109 return new OperationDetails(OperationType.DELETE, parentFdn, "", emptyList());
113 * Build OperationDetails for a specific patch item.
115 * @param requestParameters request parameters including uri-ldn-first-part, className and id
116 * @param patchItem the patch item containing operation details
117 * @return OperationDetails object for the patch item
119 public OperationDetails buildOperationDetailsForPatchItem(final RequestParameters requestParameters,
120 final PatchItem patchItem) {
121 final Map<String, Object> resourceAsObject = new HashMap<>(2);
122 resourceAsObject.put("id", requestParameters.id());
123 resourceAsObject.put("attributes", patchItem.getValue());
124 return buildOperationDetails(UPDATE, requestParameters, resourceAsObject);
127 private OperationDetails buildOperationDetailsForPatchItemWithHash(final RequestParameters requestParameters,
128 final PatchItem patchItem) {
129 final Map<String, Object> attributeHierarchyAsMap = createNestedMap(patchItem);
130 final String parentFdn = ParameterHelper.extractParentFdn(requestParameters.fdn());
131 final List<ClassInstance> classInstances
132 = List.of(new ClassInstance(requestParameters.id(), attributeHierarchyAsMap));
133 return new OperationDetails(UPDATE, parentFdn, requestParameters.className(), classInstances);
136 @SuppressWarnings("unchecked")
137 private ResourceObjectDetails createResourceObjectDetails(final Object resourceAsObject,
138 final RequestParameters requestParameters) {
139 final String resourceAsJson = jsonObjectMapper.asJsonString(resourceAsObject);
140 final Map<String, Object> resourceAsMap = jsonObjectMapper.convertJsonString(resourceAsJson, Map.class);
141 return new ResourceObjectDetails(requestParameters.id(),
142 requestParameters.className(),
143 resourceAsMap.get("attributes"));
146 private Map<String, Object> createNestedMap(final PatchItem patchItem) {
147 final Map<String, Object> attributeHierarchyMap = new HashMap<>();
148 Map<String, Object> currentLevel = attributeHierarchyMap;
149 final String[] attributeHierarchyNames = patchItem.getPath().split("#/attributes")[1]
150 .replaceAll(REGEX_FOR_LEADING_AND_TRAILING_SEPARATORS, "")
151 .split(ATTRIBUTE_NAME_SEPARATOR);
152 for (int level = 0; level < attributeHierarchyNames.length; level++) {
153 final String attributeName = attributeHierarchyNames[level];
154 if (isLastLevel(attributeHierarchyNames, level)) {
155 currentLevel.put(attributeName, patchItem.getValue());
157 final Map<String, Object> nextLevel = new HashMap<>();
158 currentLevel.put(attributeName, nextLevel);
159 currentLevel = nextLevel;
162 return attributeHierarchyMap;
165 private boolean isLastLevel(final String[] attributeNamesArray, final int level) {
166 return level == attributeNamesArray.length - 1;