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 Sleeper sleeper;
45 private final ObjectMapper objectMapper;
46 private static final Pattern ERROR_CODE_PATTERN = Pattern.compile("(\\d{3})");
47 private int decisionCounter = 0;
48 private static int slowResponseTimeInSeconds = 40;
51 public ResponseEntity<PolicyExecutionResponse> executePolicyAction(
53 final PolicyExecutionRequest policyExecutionRequest,
54 final String authorization) {
55 log.info("Stub Policy Executor Invoked (only supports 'delete' operations)");
56 if (policyExecutionRequest.getRequests().isEmpty()) {
57 return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
59 final Request firstRequest = policyExecutionRequest.getRequests().iterator().next();
60 log.info("1st Request Schema:{}", firstRequest.getSchema());
61 if (firstRequest.getSchema().contains("ncmp-delete-schema:1.0.0")) {
62 return handleNcmpDeleteSchema(firstRequest);
64 log.warn("This stub only supports 'delete' operations");
65 return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
68 private ResponseEntity<PolicyExecutionResponse> handleNcmpDeleteSchema(final Request request) {
69 final NcmpDelete ncmpDelete = objectMapper.convertValue(request.getData(), NcmpDelete.class);
71 final String targetIdentifier = ncmpDelete.getTargetIdentifier();
73 if (targetIdentifier == null) {
74 return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
77 final Matcher matcher = ERROR_CODE_PATTERN.matcher(targetIdentifier);
79 final int errorCode = Integer.parseInt(matcher.group(1));
80 return new ResponseEntity<>(HttpStatusCode.valueOf(errorCode));
83 return createPolicyExecutionResponse(targetIdentifier);
86 private ResponseEntity<PolicyExecutionResponse> createPolicyExecutionResponse(final String targetIdentifier) {
87 final String decisionId = String.valueOf(++decisionCounter);
88 final String decision;
90 if (targetIdentifier.toLowerCase(Locale.getDefault()).contains("slow")) {
92 sleeper.haveALittleRest(slowResponseTimeInSeconds);
93 } catch (final InterruptedException e) {
94 log.trace("Sleep interrupted, re-interrupting the thread");
95 Thread.currentThread().interrupt(); // Re-interrupt the thread
98 if (targetIdentifier.toLowerCase(Locale.getDefault()).contains("cps-is-great")) {
100 message = "All good";
103 message = "Only FDNs containing 'cps-is-great' are allowed";
105 log.info("Decision: {} ({})", decision, message);
106 final PolicyExecutionResponse policyExecutionResponse =
107 new PolicyExecutionResponse(decisionId, decision, message);
109 return ResponseEntity.ok(policyExecutionResponse);