2 d * ============LICENSE_START=======================================================
\r
4 * ================================================================================
\r
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
\r
7 * ================================================================================
\r
8 * Licensed under the Apache License, Version 2.0 (the "License");
\r
9 * you may not use this file except in compliance with the License.
\r
10 * You may obtain a copy of the License at
\r
12 * http://www.apache.org/licenses/LICENSE-2.0
\r
14 * Unless required by applicable law or agreed to in writing, software
\r
15 * distributed under the License is distributed on an "AS IS" BASIS,
\r
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
17 * See the License for the specific language governing permissions and
\r
18 * limitations under the License.
\r
19 * ============LICENSE_END=========================================================
\r
22 package org.openecomp.mso.asdc.client;
\r
25 import java.io.File;
\r
26 import java.io.FileOutputStream;
\r
27 import java.io.IOException;
\r
28 import java.io.UnsupportedEncodingException;
\r
29 import java.util.ArrayList;
\r
30 import java.util.List;
\r
32 import org.onap.sdc.api.IDistributionClient;
\r
33 import org.onap.sdc.api.consumer.IDistributionStatusMessage;
\r
34 import org.onap.sdc.api.consumer.IFinalDistrStatusMessage;
\r
35 import org.onap.sdc.api.consumer.INotificationCallback;
\r
36 import org.onap.sdc.api.consumer.IStatusCallback;
\r
37 import org.onap.sdc.api.notification.IArtifactInfo;
\r
38 import org.onap.sdc.api.notification.INotificationData;
\r
39 import org.onap.sdc.api.notification.IResourceInstance;
\r
40 import org.onap.sdc.api.notification.IStatusData;
\r
41 import org.onap.sdc.api.results.IDistributionClientDownloadResult;
\r
42 import org.onap.sdc.api.results.IDistributionClientResult;
\r
43 import org.onap.sdc.impl.DistributionClientFactory;
\r
44 import org.onap.sdc.utils.DistributionActionResultEnum;
\r
45 import org.onap.sdc.utils.DistributionStatusEnum;
\r
46 import org.openecomp.mso.asdc.client.exceptions.ASDCControllerException;
\r
47 import org.openecomp.mso.asdc.client.exceptions.ASDCDownloadException;
\r
48 import org.openecomp.mso.asdc.client.exceptions.ASDCParametersException;
\r
49 import org.openecomp.mso.asdc.client.exceptions.ArtifactInstallerException;
\r
50 import org.openecomp.mso.asdc.installer.IVfResourceInstaller;
\r
51 import org.openecomp.mso.asdc.installer.ToscaResourceStructure;
\r
52 import org.openecomp.mso.asdc.installer.VfResourceStructure;
\r
53 import org.openecomp.mso.asdc.installer.heat.ToscaResourceInstaller;
\r
54 import org.openecomp.mso.asdc.installer.heat.VfResourceInstaller;
\r
55 import org.openecomp.mso.asdc.tenantIsolation.DistributionStatus;
\r
56 import org.openecomp.mso.asdc.tenantIsolation.WatchdogDistribution;
\r
57 import org.openecomp.mso.asdc.util.ASDCNotificationLogging;
\r
58 import org.openecomp.mso.logger.MessageEnum;
\r
59 import org.openecomp.mso.logger.MsoAlarmLogger;
\r
60 import org.openecomp.mso.logger.MsoLogger;
\r
61 import org.openecomp.mso.properties.MsoPropertiesFactory;
\r
62 import org.openecomp.mso.requestsdb.WatchdogDistributionStatusDb;
\r
63 import org.openecomp.mso.utils.UUIDChecker;
\r
65 public class ASDCController {
\r
67 protected static final MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.ASDC);
\r
69 protected static MsoAlarmLogger alarmLogger = new MsoAlarmLogger ();
\r
71 protected boolean isAsdcClientAutoManaged = false;
\r
73 protected String controllerName;
\r
75 protected ToscaResourceInstaller toscaInstaller;
\r
77 private static MsoPropertiesFactory msoPropertiesFactory = new MsoPropertiesFactory();
\r
80 private final class ResourceInstance implements IResourceInstance {
\r
83 public String getResourceInstanceName(){
\r
84 return new String();
\r
88 public String getResourceName(){
\r
89 return new String();
\r
93 public String getResourceVersion(){
\r
94 return new String();
\r
98 public String getResourceType(){
\r
99 return new String();
\r
103 public String getResourceUUID(){
\r
104 return new String();
\r
107 // Method descriptor #10 ()Ljava/util/List;
\r
109 public java.util.List getArtifacts(){
\r
110 return new ArrayList();
\r
114 public String getResourceInvariantUUID(){
\r
115 return new String();
\r
119 public String getResourceCustomizationUUID(){
\r
120 return new String();
\r
124 public String getCategory(){
\r
125 return new String();
\r
129 public String getSubcategory(){
\r
130 return new String();
\r
135 private final class ASDCStatusCallBack implements IStatusCallback {
\r
138 public void activateCallback (IStatusData iStatus) {
\r
140 long startTime = System.currentTimeMillis ();
\r
141 UUIDChecker.generateUUID (LOGGER);
\r
142 MsoLogger.setServiceName ("ASDCStatusCallBack");
\r
143 MsoLogger.setLogContext (iStatus.getDistributionID (), iStatus.getComponentName());
\r
144 String event = "Receive a callback componentStatus in ASDC, for componentName: " + iStatus.getComponentName() + " and status of " + iStatus.getStatus() + " distributionID of " + iStatus.getDistributionID() +
\r
145 " consumerID of " + iStatus.getConsumerID() + " errorReason of " + iStatus.getErrorReason();
\r
149 if (iStatus.getStatus() == DistributionStatusEnum.COMPONENT_DONE_OK ||
\r
150 iStatus.getStatus() == DistributionStatusEnum.COMPONENT_DONE_ERROR) {
\r
152 LOGGER.debug(event);
\r
154 toscaInstaller.installTheComponentStatus(iStatus);
\r
158 }catch(ArtifactInstallerException e){
\r
159 System.out.println("Error in ASDCStatusCallback " + e.getMessage());
\r
160 LOGGER.debug("Error in ASDCStatusCallback " + e.getMessage());
\r
162 LOGGER.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Completed the Status Call Back");
\r
169 * Inner class for Notification callback
\r
173 private final class ASDCNotificationCallBack implements INotificationCallback {
\r
175 private ASDCController asdcController;
\r
177 ASDCNotificationCallBack (ASDCController controller) {
\r
178 asdcController = controller;
\r
182 * This method can be called multiple times at the same moment.
\r
183 * The controller must be thread safe !
\r
186 public void activateCallback (INotificationData iNotif) {
\r
187 long startTime = System.currentTimeMillis ();
\r
188 UUIDChecker.generateUUID (LOGGER);
\r
189 MsoLogger.setServiceName ("NotificationHandler");
\r
190 MsoLogger.setLogContext (iNotif.getDistributionID (), iNotif.getServiceUUID ());
\r
191 String event = "Receive a callback notification in ASDC, nb of resources: " + iNotif.getResources ().size ();
\r
192 LOGGER.debug(event);
\r
193 asdcController.treatNotification (iNotif);
\r
194 LOGGER.recordAuditEvent (startTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Completed the treatment of the notification");
\r
199 // ***** Controller STATUS code
\r
201 protected int nbOfNotificationsOngoing = 0;
\r
203 public int getNbOfNotificationsOngoing () {
\r
204 return nbOfNotificationsOngoing;
\r
207 private ASDCControllerStatus controllerStatus = ASDCControllerStatus.STOPPED;
\r
209 protected synchronized final void changeControllerStatus (ASDCControllerStatus newControllerStatus) {
\r
210 switch (newControllerStatus) {
\r
213 ++this.nbOfNotificationsOngoing;
\r
214 this.controllerStatus = newControllerStatus;
\r
218 if (this.nbOfNotificationsOngoing > 1) {
\r
219 --this.nbOfNotificationsOngoing;
\r
221 this.nbOfNotificationsOngoing = 0;
\r
222 this.controllerStatus = newControllerStatus;
\r
227 this.controllerStatus = newControllerStatus;
\r
233 public synchronized final ASDCControllerStatus getControllerStatus () {
\r
234 return this.controllerStatus;
\r
237 // ***** END of Controller STATUS code
\r
238 protected ASDCConfiguration asdcConfig;
\r
239 private IDistributionClient distributionClient;
\r
240 private IVfResourceInstaller resourceInstaller;
\r
242 public ASDCController (String controllerConfigName) {
\r
243 isAsdcClientAutoManaged = true;
\r
244 this.controllerName = controllerConfigName;
\r
245 this.resourceInstaller = new VfResourceInstaller();
\r
246 toscaInstaller = new ToscaResourceInstaller();
\r
249 public ASDCController (String controllerConfigName, IDistributionClient asdcClient, IVfResourceInstaller resourceinstaller) {
\r
251 distributionClient = asdcClient;
\r
252 this.resourceInstaller = resourceinstaller;
\r
253 this.controllerName = controllerConfigName;
\r
256 public ASDCController (String controllerConfigName,IDistributionClient asdcClient) {
\r
257 distributionClient = asdcClient;
\r
258 this.controllerName = controllerConfigName;
\r
259 this.resourceInstaller = new VfResourceInstaller();
\r
260 toscaInstaller = new ToscaResourceInstaller();
\r
264 * This method refresh the ASDC Controller config and restart the client.
\r
266 * @return true if config has been reloaded, false otherwise
\r
267 * @throws ASDCControllerException If case of issue with the init or close called during the config reload
\r
268 * @throws ASDCParametersException If there is an issue with the parameters
\r
269 * @throws IOException In case of the key file could not be loaded properly
\r
271 public boolean updateConfigIfNeeded () throws ASDCParametersException, ASDCControllerException, IOException {
\r
272 LOGGER.debug ("Checking whether ASDC config must be reloaded");
\r
275 if (this.asdcConfig != null && this.asdcConfig.hasASDCConfigChanged ()) {
\r
276 LOGGER.debug ("ASDC Config must be reloaded");
\r
278 this.asdcConfig.refreshASDCConfig ();
\r
282 LOGGER.debug ("ASDC Config must NOT be reloaded");
\r
285 } catch (ASDCParametersException ep) {
\r
286 // Try to close it at least to make it consistent with the file specified
\r
287 // We cannot let it run with a different config file, even if it's bad.
\r
288 // This call could potentially throw a ASDCController exception if the controller is currently BUSY.
\r
296 * This method initializes the ASDC Controller and the ASDC Client.
\r
298 * @throws ASDCControllerException It throws an exception if the ASDC Client cannot be instantiated or if an init
\r
299 * attempt is done when already initialized
\r
300 * @throws ASDCParametersException If there is an issue with the parameters provided
\r
301 * @throws IOException In case of issues when trying to load the key file
\r
303 public void initASDC () throws ASDCControllerException, ASDCParametersException, IOException {
\r
304 String event = "Initialize the ASDC Controller";
\r
305 MsoLogger.setServiceName ("InitASDC");
\r
306 LOGGER.debug (event);
\r
307 if (this.getControllerStatus () != ASDCControllerStatus.STOPPED) {
\r
308 String endEvent = "The controller is already initialized, call the closeASDC method first";
\r
309 throw new ASDCControllerException (endEvent);
\r
312 if (asdcConfig == null) {
\r
313 asdcConfig = new ASDCConfiguration (this.controllerName);
\r
316 // attempt to refresh during init as MsoProperties is may be pointing to an old file
\r
317 // Be careful this is static in MsoProperties
\r
318 asdcConfig.refreshASDCConfig ();
\r
320 if (this.distributionClient == null) {
\r
321 distributionClient = DistributionClientFactory.createDistributionClient ();
\r
323 long initStartTime = System.currentTimeMillis ();
\r
324 IDistributionClientResult result = this.distributionClient.init (asdcConfig,
\r
325 new ASDCNotificationCallBack (this), new ASDCStatusCallBack());
\r
326 if (!result.getDistributionActionResult ().equals (DistributionActionResultEnum.SUCCESS)) {
\r
327 String endEvent = "ASDC distribution client init failed with reason:"
\r
328 + result.getDistributionMessageResult ();
\r
329 LOGGER.recordMetricEvent (initStartTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.UnknownError, "Initialization of the ASDC Controller failed with reason:" + result.getDistributionMessageResult (), "ASDC", "init", null);
\r
330 LOGGER.debug (endEvent);
\r
333 this.changeControllerStatus (ASDCControllerStatus.STOPPED);
\r
334 throw new ASDCControllerException ("Initialization of the ASDC Controller failed with reason: "
\r
335 + result.getDistributionMessageResult ());
\r
337 LOGGER.recordMetricEvent (initStartTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully initialize ASDC Controller", "ASDC", "init", null);
\r
339 long clientstartStartTime = System.currentTimeMillis ();
\r
340 result = this.distributionClient.start ();
\r
341 if (!result.getDistributionActionResult ().equals (DistributionActionResultEnum.SUCCESS)) {
\r
342 String endEvent = "ASDC distribution client start failed with reason:"
\r
343 + result.getDistributionMessageResult ();
\r
344 LOGGER.recordMetricEvent (clientstartStartTime, MsoLogger.StatusCode.ERROR, MsoLogger.ResponseCode.UnknownError, endEvent, "ASDC", "start", null);
\r
345 LOGGER.debug (endEvent);
\r
347 this.changeControllerStatus (ASDCControllerStatus.STOPPED);
\r
348 throw new ASDCControllerException ("Startup of the ASDC Controller failed with reason: "
\r
349 + result.getDistributionMessageResult ());
\r
351 LOGGER.recordMetricEvent (clientstartStartTime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully start ASDC distribution client", "ASDC", "start", null);
\r
354 this.changeControllerStatus (ASDCControllerStatus.IDLE);
\r
355 LOGGER.info (MessageEnum.ASDC_INIT_ASDC_CLIENT_SUC, "ASDC", "changeControllerStatus");
\r
359 * This method closes the ASDC Controller and the ASDC Client.
\r
361 * @throws ASDCControllerException It throws an exception if the ASDC Client cannot be closed because
\r
362 * it's currently BUSY in processing notifications.
\r
364 public void closeASDC () throws ASDCControllerException {
\r
366 MsoLogger.setServiceName ("CloseController");
\r
367 if (this.getControllerStatus () == ASDCControllerStatus.BUSY) {
\r
368 throw new ASDCControllerException ("Cannot close the ASDC controller as it's currently in BUSY state");
\r
370 if (this.distributionClient != null) {
\r
371 this.distributionClient.stop ();
\r
372 // If auto managed we can set it to Null, ASDCController controls it.
\r
373 // In the other case the client of this class has specified it, so we can't reset it
\r
374 if (isAsdcClientAutoManaged) {
\r
375 // Next init will initialize it with a new ASDC Client
\r
376 this.distributionClient = null;
\r
380 this.changeControllerStatus (ASDCControllerStatus.STOPPED);
\r
383 private boolean checkResourceAlreadyDeployed (VfResourceStructure resource) throws ArtifactInstallerException {
\r
386 if (toscaInstaller.isResourceAlreadyDeployed (resource)) {
\r
387 LOGGER.info (MessageEnum.ASDC_ARTIFACT_ALREADY_EXIST,
\r
388 resource.getResourceInstance().getResourceInstanceName(),
\r
389 resource.getResourceInstance().getResourceUUID(),
\r
390 resource.getResourceInstance().getResourceName(), "", "");
\r
392 this.sendDeployNotificationsForResource(resource,DistributionStatusEnum.ALREADY_DOWNLOADED,null);
\r
393 this.sendDeployNotificationsForResource(resource,DistributionStatusEnum.ALREADY_DEPLOYED,null);
\r
401 private final static String UUID_PARAM = "(UUID:";
\r
403 private IDistributionClientDownloadResult downloadTheArtifact (IArtifactInfo artifact,
\r
404 String distributionId) throws ASDCDownloadException {
\r
406 LOGGER.debug ("Trying to download the artifact : " + artifact.getArtifactURL ()
\r
408 + artifact.getArtifactUUID ()
\r
410 IDistributionClientDownloadResult downloadResult;
\r
414 downloadResult = distributionClient.download (artifact);
\r
415 if (null == downloadResult) {
\r
416 LOGGER.info (MessageEnum.ASDC_ARTIFACT_NULL, artifact.getArtifactUUID (), "", "");
\r
417 return downloadResult;
\r
419 } catch (RuntimeException e) {
\r
420 LOGGER.debug ("Not able to download the artifact due to an exception: " + artifact.getArtifactURL ());
\r
421 this.sendASDCNotification (NotificationType.DOWNLOAD,
\r
422 artifact.getArtifactURL (),
\r
423 asdcConfig.getConsumerID (),
\r
425 DistributionStatusEnum.DOWNLOAD_ERROR,
\r
427 System.currentTimeMillis ());
\r
429 throw new ASDCDownloadException ("Exception caught when downloading the artifact", e);
\r
432 if (DistributionActionResultEnum.SUCCESS.equals(downloadResult.getDistributionActionResult ())) {
\r
434 LOGGER.info (MessageEnum.ASDC_ARTIFACT_DOWNLOAD_SUC,
\r
435 artifact.getArtifactURL (),
\r
436 artifact.getArtifactUUID (),
\r
437 String.valueOf (downloadResult.getArtifactPayload ().length), "", "");
\r
441 LOGGER.error (MessageEnum.ASDC_ARTIFACT_DOWNLOAD_FAIL,
\r
442 artifact.getArtifactName (),
\r
443 artifact.getArtifactURL (),
\r
444 artifact.getArtifactUUID (),
\r
445 downloadResult.getDistributionMessageResult (), "", "", MsoLogger.ErrorCode.DataError, "ASDC artifact download fail");
\r
447 this.sendASDCNotification (NotificationType.DOWNLOAD,
\r
448 artifact.getArtifactURL (),
\r
449 asdcConfig.getConsumerID (),
\r
451 DistributionStatusEnum.DOWNLOAD_ERROR,
\r
452 downloadResult.getDistributionMessageResult (),
\r
453 System.currentTimeMillis ());
\r
455 throw new ASDCDownloadException ("Artifact " + artifact.getArtifactName ()
\r
456 + " could not be downloaded from ASDC URL "
\r
457 + artifact.getArtifactURL ()
\r
459 + artifact.getArtifactUUID ()
\r
461 + System.lineSeparator ()
\r
462 + "Error message is "
\r
463 + downloadResult.getDistributionMessageResult ()
\r
464 + System.lineSeparator ());
\r
468 this.sendASDCNotification (NotificationType.DOWNLOAD,
\r
469 artifact.getArtifactURL (),
\r
470 asdcConfig.getConsumerID (),
\r
472 DistributionStatusEnum.DOWNLOAD_OK,
\r
474 System.currentTimeMillis ());
\r
475 return downloadResult;
\r
479 private void writeArtifactToFile (IArtifactInfo artifact,
\r
480 IDistributionClientDownloadResult resultArtifact) throws ASDCDownloadException {
\r
482 LOGGER.debug ("Trying to download the artifact : " + artifact.getArtifactURL ()
\r
484 + artifact.getArtifactUUID ()
\r
487 File spoolFile = new File(System.getProperty("mso.config.path") + "/ASDC" + "/" + artifact.getArtifactName());
\r
490 byte[] payloadBytes = resultArtifact.getArtifactPayload();
\r
493 LOGGER.info(MessageEnum.ASDC_RECEIVE_SERVICE_NOTIF, "***WRITE FILE ARTIFACT NAME", "ASDC", artifact.getArtifactName());
\r
495 try (FileOutputStream outFile = new FileOutputStream(System.getProperty("mso.config.path") + "/ASDC" + "/" + artifact.getArtifactName())) {
\r
496 outFile.write(payloadBytes, 0, payloadBytes.length);
\r
498 } catch (Exception e) {
\r
499 LOGGER.debug("Exception :",e);
\r
500 LOGGER.error(MessageEnum.ASDC_ARTIFACT_DOWNLOAD_FAIL,
\r
501 artifact.getArtifactName (),
\r
502 artifact.getArtifactURL (),
\r
503 artifact.getArtifactUUID (),
\r
504 resultArtifact.getDistributionMessageResult (), "", "", MsoLogger.ErrorCode.DataError, "ASDC write to file failed");
\r
510 private void sendDeployNotificationsForResource(VfResourceStructure vfResourceStructure,DistributionStatusEnum distribStatus, String errorReason) {
\r
512 for (IArtifactInfo artifactInfo : vfResourceStructure.getResourceInstance().getArtifacts()) {
\r
514 if ((DistributionStatusEnum.DEPLOY_OK.equals(distribStatus) && !artifactInfo.getArtifactType().equalsIgnoreCase("OTHER"))
\r
515 // This could be NULL if the artifact is a VF module artifact, this won't be present in the MAP
\r
516 && vfResourceStructure.getArtifactsMapByUUID().get(artifactInfo.getArtifactUUID()) != null
\r
517 && vfResourceStructure.getArtifactsMapByUUID().get(artifactInfo.getArtifactUUID()).getDeployedInDb() == 0) {
\r
518 this.sendASDCNotification (NotificationType.DEPLOY,
\r
519 artifactInfo.getArtifactURL (),
\r
520 asdcConfig.getConsumerID (),
\r
521 vfResourceStructure.getNotification().getDistributionID(),
\r
522 DistributionStatusEnum.DEPLOY_ERROR,
\r
523 "The artifact has not been used by the modules defined in the resource",
\r
524 System.currentTimeMillis ());
\r
526 this.sendASDCNotification (NotificationType.DEPLOY,
\r
527 artifactInfo.getArtifactURL (),
\r
528 asdcConfig.getConsumerID (),
\r
529 vfResourceStructure.getNotification().getDistributionID(),
\r
532 System.currentTimeMillis ());
\r
537 private void sendCsarDeployNotification(INotificationData iNotif, VfResourceStructure resourceStructure, ToscaResourceStructure toscaResourceStructure, boolean deploySuccessful, String errorReason) {
\r
539 IArtifactInfo csarArtifact = toscaResourceStructure.getToscaArtifact();
\r
541 if(deploySuccessful){
\r
543 this.sendASDCNotification (NotificationType.DEPLOY,
\r
544 csarArtifact.getArtifactURL (),
\r
545 asdcConfig.getConsumerID (),
\r
546 resourceStructure.getNotification().getDistributionID(),
\r
547 DistributionStatusEnum.DEPLOY_OK,
\r
549 System.currentTimeMillis ());
\r
553 this.sendASDCNotification (NotificationType.DEPLOY,
\r
554 csarArtifact.getArtifactURL (),
\r
555 asdcConfig.getConsumerID (),
\r
556 resourceStructure.getNotification().getDistributionID(),
\r
557 DistributionStatusEnum.DEPLOY_ERROR,
\r
559 System.currentTimeMillis ());
\r
564 private void deployResourceStructure (VfResourceStructure resourceStructure, ToscaResourceStructure toscaResourceStructure) throws ArtifactInstallerException {
\r
566 LOGGER.info (MessageEnum.ASDC_START_DEPLOY_ARTIFACT, resourceStructure.getResourceInstance().getResourceInstanceName(), resourceStructure.getResourceInstance().getResourceUUID(), "ASDC", "deployResourceStructure");
\r
568 String resourceType = resourceStructure.getResourceInstance().getResourceType();
\r
569 String category = resourceStructure.getResourceInstance().getCategory();
\r
570 if("VF".equals(resourceType) && !"Allotted Resource".equalsIgnoreCase(category)){
\r
571 resourceStructure.createVfModuleStructures();
\r
575 toscaInstaller.installTheResource(toscaResourceStructure, resourceStructure);
\r
578 } catch (ArtifactInstallerException e) {
\r
579 LOGGER.info (MessageEnum.ASDC_ARTIFACT_DOWNLOAD_FAIL,
\r
580 resourceStructure.getResourceInstance().getResourceName(),
\r
581 resourceStructure.getResourceInstance().getResourceUUID(),
\r
582 String.valueOf (resourceStructure.getVfModuleStructure().size()), "ASDC", "deployResourceStructure");
\r
583 sendDeployNotificationsForResource(resourceStructure,DistributionStatusEnum.DEPLOY_ERROR,e.getMessage());
\r
587 if (resourceStructure.isDeployedSuccessfully() || toscaResourceStructure.isDeployedSuccessfully()) {
\r
588 LOGGER.info (MessageEnum.ASDC_ARTIFACT_DEPLOY_SUC,
\r
589 resourceStructure.getResourceInstance().getResourceName(),
\r
590 resourceStructure.getResourceInstance().getResourceUUID(),
\r
591 String.valueOf (resourceStructure.getVfModuleStructure().size()), "ASDC", "deployResourceStructure");
\r
592 sendDeployNotificationsForResource(resourceStructure,DistributionStatusEnum.DEPLOY_OK ,null);
\r
598 private enum NotificationType {
\r
602 private void sendASDCNotification (NotificationType notificationType,
\r
603 String artifactURL,
\r
605 String distributionID,
\r
606 DistributionStatusEnum status,
\r
607 String errorReason,
\r
610 String event = "Sending " + notificationType.name ()
\r
614 + " notification to ASDC for artifact:"
\r
617 if (errorReason != null) {
\r
618 event=event+"("+errorReason+")";
\r
620 LOGGER.info (MessageEnum.ASDC_SEND_NOTIF_ASDC, notificationType.name (), status.name (), artifactURL, "ASDC", "sendASDCNotification");
\r
621 LOGGER.debug (event);
\r
623 long subStarttime = System.currentTimeMillis ();
\r
624 String action = "";
\r
626 IDistributionStatusMessage message = new DistributionStatusMessage (artifactURL,
\r
632 switch (notificationType) {
\r
634 if (errorReason != null) {
\r
635 this.distributionClient.sendDownloadStatus (message, errorReason);
\r
637 this.distributionClient.sendDownloadStatus (message);
\r
639 action = "sendDownloadStatus";
\r
642 if (errorReason != null) {
\r
643 this.distributionClient.sendDeploymentStatus (message, errorReason);
\r
645 this.distributionClient.sendDeploymentStatus (message);
\r
647 action = "sendDeploymentdStatus";
\r
652 } catch (RuntimeException e) {
\r
653 // TODO: May be a list containing the unsent notification should be
\r
655 LOGGER.warn (MessageEnum.ASDC_SEND_NOTIF_ASDC_EXEC, "ASDC", "sendASDCNotification", MsoLogger.ErrorCode.SchemaError, "RuntimeException - sendASDCNotification", e);
\r
657 LOGGER.recordMetricEvent (subStarttime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully sent notification to ASDC", "ASDC", action, null);
\r
660 private void sendFinalDistributionStatus (
\r
661 String distributionID,
\r
662 DistributionStatusEnum status,
\r
663 String errorReason) {
\r
666 LOGGER.debug ("Enter sendFinalDistributionStatus with DistributionID " + distributionID + " and Status of " + status.name() + " and ErrorReason " + errorReason);
\r
668 long subStarttime = System.currentTimeMillis ();
\r
672 IFinalDistrStatusMessage finalDistribution = new FinalDistributionStatusMessage(distributionID,status,subStarttime, asdcConfig.getConsumerID());
\r
674 if(errorReason == null){
\r
675 this.distributionClient.sendFinalDistrStatus(finalDistribution);
\r
677 this.distributionClient.sendFinalDistrStatus(finalDistribution, errorReason);
\r
681 } catch (RuntimeException e) {
\r
682 // TODO: May be a list containing the unsent notification should be
\r
684 LOGGER.debug ("Exception caught in sendFinalDistributionStatus " + e.getMessage());
\r
685 LOGGER.warn (MessageEnum.ASDC_SEND_NOTIF_ASDC_EXEC, "ASDC", "sendASDCNotification", MsoLogger.ErrorCode.SchemaError, "RuntimeException - sendASDCNotification", e);
\r
687 LOGGER.recordMetricEvent (subStarttime, MsoLogger.StatusCode.COMPLETE, MsoLogger.ResponseCode.Suc, "Successfully sent Final notification to ASDC", "ASDC", null, null);
\r
690 public void treatNotification (INotificationData iNotif) {
\r
692 int noOfArtifacts = 0;
\r
693 WatchdogDistribution wd = new WatchdogDistribution();
\r
695 for (IResourceInstance resource : iNotif.getResources ()) {
\r
696 noOfArtifacts += resource.getArtifacts ().size ();
\r
698 LOGGER.info (MessageEnum.ASDC_RECEIVE_CALLBACK_NOTIF,
\r
699 String.valueOf (noOfArtifacts),
\r
700 iNotif.getServiceUUID (), "ASDC", "treatNotification");
\r
703 LOGGER.debug(ASDCNotificationLogging.dumpASDCNotification(iNotif));
\r
704 LOGGER.info(MessageEnum.ASDC_RECEIVE_SERVICE_NOTIF, iNotif.getServiceUUID(), "ASDC", "treatNotification");
\r
705 this.changeControllerStatus(ASDCControllerStatus.BUSY);
\r
709 // Process only the Resource artifacts in MSO
\r
710 for (IResourceInstance resource : iNotif.getResources()) {
\r
712 // We process only VNF(VF), Network(VL) and PNF resources on MSO Side
\r
713 //if ("VF".equals(resource.getResourceType()) || "VL".equals(resource.getResourceType()) || "PNF".equals(resource.getResourceType())){
\r
714 this.processResourceNotification(iNotif,resource);
\r
719 //Handle services without any resources
\r
720 if (iNotif.getResources() == null || iNotif.getResources().size() < 1){
\r
722 this.processResourceNotification(iNotif, new ResourceInstance());
\r
725 //********************************************************************************************************
\r
726 //Start Watchdog loop and wait for all components to complete before reporting final status back.
\r
727 // **If timer expires first then we will report a Distribution Error back to ASDC
\r
728 //********************************************************************************************************
\r
729 long initialStartTime = System.currentTimeMillis();
\r
730 boolean componentsComplete = false;
\r
731 String distributionStatus = null;
\r
732 String watchdogError = null;
\r
733 String overallStatus = null;
\r
734 int watchDogTimeout = asdcConfig.getWatchDogTimeout() * 1000;
\r
735 boolean isDeploySuccess = false;
\r
736 WatchdogDistributionStatusDb wdDistributionStatus = WatchdogDistributionStatusDb.getInstance();
\r
739 while(componentsComplete == false && (System.currentTimeMillis() - initialStartTime) < watchDogTimeout)
\r
744 distributionStatus = wd.getOverallDistributionStatus(iNotif.getDistributionID());
\r
745 Thread.sleep(watchDogTimeout / 10);
\r
747 }catch(Exception e){
\r
748 LOGGER.debug ("Exception in Watchdog Loop " + e.getMessage());
\r
749 Thread.sleep(watchDogTimeout / 10);
\r
752 if(distributionStatus != null && !distributionStatus.equalsIgnoreCase(DistributionStatus.INCOMPLETE.name())){
\r
754 if(distributionStatus.equalsIgnoreCase(DistributionStatus.SUCCESS.name())){
\r
755 isDeploySuccess = true;
\r
756 overallStatus = DistributionStatusEnum.DISTRIBUTION_COMPLETE_OK.name();
\r
758 overallStatus = DistributionStatusEnum.DISTRIBUTION_COMPLETE_ERROR.name();
\r
761 componentsComplete = true;
\r
765 if(componentsComplete == false){
\r
766 LOGGER.debug("Timeout of " + watchDogTimeout + " seconds was reached before all components reported status");
\r
767 watchdogError = "Timeout occurred while waiting for all components to report status";
\r
768 overallStatus = DistributionStatusEnum.DISTRIBUTION_COMPLETE_ERROR.name();
\r
771 if(distributionStatus == null){
\r
772 overallStatus = DistributionStatusEnum.DISTRIBUTION_COMPLETE_ERROR.name();
\r
773 LOGGER.debug("DistributionStatus is null for DistributionId: " + iNotif.getDistributionID());
\r
778 wd.executePatchAAI(iNotif.getDistributionID(), iNotif.getServiceInvariantUUID(), overallStatus);
\r
779 LOGGER.debug ("A&AI Updated succefully with Distribution Status!");
\r
781 catch(Exception e) {
\r
782 LOGGER.debug ("Exception in Watchdog executePatchAAI(): " + e.getMessage());
\r
783 watchdogError = "Error calling A&AI " + e.getMessage();
\r
784 if(e.getCause() != null) {
\r
785 LOGGER.debug ("Exception caused by: " + e.getCause().getMessage());
\r
790 if(isDeploySuccess && watchdogError == null){
\r
791 sendFinalDistributionStatus(iNotif.getDistributionID(), DistributionStatusEnum.DISTRIBUTION_COMPLETE_OK, null);
\r
792 wdDistributionStatus.updateWatchdogDistributionIdStatus(iNotif.getDistributionID(), DistributionStatusEnum.DISTRIBUTION_COMPLETE_OK.name());
\r
794 sendFinalDistributionStatus(iNotif.getDistributionID(), DistributionStatusEnum.DISTRIBUTION_COMPLETE_ERROR, watchdogError);
\r
795 wdDistributionStatus.updateWatchdogDistributionIdStatus(iNotif.getDistributionID(), DistributionStatusEnum.DISTRIBUTION_COMPLETE_ERROR.name());
\r
800 } catch (Exception e) {
\r
801 LOGGER.error (MessageEnum.ASDC_GENERAL_EXCEPTION_ARG,
\r
802 "Unexpected exception caught during the notification processing", "ASDC", "treatNotification", MsoLogger.ErrorCode.SchemaError, "RuntimeException in treatNotification",
\r
805 sendFinalDistributionStatus(iNotif.getDistributionID(), DistributionStatusEnum.DISTRIBUTION_COMPLETE_ERROR, e.getMessage());
\r
808 this.changeControllerStatus (ASDCControllerStatus.IDLE);
\r
813 private void processResourceNotification (INotificationData iNotif,IResourceInstance resource) {
\r
814 // For each artifact, create a structure describing the VFModule in a ordered flat level
\r
815 VfResourceStructure resourceStructure = new VfResourceStructure(iNotif,resource);
\r
816 ToscaResourceStructure toscaResourceStructure = new ToscaResourceStructure();
\r
817 boolean deploySuccessful = true;
\r
818 String errorMessage = null;
\r
822 if (!this.checkResourceAlreadyDeployed(resourceStructure)) {
\r
823 for (IArtifactInfo artifact : resource.getArtifacts()) {
\r
825 IDistributionClientDownloadResult resultArtifact = this.downloadTheArtifact(artifact,
\r
826 iNotif.getDistributionID());
\r
828 if (resultArtifact != null) {
\r
829 if (ASDCConfiguration.VF_MODULES_METADATA.equals(artifact.getArtifactType())) {
\r
830 LOGGER.debug("VF_MODULE_ARTIFACT: "+new String(resultArtifact.getArtifactPayload(),"UTF-8"));
\r
831 LOGGER.debug(ASDCNotificationLogging.dumpVfModuleMetaDataList(resourceStructure.decodeVfModuleArtifact(resultArtifact.getArtifactPayload())));
\r
833 resourceStructure.addArtifactToStructure(distributionClient,artifact, resultArtifact);
\r
838 this.processCsarServiceArtifacts(iNotif, toscaResourceStructure);
\r
842 this.deployResourceStructure(resourceStructure, toscaResourceStructure);
\r
844 } catch(ArtifactInstallerException e){
\r
845 deploySuccessful = false;
\r
846 errorMessage = e.getMessage();
\r
849 this.sendCsarDeployNotification(iNotif, resourceStructure, toscaResourceStructure, deploySuccessful, errorMessage);
\r
854 } catch (ArtifactInstallerException | ASDCDownloadException | UnsupportedEncodingException e) {
\r
855 LOGGER.error(MessageEnum.ASDC_GENERAL_EXCEPTION_ARG,
\r
856 "Exception caught during Installation of artifact", "ASDC", "processResourceNotification", MsoLogger.ErrorCode.BusinessProcesssError, "Exception in processResourceNotification", e);
\r
860 private void processCsarServiceArtifacts (INotificationData iNotif, ToscaResourceStructure toscaResourceStructure) {
\r
862 List<IArtifactInfo> serviceArtifacts = iNotif.getServiceArtifacts();
\r
864 for(IArtifactInfo artifact : serviceArtifacts){
\r
866 if(artifact.getArtifactType().equals(ASDCConfiguration.TOSCA_CSAR)){
\r
870 toscaResourceStructure.setToscaArtifact(artifact);
\r
872 IDistributionClientDownloadResult resultArtifact = this.downloadTheArtifact(artifact,iNotif.getDistributionID());
\r
874 writeArtifactToFile(artifact, resultArtifact);
\r
876 toscaResourceStructure.updateResourceStructure(artifact);
\r
878 toscaResourceStructure.setServiceVersion(iNotif.getServiceVersion());
\r
880 LOGGER.debug(ASDCNotificationLogging.dumpCSARNotification(iNotif, toscaResourceStructure));
\r
883 } catch(Exception e){
\r
884 System.out.println("Whats the error " + e.getMessage());
\r
885 LOGGER.error(MessageEnum.ASDC_GENERAL_EXCEPTION_ARG,
\r
886 "Exception caught during processCsarServiceArtifacts", "ASDC", "processCsarServiceArtifacts", MsoLogger.ErrorCode.BusinessProcesssError, "Exception in processCsarServiceArtifacts", e);
\r
893 private static final String UNKNOWN="Unknown";
\r
896 * @return the address of the ASDC we are connected to.
\r
898 public String getAddress () {
\r
899 if (asdcConfig != null) {
\r
900 return asdcConfig.getAsdcAddress ();
\r
906 * @return the environment name of the ASDC we are connected to.
\r
908 public String getEnvironment () {
\r
909 if (asdcConfig != null) {
\r
910 return asdcConfig.getEnvironmentName ();
\r