2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2024 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.policyexecutor.stub.controller;
23 import com.fasterxml.jackson.databind.ObjectMapper;
24 import java.util.Locale;
25 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
27 import lombok.RequiredArgsConstructor;
28 import lombok.extern.slf4j.Slf4j;
29 import org.onap.cps.policyexecutor.stub.api.PolicyExecutorApi;
30 import org.onap.cps.policyexecutor.stub.model.NcmpDelete;
31 import org.onap.cps.policyexecutor.stub.model.PolicyExecutionRequest;
32 import org.onap.cps.policyexecutor.stub.model.PolicyExecutionResponse;
33 import org.onap.cps.policyexecutor.stub.model.Request;
34 import org.springframework.http.HttpStatus;
35 import org.springframework.http.HttpStatusCode;
36 import org.springframework.http.ResponseEntity;
37 import org.springframework.web.bind.annotation.RestController;
40 @RequiredArgsConstructor
42 public class PolicyExecutorStubController implements PolicyExecutorApi {
44 private final ObjectMapper objectMapper;
45 private static final Pattern ERROR_CODE_PATTERN = Pattern.compile("(\\d{3})");
46 private int decisionCounter = 0;
49 public ResponseEntity<PolicyExecutionResponse> executePolicyAction(
51 final PolicyExecutionRequest policyExecutionRequest,
52 final String authorization) {
53 log.info("Stub Policy Executor Invoked (only supports 'delete' operations)");
54 if (policyExecutionRequest.getRequests().isEmpty()) {
55 return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
57 final Request firstRequest = policyExecutionRequest.getRequests().iterator().next();
58 log.info("1st Request Schema:{}", firstRequest.getSchema());
59 if (firstRequest.getSchema().contains("ncmp-delete-schema:1.0.0")) {
60 return handleNcmpDeleteSchema(firstRequest);
62 log.warn("This stub only supports 'delete' operations");
63 return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
66 private ResponseEntity<PolicyExecutionResponse> handleNcmpDeleteSchema(final Request request) {
67 final NcmpDelete ncmpDelete = objectMapper.convertValue(request.getData(), NcmpDelete.class);
69 final String targetIdentifier = ncmpDelete.getTargetIdentifier();
71 if (targetIdentifier == null) {
72 return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
75 final Matcher matcher = ERROR_CODE_PATTERN.matcher(targetIdentifier);
77 final int errorCode = Integer.parseInt(matcher.group(1));
78 return new ResponseEntity<>(HttpStatusCode.valueOf(errorCode));
81 return createPolicyExecutionResponse(targetIdentifier);
84 private ResponseEntity<PolicyExecutionResponse> createPolicyExecutionResponse(final String targetIdentifier) {
85 final String decisionId = String.valueOf(++decisionCounter);
86 final String decision;
89 if (targetIdentifier.toLowerCase(Locale.getDefault()).contains("cps-is-great")) {
94 message = "Only FDNs containing 'cps-is-great' are allowed";
96 log.info("Decision: {} ({})", decision, message);
97 final PolicyExecutionResponse policyExecutionResponse =
98 new PolicyExecutionResponse(decisionId, decision, message);
100 return ResponseEntity.ok(policyExecutionResponse);