88073c0a0f8508412dceba0b280d2c57a8c71764
[cps.git] /
1 /*
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
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 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;
38
39 @RestController
40 @RequiredArgsConstructor
41 @Slf4j
42 public class PolicyExecutorStubController implements PolicyExecutorApi {
43
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;
49
50     @Override
51     public ResponseEntity<PolicyExecutionResponse> executePolicyAction(
52                                                      final String action,
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);
58         }
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);
63         }
64         log.warn("This stub only supports 'delete' operations");
65         return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
66     }
67
68     private ResponseEntity<PolicyExecutionResponse> handleNcmpDeleteSchema(final Request request) {
69         final NcmpDelete ncmpDelete = objectMapper.convertValue(request.getData(), NcmpDelete.class);
70
71         final String targetIdentifier = ncmpDelete.getTargetIdentifier();
72
73         if (targetIdentifier == null) {
74             return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
75         }
76
77         final Matcher matcher = ERROR_CODE_PATTERN.matcher(targetIdentifier);
78         if (matcher.find()) {
79             final int errorCode = Integer.parseInt(matcher.group(1));
80             return new ResponseEntity<>(HttpStatusCode.valueOf(errorCode));
81         }
82
83         return createPolicyExecutionResponse(targetIdentifier);
84     }
85
86     private ResponseEntity<PolicyExecutionResponse> createPolicyExecutionResponse(final String targetIdentifier) {
87         final String decisionId = String.valueOf(++decisionCounter);
88         final String decision;
89         final String message;
90         if (targetIdentifier.toLowerCase(Locale.getDefault()).contains("slow")) {
91             try {
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
96             }
97         }
98         if (targetIdentifier.toLowerCase(Locale.getDefault()).contains("cps-is-great")) {
99             decision = "allow";
100             message = "All good";
101         } else {
102             decision = "deny";
103             message = "Only FDNs containing 'cps-is-great' are allowed";
104         }
105         log.info("Decision: {} ({})", decision, message);
106         final PolicyExecutionResponse policyExecutionResponse =
107             new PolicyExecutionResponse(decisionId, decision, message);
108
109         return ResponseEntity.ok(policyExecutionResponse);
110     }
111
112 }