From 3673c303b0939692ded673e96024316af326d342 Mon Sep 17 00:00:00 2001 From: FrancescoFioraEst Date: Tue, 7 Nov 2023 16:44:03 +0000 Subject: [PATCH] Fix synchronization issues in policy-api Issue-ID: POLICY-4864 Change-Id: Ieed49019bee3dbd9e5c81dde341d18e6f4bac784 Signed-off-by: FrancescoFioraEst --- .../policy/api/main/rest/ApiRestController.java | 36 ++++++++++++++++++++++ .../policy/api/main/rest/CommonRestController.java | 3 ++ .../api/main/rest/NodeTemplateController.java | 18 +++++++++++ 3 files changed, 57 insertions(+) mode change 100644 => 100755 main/src/main/java/org/onap/policy/api/main/rest/ApiRestController.java diff --git a/main/src/main/java/org/onap/policy/api/main/rest/ApiRestController.java b/main/src/main/java/org/onap/policy/api/main/rest/ApiRestController.java old mode 100644 new mode 100755 index b44e60fa..9745ec94 --- a/main/src/main/java/org/onap/policy/api/main/rest/ApiRestController.java +++ b/main/src/main/java/org/onap/policy/api/main/rest/ApiRestController.java @@ -158,11 +158,17 @@ public class ApiRestController extends CommonRestController implements PolicyDes NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST, "/policytypes", toJson(body)); } try { + mutex.acquire(); ToscaServiceTemplate serviceTemplate = toscaServiceTemplateService.createPolicyType(body); return makeOkResponse(requestId, serviceTemplate); } catch (PfModelRuntimeException pfme) { final var msg = "POST /policytypes"; throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new PolicyApiRuntimeException(e.getMessage(), null, null, requestId); + } finally { + mutex.release(); } } @@ -179,12 +185,18 @@ public class ApiRestController extends CommonRestController implements PolicyDes String versionId, UUID requestId) { try { + mutex.acquire(); ToscaServiceTemplate serviceTemplate = toscaServiceTemplateService.deletePolicyType(policyTypeId, versionId); return makeOkResponse(requestId, serviceTemplate); } catch (PfModelRuntimeException pfme) { var msg = String.format("DELETE /policytypes/%s/versions/%s", policyTypeId, versionId); throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new PolicyApiRuntimeException(e.getMessage(), null, null, requestId); + } finally { + mutex.release(); } } @@ -310,12 +322,18 @@ public class ApiRestController extends CommonRestController implements PolicyDes "/policytypes/" + policyTypeId + "/versions/" + policyTypeVersion + "/policies", toJson(body)); } try { + mutex.acquire(); ToscaServiceTemplate serviceTemplate = toscaServiceTemplateService.createPolicy(policyTypeId, policyTypeVersion, body); return makeOkResponse(requestId, serviceTemplate); } catch (PfModelRuntimeException pfme) { var msg = String.format("POST /policytypes/%s/versions/%s/policies", policyTypeId, policyTypeVersion); throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new PolicyApiRuntimeException(e.getMessage(), null, null, requestId); + } finally { + mutex.release(); } } @@ -336,6 +354,7 @@ public class ApiRestController extends CommonRestController implements PolicyDes String policyVersion, UUID requestId) { try { + mutex.acquire(); ToscaServiceTemplate serviceTemplate = toscaServiceTemplateService.deletePolicy(policyTypeId, policyTypeVersion, policyId, policyVersion); return makeOkResponse(requestId, serviceTemplate); @@ -343,6 +362,11 @@ public class ApiRestController extends CommonRestController implements PolicyDes var msg = String.format("DELETE /policytypes/%s/versions/%s/policies/%s/versions/%s", policyTypeId, policyTypeVersion, policyId, policyVersion); throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new PolicyApiRuntimeException(e.getMessage(), null, null, requestId); + } finally { + mutex.release(); } } @@ -406,11 +430,17 @@ public class ApiRestController extends CommonRestController implements PolicyDes NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST, "/policies", toJson(body)); } try { + mutex.acquire(); ToscaServiceTemplate serviceTemplate = toscaServiceTemplateService.createPolicies(body); return makeOkResponse(requestId, serviceTemplate); } catch (PfModelRuntimeException pfme) { final var msg = "POST /policies"; throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new PolicyApiRuntimeException(e.getMessage(), null, null, requestId); + } finally { + mutex.release(); } } @@ -427,12 +457,18 @@ public class ApiRestController extends CommonRestController implements PolicyDes String policyVersion, UUID requestId) { try { + mutex.acquire(); ToscaServiceTemplate serviceTemplate = toscaServiceTemplateService.deletePolicy(null, null, policyId, policyVersion); return makeOkResponse(requestId, serviceTemplate); } catch (PfModelRuntimeException pfme) { var msg = String.format("DELETE /policies/%s/versions/%s", policyId, policyVersion); throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new PolicyApiRuntimeException(e.getMessage(), null, null, requestId); + } finally { + mutex.release(); } } } diff --git a/main/src/main/java/org/onap/policy/api/main/rest/CommonRestController.java b/main/src/main/java/org/onap/policy/api/main/rest/CommonRestController.java index eadd8d34..d105058e 100644 --- a/main/src/main/java/org/onap/policy/api/main/rest/CommonRestController.java +++ b/main/src/main/java/org/onap/policy/api/main/rest/CommonRestController.java @@ -26,6 +26,7 @@ package org.onap.policy.api.main.rest; import java.util.Objects; import java.util.UUID; +import java.util.concurrent.Semaphore; import org.onap.policy.api.main.exception.PolicyApiRuntimeException; import org.onap.policy.common.utils.coder.Coder; import org.onap.policy.common.utils.coder.CoderException; @@ -42,6 +43,8 @@ import org.springframework.web.context.request.WebRequest; */ public class CommonRestController { + protected static Semaphore mutex = new Semaphore(1); + private static final Logger LOGGER = LoggerFactory.getLogger(CommonRestController.class); protected static final String EXTENSION_NAME = "interface info"; diff --git a/main/src/main/java/org/onap/policy/api/main/rest/NodeTemplateController.java b/main/src/main/java/org/onap/policy/api/main/rest/NodeTemplateController.java index 44962663..944a6f85 100644 --- a/main/src/main/java/org/onap/policy/api/main/rest/NodeTemplateController.java +++ b/main/src/main/java/org/onap/policy/api/main/rest/NodeTemplateController.java @@ -63,11 +63,17 @@ public class NodeTemplateController extends CommonRestController implements Tosc toJson(body)); } try { + mutex.acquire(); ToscaServiceTemplate nodeTemplates = toscaServiceTemplateService.createToscaNodeTemplates(body); return makeOkResponse(requestId, nodeTemplates); } catch (PfModelException | PfModelRuntimeException pfme) { final var msg = "POST /nodetemplates"; throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new PolicyApiRuntimeException(e.getMessage(), null, null, requestId); + } finally { + mutex.release(); } } @@ -86,11 +92,17 @@ public class NodeTemplateController extends CommonRestController implements Tosc toJson(body)); } try { + mutex.acquire(); ToscaServiceTemplate nodeTemplates = toscaServiceTemplateService.updateToscaNodeTemplates(body); return makeOkResponse(requestId, nodeTemplates); } catch (PfModelException | PfModelRuntimeException pfme) { final var msg = "PUT /nodetemplates"; throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new PolicyApiRuntimeException(e.getMessage(), null, null, requestId); + } finally { + mutex.release(); } } @@ -104,11 +116,17 @@ public class NodeTemplateController extends CommonRestController implements Tosc @Override public ResponseEntity deleteToscaNodeTemplates(String name, String version, UUID requestId) { try { + mutex.acquire(); ToscaServiceTemplate nodeTemplates = toscaServiceTemplateService.deleteToscaNodeTemplate(name, version); return makeOkResponse(requestId, nodeTemplates); } catch (PfModelException | PfModelRuntimeException pfme) { final var msg = String.format("DELETE /nodetemplates/%s/versions/%s", name, version); throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new PolicyApiRuntimeException(e.getMessage(), null, null, requestId); + } finally { + mutex.release(); } } -- 2.16.6