Fix synchronization issues in policy-api 29/136429/1
authorFrancescoFioraEst <francesco.fiora@est.tech>
Tue, 7 Nov 2023 16:44:03 +0000 (16:44 +0000)
committerFrancesco Fiora <francesco.fiora@est.tech>
Fri, 10 Nov 2023 09:29:16 +0000 (09:29 +0000)
Issue-ID: POLICY-4864
Change-Id: Ieed49019bee3dbd9e5c81dde341d18e6f4bac784
Signed-off-by: FrancescoFioraEst <francesco.fiora@est.tech>
main/src/main/java/org/onap/policy/api/main/rest/ApiRestController.java [changed mode: 0644->0755]
main/src/main/java/org/onap/policy/api/main/rest/CommonRestController.java
main/src/main/java/org/onap/policy/api/main/rest/NodeTemplateController.java

old mode 100644 (file)
new mode 100755 (executable)
index b44e60f..9745ec9
@@ -158,11 +158,17 @@ public class ApiRestController extends CommonRestController implements PolicyDes
             NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST, "/policytypes", toJson(body));\r
         }\r
         try {\r
+            mutex.acquire();\r
             ToscaServiceTemplate serviceTemplate = toscaServiceTemplateService.createPolicyType(body);\r
             return makeOkResponse(requestId, serviceTemplate);\r
         } catch (PfModelRuntimeException pfme) {\r
             final var msg = "POST /policytypes";\r
             throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);\r
+        } catch (InterruptedException e) {\r
+            Thread.currentThread().interrupt();\r
+            throw new PolicyApiRuntimeException(e.getMessage(), null, null, requestId);\r
+        } finally {\r
+            mutex.release();\r
         }\r
     }\r
 \r
@@ -179,12 +185,18 @@ public class ApiRestController extends CommonRestController implements PolicyDes
         String versionId,\r
         UUID requestId) {\r
         try {\r
+            mutex.acquire();\r
             ToscaServiceTemplate serviceTemplate =\r
                 toscaServiceTemplateService.deletePolicyType(policyTypeId, versionId);\r
             return makeOkResponse(requestId, serviceTemplate);\r
         } catch (PfModelRuntimeException pfme) {\r
             var msg = String.format("DELETE /policytypes/%s/versions/%s", policyTypeId, versionId);\r
             throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);\r
+        } catch (InterruptedException e) {\r
+            Thread.currentThread().interrupt();\r
+            throw new PolicyApiRuntimeException(e.getMessage(), null, null, requestId);\r
+        } finally {\r
+            mutex.release();\r
         }\r
     }\r
 \r
@@ -310,12 +322,18 @@ public class ApiRestController extends CommonRestController implements PolicyDes
                 "/policytypes/" + policyTypeId + "/versions/" + policyTypeVersion + "/policies", toJson(body));\r
         }\r
         try {\r
+            mutex.acquire();\r
             ToscaServiceTemplate serviceTemplate =\r
                 toscaServiceTemplateService.createPolicy(policyTypeId, policyTypeVersion, body);\r
             return makeOkResponse(requestId, serviceTemplate);\r
         } catch (PfModelRuntimeException pfme) {\r
             var msg = String.format("POST /policytypes/%s/versions/%s/policies", policyTypeId, policyTypeVersion);\r
             throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);\r
+        } catch (InterruptedException e) {\r
+            Thread.currentThread().interrupt();\r
+            throw new PolicyApiRuntimeException(e.getMessage(), null, null, requestId);\r
+        } finally {\r
+            mutex.release();\r
         }\r
     }\r
 \r
@@ -336,6 +354,7 @@ public class ApiRestController extends CommonRestController implements PolicyDes
         String policyVersion,\r
         UUID requestId) {\r
         try {\r
+            mutex.acquire();\r
             ToscaServiceTemplate serviceTemplate =\r
                 toscaServiceTemplateService.deletePolicy(policyTypeId, policyTypeVersion, policyId, policyVersion);\r
             return makeOkResponse(requestId, serviceTemplate);\r
@@ -343,6 +362,11 @@ public class ApiRestController extends CommonRestController implements PolicyDes
             var msg = String.format("DELETE /policytypes/%s/versions/%s/policies/%s/versions/%s",\r
                 policyTypeId, policyTypeVersion, policyId, policyVersion);\r
             throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);\r
+        } catch (InterruptedException e) {\r
+            Thread.currentThread().interrupt();\r
+            throw new PolicyApiRuntimeException(e.getMessage(), null, null, requestId);\r
+        } finally {\r
+            mutex.release();\r
         }\r
     }\r
 \r
@@ -406,11 +430,17 @@ public class ApiRestController extends CommonRestController implements PolicyDes
             NetLoggerUtil.log(EventType.IN, CommInfrastructure.REST, "/policies", toJson(body));\r
         }\r
         try {\r
+            mutex.acquire();\r
             ToscaServiceTemplate serviceTemplate = toscaServiceTemplateService.createPolicies(body);\r
             return makeOkResponse(requestId, serviceTemplate);\r
         } catch (PfModelRuntimeException pfme) {\r
             final var msg = "POST /policies";\r
             throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);\r
+        } catch (InterruptedException e) {\r
+            Thread.currentThread().interrupt();\r
+            throw new PolicyApiRuntimeException(e.getMessage(), null, null, requestId);\r
+        } finally {\r
+            mutex.release();\r
         }\r
     }\r
 \r
@@ -427,12 +457,18 @@ public class ApiRestController extends CommonRestController implements PolicyDes
         String policyVersion,\r
         UUID requestId) {\r
         try {\r
+            mutex.acquire();\r
             ToscaServiceTemplate serviceTemplate =\r
                 toscaServiceTemplateService.deletePolicy(null, null, policyId, policyVersion);\r
             return makeOkResponse(requestId, serviceTemplate);\r
         } catch (PfModelRuntimeException pfme) {\r
             var msg = String.format("DELETE /policies/%s/versions/%s", policyId, policyVersion);\r
             throw new PolicyApiRuntimeException(msg, pfme.getCause(), pfme.getErrorResponse(), requestId);\r
+        } catch (InterruptedException e) {\r
+            Thread.currentThread().interrupt();\r
+            throw new PolicyApiRuntimeException(e.getMessage(), null, null, requestId);\r
+        } finally {\r
+            mutex.release();\r
         }\r
     }\r
 }\r
index eadd8d3..d105058 100644 (file)
@@ -26,6 +26,7 @@ package org.onap.policy.api.main.rest;
 \r
 import java.util.Objects;\r
 import java.util.UUID;\r
+import java.util.concurrent.Semaphore;\r
 import org.onap.policy.api.main.exception.PolicyApiRuntimeException;\r
 import org.onap.policy.common.utils.coder.Coder;\r
 import org.onap.policy.common.utils.coder.CoderException;\r
@@ -42,6 +43,8 @@ import org.springframework.web.context.request.WebRequest;
  */\r
 public class CommonRestController {\r
 \r
+    protected static Semaphore mutex = new Semaphore(1);\r
+\r
     private static final Logger LOGGER = LoggerFactory.getLogger(CommonRestController.class);\r
 \r
     protected static final String EXTENSION_NAME = "interface info";\r
index 4496266..944a6f8 100644 (file)
@@ -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<ToscaServiceTemplate> 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();
         }
     }