+
+ @PUT
+ @Path("/deploy/{modelName}")
+ @Consumes(MediaType.APPLICATION_JSON)
+ @Produces(MediaType.APPLICATION_JSON)
+ public CldsModel deployModel(@PathParam("action") String action, @PathParam("modelName") String modelName,
+ @QueryParam("test") String test, CldsModel model) throws IOException {
+ Date startTime = new Date();
+ LoggingUtils.setRequestContext("CldsService: Deploy model", getPrincipalName());
+ String 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");
+ return model;
+ }
+
+ @PUT
+ @Path("/undeploy/{modelName}")
+ @Consumes(MediaType.APPLICATION_JSON)
+ @Produces(MediaType.APPLICATION_JSON)
+ public CldsModel unDeployModel(@PathParam("action") String action, @PathParam("modelName") String modelName,
+ @QueryParam("test") String test, CldsModel model) throws IOException {
+ Date startTime = new Date();
+ LoggingUtils.setRequestContext("CldsService: Undeploy model", getPrincipalName());
+ 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");
+ return model;
+ }
+
+ private String getGlobalCldsString() {
+ try {
+ if (null == globalCldsProperties) {
+ globalCldsProperties = new Properties();
+ globalCldsProperties.load(appContext.getResource(globalClds).getInputStream());
+ }
+ return (String) globalCldsProperties.get("globalCldsProps");
+ } catch (IOException e) {
+ logger.error("Unable to load the globalClds due to an exception", e);
+ throw new CldsConfigException("Unable to load the globalClds due to an exception", e);
+ }