diff options
author | Steve Alphonse Siani <alphonse.steve.siani.djissitchi@ibm.com> | 2019-01-09 14:34:06 -0500 |
---|---|---|
committer | Balazinski <bartosz.balazinski@ibm.com> | 2019-01-11 08:51:07 -0500 |
commit | 83eab67a658e57548f398a7ddd872de4ebab5ca3 (patch) | |
tree | ca47e10091ac397ccb3f1ace727eb2578bc64d74 /ms/controllerblueprints/modules/service/src/main/java | |
parent | 6213aad62019a361126ee218b6b550e539e3582f (diff) |
Blueprint exception handler and REST responses
Change-Id: I5727238cd4c3f3f5475c3f3022e56f1acc0d73bf
Issue-ID: CCSDK-418
Signed-off-by: Steve Alphonse Siani <alphonse.steve.siani.djissitchi@ibm.com>
Signed-off-by: Balazinski <bartosz.balazinski@ibm.com>
Diffstat (limited to 'ms/controllerblueprints/modules/service/src/main/java')
5 files changed, 117 insertions, 40 deletions
diff --git a/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/BlueprintModelService.java b/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/BlueprintModelService.java index ca0e2439..e80fa8cd 100644 --- a/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/BlueprintModelService.java +++ b/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/BlueprintModelService.java @@ -17,11 +17,11 @@ package org.onap.ccsdk.apps.controllerblueprints.service;
-import com.att.eelf.configuration.EELFLogger;
-import com.att.eelf.configuration.EELFManager;
import org.jetbrains.annotations.NotNull;
import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException;
+import org.onap.ccsdk.apps.controllerblueprints.core.common.ApplicationConstants;
import org.onap.ccsdk.apps.controllerblueprints.core.config.BluePrintLoadConfiguration;
+import org.onap.ccsdk.apps.controllerblueprints.core.data.ErrorCode;
import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintCatalogService;
import org.onap.ccsdk.apps.controllerblueprints.core.utils.BluePrintFileUtils;
import org.onap.ccsdk.apps.controllerblueprints.service.domain.BlueprintModel;
@@ -56,8 +56,6 @@ import java.util.Optional; @Service
public class BlueprintModelService {
- private static EELFLogger log = EELFManager.getInstance().getLogger(BlueprintModelService.class);
-
@Autowired
private BluePrintLoadConfiguration bluePrintLoadConfiguration;
@@ -73,9 +71,9 @@ public class BlueprintModelService { @Autowired
private ControllerBlueprintModelContentRepository blueprintModelContentRepository;
- private static final String BLUEPRINT_MODEL_ID_FAILURE_MSG = "failed to get blueprint model id(%d) from repo";
- private static final String BLUEPRINT_MODEL_NAME_VERSION_FAILURE_MSG = "failed to get blueprint model by name(%d)" +
- " and version(%d) from repo";
+ private static final String BLUEPRINT_MODEL_ID_FAILURE_MSG = "failed to get blueprint model id(%s) from repo";
+ private static final String BLUEPRINT_MODEL_NAME_VERSION_FAILURE_MSG = "failed to get blueprint model by name(%s)" +
+ " and version(%s) from repo";
/**
* This is a saveBlueprintModel method
@@ -87,28 +85,36 @@ public class BlueprintModelService { public Mono<BlueprintModelSearch> saveBlueprintModel(FilePart filePart) throws BluePrintException {
try {
Path cbaLocation = BluePrintFileUtils.Companion
- .getCbaStorageDirectory(bluePrintLoadConfiguration.blueprintArchivePath);
+ .getCbaStorageDirectory(bluePrintLoadConfiguration.blueprintArchivePath);
return BluePrintEnhancerUtils.Companion.saveCBAFile(filePart, cbaLocation).map(fileName -> {
String blueprintId = bluePrintCatalogService
- .uploadToDataBase(cbaLocation.resolve(fileName).toString(), false);
+ .uploadToDataBase(cbaLocation.resolve(fileName).toString(), false);
return blueprintModelSearchRepository.findById(blueprintId).get();
});
-
- } catch (IOException | BluePrintException e) {
- return Mono.error(new BluePrintException("Error uploading the CBA file in channel.", e));
+ } catch (IOException e) {
+ throw new BluePrintException(ErrorCode.IO_FILE_INTERRUPT.getValue(),
+ String.format("I/O Error while uploading the CBA file: %s", e.getMessage()), e);
}
}
/**
- * This is a publishBlueprintModel method
+ * This is a publishBlueprintModel method to change the status published to YES
*
* @param id id
* @return BlueprintModelSearch
* @throws BluePrintException BluePrintException
*/
public BlueprintModelSearch publishBlueprintModel(String id) throws BluePrintException {
- // TODO Implement publish Functionality
- return null;
+ BlueprintModelSearch blueprintModelSearch;
+ Optional<BlueprintModelSearch> dbBlueprintModel = blueprintModelSearchRepository.findById(id);
+ if (dbBlueprintModel.isPresent()) {
+ blueprintModelSearch = dbBlueprintModel.get();
+ } else {
+ String msg = String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id);
+ throw new BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.getValue(), msg);
+ }
+ blueprintModelSearch.setPublished(ApplicationConstants.ACTIVE_Y);
+ return blueprintModelSearchRepository.saveAndFlush(blueprintModelSearch);
}
/**
@@ -122,44 +128,78 @@ public class BlueprintModelService { }
/**
- * This is a getBlueprintModelByNameAndVersion method
+ * This is a getBlueprintModelSearchByNameAndVersion method
*
* @param name name
* @param version version
* @return BlueprintModelSearch
+ * @throws BluePrintException BluePrintException
*/
- public BlueprintModelSearch getBlueprintModelByNameAndVersion(@NotNull String name, @NotNull String version)
- throws BluePrintException {
+ public BlueprintModelSearch getBlueprintModelSearchByNameAndVersion(@NotNull String name, @NotNull String version)
+ throws BluePrintException {
BlueprintModelSearch blueprintModelSearch;
Optional<BlueprintModelSearch> dbBlueprintModel = blueprintModelSearchRepository
- .findByArtifactNameAndArtifactVersion(name, version);
+ .findByArtifactNameAndArtifactVersion(name, version);
if (dbBlueprintModel.isPresent()) {
blueprintModelSearch = dbBlueprintModel.get();
} else {
- throw new BluePrintException(String.format(BLUEPRINT_MODEL_NAME_VERSION_FAILURE_MSG, name, version));
+ throw new BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.getValue(),
+ String.format(BLUEPRINT_MODEL_NAME_VERSION_FAILURE_MSG, name, version));
}
-
return blueprintModelSearch;
}
/**
- * This is a downloadBlueprintModelFile method to find the target file to download and return a file resource using MONO
+ * This is a downloadBlueprintModelFileByNameAndVersion method to download a Blueprint by Name and Version
*
+ * @param name name
+ * @param version version
* @return ResponseEntity<Resource>
+ * @throws BluePrintException BluePrintException
+ */
+ public ResponseEntity<Resource> downloadBlueprintModelFileByNameAndVersion(@NotNull String name, @NotNull String version)
+ throws BluePrintException {
+ BlueprintModel blueprintModel;
+ try {
+ blueprintModel = getBlueprintModelByNameAndVersion(name, version);
+ } catch (BluePrintException e) {
+ throw new BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.getValue(), String.format("Error while " +
+ "downloading the CBA file: %s", e.getMessage()), e);
+ }
+ String fileName = blueprintModel.getId() + ".zip";
+ byte[] file = blueprintModel.getBlueprintModelContent().getContent();
+ return prepareResourceEntity(fileName, file);
+ }
+
+ /**
+ * This is a downloadBlueprintModelFile method to find the target file to download and return a file resource
+ *
+ * @return ResponseEntity<Resource>
+ * @throws BluePrintException BluePrintException
*/
public ResponseEntity<Resource> downloadBlueprintModelFile(@NotNull String id) throws BluePrintException {
BlueprintModel blueprintModel;
try {
blueprintModel = getBlueprintModel(id);
} catch (BluePrintException e) {
- throw new BluePrintException("Error uploading the CBA file in channel.", e);
+ throw new BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.getValue(), String.format("Error while " +
+ "downloading the CBA file: %s", e.getMessage()), e);
}
String fileName = blueprintModel.getId() + ".zip";
byte[] file = blueprintModel.getBlueprintModelContent().getContent();
+ return prepareResourceEntity(fileName, file);
+ }
+
+ /**
+ *
+ * @param (fileName, file)
+ * @return ResponseEntity<Resource>
+ */
+ private ResponseEntity<Resource> prepareResourceEntity(String fileName, byte[] file) {
return ResponseEntity.ok()
- .contentType(MediaType.parseMediaType("text/plain"))
- .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"")
- .body(new ByteArrayResource(file));
+ .contentType(MediaType.parseMediaType("text/plain"))
+ .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"")
+ .body(new ByteArrayResource(file));
}
/**
@@ -175,9 +215,30 @@ public class BlueprintModelService { if (dbBlueprintModel.isPresent()) {
blueprintModel = dbBlueprintModel.get();
} else {
- throw new BluePrintException(String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id));
+ String msg = String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id);
+ throw new BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.getValue(), msg);
}
+ return blueprintModel;
+ }
+ /**
+ * This is a getBlueprintModelByNameAndVersion method
+ *
+ * @param name name
+ * @param version version
+ * @return BlueprintModel
+ * @throws BluePrintException BluePrintException
+ */
+ private BlueprintModel getBlueprintModelByNameAndVersion(@NotNull String name, @NotNull String version)
+ throws BluePrintException {
+ BlueprintModel blueprintModel;
+ Optional<BlueprintModel> dbBlueprintModel = blueprintModelRepository.findByArtifactNameAndArtifactVersion(name, version);
+ if (dbBlueprintModel.isPresent()) {
+ blueprintModel = dbBlueprintModel.get();
+ } else {
+ String msg = String.format(BLUEPRINT_MODEL_NAME_VERSION_FAILURE_MSG, name, version);
+ throw new BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.getValue(), msg);
+ }
return blueprintModel;
}
@@ -194,7 +255,8 @@ public class BlueprintModelService { if (dbBlueprintModel.isPresent()) {
blueprintModelSearch = dbBlueprintModel.get();
} else {
- throw new BluePrintException(String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id));
+ String msg = String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id);
+ throw new BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.getValue(), msg);
}
return blueprintModelSearch;
@@ -213,17 +275,17 @@ public class BlueprintModelService { blueprintModelContentRepository.deleteByBlueprintModel(dbBlueprintModel.get());
blueprintModelRepository.delete(dbBlueprintModel.get());
} else {
- throw new BluePrintException(String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id));
+ String msg = String.format(BLUEPRINT_MODEL_ID_FAILURE_MSG, id);
+ throw new BluePrintException(ErrorCode.RESOURCE_NOT_FOUND.getValue(), msg);
}
}
/**
* This is a getAllBlueprintModel method to retrieve all the BlueprintModel in Database
*
- * @return List<BlueprintModelSearch> list with the controller blueprint archives
+ * @return List<BlueprintModelSearch> list of the controller blueprint archives
*/
public List<BlueprintModelSearch> getAllBlueprintModel() {
return blueprintModelSearchRepository.findAll();
}
-
}
diff --git a/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/common/ErrorMessage.java b/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/common/ErrorMessage.java index 43164126..5486262f 100644 --- a/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/common/ErrorMessage.java +++ b/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/common/ErrorMessage.java @@ -19,11 +19,15 @@ package org.onap.ccsdk.apps.controllerblueprints.service.common; import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
+import com.fasterxml.jackson.annotation.JsonTypeInfo;
+import com.fasterxml.jackson.annotation.JsonTypeName;
import java.io.Serializable;
import java.util.Date;
@JsonInclude(Include.NON_NULL)
+@JsonTypeName("errorMessage")
+@JsonTypeInfo(include= JsonTypeInfo.As.WRAPPER_OBJECT, use=JsonTypeInfo.Id.NAME)
public class ErrorMessage implements Serializable {
private String message;
private Integer code;
diff --git a/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/domain/BlueprintModel.java b/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/domain/BlueprintModel.java index 93954daa..245e4a80 100755 --- a/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/domain/BlueprintModel.java +++ b/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/domain/BlueprintModel.java @@ -36,7 +36,7 @@ import java.util.Date; @EntityListeners({AuditingEntityListener.class})
@Entity
-@Table(name = "CONFIG_MODEL")
+@Table(name = "CONFIG_MODEL", uniqueConstraints=@UniqueConstraint(columnNames={"artifact_name","artifact_version"}))
@Proxy(lazy=false)
public class BlueprintModel implements Serializable {
private static final long serialVersionUID = 1L;
diff --git a/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/domain/BlueprintModelSearch.java b/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/domain/BlueprintModelSearch.java index 8b51bce3..33753b2f 100644 --- a/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/domain/BlueprintModelSearch.java +++ b/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/domain/BlueprintModelSearch.java @@ -18,6 +18,8 @@ package org.onap.ccsdk.apps.controllerblueprints.service.domain;
import com.fasterxml.jackson.annotation.JsonFormat;
+import com.fasterxml.jackson.annotation.JsonTypeInfo;
+import com.fasterxml.jackson.annotation.JsonTypeName;
import org.springframework.data.annotation.LastModifiedDate;
import javax.persistence.*;
@@ -26,6 +28,8 @@ import java.util.Date; @Entity
@Table(name = "CONFIG_MODEL")
+@JsonTypeName("blueprintModel")
+@JsonTypeInfo(include= JsonTypeInfo.As.WRAPPER_OBJECT, use=JsonTypeInfo.Id.NAME)
public class BlueprintModelSearch implements Serializable {
private static final long serialVersionUID = 1L;
diff --git a/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/rs/BlueprintModelRest.java b/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/rs/BlueprintModelRest.java index 9d0b1e3e..255137bf 100644 --- a/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/rs/BlueprintModelRest.java +++ b/ms/controllerblueprints/modules/service/src/main/java/org/onap/ccsdk/apps/controllerblueprints/service/rs/BlueprintModelRest.java @@ -42,31 +42,38 @@ public class BlueprintModelRest { @PostMapping(path = "", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public @ResponseBody
- Mono<BlueprintModelSearch> saveBluePrint(@RequestPart("file") FilePart file) throws BluePrintException{
+ Mono<BlueprintModelSearch> saveBlueprint(@RequestPart("file") FilePart file) throws BluePrintException{
return blueprintModelService.saveBlueprintModel(file);
}
@DeleteMapping(path = "/{id}")
- public void deleteBluePrint(@PathVariable(value = "id") String id) throws BluePrintException {
+ public void deleteBlueprint(@PathVariable(value = "id") String id) throws BluePrintException {
this.blueprintModelService.deleteBlueprintModel(id);
}
@GetMapping(path = "/by-name/{name}/version/{version}", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody
- BlueprintModelSearch getBluePrintByNameAndVersion(@PathVariable(value = "name") String name,
- @PathVariable(value = "version") String version) throws BluePrintException {
- return this.blueprintModelService.getBlueprintModelByNameAndVersion(name, version);
+ BlueprintModelSearch getBlueprintByNameAndVersion(@PathVariable(value = "name") String name,
+ @PathVariable(value = "version") String version) throws BluePrintException {
+ return this.blueprintModelService.getBlueprintModelSearchByNameAndVersion(name, version);
+ }
+
+ @GetMapping(path = "/download/by-name/{name}/version/{version}", produces = MediaType.APPLICATION_JSON_VALUE)
+ public @ResponseBody
+ ResponseEntity<Resource> downloadBlueprintByNameAndVersion(@PathVariable(value = "name") String name,
+ @PathVariable(value = "version") String version) throws BluePrintException {
+ return this.blueprintModelService.downloadBlueprintModelFileByNameAndVersion(name, version);
}
@GetMapping(path = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody
- BlueprintModelSearch getCBA(@PathVariable(value = "id") String id) throws BluePrintException {
+ BlueprintModelSearch getBlueprintModel(@PathVariable(value = "id") String id) throws BluePrintException {
return this.blueprintModelService.getBlueprintModelSearch(id);
}
@GetMapping(path = "", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody
- List<BlueprintModelSearch> getAllCBA() {
+ List<BlueprintModelSearch> getAllBlueprintModel() {
return this.blueprintModelService.getAllBlueprintModel();
}
@@ -76,7 +83,7 @@ public class BlueprintModelRest { return this.blueprintModelService.downloadBlueprintModelFile(id);
}
- @GetMapping(path = "/publish/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
+ @PutMapping(path = "/publish/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody
BlueprintModelSearch publishBlueprintModel(@PathVariable(value = "id") String id) throws BluePrintException {
return this.blueprintModelService.publishBlueprintModel(id);
|