2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2024-2025 OpenInfra Foundation Europe. All rights reserved.
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 java.util.regex.Matcher;
24 import java.util.regex.Pattern;
25 import lombok.RequiredArgsConstructor;
26 import lombok.extern.slf4j.Slf4j;
27 import org.onap.cps.policyexecutor.stub.api.OperationPermissionApi;
28 import org.onap.cps.policyexecutor.stub.model.Operation;
29 import org.onap.cps.policyexecutor.stub.model.PermissionRequest;
30 import org.onap.cps.policyexecutor.stub.model.PermissionResponse;
31 import org.springframework.http.HttpStatus;
32 import org.springframework.http.HttpStatusCode;
33 import org.springframework.http.ResponseEntity;
34 import org.springframework.web.bind.annotation.RequestMapping;
35 import org.springframework.web.bind.annotation.RestController;
38 @RequestMapping("/operation-permission/v1")
39 @RequiredArgsConstructor
41 public class PolicyExecutorStubController implements OperationPermissionApi {
43 private final Sleeper sleeper;
45 private static final Pattern PATTERN_SIMULATION = Pattern.compile("policySimulation=(\\w+_\\w+)");
46 private static final Pattern PATTERN_HTTP_ERROR = Pattern.compile("httpError_(\\d{3})");
47 private static final Pattern PATTERN_SLOW_RESPONSE = Pattern.compile("slowResponse_(\\d{1,3})");
48 private static final Pattern PATTERN_POLICY_RESPONSE = Pattern.compile("policyResponse_(\\w+)");
50 private int decisionCounter = 0;
53 public ResponseEntity<PermissionResponse> initiatePermissionRequest(final String contentType,
54 final PermissionRequest permissionRequest,
56 final String authorization) {
57 log.info("Stub Policy Executor Invoked");
58 if (permissionRequest.getOperations().isEmpty()) {
59 return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
61 final Operation firstOperation = permissionRequest.getOperations().iterator().next();
62 log.info("1st Operation: {} for resource: {}", firstOperation.getOperation(),
63 firstOperation.getResourceIdentifier());
64 if (!"delete".equals(firstOperation.getOperation()) && firstOperation.getChangeRequest() == null) {
65 log.warn("Change Request is required for {} operations", firstOperation.getOperation());
66 return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
68 return createPolicyExecutionResponse(firstOperation.getResourceIdentifier());
71 private ResponseEntity<PermissionResponse> createPolicyExecutionResponse(final String resourceIdentifier) {
72 final String id = String.valueOf(++decisionCounter);
73 String permissionResult = "allow";
74 String message = "all good";
75 Matcher matcher = PATTERN_SIMULATION.matcher(resourceIdentifier);
77 final String simulation = matcher.group(1);
78 matcher = PATTERN_SLOW_RESPONSE.matcher(simulation);
79 if (matcher.matches()) {
81 final int slowResponseTimeInSeconds = Integer.parseInt(matcher.group(1));
82 sleeper.haveALittleRest(slowResponseTimeInSeconds);
83 } catch (final InterruptedException e) {
84 log.trace("Sleep interrupted, re-interrupting the thread");
85 Thread.currentThread().interrupt(); // Re-interrupt the thread
88 matcher = PATTERN_HTTP_ERROR.matcher(simulation);
89 if (matcher.matches()) {
90 final int errorCode = Integer.parseInt(matcher.group(1));
91 log.warn("Stub is mocking an error response, code: {}", errorCode);
92 return new ResponseEntity<>(HttpStatusCode.valueOf(errorCode));
94 matcher = PATTERN_POLICY_RESPONSE.matcher(simulation);
95 if (matcher.matches()) {
96 permissionResult = matcher.group(1);
97 message = "Stub is mocking a policy response: " + permissionResult;
100 log.info("Decision: {} ({})", permissionResult, message);
101 return ResponseEntity.ok(new PermissionResponse(id, permissionResult, message));