2 d * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
7 * ================================================================================
8 * Modifications Copyright (c) 2019 Samsung
9 * ================================================================================
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 * ============LICENSE_END=========================================================
24 package org.onap.so.asdc.client;
28 import java.io.FileOutputStream;
29 import java.io.IOException;
30 import java.io.UnsupportedEncodingException;
31 import java.nio.file.Paths;
32 import java.util.List;
33 import java.util.Optional;
35 import org.onap.sdc.api.IDistributionClient;
36 import org.onap.sdc.api.consumer.IDistributionStatusMessage;
37 import org.onap.sdc.api.consumer.IFinalDistrStatusMessage;
38 import org.onap.sdc.api.notification.IArtifactInfo;
39 import org.onap.sdc.api.notification.INotificationData;
40 import org.onap.sdc.api.notification.IResourceInstance;
41 import org.onap.sdc.api.results.IDistributionClientDownloadResult;
42 import org.onap.sdc.api.results.IDistributionClientResult;
43 import org.onap.sdc.impl.DistributionClientFactory;
44 import org.onap.sdc.utils.DistributionActionResultEnum;
45 import org.onap.sdc.utils.DistributionStatusEnum;
46 import org.onap.so.asdc.client.exceptions.ASDCControllerException;
47 import org.onap.so.asdc.client.exceptions.ASDCDownloadException;
48 import org.onap.so.asdc.client.exceptions.ASDCParametersException;
49 import org.onap.so.asdc.client.exceptions.ArtifactInstallerException;
50 import org.onap.so.asdc.installer.IVfResourceInstaller;
51 import org.onap.so.asdc.installer.ToscaResourceStructure;
52 import org.onap.so.asdc.installer.VfResourceStructure;
53 import org.onap.so.asdc.installer.bpmn.BpmnInstaller;
54 import org.onap.so.asdc.installer.heat.ToscaResourceInstaller;
55 import org.onap.so.asdc.tenantIsolation.DistributionStatus;
56 import org.onap.so.asdc.tenantIsolation.WatchdogDistribution;
57 import org.onap.so.asdc.util.ASDCNotificationLogging;
58 import org.onap.so.db.request.beans.WatchdogDistributionStatus;
59 import org.onap.so.db.request.data.repository.WatchdogDistributionStatusRepository;
60 import org.onap.so.logger.ErrorCode;
61 import org.onap.so.logger.MessageEnum;
62 import org.slf4j.Logger;
63 import org.slf4j.LoggerFactory;
64 import org.springframework.beans.factory.annotation.Autowired;
65 import org.springframework.orm.ObjectOptimisticLockingFailureException;
66 import org.springframework.stereotype.Component;
68 import com.fasterxml.jackson.annotation.JsonInclude.Include;
69 import com.fasterxml.jackson.core.JsonProcessingException;
70 import com.fasterxml.jackson.databind.DeserializationFeature;
71 import com.fasterxml.jackson.databind.MapperFeature;
72 import com.fasterxml.jackson.databind.ObjectMapper;
75 public class ASDCController {
77 protected static final Logger logger = LoggerFactory.getLogger(ASDCController.class);
79 protected boolean isAsdcClientAutoManaged = false;
81 protected String controllerName;
83 private ASDCControllerStatus controllerStatus = ASDCControllerStatus.STOPPED;
85 protected int nbOfNotificationsOngoing = 0;
88 private ToscaResourceInstaller toscaInstaller;
91 private BpmnInstaller bpmnInstaller;
94 private WatchdogDistributionStatusRepository wdsRepo;
97 private ASDCConfiguration asdcConfig;
100 private ASDCStatusCallBack asdcStatusCallBack;
103 private ASDCNotificationCallBack asdcNotificationCallBack;
105 private IDistributionClient distributionClient;
107 private static final String UUID_PARAM = "(UUID:";
110 private WatchdogDistribution wd;
113 public int getNbOfNotificationsOngoing () {
114 return nbOfNotificationsOngoing;
117 public IDistributionClient getDistributionClient() {
118 return distributionClient;
123 public void setDistributionClient(IDistributionClient distributionClient) {
124 this.distributionClient = distributionClient;
129 protected void changeControllerStatus (ASDCControllerStatus newControllerStatus) {
130 switch (newControllerStatus) {
133 ++this.nbOfNotificationsOngoing;
134 this.controllerStatus = newControllerStatus;
138 if (this.nbOfNotificationsOngoing > 1) {
139 --this.nbOfNotificationsOngoing;
141 this.nbOfNotificationsOngoing = 0;
142 this.controllerStatus = newControllerStatus;
147 this.controllerStatus = newControllerStatus;
153 public ASDCControllerStatus getControllerStatus () {
154 return this.controllerStatus;
157 public ASDCController () {
158 isAsdcClientAutoManaged = true;
161 public ASDCController (String controllerConfigName) {
162 isAsdcClientAutoManaged = true;
163 this.controllerName = controllerConfigName;
166 public ASDCController (String controllerConfigName, IDistributionClient asdcClient, IVfResourceInstaller resourceinstaller) {
167 distributionClient = asdcClient;
170 public ASDCController (String controllerConfigName,IDistributionClient asdcClient) {
171 distributionClient = asdcClient;
172 this.controllerName = controllerConfigName;
174 public String getControllerName() {
175 return controllerName;
178 public void setControllerName(String controllerName) {
179 this.controllerName = controllerName;
183 * This method initializes the ASDC Controller and the ASDC Client.
185 * @throws ASDCControllerException It throws an exception if the ASDC Client cannot be instantiated or if an init
186 * attempt is done when already initialized
187 * @throws ASDCParametersException If there is an issue with the parameters provided
188 * @throws IOException In case of issues when trying to load the key file
190 public void initASDC () throws ASDCControllerException {
191 String event = "Initialize the ASDC Controller";
193 if (this.getControllerStatus () != ASDCControllerStatus.STOPPED) {
194 String endEvent = "The controller is already initialized, call the closeASDC method first";
195 throw new ASDCControllerException (endEvent);
198 if (asdcConfig != null) {
199 asdcConfig.setAsdcControllerName(controllerName);
202 if (this.distributionClient == null) {
203 distributionClient = DistributionClientFactory.createDistributionClient ();
206 IDistributionClientResult result = this.distributionClient.init (asdcConfig,
207 asdcNotificationCallBack, asdcStatusCallBack);
208 if (!result.getDistributionActionResult ().equals (DistributionActionResultEnum.SUCCESS)) {
209 String endEvent = "ASDC distribution client init failed with reason:"
210 + result.getDistributionMessageResult ();
211 logger.debug (endEvent);
212 this.changeControllerStatus (ASDCControllerStatus.STOPPED);
213 throw new ASDCControllerException ("Initialization of the ASDC Controller failed with reason: "
214 + result.getDistributionMessageResult ());
217 result = this.distributionClient.start ();
218 if (!result.getDistributionActionResult ().equals (DistributionActionResultEnum.SUCCESS)) {
219 String endEvent = "ASDC distribution client start failed with reason:"
220 + result.getDistributionMessageResult ();
221 logger.debug (endEvent);
222 this.changeControllerStatus (ASDCControllerStatus.STOPPED);
223 throw new ASDCControllerException ("Startup of the ASDC Controller failed with reason: "
224 + result.getDistributionMessageResult ());
227 this.changeControllerStatus (ASDCControllerStatus.IDLE);
228 logger.info("{} {} {}", MessageEnum.ASDC_INIT_ASDC_CLIENT_SUC.toString(), "ASDC", "changeControllerStatus");
232 * This method closes the ASDC Controller and the ASDC Client.
234 * @throws ASDCControllerException It throws an exception if the ASDC Client cannot be closed because
235 * it's currently BUSY in processing notifications.
237 public void closeASDC () throws ASDCControllerException {
239 if (this.getControllerStatus () == ASDCControllerStatus.BUSY) {
240 throw new ASDCControllerException ("Cannot close the ASDC controller as it's currently in BUSY state");
242 if (this.distributionClient != null) {
243 this.distributionClient.stop ();
244 // If auto managed we can set it to Null, ASDCController controls it.
245 // In the other case the client of this class has specified it, so we can't reset it
246 if (isAsdcClientAutoManaged) {
247 // Next init will initialize it with a new ASDC Client
248 this.distributionClient = null;
252 this.changeControllerStatus (ASDCControllerStatus.STOPPED);
255 private boolean checkResourceAlreadyDeployed (VfResourceStructure resource) throws ArtifactInstallerException {
258 if (toscaInstaller.isResourceAlreadyDeployed (resource)) {
259 logger.info("{} {} {} {}", MessageEnum.ASDC_ARTIFACT_ALREADY_EXIST.toString(),
260 resource.getResourceInstance().getResourceInstanceName(),
261 resource.getResourceInstance().getResourceUUID(),
262 resource.getResourceInstance().getResourceName());
264 this.sendDeployNotificationsForResource(resource,DistributionStatusEnum.ALREADY_DOWNLOADED,null);
265 this.sendDeployNotificationsForResource(resource,DistributionStatusEnum.ALREADY_DEPLOYED,null);
275 protected IDistributionClientDownloadResult downloadTheArtifact (IArtifactInfo artifact,
276 String distributionId) throws ASDCDownloadException {
278 logger.debug("Trying to download the artifact : " + artifact.getArtifactURL ()
280 + artifact.getArtifactUUID ()
282 IDistributionClientDownloadResult downloadResult;
286 downloadResult = distributionClient.download (artifact);
287 if (null == downloadResult) {
288 logger.info ("{} {}", MessageEnum.ASDC_ARTIFACT_NULL.toString(), artifact.getArtifactUUID());
289 return downloadResult;
291 } catch (RuntimeException e) {
292 logger.debug ("Not able to download the artifact due to an exception: " + artifact.getArtifactURL ());
293 this.sendASDCNotification (NotificationType.DOWNLOAD,
294 artifact.getArtifactURL (),
295 asdcConfig.getConsumerID (),
297 DistributionStatusEnum.DOWNLOAD_ERROR,
299 System.currentTimeMillis ());
301 throw new ASDCDownloadException ("Exception caught when downloading the artifact", e);
304 if (DistributionActionResultEnum.SUCCESS.equals(downloadResult.getDistributionActionResult ())) {
305 logger.info("{} {} {} {}", MessageEnum.ASDC_ARTIFACT_DOWNLOAD_SUC.toString(), artifact.getArtifactURL(),
306 artifact.getArtifactUUID(), String.valueOf(downloadResult.getArtifactPayload().length));
309 logger.error("{} {} {} {} {} {} {}", MessageEnum.ASDC_ARTIFACT_DOWNLOAD_FAIL.toString(),
310 artifact.getArtifactName(), artifact.getArtifactURL(), artifact.getArtifactUUID(),
311 downloadResult.getDistributionMessageResult(), ErrorCode.DataError.getValue(),
312 "ASDC artifact download fail");
314 this.sendASDCNotification (NotificationType.DOWNLOAD,
315 artifact.getArtifactURL (),
316 asdcConfig.getConsumerID (),
318 DistributionStatusEnum.DOWNLOAD_ERROR,
319 downloadResult.getDistributionMessageResult (),
320 System.currentTimeMillis ());
322 throw new ASDCDownloadException ("Artifact " + artifact.getArtifactName ()
323 + " could not be downloaded from ASDC URL "
324 + artifact.getArtifactURL ()
326 + artifact.getArtifactUUID ()
328 + System.lineSeparator ()
329 + "Error message is "
330 + downloadResult.getDistributionMessageResult ()
331 + System.lineSeparator ());
335 this.sendASDCNotification (NotificationType.DOWNLOAD,
336 artifact.getArtifactURL (),
337 asdcConfig.getConsumerID (),
339 DistributionStatusEnum.DOWNLOAD_OK,
341 System.currentTimeMillis ());
342 return downloadResult;
346 private void writeArtifactToFile (IArtifactInfo artifact,
347 IDistributionClientDownloadResult resultArtifact) {
350 "Trying to write artifact to file : " + artifact.getArtifactURL() + UUID_PARAM + artifact.getArtifactUUID()
353 String filePath = Paths.get(System.getProperty("mso.config.path"), "ASDC", artifact.getArtifactVersion(), artifact.getArtifactName()).normalize().toString();
354 // make parent directory
355 File file = new File(filePath);
356 File fileParent = file.getParentFile();
357 if (!fileParent.exists()) {
361 byte[] payloadBytes = resultArtifact.getArtifactPayload();
363 try (FileOutputStream outFile = new FileOutputStream(filePath)) {
364 logger.info("{} {} {} {}", MessageEnum.ASDC_RECEIVE_SERVICE_NOTIF.toString(), "***WRITE FILE ARTIFACT NAME",
365 "ASDC", artifact.getArtifactName());
366 outFile.write(payloadBytes, 0, payloadBytes.length);
368 } catch (Exception e) {
369 logger.debug("Exception :", e);
370 logger.error("{} {} {} {} {} {} {}", MessageEnum.ASDC_ARTIFACT_DOWNLOAD_FAIL.toString(),
371 artifact.getArtifactName(), artifact.getArtifactURL(), artifact.getArtifactUUID(),
372 resultArtifact.getDistributionMessageResult(), ErrorCode.DataError.getValue(),
373 "ASDC write to file failed");
379 protected void sendDeployNotificationsForResource(VfResourceStructure vfResourceStructure,DistributionStatusEnum distribStatus, String errorReason) {
381 for (IArtifactInfo artifactInfo : vfResourceStructure.getResourceInstance().getArtifacts()) {
383 if ((DistributionStatusEnum.DEPLOY_OK.equals(distribStatus) && !artifactInfo.getArtifactType().equalsIgnoreCase("OTHER") && !vfResourceStructure.isAlreadyDeployed())
384 // This could be NULL if the artifact is a VF module artifact, this won't be present in the MAP
385 && vfResourceStructure.getArtifactsMapByUUID().get(artifactInfo.getArtifactUUID()) != null
386 && vfResourceStructure.getArtifactsMapByUUID().get(artifactInfo.getArtifactUUID()).getDeployedInDb() == 0) {
387 this.sendASDCNotification (NotificationType.DEPLOY,
388 artifactInfo.getArtifactURL (),
389 asdcConfig.getConsumerID (),
390 vfResourceStructure.getNotification().getDistributionID(),
391 DistributionStatusEnum.DEPLOY_ERROR,
392 "The artifact has not been used by the modules defined in the resource",
393 System.currentTimeMillis ());
395 this.sendASDCNotification (NotificationType.DEPLOY,
396 artifactInfo.getArtifactURL (),
397 asdcConfig.getConsumerID (),
398 vfResourceStructure.getNotification().getDistributionID(),
401 System.currentTimeMillis ());
406 protected void sendCsarDeployNotification(INotificationData iNotif, VfResourceStructure resourceStructure, ToscaResourceStructure toscaResourceStructure, boolean deploySuccessful, String errorReason) {
408 IArtifactInfo csarArtifact = toscaResourceStructure.getToscaArtifact();
410 if(deploySuccessful){
412 this.sendASDCNotification (NotificationType.DEPLOY,
413 csarArtifact.getArtifactURL (),
414 asdcConfig.getConsumerID (),
415 resourceStructure.getNotification().getDistributionID(),
416 DistributionStatusEnum.DEPLOY_OK,
418 System.currentTimeMillis ());
422 this.sendASDCNotification (NotificationType.DEPLOY,
423 csarArtifact.getArtifactURL (),
424 asdcConfig.getConsumerID (),
425 resourceStructure.getNotification().getDistributionID(),
426 DistributionStatusEnum.DEPLOY_ERROR,
428 System.currentTimeMillis ());
433 protected void deployResourceStructure (VfResourceStructure resourceStructure, ToscaResourceStructure toscaResourceStructure) throws ArtifactInstallerException {
435 logger.info("{} {} {} {}", MessageEnum.ASDC_START_DEPLOY_ARTIFACT.toString(),
436 resourceStructure.getResourceInstance().getResourceInstanceName(),
437 resourceStructure.getResourceInstance().getResourceUUID(), "ASDC");
439 String resourceType = resourceStructure.getResourceInstance().getResourceType();
440 String category = resourceStructure.getResourceInstance().getCategory();
441 if("VF".equals(resourceType) && !"Allotted Resource".equalsIgnoreCase(category)){
442 resourceStructure.createVfModuleStructures();
444 toscaInstaller.installTheResource(toscaResourceStructure, resourceStructure);
446 } catch (ArtifactInstallerException e) {
447 logger.info("{} {} {} {} {} {}", MessageEnum.ASDC_ARTIFACT_DOWNLOAD_FAIL.toString(),
448 resourceStructure.getResourceInstance().getResourceName(),
449 resourceStructure.getResourceInstance().getResourceUUID(),
450 String.valueOf(resourceStructure.getVfModuleStructure().size()), "ASDC", "deployResourceStructure");
451 sendDeployNotificationsForResource(resourceStructure, DistributionStatusEnum.DEPLOY_ERROR, e.getMessage());
455 if (resourceStructure.isDeployedSuccessfully() || toscaResourceStructure.isDeployedSuccessfully()) {
456 logger.info("{} {} {} {} {} {}", MessageEnum.ASDC_ARTIFACT_DEPLOY_SUC.toString(),
457 resourceStructure.getResourceInstance().getResourceName(),
458 resourceStructure.getResourceInstance().getResourceUUID(),
459 String.valueOf(resourceStructure.getVfModuleStructure().size()), "ASDC", "deployResourceStructure");
460 sendDeployNotificationsForResource(resourceStructure, DistributionStatusEnum.DEPLOY_OK, null);
466 private enum NotificationType {
470 protected void sendASDCNotification (NotificationType notificationType,
473 String distributionID,
474 DistributionStatusEnum status,
478 String event = "Sending " + notificationType.name ()
482 + " notification to ASDC for artifact:"
485 if (errorReason != null) {
486 event=event+"("+errorReason+")";
488 logger.info("{} {} {} {} {} {}", MessageEnum.ASDC_SEND_NOTIF_ASDC.toString(), notificationType.name(),
489 status.name(), artifactURL, "ASDC", "sendASDCNotification");
490 logger.debug (event);
494 IDistributionStatusMessage message = new DistributionStatusMessage (artifactURL,
500 switch (notificationType) {
502 if (errorReason != null) {
503 this.distributionClient.sendDownloadStatus (message, errorReason);
505 this.distributionClient.sendDownloadStatus (message);
507 action = "sendDownloadStatus";
510 if (errorReason != null) {
511 this.distributionClient.sendDeploymentStatus (message, errorReason);
513 this.distributionClient.sendDeploymentStatus (message);
515 action = "sendDeploymentdStatus";
520 } catch (RuntimeException e) {
521 logger.warn("{} {} {} {} {}", MessageEnum.ASDC_SEND_NOTIF_ASDC_EXEC.toString(), "ASDC",
522 "sendASDCNotification", ErrorCode.SchemaError.getValue(),
523 "RuntimeException - sendASDCNotification", e);
527 protected void sendFinalDistributionStatus (
528 String distributionID,
529 DistributionStatusEnum status,
530 String errorReason) {
533 logger.debug("Enter sendFinalDistributionStatus with DistributionID " + distributionID + " and Status of " + status
534 .name() + " and ErrorReason " + errorReason);
536 long subStarttime = System.currentTimeMillis ();
540 IFinalDistrStatusMessage finalDistribution = new FinalDistributionStatusMessage(distributionID,status,subStarttime, asdcConfig.getConsumerID());
542 if(errorReason == null){
543 this.distributionClient.sendFinalDistrStatus(finalDistribution);
545 this.distributionClient.sendFinalDistrStatus(finalDistribution, errorReason);
549 } catch (RuntimeException e) {
550 logger.debug("Exception caught in sendFinalDistributionStatus {}", e.getMessage());
551 logger.warn("{} {} {} {} {}", MessageEnum.ASDC_SEND_NOTIF_ASDC_EXEC.toString(), "ASDC", "sendASDCNotification",
552 ErrorCode.SchemaError.getValue(), "RuntimeException - sendASDCNotification", e);
556 private Optional<String> getNotificationJson(INotificationData iNotif) {
557 ObjectMapper mapper = new ObjectMapper();
558 mapper.setSerializationInclusion(Include.NON_NULL);
559 mapper.setSerializationInclusion(Include.NON_EMPTY);
560 mapper.setSerializationInclusion(Include.NON_ABSENT);
561 mapper.enable(MapperFeature.USE_ANNOTATIONS);
562 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
563 Optional<String> returnValue = Optional.empty();
565 returnValue = Optional.of(mapper.writeValueAsString(iNotif));
566 } catch (JsonProcessingException e) {
567 logger.error("Error converting incoming ASDC notification to JSON" , e);
572 public void treatNotification (INotificationData iNotif) {
574 int noOfArtifacts = 0;
577 for (IResourceInstance resource : iNotif.getResources ()) {
578 noOfArtifacts += resource.getArtifacts ().size ();
580 logger.info("{} {} {} {}", MessageEnum.ASDC_RECEIVE_CALLBACK_NOTIF.toString(), String.valueOf(noOfArtifacts),
581 iNotif.getServiceUUID(), "ASDC");
583 logger.debug(ASDCNotificationLogging.dumpASDCNotification(iNotif));
584 logger.info("{} {} {} {}", MessageEnum.ASDC_RECEIVE_SERVICE_NOTIF.toString(), iNotif.getServiceUUID(), "ASDC",
585 "treatNotification");
587 this.changeControllerStatus(ASDCControllerStatus.BUSY);
588 Optional<String> notificationMessage = getNotificationJson(iNotif);
589 toscaInstaller.processWatchdog(iNotif.getDistributionID(), iNotif.getServiceUUID(), notificationMessage,
590 asdcConfig.getConsumerID());
592 // Process only the Resource artifacts in MSO
593 this.processResourceNotification(iNotif);
595 //********************************************************************************************************
596 //Start Watchdog loop and wait for all components to complete before reporting final status back.
597 // **If timer expires first then we will report a Distribution Error back to ASDC
598 //********************************************************************************************************
599 long initialStartTime = System.currentTimeMillis();
600 boolean componentsComplete = false;
601 String distributionStatus = null;
602 String watchdogError = null;
603 String overallStatus = null;
604 int watchDogTimeout = asdcConfig.getWatchDogTimeout() * 1000;
605 boolean isDeploySuccess = false;
607 while(!componentsComplete && (System.currentTimeMillis() - initialStartTime) < watchDogTimeout)
611 distributionStatus = wd.getOverallDistributionStatus(iNotif.getDistributionID());
612 Thread.sleep(watchDogTimeout / 10);
614 logger.debug ("Exception in Watchdog Loop {}", e.getMessage());
615 Thread.sleep(watchDogTimeout / 10);
618 if(distributionStatus != null && !distributionStatus.equalsIgnoreCase(DistributionStatus.INCOMPLETE.name())){
620 if(distributionStatus.equalsIgnoreCase(DistributionStatus.SUCCESS.name())){
621 isDeploySuccess = true;
622 overallStatus = DistributionStatusEnum.DISTRIBUTION_COMPLETE_OK.name();
624 overallStatus = DistributionStatusEnum.DISTRIBUTION_COMPLETE_ERROR.name();
626 componentsComplete = true;
630 if(!componentsComplete){
631 logger.debug("Timeout of {} seconds was reached before all components reported status", watchDogTimeout);
632 watchdogError = "Timeout occurred while waiting for all components to report status";
633 overallStatus = DistributionStatusEnum.DISTRIBUTION_COMPLETE_ERROR.name();
636 if(distributionStatus == null){
637 overallStatus = DistributionStatusEnum.DISTRIBUTION_COMPLETE_ERROR.name();
638 logger.debug("DistributionStatus is null for DistributionId: {}", iNotif.getDistributionID());
642 wd.executePatchAAI(iNotif.getDistributionID(), iNotif.getServiceInvariantUUID(), overallStatus);
643 logger.debug("A&AI Updated succefully with Distribution Status!");
646 logger.debug("Exception in Watchdog executePatchAAI(): {}", e.getMessage());
647 watchdogError = "Error calling A&AI " + e.getMessage();
648 if(e.getCause() != null) {
649 logger.debug("Exception caused by: {}", e.getCause().getMessage());
654 if(isDeploySuccess && watchdogError == null){
655 sendFinalDistributionStatus(iNotif.getDistributionID(), DistributionStatusEnum.DISTRIBUTION_COMPLETE_OK, null);
656 WatchdogDistributionStatus wds = new WatchdogDistributionStatus(iNotif.getDistributionID());
657 wds.setDistributionIdStatus(DistributionStatusEnum.DISTRIBUTION_COMPLETE_OK.toString());
660 sendFinalDistributionStatus(iNotif.getDistributionID(), DistributionStatusEnum.DISTRIBUTION_COMPLETE_ERROR, watchdogError);
661 WatchdogDistributionStatus wds = new WatchdogDistributionStatus(iNotif.getDistributionID());
662 wds.setDistributionIdStatus(DistributionStatusEnum.DISTRIBUTION_COMPLETE_ERROR.toString());
667 } catch(ObjectOptimisticLockingFailureException e) {
669 logger.debug("OptimisticLockingFailure for DistributionId: {} Another process "
670 + "has already altered this distribution, so not going to process it on this site.",
671 iNotif.getDistributionID());
672 logger.error("{} {} {} {} {} {}", MessageEnum.ASDC_GENERAL_EXCEPTION_ARG.toString(),
673 "Database concurrency exception: ", "ASDC", "treatNotification",
674 ErrorCode.BusinessProcesssError.getValue(), "RuntimeException in treatNotification", e);
676 } catch (Exception e) {
677 logger.error("", MessageEnum.ASDC_GENERAL_EXCEPTION_ARG.toString(),
678 "Unexpected exception caught during the notification processing", "ASDC",
679 "treatNotification", ErrorCode.SchemaError.getValue(), "RuntimeException in treatNotification",
683 wd.executePatchAAI(iNotif.getDistributionID(), iNotif.getServiceInvariantUUID(),
684 DistributionStatusEnum.DISTRIBUTION_COMPLETE_ERROR.name());
685 logger.debug("A&AI Updated succefully with Distribution Status of {}",
686 DistributionStatusEnum.DISTRIBUTION_COMPLETE_ERROR.name());
687 } catch (Exception aaiException) {
688 logger.debug("Exception in executePatchAAI(): {}", aaiException);
689 if (aaiException.getCause() != null) {
690 logger.debug("Exception caused by: {}", aaiException.getCause().getMessage());
694 sendFinalDistributionStatus(iNotif.getDistributionID(), DistributionStatusEnum.DISTRIBUTION_COMPLETE_ERROR, e.getMessage());
696 WatchdogDistributionStatus wds = new WatchdogDistributionStatus(iNotif.getDistributionID());
697 wds.setDistributionIdStatus(DistributionStatusEnum.DISTRIBUTION_COMPLETE_ERROR.toString());
701 this.changeControllerStatus (ASDCControllerStatus.IDLE);
705 protected void processResourceNotification (INotificationData iNotif) {
706 // For each artifact, create a structure describing the VFModule in a ordered flat level
707 VfResourceStructure resourceStructure = null;
708 ToscaResourceStructure toscaResourceStructure = new ToscaResourceStructure();
709 boolean deploySuccessful = true;
710 boolean hasVFResource = false;
711 String errorMessage = null;
715 this.processCsarServiceArtifacts(iNotif, toscaResourceStructure);
716 IArtifactInfo iArtifact = toscaResourceStructure.getToscaArtifact();
717 String filePath = System.getProperty("mso.config.path") + "/ASDC/" + iArtifact.getArtifactVersion() + "/" + iArtifact.getArtifactName();
718 File csarFile = new File(filePath);
719 String csarFilePath = csarFile.getAbsolutePath();
720 if (bpmnInstaller.containsWorkflows(csarFilePath)) {
721 bpmnInstaller.installBpmn(csarFilePath);
724 for (IResourceInstance resource : iNotif.getResources()){
726 resourceStructure = new VfResourceStructure(iNotif,resource);
728 String resourceType = resourceStructure.getResourceInstance().getResourceType();
729 String category = resourceStructure.getResourceInstance().getCategory();
731 logger.debug("Processing Resource Type: " + resourceType + " and Model UUID: " + resourceStructure
732 .getResourceInstance().getResourceUUID());
734 if("VF".equals(resourceType) && !"Allotted Resource".equalsIgnoreCase(category)){
736 hasVFResource = true;
738 for (IArtifactInfo artifact : resource.getArtifacts()) {
739 IDistributionClientDownloadResult resultArtifact = this.downloadTheArtifact(artifact,
740 iNotif.getDistributionID());
741 if (resultArtifact != null) {
743 if (ASDCConfiguration.VF_MODULES_METADATA.equals(artifact.getArtifactType())) {
744 logger.debug("VF_MODULE_ARTIFACT: "+ new String(resultArtifact.getArtifactPayload(),"UTF-8"));
745 logger.debug(ASDCNotificationLogging.dumpVfModuleMetaDataList(resourceStructure.decodeVfModuleArtifact
746 (resultArtifact.getArtifactPayload())));
748 resourceStructure.addArtifactToStructure(distributionClient,artifact, resultArtifact);
752 //Deploy VF resource and artifacts
753 logger.debug("Preparing to deploy Service: {}", iNotif.getServiceUUID());
756 this.deployResourceStructure(resourceStructure, toscaResourceStructure);
758 } catch(ArtifactInstallerException e){
759 deploySuccessful = false;
760 errorMessage = e.getMessage();
761 logger.error("Exception occurred", e);
767 // There are cases where the Service has no VF resources, those are handled here
768 if (!hasVFResource) {
770 logger.debug("No resources found for Service: {}", iNotif.getServiceUUID());
773 resourceStructure = new VfResourceStructure(iNotif,new ResourceInstance());
775 this.deployResourceStructure(resourceStructure, toscaResourceStructure);
777 } catch(ArtifactInstallerException e){
778 deploySuccessful = false;
779 errorMessage = e.getMessage();
780 logger.error("Exception occurred", e);
783 this.sendCsarDeployNotification(iNotif, resourceStructure, toscaResourceStructure, deploySuccessful, errorMessage);
785 } catch (ASDCDownloadException | UnsupportedEncodingException e) {
786 logger.error("{} {} {} {} {} {}", MessageEnum.ASDC_GENERAL_EXCEPTION_ARG.toString(),
787 "Exception caught during Installation of artifact", "ASDC", "processResourceNotification",
788 ErrorCode.BusinessProcesssError.getValue(), "Exception in processResourceNotification", e);
791 protected void processCsarServiceArtifacts (INotificationData iNotif, ToscaResourceStructure toscaResourceStructure) {
793 List<IArtifactInfo> serviceArtifacts = iNotif.getServiceArtifacts();
795 for(IArtifactInfo artifact : serviceArtifacts){
797 if(artifact.getArtifactType().equals(ASDCConfiguration.TOSCA_CSAR)){
801 toscaResourceStructure.setToscaArtifact(artifact);
803 IDistributionClientDownloadResult resultArtifact = this.downloadTheArtifact(artifact,iNotif.getDistributionID());
805 writeArtifactToFile(artifact, resultArtifact);
807 toscaResourceStructure.updateResourceStructure(artifact);
809 toscaResourceStructure.setServiceVersion(iNotif.getServiceVersion());
811 logger.debug(ASDCNotificationLogging.dumpCSARNotification(iNotif, toscaResourceStructure));
814 } catch(Exception e){
815 logger.error("{} {} {} {} {} {}", MessageEnum.ASDC_GENERAL_EXCEPTION_ARG.toString(),
816 "Exception caught during processCsarServiceArtifacts", "ASDC", "processCsarServiceArtifacts",
817 ErrorCode.BusinessProcesssError.getValue(), "Exception in processCsarServiceArtifacts", e);
820 else if(artifact.getArtifactType().equals(ASDCConfiguration.WORKFLOWS)){
824 IDistributionClientDownloadResult resultArtifact = this.downloadTheArtifact(artifact,iNotif.getDistributionID());
826 writeArtifactToFile(artifact, resultArtifact);
828 toscaResourceStructure.setToscaArtifact(artifact);
830 logger.debug(ASDCNotificationLogging.dumpASDCNotification(iNotif));
833 } catch(Exception e){
834 logger.info("Whats the error {}", e.getMessage());
835 logger.error("{} {} {} {} {} {}", MessageEnum.ASDC_GENERAL_EXCEPTION_ARG.toString(),
836 "Exception caught during processCsarServiceArtifacts", "ASDC", "processCsarServiceArtifacts",
837 ErrorCode.BusinessProcesssError.getValue(), "Exception in processCsarServiceArtifacts",
846 private static final String UNKNOWN="Unknown";
849 * @return the address of the ASDC we are connected to.
851 public String getAddress () {
852 if (asdcConfig != null) {
853 return asdcConfig.getAsdcAddress ();
859 * @return the environment name of the ASDC we are connected to.
861 public String getEnvironment () {
862 if (asdcConfig != null) {
863 return asdcConfig.getEnvironmentName ();