From 44cb2fcd7f6686e6531a5a5222d45bdf31739efb Mon Sep 17 00:00:00 2001 From: Alexis de Talhouët Date: Sun, 13 Jan 2019 16:49:32 -0500 Subject: Implement BluePrintManagementServiceGrpc MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I66a6a3c23b5a3c24ce8d61178f00f053cb8bbbdc Issue-ID: CCSDK-909 Signed-off-by: Alexis de Talhouët --- .../api/BluePrintManagementGRPCHandler.kt | 93 ++++++++++++++++------ .../selfservice/api/utils/TimeUtils.kt | 27 +++++++ 2 files changed, 97 insertions(+), 23 deletions(-) create mode 100644 ms/blueprintsprocessor/modules/inbounds/selfservice-api/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/selfservice/api/utils/TimeUtils.kt (limited to 'ms/blueprintsprocessor/modules/inbounds/selfservice-api/src/main/kotlin') diff --git a/ms/blueprintsprocessor/modules/inbounds/selfservice-api/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/selfservice/api/BluePrintManagementGRPCHandler.kt b/ms/blueprintsprocessor/modules/inbounds/selfservice-api/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/selfservice/api/BluePrintManagementGRPCHandler.kt index 17191f31d..aa01f2204 100644 --- a/ms/blueprintsprocessor/modules/inbounds/selfservice-api/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/selfservice/api/BluePrintManagementGRPCHandler.kt +++ b/ms/blueprintsprocessor/modules/inbounds/selfservice-api/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/selfservice/api/BluePrintManagementGRPCHandler.kt @@ -1,5 +1,6 @@ /* * Copyright © 2017-2018 AT&T Intellectual Property. + * Modifications Copyright © 2019 Bell Canada. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,48 +17,94 @@ package org.onap.ccsdk.apps.blueprintsprocessor.selfservice.api +import io.grpc.StatusException import io.grpc.stub.StreamObserver import org.apache.commons.io.FileUtils import org.onap.ccsdk.apps.blueprintsprocessor.core.BluePrintCoreConfiguration -import org.onap.ccsdk.apps.controllerblueprints.management.api.* +import org.onap.ccsdk.apps.blueprintsprocessor.selfservice.api.utils.currentTimestamp +import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintCatalogService +import org.onap.ccsdk.apps.controllerblueprints.management.api.BluePrintManagementInput +import org.onap.ccsdk.apps.controllerblueprints.management.api.BluePrintManagementOutput +import org.onap.ccsdk.apps.controllerblueprints.management.api.BluePrintManagementServiceGrpc +import org.onap.ccsdk.apps.controllerblueprints.management.api.CommonHeader +import org.onap.ccsdk.apps.controllerblueprints.management.api.Status import org.slf4j.LoggerFactory import org.springframework.stereotype.Service import java.io.File @Service -class BluePrintManagementGRPCHandler(private val bluePrintCoreConfiguration: BluePrintCoreConfiguration) +class BluePrintManagementGRPCHandler(private val bluePrintCoreConfiguration: BluePrintCoreConfiguration, + private val bluePrintCatalogService: BluePrintCatalogService) : BluePrintManagementServiceGrpc.BluePrintManagementServiceImplBase() { private val log = LoggerFactory.getLogger(BluePrintManagementGRPCHandler::class.java) - override fun uploadBlueprint(request: BluePrintUploadInput, responseObserver: StreamObserver) { - val response = BluePrintUploadOutput.newBuilder().setCommonHeader(request.commonHeader).build() + override fun uploadBlueprint(request: BluePrintManagementInput, responseObserver: StreamObserver) { + val blueprintName = request.blueprintName + val blueprintVersion = request.blueprintVersion + val blueprint = "blueprint $blueprintName:$blueprintVersion" + + log.info("request(${request.commonHeader.requestId}): Received upload $blueprint") + + val blueprintArchivedFilePath = "${bluePrintCoreConfiguration.archivePath}/$blueprintName/$blueprintVersion/$blueprintName.zip" try { - val blueprintName = request.blueprintName - val blueprintVersion = request.blueprintVersion - val filePath = "${bluePrintCoreConfiguration.archivePath}/$blueprintName/$blueprintVersion" - val blueprintDir = File(filePath) + val blueprintArchivedFile = File(blueprintArchivedFilePath) + + saveToDisk(request, blueprintArchivedFile) + val blueprintId = bluePrintCatalogService.saveToDatabase(blueprintArchivedFile) + + File("${bluePrintCoreConfiguration.archivePath}/$blueprintName").deleteRecursively() - log.info("Re-creating blueprint directory(${blueprintDir.absolutePath})") - FileUtils.deleteDirectory(blueprintDir) - FileUtils.forceMkdir(blueprintDir) + responseObserver.onNext(successStatus("Successfully uploaded $blueprint with id($blueprintId)", request.commonHeader)) + responseObserver.onCompleted() + } catch (e: Exception) { + failStatus("request(${request.commonHeader.requestId}): Failed to upload $blueprint at path $blueprintArchivedFilePath", e) + } + } - val file = File("${blueprintDir.absolutePath}/$blueprintName.zip") - log.info("Writing CBA File under :${file.absolutePath}") + override fun removeBlueprint(request: BluePrintManagementInput, responseObserver: StreamObserver) { + val blueprintName = request.blueprintName + val blueprintVersion = request.blueprintVersion + val blueprint = "blueprint $blueprintName:$blueprintVersion" - val fileChunk = request.fileChunk + log.info("request(${request.commonHeader.requestId}): Received delete $blueprint") - file.writeBytes(fileChunk.chunk.toByteArray()).apply { - log.info("CBA file(${file.absolutePath} written successfully") - } + try { + bluePrintCatalogService.deleteFromDatabase(blueprintName, blueprintVersion) + responseObserver.onNext(successStatus("Successfully deleted $blueprint", request.commonHeader)) + responseObserver.onCompleted() } catch (e: Exception) { - log.error("failed to upload file ", e) + failStatus("request(${request.commonHeader.requestId}): Failed to delete $blueprint", e) + } + } + + private fun saveToDisk(request: BluePrintManagementInput, blueprintDir: File) { + log.debug("request(${request.commonHeader.requestId}): Writing CBA File under :${blueprintDir.absolutePath}") + if (blueprintDir.exists()) { + log.debug("request(${request.commonHeader.requestId}): Re-creating blueprint directory(${blueprintDir.absolutePath})") + FileUtils.deleteDirectory(blueprintDir.parentFile) + } + FileUtils.forceMkdir(blueprintDir.parentFile) + blueprintDir.writeBytes(request.fileChunk.chunk.toByteArray()).apply { + log.debug("request(${request.commonHeader.requestId}): CBA file(${blueprintDir.absolutePath} written successfully") } - responseObserver.onNext(response) - responseObserver.onCompleted() } - override fun removeBlueprint(request: BluePrintRemoveInput?, responseObserver: StreamObserver?) { - //TODO + private fun successStatus(message: String, header: CommonHeader): BluePrintManagementOutput = + BluePrintManagementOutput.newBuilder() + .setCommonHeader(header) + .setStatus(Status.newBuilder() + .setTimestamp(currentTimestamp()) + .setMessage(message) + .setCode(200) + .build()) + .build() + + private fun failStatus(message: String, e: Exception): StatusException { + log.error(message, e) + return io.grpc.Status.INTERNAL + .withDescription(message) + .withCause(e) + .asException() } -} \ No newline at end of file +} diff --git a/ms/blueprintsprocessor/modules/inbounds/selfservice-api/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/selfservice/api/utils/TimeUtils.kt b/ms/blueprintsprocessor/modules/inbounds/selfservice-api/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/selfservice/api/utils/TimeUtils.kt new file mode 100644 index 000000000..78fd022fe --- /dev/null +++ b/ms/blueprintsprocessor/modules/inbounds/selfservice-api/src/main/kotlin/org/onap/ccsdk/apps/blueprintsprocessor/selfservice/api/utils/TimeUtils.kt @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2019 Bell Canada. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.onap.ccsdk.apps.blueprintsprocessor.selfservice.api.utils + +import java.time.LocalDateTime +import java.time.ZoneId +import java.time.format.DateTimeFormatter + + +fun currentTimestamp(): String { + val now = LocalDateTime.now(ZoneId.systemDefault()) + val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'") + return formatter.format(now) +} -- cgit 1.2.3-korg