2 * ============LICENSE_START=======================================================
\r
4 * ================================================================================
\r
5 * Copyright (C) 2018 AT&T Intellectual Property. All rights
\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
20 * ===================================================================
\r
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
24 package org.onap.clamp.clds.sdc.controller;
\r
26 import com.att.eelf.configuration.EELFLogger;
\r
27 import com.att.eelf.configuration.EELFManager;
\r
29 import java.util.Date;
\r
30 import java.util.concurrent.ThreadLocalRandom;
\r
32 import org.onap.clamp.clds.config.ClampProperties;
\r
33 import org.onap.clamp.clds.config.sdc.SdcSingleControllerConfiguration;
\r
34 import org.onap.clamp.clds.exception.sdc.controller.CsarHandlerException;
\r
35 import org.onap.clamp.clds.exception.sdc.controller.SdcArtifactInstallerException;
\r
36 import org.onap.clamp.clds.exception.sdc.controller.SdcControllerException;
\r
37 import org.onap.clamp.clds.exception.sdc.controller.SdcDownloadException;
\r
38 import org.onap.clamp.clds.exception.sdc.controller.SdcParametersException;
\r
39 import org.onap.clamp.clds.sdc.controller.installer.CsarHandler;
\r
40 import org.onap.clamp.clds.sdc.controller.installer.CsarInstaller;
\r
41 import org.onap.clamp.clds.util.LoggingUtils;
\r
42 import org.onap.sdc.api.IDistributionClient;
\r
43 import org.onap.sdc.api.consumer.IDistributionStatusMessage;
\r
44 import org.onap.sdc.api.consumer.INotificationCallback;
\r
45 import org.onap.sdc.api.notification.IArtifactInfo;
\r
46 import org.onap.sdc.api.notification.INotificationData;
\r
47 import org.onap.sdc.api.results.IDistributionClientDownloadResult;
\r
48 import org.onap.sdc.api.results.IDistributionClientResult;
\r
49 import org.onap.sdc.impl.DistributionClientFactory;
\r
50 import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
\r
51 import org.onap.sdc.utils.DistributionActionResultEnum;
\r
52 import org.onap.sdc.utils.DistributionStatusEnum;
\r
55 * This class handles one sdc controller defined in the config.
\r
57 public class SdcSingleController {
\r
59 private static final EELFLogger logger = EELFManager.getInstance().getLogger(SdcSingleController.class);
\r
60 private boolean isSdcClientAutoManaged = false;
\r
61 private CsarInstaller csarInstaller;
\r
62 private ClampProperties refProp;
\r
63 public static final String CONFIG_SDC_FOLDER = "sdc.csarFolder";
\r
64 private int nbOfNotificationsOngoing = 0;
\r
65 private SdcSingleControllerStatus controllerStatus = SdcSingleControllerStatus.STOPPED;
\r
66 private SdcSingleControllerConfiguration sdcConfig;
\r
67 private IDistributionClient distributionClient;
\r
70 * Inner class for Notification callback
\r
72 private final class SdcNotificationCallBack implements INotificationCallback {
\r
74 private SdcSingleController sdcController;
\r
76 SdcNotificationCallBack(SdcSingleController controller) {
\r
77 sdcController = controller;
\r
81 * This method can be called multiple times at the same moment. The
\r
82 * controller must be thread safe !
\r
85 public void activateCallback(INotificationData iNotif) {
\r
86 Date startTime = new Date();
\r
87 String event = "Receive a callback notification in SDC, nb of resources: " + iNotif.getResources().size();
\r
88 logger.debug(event);
\r
89 sdcController.treatNotification(iNotif);
\r
90 LoggingUtils.setTimeContext(startTime, new Date());
\r
91 LoggingUtils.setResponseContext("0", "SDC Notification received and processed successfully",
\r
92 this.getClass().getName());
\r
96 public int getNbOfNotificationsOngoing() {
\r
97 return nbOfNotificationsOngoing;
\r
100 private void changeControllerStatusIdle() {
\r
101 if (this.nbOfNotificationsOngoing > 1) {
\r
102 --this.nbOfNotificationsOngoing;
\r
104 this.nbOfNotificationsOngoing = 0;
\r
105 this.controllerStatus = SdcSingleControllerStatus.IDLE;
\r
109 protected final synchronized void changeControllerStatus(SdcSingleControllerStatus newControllerStatus) {
\r
110 switch (newControllerStatus) {
\r
112 ++this.nbOfNotificationsOngoing;
\r
113 this.controllerStatus = newControllerStatus;
\r
116 this.changeControllerStatusIdle();
\r
119 this.controllerStatus = newControllerStatus;
\r
124 public final synchronized SdcSingleControllerStatus getControllerStatus() {
\r
125 return this.controllerStatus;
\r
128 public SdcSingleController(ClampProperties clampProp, CsarInstaller csarInstaller,
\r
129 SdcSingleControllerConfiguration sdcSingleConfig, boolean isClientAutoManaged) {
\r
130 this.isSdcClientAutoManaged = isClientAutoManaged;
\r
131 this.sdcConfig = sdcSingleConfig;
\r
132 this.refProp = clampProp;
\r
133 this.csarInstaller = csarInstaller;
\r
137 * This method initializes the SDC Controller and the SDC Client.
\r
139 * @throws SdcControllerException
\r
140 * It throws an exception if the SDC Client cannot be
\r
141 * instantiated or if an init attempt is done when already
\r
143 * @throws SdcParametersException
\r
144 * If there is an issue with the parameters provided
\r
146 public void initSdc() throws SdcControllerException {
\r
147 logger.debug("Attempt to initialize the SDC Controller");
\r
148 if (this.getControllerStatus() != SdcSingleControllerStatus.STOPPED) {
\r
149 throw new SdcControllerException("The controller is already initialized, call the closeSDC method first");
\r
151 if (this.distributionClient == null) {
\r
152 distributionClient = DistributionClientFactory.createDistributionClient();
\r
154 IDistributionClientResult result = this.distributionClient.init(sdcConfig, new SdcNotificationCallBack(this));
\r
155 if (!result.getDistributionActionResult().equals(DistributionActionResultEnum.SUCCESS)) {
\r
156 logger.error("SDC distribution client init failed with reason:" + result.getDistributionMessageResult());
\r
157 this.changeControllerStatus(SdcSingleControllerStatus.STOPPED);
\r
158 throw new SdcControllerException("Initialization of the SDC Controller failed with reason: "
\r
159 + result.getDistributionMessageResult());
\r
161 result = this.distributionClient.start();
\r
162 if (!result.getDistributionActionResult().equals(DistributionActionResultEnum.SUCCESS)) {
\r
163 logger.debug("SDC distribution client start failed with reason:" + result.getDistributionMessageResult());
\r
164 this.changeControllerStatus(SdcSingleControllerStatus.STOPPED);
\r
165 throw new SdcControllerException(
\r
166 "Startup of the SDC Controller failed with reason: " + result.getDistributionMessageResult());
\r
168 this.changeControllerStatus(SdcSingleControllerStatus.IDLE);
\r
172 * This method closes the SDC Controller and the SDC Client.
\r
174 * @throws SdcControllerException
\r
175 * It throws an exception if the SDC Client cannot be closed
\r
176 * because it's currently BUSY in processing notifications.
\r
178 public void closeSdc() throws SdcControllerException {
\r
179 if (this.getControllerStatus() == SdcSingleControllerStatus.BUSY) {
\r
180 throw new SdcControllerException("Cannot close the SDC controller as it's currently in BUSY state");
\r
182 if (this.distributionClient != null) {
\r
183 this.distributionClient.stop();
\r
184 // If auto managed we can set it to Null, SdcController controls it.
\r
185 // In the other case the client of this class has specified it, so
\r
186 // we can't reset it
\r
187 if (isSdcClientAutoManaged) {
\r
188 // Next init will initialize it with a new SDC Client
\r
189 this.distributionClient = null;
\r
192 this.changeControllerStatus(SdcSingleControllerStatus.STOPPED);
\r
196 * This method processes the notification received from Sdc.
\r
199 * The INotificationData
\r
201 public void treatNotification(INotificationData iNotif) {
\r
202 CsarHandler csar = null;
\r
204 // wait for a random time, so that 2 running Clamp will not treat the same Notification at the same time
\r
205 int i = ThreadLocalRandom.current().nextInt(1, 5);
\r
206 Thread.sleep(i * 1000);
\r
208 logger.info("Notification received for service UUID:" + iNotif.getServiceUUID());
\r
209 this.changeControllerStatus(SdcSingleControllerStatus.BUSY);
\r
210 csar = new CsarHandler(iNotif, this.sdcConfig.getSdcControllerName(),
\r
211 refProp.getStringValue(CONFIG_SDC_FOLDER));
\r
212 csar.save(downloadTheArtifact(csar.getArtifactElement()));
\r
213 if (csarInstaller.isCsarAlreadyDeployed(csar)) {
\r
214 this.sendSdcNotification(NotificationType.DOWNLOAD, csar.getArtifactElement().getArtifactURL(),
\r
215 sdcConfig.getConsumerID(), iNotif.getDistributionID(),
\r
216 DistributionStatusEnum.ALREADY_DOWNLOADED, null, System.currentTimeMillis());
\r
217 this.sendSdcNotification(NotificationType.DOWNLOAD, csar.getArtifactElement().getArtifactURL(),
\r
218 sdcConfig.getConsumerID(), iNotif.getDistributionID(), DistributionStatusEnum.ALREADY_DEPLOYED,
\r
219 null, System.currentTimeMillis());
\r
221 this.sendSdcNotification(NotificationType.DOWNLOAD, csar.getArtifactElement().getArtifactURL(),
\r
222 sdcConfig.getConsumerID(), iNotif.getDistributionID(), DistributionStatusEnum.DOWNLOAD_OK, null,
\r
223 System.currentTimeMillis());
\r
224 csarInstaller.installTheCsar(csar);
\r
225 this.sendSdcNotification(NotificationType.DEPLOY, csar.getArtifactElement().getArtifactURL(),
\r
226 sdcConfig.getConsumerID(), iNotif.getDistributionID(), DistributionStatusEnum.DEPLOY_OK, null,
\r
227 System.currentTimeMillis());
\r
229 } catch (SdcArtifactInstallerException e) {
\r
230 logger.error("SdcArtifactInstallerException exception caught during the notification processing", e);
\r
231 this.sendSdcNotification(NotificationType.DEPLOY, csar.getArtifactElement().getArtifactURL(),
\r
232 sdcConfig.getConsumerID(), iNotif.getDistributionID(), DistributionStatusEnum.DEPLOY_ERROR,
\r
233 e.getMessage(), System.currentTimeMillis());
\r
234 } catch (SdcDownloadException e) {
\r
235 logger.error("SdcDownloadException exception caught during the notification processing", e);
\r
236 this.sendSdcNotification(NotificationType.DOWNLOAD, csar.getArtifactElement().getArtifactURL(),
\r
237 sdcConfig.getConsumerID(), iNotif.getDistributionID(), DistributionStatusEnum.DOWNLOAD_ERROR,
\r
238 e.getMessage(), System.currentTimeMillis());
\r
239 } catch (CsarHandlerException e) {
\r
240 logger.error("CsarHandlerException exception caught during the notification processing", e);
\r
241 this.sendSdcNotification(NotificationType.DOWNLOAD, null, sdcConfig.getConsumerID(),
\r
242 iNotif.getDistributionID(), DistributionStatusEnum.DOWNLOAD_ERROR, e.getMessage(),
\r
243 System.currentTimeMillis());
\r
244 } catch (SdcToscaParserException e) {
\r
245 logger.error("SdcToscaParserException exception caught during the notification processing", e);
\r
246 this.sendSdcNotification(NotificationType.DEPLOY, csar.getArtifactElement().getArtifactURL(),
\r
247 sdcConfig.getConsumerID(), iNotif.getDistributionID(), DistributionStatusEnum.DEPLOY_ERROR,
\r
248 e.getMessage(), System.currentTimeMillis());
\r
249 } catch (InterruptedException e) {
\r
250 logger.error("Interrupt exception caught during the notification processing", e);
\r
251 } catch (RuntimeException e) {
\r
252 logger.error("Unexpected exception caught during the notification processing", e);
\r
254 this.changeControllerStatus(SdcSingleControllerStatus.IDLE);
\r
258 private enum NotificationType {
\r
262 private IDistributionClientDownloadResult downloadTheArtifact(IArtifactInfo artifact) throws SdcDownloadException {
\r
263 logger.debug("Trying to download the artifact : " + artifact.getArtifactURL() + " UUID: "
\r
264 + artifact.getArtifactUUID());
\r
265 IDistributionClientDownloadResult downloadResult;
\r
267 downloadResult = distributionClient.download(artifact);
\r
268 if (null == downloadResult) {
\r
269 logger.info("downloadResult is Null for: " + artifact.getArtifactUUID());
\r
272 } catch (RuntimeException e) {
\r
273 throw new SdcDownloadException("Exception caught when downloading the artifact", e);
\r
275 if (DistributionActionResultEnum.SUCCESS.equals(downloadResult.getDistributionActionResult())) {
\r
276 logger.info("Successfully downloaded the artifact " + artifact.getArtifactURL() + " UUID "
\r
277 + artifact.getArtifactUUID() + "Size of payload " + downloadResult.getArtifactPayload().length);
\r
279 throw new SdcDownloadException("Artifact " + artifact.getArtifactName()
\r
280 + " could not be downloaded from SDC URL " + artifact.getArtifactURL() + " UUID "
\r
281 + artifact.getArtifactUUID() + ")" + System.lineSeparator() + "Error message is "
\r
282 + downloadResult.getDistributionMessageResult() + System.lineSeparator());
\r
284 return downloadResult;
\r
287 private void sendSdcNotification(NotificationType notificationType, String artifactURL, String consumerID,
\r
288 String distributionID, DistributionStatusEnum status, String errorReason, long timestamp) {
\r
289 String event = "Sending " + notificationType.name() + "(" + status.name() + ")"
\r
290 + " notification to SDC for artifact:" + artifactURL;
\r
291 if (errorReason != null) {
\r
292 event = event + "(" + errorReason + ")";
\r
294 logger.info(event);
\r
295 String action = "";
\r
297 IDistributionStatusMessage message = new DistributionStatusMessage(artifactURL, consumerID, distributionID,
\r
298 status, timestamp);
\r
299 switch (notificationType) {
\r
301 this.sendDownloadStatus(message, errorReason);
\r
302 action = "sendDownloadStatus";
\r
305 this.sendDeploymentStatus(message, errorReason);
\r
306 action = "sendDeploymentdStatus";
\r
311 } catch (RuntimeException e) {
\r
312 logger.warn("Unable to send the SDC Notification (" + action + ") due to an exception", e);
\r
314 logger.info("SDC Notification sent successfully(" + action + ")");
\r
317 private void sendDownloadStatus(IDistributionStatusMessage message, String errorReason) {
\r
318 if (errorReason != null) {
\r
319 this.distributionClient.sendDownloadStatus(message, errorReason);
\r
321 this.distributionClient.sendDownloadStatus(message);
\r
325 private void sendDeploymentStatus(IDistributionStatusMessage message, String errorReason) {
\r
326 if (errorReason != null) {
\r
327 this.distributionClient.sendDeploymentStatus(message, errorReason);
\r
329 this.distributionClient.sendDeploymentStatus(message);
\r