summaryrefslogtreecommitdiffstats
path: root/runtime-acm/src/main/java
diff options
context:
space:
mode:
authorFrancescoFioraEst <francesco.fiora@est.tech>2023-01-03 16:15:39 +0000
committerFrancescoFioraEst <francesco.fiora@est.tech>2023-01-04 11:15:03 +0000
commita76f5614e10f99658114ea1ff2eaf334e0df01c8 (patch)
treee3d468119c6795d54d3894e363ba9a7d6e11fa88 /runtime-acm/src/main/java
parentc6fb86de3dae6f26aec9813cd5ef4cc8d9268359 (diff)
Refactor Post instances to create and update ACM instances
Issue-ID: POLICY-4493 Change-Id: I31c00e6d61a53dcd6a03a911a618f1c6495f2d38 Signed-off-by: FrancescoFioraEst <francesco.fiora@est.tech>
Diffstat (limited to 'runtime-acm/src/main/java')
-rw-r--r--runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/instantiation/AutomationCompositionInstantiationProvider.java33
-rw-r--r--runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/main/rest/InstantiationController.java33
-rw-r--r--runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/main/rest/stub/CommissioningControllerStub.java62
-rw-r--r--runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/main/rest/stub/InstantiationControllerStub.java69
-rw-r--r--runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/main/rest/stub/StubUtils.java16
5 files changed, 76 insertions, 137 deletions
diff --git a/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/instantiation/AutomationCompositionInstantiationProvider.java b/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/instantiation/AutomationCompositionInstantiationProvider.java
index b723dba7f..099c0e740 100644
--- a/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/instantiation/AutomationCompositionInstantiationProvider.java
+++ b/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/instantiation/AutomationCompositionInstantiationProvider.java
@@ -37,7 +37,6 @@ import org.onap.policy.clamp.models.acm.concepts.AutomationCompositions;
import org.onap.policy.clamp.models.acm.concepts.Participant;
import org.onap.policy.clamp.models.acm.messages.rest.instantiation.InstantiationCommand;
import org.onap.policy.clamp.models.acm.messages.rest.instantiation.InstantiationResponse;
-import org.onap.policy.clamp.models.acm.messages.rest.instantiation.InstantiationUpdate;
import org.onap.policy.clamp.models.acm.persistence.provider.AcDefinitionProvider;
import org.onap.policy.clamp.models.acm.persistence.provider.AutomationCompositionProvider;
import org.onap.policy.clamp.models.acm.persistence.provider.ParticipantProvider;
@@ -102,29 +101,27 @@ public class AutomationCompositionInstantiationProvider {
* Update automation composition.
*
* @param compositionId The UUID of the automation composition definition
- * @param instanceId The UUID of the automation composition instance
- * @param instanceUpdate the automation composition
+ * @param automationComposition the automation composition
* @return the result of the update
*/
- public InstantiationResponse updateAutomationComposition(UUID compositionId, UUID instanceId,
- InstantiationUpdate instanceUpdate) {
- var automationComposition = automationCompositionProvider.getAutomationComposition(instanceId);
- if (!compositionId.equals(automationComposition.getCompositionId())) {
+ public InstantiationResponse updateAutomationComposition(UUID compositionId,
+ AutomationComposition automationComposition) {
+ var instanceId = automationComposition.getInstanceId();
+ var acToUpdate = automationCompositionProvider.getAutomationComposition(instanceId);
+ if (!compositionId.equals(acToUpdate.getCompositionId())) {
throw new PfModelRuntimeException(Response.Status.BAD_REQUEST,
automationComposition.getCompositionId() + DO_NOT_MATCH + compositionId);
}
- if (instanceUpdate.getElements() != null) {
- automationComposition.setElements(instanceUpdate.getElements());
- var validationResult = validateAutomationComposition(automationComposition);
- if (!validationResult.isValid()) {
- throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
- }
- automationComposition = automationCompositionProvider.updateAutomationComposition(automationComposition);
- }
-
- if (instanceUpdate.getInstantiationCommand() != null) {
- issueAutomationCompositionCommand(automationComposition, instanceUpdate.getInstantiationCommand());
+ acToUpdate.setElements(automationComposition.getElements());
+ acToUpdate.setName(automationComposition.getName());
+ acToUpdate.setVersion(automationComposition.getVersion());
+ acToUpdate.setDescription(automationComposition.getDescription());
+ acToUpdate.setDerivedFrom(automationComposition.getDerivedFrom());
+ var validationResult = validateAutomationComposition(acToUpdate);
+ if (!validationResult.isValid()) {
+ throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, validationResult.getResult());
}
+ automationComposition = automationCompositionProvider.updateAutomationComposition(acToUpdate);
var response = new InstantiationResponse();
response.setInstanceId(instanceId);
diff --git a/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/main/rest/InstantiationController.java b/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/main/rest/InstantiationController.java
index 1ff7c019e..94c111e00 100644
--- a/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/main/rest/InstantiationController.java
+++ b/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/main/rest/InstantiationController.java
@@ -31,7 +31,6 @@ import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
import org.onap.policy.clamp.models.acm.concepts.AutomationCompositions;
import org.onap.policy.clamp.models.acm.messages.rest.instantiation.AcInstanceStateUpdate;
import org.onap.policy.clamp.models.acm.messages.rest.instantiation.InstantiationResponse;
-import org.onap.policy.clamp.models.acm.messages.rest.instantiation.InstantiationUpdate;
import org.springframework.context.annotation.Profile;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;
@@ -59,10 +58,14 @@ public class InstantiationController extends AbstractRestController implements A
public ResponseEntity<InstantiationResponse> createCompositionInstance(UUID compositionId,
AutomationComposition automationComposition, UUID requestId) {
- var response = provider.createAutomationComposition(compositionId, automationComposition);
- return ResponseEntity
- .created(createUri("/compositions/" + compositionId + "/instances/" + response.getInstanceId()))
- .body(response);
+ if (automationComposition.getInstanceId() == null) {
+ var response = provider.createAutomationComposition(compositionId, automationComposition);
+ return ResponseEntity
+ .created(createUri("/compositions/" + compositionId + "/instances/" + response.getInstanceId()))
+ .body(response);
+ } else {
+ return ResponseEntity.ok().body(provider.updateAutomationComposition(compositionId, automationComposition));
+ }
}
/**
@@ -96,22 +99,6 @@ public class InstantiationController extends AbstractRestController implements A
}
/**
- * Updates a automation composition.
- *
- * @param compositionId The UUID of the automation composition definition
- * @param instanceId The UUID of the automation composition instance
- * @param instanceUpdate the automation composition to update
- * @param requestId request ID used in ONAP logging
- * @return a response
- */
- public ResponseEntity<InstantiationResponse> updateCompositionInstance(UUID compositionId, UUID instanceId,
- InstantiationUpdate instanceUpdate, UUID requestId) {
-
- return ResponseEntity.ok()
- .body(provider.updateAutomationComposition(compositionId, instanceId, instanceUpdate));
- }
-
- /**
* Deletes a automation composition definition.
*
* @param compositionId The UUID of the automation composition definition
@@ -127,8 +114,8 @@ public class InstantiationController extends AbstractRestController implements A
}
@Override
- public ResponseEntity<Void> ompositionInstanceState(UUID compositionId, UUID instanceId,
- @Valid AcInstanceStateUpdate body, UUID requestId) {
+ public ResponseEntity<Void> compositionInstanceState(UUID compositionId, UUID instanceId,
+ @Valid AcInstanceStateUpdate body, UUID requestId) {
// TODO Auto-generated method stub
return null;
}
diff --git a/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/main/rest/stub/CommissioningControllerStub.java b/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/main/rest/stub/CommissioningControllerStub.java
index 833b3fa0b..98c71dc6f 100644
--- a/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/main/rest/stub/CommissioningControllerStub.java
+++ b/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/main/rest/stub/CommissioningControllerStub.java
@@ -21,37 +21,24 @@
package org.onap.policy.clamp.acm.runtime.main.rest.stub;
import java.util.UUID;
-import javax.servlet.http.HttpServletRequest;
-import javax.validation.Valid;
+import lombok.RequiredArgsConstructor;
import org.onap.policy.clamp.acm.runtime.main.rest.gen.AutomationCompositionDefinitionApi;
import org.onap.policy.clamp.acm.runtime.main.web.AbstractRestController;
import org.onap.policy.clamp.models.acm.messages.rest.commissioning.AcTypeStateUpdate;
import org.onap.policy.clamp.models.acm.messages.rest.commissioning.CommissioningResponse;
import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplates;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Profile;
import org.springframework.http.ResponseEntity;
-import org.springframework.web.bind.annotation.PathVariable;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RequestHeader;
-import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@Profile("stub")
-public class CommissioningControllerStub extends AbstractRestController
- implements AutomationCompositionDefinitionApi {
+@RequiredArgsConstructor
+public class CommissioningControllerStub extends AbstractRestController implements AutomationCompositionDefinitionApi {
- private static final Logger log = LoggerFactory.getLogger(CommissioningControllerStub.class);
-
- private StubUtils stubUtils = new StubUtils();
-
- @Autowired
- private HttpServletRequest request;
+ private final StubUtils stubUtils;
@Value("${stub.deleteCompositionDefinitionResponse}")
private String pathToResponseFile;
@@ -69,44 +56,35 @@ public class CommissioningControllerStub extends AbstractRestController
private String pathToPutUpdate;
@Override
- public ResponseEntity<CommissioningResponse> createCompositionDefinitions(
- @Valid @RequestBody ToscaServiceTemplate body,
- @RequestHeader(value = "X-onap-RequestId", required = false) UUID xonaprequestid) {
- return stubUtils.getResponse(pathToPostResponse, CommissioningResponse.class, request, log);
+ public ResponseEntity<CommissioningResponse> createCompositionDefinitions(ToscaServiceTemplate body,
+ UUID xonaprequestid) {
+ var compositionId = body.getMetadata() != null ? body.getMetadata().get("compositionId") : null;
+ if (compositionId == null) {
+ return stubUtils.getResponse(pathToPostResponse, CommissioningResponse.class);
+ } else {
+ return stubUtils.getResponse(pathToPutUpdate, CommissioningResponse.class);
+ }
}
@Override
- public ResponseEntity<CommissioningResponse> deleteCompositionDefinition(
- @PathVariable("compositionId") UUID compositionId,
- @RequestHeader(value = "X-onap-RequestId", required = false) UUID xonaprequestid) {
- return stubUtils.getResponse(pathToResponseFile, CommissioningResponse.class, request, log);
+ public ResponseEntity<CommissioningResponse> deleteCompositionDefinition(UUID compositionId, UUID xonaprequestid) {
+ return stubUtils.getResponse(pathToResponseFile, CommissioningResponse.class);
}
@Override
- public ResponseEntity<ToscaServiceTemplate> getCompositionDefinition(
- @PathVariable("compositionId") UUID compositionId,
- @RequestHeader(value = "X-onap-RequestId", required = false) UUID xonaprequestid) {
- return stubUtils.getResponse(pathToSingleDefinition, ToscaServiceTemplate.class, request, log);
+ public ResponseEntity<ToscaServiceTemplate> getCompositionDefinition(UUID compositionId, UUID xonaprequestid) {
+ return stubUtils.getResponse(pathToSingleDefinition, ToscaServiceTemplate.class);
}
@Override
- public ResponseEntity<ToscaServiceTemplates> queryCompositionDefinitions(
- @Valid @RequestParam(value = "name", required = false) String name,
- @Valid @RequestParam(value = "version", required = false) String version,
- @RequestHeader(value = "X-onap-RequestId", required = false) UUID xonaprequestid) {
- return stubUtils.getResponse(pathToAllDefinitions, ToscaServiceTemplates.class, request, log);
- }
-
- public ResponseEntity<CommissioningResponse> updateCompositionDefinition(
- @PathVariable("compositionId") UUID compositionId,
- @Valid @RequestBody ToscaServiceTemplate body,
- @RequestHeader(value = "X-onap-RequestId", required = false) UUID xonaprequestid) {
- return stubUtils.getResponse(pathToPutUpdate, CommissioningResponse.class, request, log);
+ public ResponseEntity<ToscaServiceTemplates> queryCompositionDefinitions(String name, String version,
+ UUID xonaprequestid) {
+ return stubUtils.getResponse(pathToAllDefinitions, ToscaServiceTemplates.class);
}
@Override
public ResponseEntity<Void> compositionDefinitionPriming(UUID compositionId, UUID requestId,
- AcTypeStateUpdate body) {
+ AcTypeStateUpdate body) {
// TODO Auto-generated method stub
return null;
}
diff --git a/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/main/rest/stub/InstantiationControllerStub.java b/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/main/rest/stub/InstantiationControllerStub.java
index ac34f227d..d0b190dd3 100644
--- a/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/main/rest/stub/InstantiationControllerStub.java
+++ b/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/main/rest/stub/InstantiationControllerStub.java
@@ -21,37 +21,25 @@
package org.onap.policy.clamp.acm.runtime.main.rest.stub;
import java.util.UUID;
-import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
+import lombok.RequiredArgsConstructor;
import org.onap.policy.clamp.acm.runtime.main.rest.gen.AutomationCompositionInstanceApi;
import org.onap.policy.clamp.acm.runtime.main.web.AbstractRestController;
import org.onap.policy.clamp.models.acm.concepts.AutomationComposition;
import org.onap.policy.clamp.models.acm.concepts.AutomationCompositions;
import org.onap.policy.clamp.models.acm.messages.rest.instantiation.AcInstanceStateUpdate;
import org.onap.policy.clamp.models.acm.messages.rest.instantiation.InstantiationResponse;
-import org.onap.policy.clamp.models.acm.messages.rest.instantiation.InstantiationUpdate;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Profile;
import org.springframework.http.ResponseEntity;
-import org.springframework.web.bind.annotation.PathVariable;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RequestHeader;
-import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@Profile("stub")
+@RequiredArgsConstructor
public class InstantiationControllerStub extends AbstractRestController implements AutomationCompositionInstanceApi {
- private static final Logger log = LoggerFactory.getLogger(InstantiationControllerStub.class);
-
- private StubUtils stubUtils = new StubUtils();
-
- @Autowired
- private HttpServletRequest request;
+ private final StubUtils stubUtils;
@Value("${stub.deleteCompositionInstanceResponse}")
private String pathToResponseFile;
@@ -69,50 +57,37 @@ public class InstantiationControllerStub extends AbstractRestController implemen
private String pathToPutUpdate;
@Override
- public ResponseEntity<InstantiationResponse> createCompositionInstance(
- @PathVariable("compositionId") UUID compositionId,
- @Valid @RequestBody AutomationComposition body,
- @RequestHeader(value = "X-onap-RequestId", required = false) UUID xonaprequestid) {
- return stubUtils.getResponse(pathPostIntance, InstantiationResponse.class, request, log);
+ public ResponseEntity<InstantiationResponse> createCompositionInstance(UUID compositionId,
+ AutomationComposition body, UUID xonaprequestid) {
+ if (body.getInstanceId() == null) {
+ return stubUtils.getResponse(pathPostIntance, InstantiationResponse.class);
+ } else {
+ return stubUtils.getResponse(pathToResponseFile, InstantiationResponse.class);
+ }
}
@Override
- public ResponseEntity<InstantiationResponse> deleteCompositionInstance(
- @PathVariable("compositionId") UUID compositionId,
- @PathVariable("instanceId") UUID instanceId,
- @RequestHeader(value = "X-onap-RequestId", required = false) UUID xonaprequestid) {
- return stubUtils.getResponse(pathToResponseFile, InstantiationResponse.class, request, log);
+ public ResponseEntity<InstantiationResponse> deleteCompositionInstance(UUID compositionId, UUID instanceId,
+ UUID xonaprequestid) {
+ return stubUtils.getResponse(pathToResponseFile, InstantiationResponse.class);
}
@Override
- public ResponseEntity<AutomationComposition> getCompositionInstance(
- @PathVariable("compositionId") UUID compositionId,
- @PathVariable("instanceId") UUID instanceId,
- @RequestHeader(value = "X-onap-RequestId", required = false) UUID xonaprequestid) {
- return stubUtils.getResponse(pathToSingleIntance, AutomationComposition.class, request, log);
+ public ResponseEntity<AutomationComposition> getCompositionInstance(UUID compositionId, UUID instanceId,
+ UUID xonaprequestid) {
+ return stubUtils.getResponse(pathToSingleIntance, AutomationComposition.class);
}
@Override
- public ResponseEntity<AutomationCompositions> queryCompositionInstances(
- @PathVariable("compositionId") UUID compositionId,
- @Valid @RequestParam(value = "name", required = false) String name,
- @Valid @RequestParam(value = "version", required = false) String version,
- @RequestHeader(value = "X-onap-RequestId", required = false) UUID xonaprequestid) {
- return stubUtils.getResponse(pathToAllIntances, AutomationCompositions.class, request, log);
- }
-
- public ResponseEntity<InstantiationResponse> updateCompositionInstance(
- UUID compositionId,
- UUID instanceId,
- InstantiationUpdate body,
- UUID xonaprequestid) {
- return stubUtils.getResponse(pathToResponseFile, InstantiationResponse.class, request, log);
+ public ResponseEntity<AutomationCompositions> queryCompositionInstances(UUID compositionId, String name,
+ String version, UUID xonaprequestid) {
+ return stubUtils.getResponse(pathToAllIntances, AutomationCompositions.class);
}
@Override
- public ResponseEntity<Void> ompositionInstanceState(UUID compositionId, UUID instanceId,
- @Valid AcInstanceStateUpdate body, UUID requestId) {
+ public ResponseEntity<Void> compositionInstanceState(UUID compositionId, UUID instanceId,
+ @Valid AcInstanceStateUpdate body, UUID requestId) {
// TODO Auto-generated method stub
return null;
}
-} \ No newline at end of file
+}
diff --git a/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/main/rest/stub/StubUtils.java b/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/main/rest/stub/StubUtils.java
index 650426681..3a209a096 100644
--- a/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/main/rest/stub/StubUtils.java
+++ b/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/main/rest/stub/StubUtils.java
@@ -22,13 +22,13 @@ package org.onap.policy.clamp.acm.runtime.main.rest.stub;
import com.google.gson.Gson;
import java.io.IOException;
-import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import javax.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.common.utils.coder.StandardYamlCoder;
import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Profile;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpStatus;
@@ -40,21 +40,23 @@ import org.springframework.stereotype.Service;
@Profile("stub")
public class StubUtils {
+ private static final Logger log = LoggerFactory.getLogger(StubUtils.class);
+ private final HttpServletRequest request;
+
private static final StandardYamlCoder YAML_TRANSLATOR = new StandardYamlCoder();
private static final Gson JSON_TRANSLATOR = new Gson();
private static final String YAML = "application/yaml";
private static final String ACCEPT = "Accept";
- <T> ResponseEntity<T> getResponse(String path, Class<T> clazz,
- HttpServletRequest request, Logger log) {
- String accept = request.getHeader(ACCEPT);
- final ClassPathResource resource = new ClassPathResource(path);
- try (InputStream inputStream = resource.getInputStream()) {
+ <T> ResponseEntity<T> getResponse(String path, Class<T> clazz) {
+ var accept = request.getHeader(ACCEPT);
+ final var resource = new ClassPathResource(path);
+ try (var inputStream = resource.getInputStream()) {
if (accept.contains(YAML)) {
var targetObject = YAML_TRANSLATOR.decode(inputStream, clazz);
return new ResponseEntity<>(targetObject, HttpStatus.OK);
} else {
- final String string = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
+ final var string = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
var targetObject = JSON_TRANSLATOR.fromJson(string, clazz);
return new ResponseEntity<>(targetObject, HttpStatus.OK);
}