Skip to content

Commit

Permalink
feat: Blobtrigger (#6)
Browse files Browse the repository at this point in the history
* taxonomy update

* New taxonomy versioning, minor fixes

* Added TaxonomyGetDatalakeFunction.java

* fix log

* Grouped in a single function, added queryparam, renamed datalake

* Added BLobTrigger and cleanup

* Added BLobTrigger and cleanup

* getordefault implementation

* Smell fixed

* Smell fixed

* Added few more catches and fixes

* Smell fixed

* wip

* wip

* Smell fix

* wip

* removed code duplication and fixes

* removed code duplication and fixes

* fixes smell

* Fixed alert labels

* Fixed smells

---------

Co-authored-by: GiampieroPorfiri <134400237+GiampieroPorfiri@users.noreply.github.com>
Co-authored-by: Giampiero Porfiri <72532099+Giampiero-Porfiri@users.noreply.github.com>
Co-authored-by: Francesco Cesareo <cesareo.francesco@gmail.com>
Co-authored-by: andrea-barchi <andrea.barchi@tasgroup.eu>
  • Loading branch information
5 people authored Sep 4, 2023
1 parent 4f83c4a commit 703358c
Show file tree
Hide file tree
Showing 13 changed files with 357 additions and 144 deletions.
2 changes: 1 addition & 1 deletion host.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"routePrefix": ""
}
},
"functions": [ "FnHttpInfo", "FnHttpGenerate", "FnHttpGet"],
"functions": [ "FnHttpInfo", "FnHttpGenerate", "FnHttpGet", "FnBlobTriggerGenerate"],
"logging": {
"applicationInsights": {
"samplingSettings": {
Expand Down
120 changes: 99 additions & 21 deletions src/main/java/it/gov/pagopa/taxonomy/TaxonomyGetFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,59 @@
import com.azure.storage.blob.BlobContainerClient;
import com.azure.storage.blob.BlobServiceClient;
import com.azure.storage.blob.BlobServiceClientBuilder;
import com.azure.storage.blob.models.BlobErrorCode;
import com.azure.storage.blob.models.BlobStorageException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.microsoft.azure.functions.*;
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
import com.microsoft.azure.functions.annotation.FunctionName;
import com.microsoft.azure.functions.annotation.HttpTrigger;
import it.gov.pagopa.taxonomy.enums.VersionEnum;
import it.gov.pagopa.taxonomy.exception.AppErrorCodeMessageEnum;
import it.gov.pagopa.taxonomy.exception.AppException;
import it.gov.pagopa.taxonomy.model.function.ErrorMessage;
import it.gov.pagopa.taxonomy.model.json.TaxonomyJson;
import it.gov.pagopa.taxonomy.model.json.TaxonomyStandard;
import it.gov.pagopa.taxonomy.model.json.TaxonomyTopicFlag;
import it.gov.pagopa.taxonomy.util.AppConstant;
import it.gov.pagopa.taxonomy.util.AppMessageUtil;
import it.gov.pagopa.taxonomy.util.AppUtil;
import org.apache.commons.lang3.exception.ExceptionUtils;

import java.text.MessageFormat;
import java.time.Instant;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;

public class TaxonomyGetFunction {

private static final String storageConnString = System.getenv("STORAGE_ACCOUNT_CONN_STRING");
private static final String blobContainerNameOutput = System.getenv("BLOB_CONTAINER_NAME_OUTPUT");
private static final String jsonName = System.getenv("JSON_NAME");
private static String msg = null;
private static final String VERSION_NOT_EXISTS_ERROR = AppMessageUtil.getMessage("version.not.exists.error");
private static final String GENERIC_RETRIEVAL_ERROR = AppMessageUtil.getMessage("generic.retrieval.error");
private static final String STORAGE_CONN_STRING = System.getenv("STORAGE_ACCOUNT_CONN_STRING");
private static final String BLOB_CONTAINER_NAME_OUTPUT = System.getenv("BLOB_CONTAINER_NAME_OUTPUT");
private static final String JSON_NAME = System.getenv("JSON_NAME");
private static ObjectMapper objectMapper = null;

private static BlobContainerClient blobContainerClientOutput;
private static BlobServiceClient blobServiceClient;

private static BlobServiceClient getBlobServiceClient(){
if(blobServiceClient == null){
blobServiceClient = new BlobServiceClientBuilder().connectionString(storageConnString).buildClient();
blobServiceClient = new BlobServiceClientBuilder().connectionString(STORAGE_CONN_STRING).buildClient();
}
return blobServiceClient;
}

private static BlobContainerClient getBlobContainerClientOutput(){
if(blobContainerClientOutput == null){
blobContainerClientOutput = getBlobServiceClient().createBlobContainerIfNotExists(blobContainerNameOutput);
blobContainerClientOutput = getBlobServiceClient().createBlobContainerIfNotExists(BLOB_CONTAINER_NAME_OUTPUT);
}
return blobContainerClientOutput;
}
Expand All @@ -62,39 +73,84 @@ public HttpResponseMessage getTaxonomy(
@HttpTrigger(
name = "FnHttpGetTrigger",
methods = {HttpMethod.GET},
route = "taxonomy",
route = "taxonomy/{version?}",
authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request,
final ExecutionContext context) {
Logger logger = context.getLogger();

try {
Map<String, String> queryParams = request.getQueryParameters();
String version = queryParams.getOrDefault("version", VersionEnum.STANDARD.toString());

if(!version.equalsIgnoreCase(VersionEnum.STANDARD.toString()) &&
!version.equalsIgnoreCase(VersionEnum.TOPICFLAG.toString())) {

logger.info(VERSION_NOT_EXISTS_ERROR);
String payload = AppUtil.getPayload(getObjectMapper(), ErrorMessage.builder()
.message(AppMessageUtil.getMessage(GENERIC_RETRIEVAL_ERROR))
.error(VERSION_NOT_EXISTS_ERROR)
.build());

return AppUtil.writeResponse(request,
HttpStatus.BAD_REQUEST,
payload);
}

TaxonomyJson taxonomyJson = getTaxonomy(logger);

Map<String, String> map = new LinkedHashMap<>();
map.put(AppConstant.RESPONSE_HEADER_UUID, taxonomyJson.getUuid());
map.put(AppConstant.RESPONSE_HEADER_CREATED, taxonomyJson.getCreated().toString());
map.put(AppConstant.RESPONSE_HEADER_VERSION, version);

String payload = generatePayload(logger, version, taxonomyJson);

String payload = AppUtil.getPayload(getObjectMapper(), taxonomyJson.getTaxonomyList());
logger.info("Taxonomy retrieved successfully");
return AppUtil.writeResponseWithHeaders(request,
HttpStatus.OK,
payload,
map);

} catch (AppException e) {
logger.log(Level.SEVERE, "[ALERT] AppException at " + Instant.now() + "\n" + ExceptionUtils.getStackTrace(e), e);
logger.log(Level.SEVERE, MessageFormat.format("[ALERT][Get] AppException at {0}\n {1}", Instant.now(), ExceptionUtils.getMessage(e)));

String payload = AppUtil.getPayload(getObjectMapper(), ErrorMessage.builder()
.message("Taxonomy retrieval failed")
.message(AppMessageUtil.getMessage(GENERIC_RETRIEVAL_ERROR))
.error(e.getCodeMessage().message(e.getArgs()))
.build());
return AppUtil.writeResponse(request,
HttpStatus.valueOf(e.getCodeMessage().httpStatus().name()),
payload);
HttpStatus.valueOf(e.getCodeMessage().httpStatus().name()),
payload);
} catch (BlobStorageException e) {
if(e.getErrorCode().equals(BlobErrorCode.BLOB_NOT_FOUND)) {
logger.log(Level.SEVERE, MessageFormat.format("[ALERT][Get] BlobStorageException at {0}\n {1}",Instant.now(), ExceptionUtils.getStackTrace(e)));

AppException appException = new AppException(e, AppErrorCodeMessageEnum.BLOB_NOT_FOUND_JSON_ERROR);
String payload = AppUtil.getPayload(getObjectMapper(), ErrorMessage.builder()
.message(GENERIC_RETRIEVAL_ERROR)
.error(appException.getCodeMessage().message(appException.getArgs()))
.build());
return AppUtil.writeResponse(request,
HttpStatus.valueOf(appException.getCodeMessage().httpStatus().name()),
payload
);
} else {
logger.log(Level.SEVERE, MessageFormat.format("[ALERT][Get] BlobStorageException at {0}\n {1}", Instant.now(), ExceptionUtils.getStackTrace(e)));
AppException appException = new AppException(e, AppErrorCodeMessageEnum.ERROR);
String payload = AppUtil.getPayload(getObjectMapper(), ErrorMessage.builder()
.message(GENERIC_RETRIEVAL_ERROR)
.error(appException.getCodeMessage().message(appException.getArgs()))
.build());
return AppUtil.writeResponse(request,
HttpStatus.valueOf(appException.getCodeMessage().httpStatus().name()),
payload
);
}
} catch (Exception e) {
logger.log(Level.SEVERE, "[ALERT] Generic error at " + Instant.now() + "\n" + ExceptionUtils.getStackTrace(e), e);
logger.log(Level.SEVERE, MessageFormat.format("[ALERT][Get] GenericError at {0}\n {1}", Instant.now(), ExceptionUtils.getMessage(e)));

AppException appException = new AppException(e, AppErrorCodeMessageEnum.ERROR);
String payload = AppUtil.getPayload(getObjectMapper(), ErrorMessage.builder()
.message("Taxonomy retrieval failed")
.message(AppMessageUtil.getMessage(GENERIC_RETRIEVAL_ERROR))
.error(appException.getCodeMessage().message(appException.getArgs()))
.build());
return AppUtil.writeResponse(request,
Expand All @@ -105,15 +161,37 @@ public HttpResponseMessage getTaxonomy(

private static TaxonomyJson getTaxonomy(Logger logger) {
try {
Instant now = Instant.now();
logger.info("Retrieving standard json from the blob storage at: [" + now + "]");
String content = getBlobContainerClientOutput().getBlobClient(jsonName).downloadContent().toString();
TaxonomyJson taxonomyJson = getObjectMapper().readValue(content, TaxonomyJson.class);
logger.info("Versioning json id = [" + taxonomyJson.getUuid() + "] to the standard version");
return taxonomyJson;
msg=MessageFormat.format("Retrieving the json file from the blob storage at: [{0}]", Instant.now());
logger.info(msg);
String content = getBlobContainerClientOutput()
.getBlobClient(JSON_NAME)
.downloadContent()
.toString();
return getObjectMapper().readValue(content, TaxonomyJson.class);
} catch (JsonProcessingException parsingException) {
logger.info("An AppException has occurred");
throw new AppException(parsingException, AppErrorCodeMessageEnum.JSON_PARSING_ERROR);
}
}

private static String generatePayload(Logger logger, String version, TaxonomyJson taxonomyJson) {
String payload = null;
if (version.equalsIgnoreCase(VersionEnum.STANDARD.toString())) {
msg=MessageFormat.format("Versioning json id = [{0}] to the {1} version", taxonomyJson.getUuid(), VersionEnum.STANDARD);
logger.info(msg);
List<TaxonomyStandard> taxonomyList = getObjectMapper().convertValue(taxonomyJson.getTaxonomyList(), new TypeReference<>() {});
payload = AppUtil.getPayload(getObjectMapper(), taxonomyList);
msg=MessageFormat.format("{0} taxonomy retrieved successfully", VersionEnum.STANDARD);
logger.info(msg);
} else if (version.equalsIgnoreCase(VersionEnum.TOPICFLAG.toString())) {
msg=MessageFormat.format("Versioning json id = [{0}] to the {1} version", taxonomyJson.getUuid(), VersionEnum.TOPICFLAG);
logger.info(msg);

List<TaxonomyTopicFlag> taxonomyList = getObjectMapper().convertValue(taxonomyJson.getTaxonomyList(), new TypeReference<>() {});
payload = AppUtil.getPayload(getObjectMapper(), taxonomyList);
msg=MessageFormat.format("{0} taxonomy retrieved successfully", VersionEnum.TOPICFLAG);
logger.info(msg);
}
return payload;
}
}
Loading

0 comments on commit 703358c

Please sign in to comment.