X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?p=it%2Fotf.git;a=blobdiff_plain;f=otf-service-api%2Fsrc%2Fmain%2Fjava%2Forg%2Foran%2Fotf%2Fapi%2Fhandler%2FCamundaProcessDeploymentHandler.java;fp=otf-service-api%2Fsrc%2Fmain%2Fjava%2Forg%2Foran%2Fotf%2Fapi%2Fhandler%2FCamundaProcessDeploymentHandler.java;h=25c06b37891e0c848d68f54afaaec4fb032e4b61;hp=0000000000000000000000000000000000000000;hb=14f6f95c84a4a1fa8774190db4a03fd0214ec55f;hpb=f49bd1efeaaddd4891c1f329b18d8cfb28b3e75b diff --git a/otf-service-api/src/main/java/org/oran/otf/api/handler/CamundaProcessDeploymentHandler.java b/otf-service-api/src/main/java/org/oran/otf/api/handler/CamundaProcessDeploymentHandler.java new file mode 100644 index 0000000..25c06b3 --- /dev/null +++ b/otf-service-api/src/main/java/org/oran/otf/api/handler/CamundaProcessDeploymentHandler.java @@ -0,0 +1,131 @@ +/* Copyright (c) 2019 AT&T Intellectual Property. # +# # +# 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.oran.otf.api.handler; + +import org.oran.otf.api.Utilities; +import org.oran.otf.common.utility.http.ResponseUtility; +import java.io.InputStream; +import java.util.Base64; +import javax.ws.rs.core.Response; +import org.apache.http.HttpEntity; +import org.apache.http.client.ClientProtocolException; +import org.apache.http.client.ResponseHandler; +import org.apache.http.client.methods.HttpUriRequest; +import org.apache.http.client.methods.RequestBuilder; +import org.apache.http.conn.HttpHostConnectException; +import org.apache.http.conn.ssl.NoopHostnameVerifier; +import org.apache.http.entity.ContentType; +import org.apache.http.entity.mime.MultipartEntityBuilder; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +@Component +public class CamundaProcessDeploymentHandler { + private static final Logger logger = + LoggerFactory.getLogger(CamundaProcessDeploymentHandler.class); + + private CamundaProcessDeploymentHandler() { + // prevent instantiation + } + + public Response start(InputStream bpmn, InputStream compressedResources) { + // Read necessary environment variables - Avoiding using Spring dependencies (@Value) + String host = System.getenv("otf.camunda.host"); + String path = System.getenv("otf.camunda.uri.deploy-test-strategy-zip"); + int port = Utilities.TryGetEnvironmentVariable("otf.camunda.port"); + String aafCredentialsDecoded = + System.getenv("AAF_ID") + ":" + System.getenv("AAF_MECH_PASSWORD"); + + if (!Utilities.isHostValid(host)) { + logger.error("Host (%s) must use either the http or https protocol.", host); + return null; + } + + if (!Utilities.isPortValid(port)) { + logger.error( + "Invalid port (%s) specified as environment variable 'otf.camunda.port'.", + System.getenv("otf.camunda.port")); + return null; + } + + // Form the full url + String postUrl = String.format("%s:%s/%s", host, port, path); + + try (CloseableHttpClient httpclient = + HttpClients.custom().setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE).build()) { + + // build multipart upload request + MultipartEntityBuilder builder = + MultipartEntityBuilder.create() + .addBinaryBody("bpmn", bpmn, ContentType.DEFAULT_BINARY, "bpmn"); + + // add resources to the request if they were supplied + if (compressedResources != null) { + builder.addBinaryBody( + "resources", compressedResources, ContentType.DEFAULT_BINARY, "resources"); + } + + HttpEntity data = builder.build(); + + // build http request and assign multipart upload data + HttpUriRequest request = + RequestBuilder.post(postUrl) + .addHeader( + "Authorization", + "Basic " + Base64.getEncoder().encodeToString(aafCredentialsDecoded.getBytes())) + .setEntity(data) + .build(); + + System.out.println("Executing request " + request.getRequestLine()); + + // Create a custom response handler + ResponseHandler responseHandler = + response -> { + int status = response.getStatusLine().getStatusCode(); + if (status >= 200 && status < 300) { + HttpEntity entity = response.getEntity(); + String message = entity != null ? EntityUtils.toString(entity) : null; + return Response.ok(message).build(); + } else if (status == 400) { + HttpEntity entity = response.getEntity(); + String message = + entity != null + ? EntityUtils.toString(entity) + : "Supplied bpmn file is not deployable."; + return Utilities.Http.BuildResponse.badRequestWithMessage(message); + } else { + throw new ClientProtocolException("Unexpected response status: " + status); + } + }; + + Response responseBody = httpclient.execute(request, responseHandler); + System.out.println("----------------------------------------"); + System.out.println(responseBody.getEntity().toString()); + + return responseBody; + } catch (HttpHostConnectException e) { + return ResponseUtility.Build.serviceUnavailableWithMessage(e.getMessage()); + } catch (Exception e) { + e.printStackTrace(); + return ResponseUtility.Build.internalServerErrorWithMessage("Unable to deploy definition."); + } + } +}