+
+ @PUT
+ @Path("/deploy/{modelName}")
+ @Consumes(MediaType.APPLICATION_JSON)
+ @Produces(MediaType.APPLICATION_JSON)
+ public Response deployModel(@PathParam("action") String action, @PathParam("modelName") String modelName,
+ @QueryParam("test") String test, CldsModel model) {
+ Date startTime = new Date();
+ LoggingUtils.setRequestContext("CldsService: Deploy model", getPrincipalName());
+ Boolean errorCase = false;
+ try {
+ try {
+ checkForDuplicateServiceVf(modelName, model.getPropText());
+ } catch (IOException | BadRequestException e) {
+ errorCase = true;
+ logger.error("Exception occured during duplicate check for service and VF", e);
+ }
+ String deploymentId = "";
+ // If model is already deployed then pass same deployment id
+ if (model.getDeploymentId() != null && !model.getDeploymentId().isEmpty()) {
+ deploymentId = model.getDeploymentId();
+ } else {
+ deploymentId = "closedLoop_" + UUID.randomUUID() + "_deploymentId";
+ }
+ String createNewDeploymentStatusUrl = dcaeDispatcherServices.createNewDeployment(deploymentId,
+ model.getTypeId());
+ String operationStatus = "processing";
+ long waitingTime = System.nanoTime() + TimeUnit.MINUTES.toNanos(10);
+ while ("processing".equalsIgnoreCase(operationStatus)) {
+ // Break the loop if waiting for more than 10 mins
+ if (waitingTime < System.nanoTime()) {
+ break;
+ }
+ operationStatus = dcaeDispatcherServices.getOperationStatus(createNewDeploymentStatusUrl);
+ }
+ if ("succeeded".equalsIgnoreCase(operationStatus)) {
+ String artifactName = model.getControlName();
+ if (artifactName != null) {
+ artifactName = artifactName + ".yml";
+ }
+ DcaeEvent dcaeEvent = new DcaeEvent();
+ /* set dcae events */
+ dcaeEvent.setArtifactName(artifactName);
+ dcaeEvent.setEvent(DcaeEvent.EVENT_DEPLOYMENT);
+ CldsEvent.insEvent(cldsDao, dcaeEvent.getControlName(), getUserId(), dcaeEvent.getCldsActionCd(),
+ CldsEvent.ACTION_STATE_RECEIVED, null);
+ model.setDeploymentId(deploymentId);
+ model.save(cldsDao, getUserId());
+ } else {
+ logger.info("Deploy model (" + modelName + ") failed...Operation Status is - " + operationStatus);
+ throw new HttpClientErrorException(HttpStatus.INTERNAL_SERVER_ERROR,
+ "Deploy model (" + modelName + ") failed...Operation Status is - " + operationStatus);
+ }
+ logger.info("Deploy model (" + modelName + ") succeeded...Deployment Id is - " + deploymentId);
+ // audit log
+ LoggingUtils.setTimeContext(startTime, new Date());
+ LoggingUtils.setResponseContext("0", "Deploy model success", this.getClass().getName());
+ auditLogger.info("Deploy model completed");
+ } catch (Exception e) {
+ errorCase = true;
+ logger.error("Exception occured during deployModel", e);
+ }
+
+ if (errorCase) {
+ return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(model).build();
+ }
+ return Response.status(Response.Status.OK).entity(model).build();
+ }
+
+ @PUT
+ @Path("/undeploy/{modelName}")
+ @Consumes(MediaType.APPLICATION_JSON)
+ @Produces(MediaType.APPLICATION_JSON)
+ public Response unDeployModel(@PathParam("action") String action, @PathParam("modelName") String modelName,
+ @QueryParam("test") String test, CldsModel model) {
+ Date startTime = new Date();
+ LoggingUtils.setRequestContext("CldsService: Undeploy model", getPrincipalName());
+
+ Boolean errorCase = false;
+ try {
+ String operationStatusUndeployUrl = dcaeDispatcherServices.deleteExistingDeployment(model.getDeploymentId(),
+ model.getTypeId());
+ String operationStatus = "processing";
+ long waitingTime = System.nanoTime() + TimeUnit.MINUTES.toNanos(10);
+ while ("processing".equalsIgnoreCase(operationStatus)) {
+ if (waitingTime < System.nanoTime()) {
+ break;
+ }
+ operationStatus = dcaeDispatcherServices.getOperationStatus(operationStatusUndeployUrl);
+ }
+ if ("succeeded".equalsIgnoreCase(operationStatus)) {
+ String artifactName = model.getControlName();
+ if (artifactName != null) {
+ artifactName = artifactName + ".yml";
+ }
+ DcaeEvent dcaeEvent = new DcaeEvent();
+ // set dcae events
+ dcaeEvent.setArtifactName(artifactName);
+ dcaeEvent.setEvent(DcaeEvent.EVENT_UNDEPLOYMENT);
+ CldsEvent.insEvent(cldsDao, model.getControlName(), getUserId(), dcaeEvent.getCldsActionCd(),
+ CldsEvent.ACTION_STATE_RECEIVED, null);
+ model.setDeploymentId(null);
+ model.save(cldsDao, getUserId());
+ } else {
+ logger.info("Undeploy model (" + modelName + ") failed...Operation Status is - " + operationStatus);
+ throw new HttpClientErrorException(HttpStatus.INTERNAL_SERVER_ERROR,
+ "Undeploy model (" + modelName + ") failed...Operation Status is - " + operationStatus);
+ }
+ logger.info("Undeploy model (" + modelName + ") succeeded.");
+ // audit log
+ LoggingUtils.setTimeContext(startTime, new Date());
+ LoggingUtils.setResponseContext("0", "Undeploy model success", this.getClass().getName());
+ auditLogger.info("Undeploy model completed");
+ } catch (Exception e) {
+ errorCase = true;
+ logger.error("Exception occured during unDeployModel", e);
+ }
+
+ if (errorCase) {
+ return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(model).build();
+ }
+ return Response.status(Response.Status.OK).entity(model).build();
+ }
+
+ private void checkForDuplicateServiceVf(String modelName, String modelPropText) throws IOException {
+ JsonNode modelJson = new ObjectMapper().readTree(modelPropText);
+ JsonNode globalNode = modelJson.get("global");
+ String service = AbstractModelElement.getValueByName(globalNode, "service");
+ List<String> resourceVf = AbstractModelElement.getValuesByName(globalNode, "vf");
+ if (service != null && resourceVf != null && !resourceVf.isEmpty()) {
+ List<CldsModelProp> cldsModelPropList = cldsDao.getDeployedModelProperties();
+ for (CldsModelProp cldsModelProp : cldsModelPropList) {
+ JsonNode currentJson = new ObjectMapper().readTree(cldsModelProp.getPropText());
+ JsonNode currentNode = currentJson.get("global");
+ String currentService = AbstractModelElement.getValueByName(currentNode, "service");
+ List<String> currentVf = AbstractModelElement.getValuesByName(currentNode, "vf");
+ if (currentVf != null && !currentVf.isEmpty()) {
+ if (!modelName.equalsIgnoreCase(cldsModelProp.getName()) && service.equalsIgnoreCase(currentService)
+ && resourceVf.get(0).equalsIgnoreCase(currentVf.get(0))) {
+ throw new BadRequestException("Same Service/VF already exists in " + cldsModelProp.getName()
+ + " model, please select different Service/VF.");
+ }
+ }
+ }
+ }