4d85592bdb6a820d760976c4c0022a247f5bcd12
[cps.git] /
1 /*
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.policyexecutor.stub.controller;
22
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;
36
37 @RestController
38 @RequestMapping("/operation-permission/v1")
39 @RequiredArgsConstructor
40 @Slf4j
41 public class PolicyExecutorStubController implements OperationPermissionApi {
42
43     private final Sleeper sleeper;
44
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+)");
49
50     private int decisionCounter = 0;
51
52     @Override
53     public ResponseEntity<PermissionResponse> initiatePermissionRequest(final String contentType,
54                                                                         final PermissionRequest permissionRequest,
55                                                                         final String accept,
56                                                                         final String authorization) {
57         log.info("Stub Policy Executor Invoked");
58         if (permissionRequest.getOperations().isEmpty()) {
59             return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
60         }
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);
67         }
68         return createPolicyExecutionResponse(firstOperation.getResourceIdentifier());
69     }
70
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);
76         if (matcher.find()) {
77             final String simulation = matcher.group(1);
78             matcher = PATTERN_SLOW_RESPONSE.matcher(simulation);
79             if (matcher.matches()) {
80                 try {
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
86                 }
87             }
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));
93             }
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;
98             }
99         }
100         log.info("Decision: {} ({})", permissionResult, message);
101         return ResponseEntity.ok(new PermissionResponse(id, permissionResult, message));
102     }
103
104 }