2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2025 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 com.fasterxml.jackson.core.JsonProcessingException;
24 import com.fasterxml.jackson.core.type.TypeReference;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import com.google.common.base.Strings;
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.inventory.models.YangModelCmHandle;
35 import org.onap.cps.ncmp.impl.provmns.RequestPathParameters;
36 import org.onap.cps.ncmp.impl.provmns.model.PatchItem;
37 import org.onap.cps.utils.JsonObjectMapper;
38 import org.springframework.stereotype.Service;
42 @RequiredArgsConstructor
43 public class OperationDetailsFactory {
45 private static final String ATTRIBUTE_NAME_SEPARATOR = "/";
46 private static final String REGEX_FOR_LEADING_AND_TRAILING_SEPARATORS = "(^/)|(/$)";
47 private static final String NO_AUTHORIZATION = null;
48 private static final String UNSUPPORTED_OPERATION = "UNSUPPORTED_OP";
50 private final JsonObjectMapper jsonObjectMapper;
51 private final ObjectMapper objectMapper;
52 private final PolicyExecutor policyExecutor;
55 * Build an operation details object from ProvMnS request details and send it to Policy Executor.
57 * @param requestPathParameters request parameters including uri-ldn-first-part, className and id
58 * @param patchItems provided request list of patch Items
59 * @param yangModelCmHandle representation of the cm handle to check
61 public void checkPermissionForEachPatchItem(final RequestPathParameters requestPathParameters,
62 final List<PatchItem> patchItems,
63 final YangModelCmHandle yangModelCmHandle) {
64 OperationDetails operationDetails;
65 for (final PatchItem patchItem : patchItems) {
66 switch (patchItem.getOp()) {
67 case ADD -> operationDetails = buildCreateOperationDetails(OperationType.CREATE, requestPathParameters,
68 patchItem.getValue());
69 case REPLACE -> operationDetails = buildCreateOperationDetailsForUpdate(OperationType.UPDATE,
70 requestPathParameters,
72 case REMOVE -> operationDetails = buildDeleteOperationDetails(requestPathParameters.toAlternateId());
73 default -> throw new ProvMnSException(UNSUPPORTED_OPERATION,
74 "Unsupported Patch Operation Type: " + patchItem.getOp().getValue());
76 policyExecutor.checkPermission(yangModelCmHandle,
77 OperationType.fromOperationName(operationDetails.operation()),
79 requestPathParameters.toAlternateId(),
80 jsonObjectMapper.asJsonString(operationDetails)
86 * Build a CreateOperationDetails object from ProvMnS request details.
88 * @param operationType Type of operation create, update.
89 * @param requestPathParameters request parameters including uri-ldn-first-part, className and id
90 * @param resourceAsObject provided request payload
91 * @return CreateOperationDetails object
93 public CreateOperationDetails buildCreateOperationDetails(final OperationType operationType,
94 final RequestPathParameters requestPathParameters,
95 final Object resourceAsObject) {
97 final ResourceObjectDetails resourceObjectDetails = createResourceObjectDetails(resourceAsObject,
98 requestPathParameters);
100 final OperationEntry operationEntry = new OperationEntry(resourceObjectDetails.id(),
101 resourceObjectDetails.attributes());
103 final Map<String, List<OperationEntry>> operationEntriesPerObjectClass =
104 Map.of(resourceObjectDetails.objectClass(), List.of(operationEntry));
106 return new CreateOperationDetails(
107 operationType.name(),
108 requestPathParameters.getUriLdnFirstPart(),
109 operationEntriesPerObjectClass
114 * Build a CreateOperationDetails object from ProvMnS request details.
116 * @param operationType Type of operation create, update.
117 * @param requestPathParameters request parameters including uri-ldn-first-part, className and id
118 * @param patchItem provided request
119 * @return CreateOperationDetails object
121 public CreateOperationDetails buildCreateOperationDetailsForUpdate(final OperationType operationType,
122 final RequestPathParameters requestPathParameters,
123 final PatchItem patchItem) {
124 if (patchItem.getPath().contains("#/attributes")) {
125 return buildCreateOperationDetailsForUpdateWithHash(operationType, requestPathParameters, patchItem);
127 return buildCreateOperationDetails(operationType, requestPathParameters, patchItem.getValue());
132 * Builds a DeleteOperationDetails object from provided alternate id.
134 * @param alternateId alternate id for request
135 * @return DeleteOperationDetails object
137 public DeleteOperationDetails buildDeleteOperationDetails(final String alternateId) {
138 return new DeleteOperationDetails(OperationType.DELETE.name(), alternateId);
141 private ResourceObjectDetails createResourceObjectDetails(final Object resourceAsObject,
142 final RequestPathParameters requestPathParameters) {
144 final String resourceAsJson = jsonObjectMapper.asJsonString(resourceAsObject);
145 final TypeReference<Map<String, Object>> typeReference = new TypeReference<>() {};
146 final Map<String, Object> resourceAsMap = objectMapper.readValue(resourceAsJson, typeReference);
148 return new ResourceObjectDetails(requestPathParameters.getId(),
149 extractObjectClass(resourceAsMap, requestPathParameters),
150 resourceAsMap.get("attributes"));
151 } catch (final JsonProcessingException e) {
152 log.debug("JSON processing error: {}", e.getMessage());
153 throw new ProvMnSException("Cannot convert Resource Object", e.getMessage());
157 private static String extractObjectClass(final Map<String, Object> resourceAsMap,
158 final RequestPathParameters requestPathParameters) {
159 final String objectClass = (String) resourceAsMap.get("objectClass");
160 if (Strings.isNullOrEmpty(objectClass)) {
161 return requestPathParameters.getClassName();
167 private CreateOperationDetails buildCreateOperationDetailsForUpdateWithHash(final OperationType operationType,
168 final RequestPathParameters requestPathParameters,
169 final PatchItem patchItem) {
170 final Map<String, List<OperationEntry>> operationEntriesPerObjectClass = new HashMap<>();
171 final String className = requestPathParameters.getClassName();
173 final Map<String, Object> attributeHierarchyAsMap = createNestedMap(patchItem);
175 final OperationEntry operationEntry = new OperationEntry(requestPathParameters.getId(),
176 attributeHierarchyAsMap);
177 operationEntriesPerObjectClass.put(className, List.of(operationEntry));
179 return new CreateOperationDetails(operationType.getOperationName(),
180 requestPathParameters.getUriLdnFirstPart(),
181 operationEntriesPerObjectClass);
184 private Map<String, Object> createNestedMap(final PatchItem patchItem) {
185 final Map<String, Object> attributeHierarchyMap = new HashMap<>();
186 Map<String, Object> currentLevel = attributeHierarchyMap;
188 final String[] attributeHierarchyNames = patchItem.getPath().split("#/attributes")[1]
189 .replaceAll(REGEX_FOR_LEADING_AND_TRAILING_SEPARATORS, "")
190 .split(ATTRIBUTE_NAME_SEPARATOR);
192 for (int level = 0; level < attributeHierarchyNames.length; level++) {
193 final String attributeName = attributeHierarchyNames[level];
195 if (isLastLevel(attributeHierarchyNames, level)) {
196 currentLevel.put(attributeName, patchItem.getValue());
198 final Map<String, Object> nextLevel = new HashMap<>();
199 currentLevel.put(attributeName, nextLevel);
200 currentLevel = nextLevel;
203 return attributeHierarchyMap;
206 private boolean isLastLevel(final String[] attributeNamesArray, final int level) {
207 return level == attributeNamesArray.length - 1;