aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/clamp/clds/sdc/controller/SdcSingleController.java
blob: 9cad683af2affe5c774edc74e543ae15596490ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*-
 * ============LICENSE_START=======================================================
 * ONAP CLAMP
 * ================================================================================
 * Copyright (C) 2018 AT&T Intellectual Property. All rights
 *                             reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END============================================
 * ===================================================================
 * 
 */

package org.onap.clamp.clds.sdc.controller;

import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;

import java.util.Date;
import java.util.Map.Entry;
import java.util.concurrent.ThreadLocalRandom;

import org.onap.clamp.clds.config.ClampProperties;
import org.onap.clamp.clds.config.sdc.SdcSingleControllerConfiguration;
import org.onap.clamp.clds.exception.sdc.controller.CsarHandlerException;
import org.onap.clamp.clds.exception.sdc.controller.SdcArtifactInstallerException;
import org.onap.clamp.clds.exception.sdc.controller.SdcControllerException;
import org.onap.clamp.clds.exception.sdc.controller.SdcDownloadException;
import org.onap.clamp.clds.exception.sdc.controller.SdcParametersException;
import org.onap.clamp.clds.sdc.controller.installer.BlueprintArtifact;
import org.onap.clamp.clds.sdc.controller.installer.CsarHandler;
import org.onap.clamp.clds.sdc.controller.installer.CsarInstaller;
import org.onap.clamp.clds.util.LoggingUtils;
import org.onap.sdc.api.IDistributionClient;
import org.onap.sdc.api.consumer.IDistributionStatusMessage;
import org.onap.sdc.api.consumer.INotificationCallback;
import org.onap.sdc.api.notification.IArtifactInfo;
import org.onap.sdc.api.notification.INotificationData;
import org.onap.sdc.api.results.IDistributionClientDownloadResult;
import org.onap.sdc.api.results.IDistributionClientResult;
import org.onap.sdc.impl.DistributionClientFactory;
import org.onap.sdc.tosca.parser.exceptions.SdcToscaParserException;
import org.onap.sdc.utils.DistributionActionResultEnum;
import org.onap.sdc.utils.DistributionStatusEnum;

/**
 * This class handles one sdc controller defined in the config.
 */
public class SdcSingleController {

    private static final EELFLogger logger = EELFManager.getInstance().getLogger(SdcSingleController.class);
    private boolean isSdcClientAutoManaged = false;
    private CsarInstaller csarInstaller;
    private ClampProperties refProp;
    public static final String CONFIG_SDC_FOLDER = "sdc.csarFolder";
    private int nbOfNotificationsOngoing = 0;
    private SdcSingleControllerStatus controllerStatus = SdcSingleControllerStatus.STOPPED;
    private SdcSingleControllerConfiguration sdcConfig;
    private IDistributionClient distributionClient;

    /**
     * Inner class for Notification callback
     */
    private final class SdcNotificationCallBack implements INotificationCallback {

        private SdcSingleController sdcController;

        SdcNotificationCallBack(SdcSingleController controller) {
            sdcController = controller;
        }

        /**
         * This method can be called multiple times at the same moment. The
         * controller must be thread safe !
         */
        @Override
        public void activateCallback(INotificationData iNotif) {
            Date startTime = new Date();
            logger.info("Receive a callback notification in SDC, nb of resources: " + iNotif.getResources().size());
            sdcController.treatNotification(iNotif);
            LoggingUtils.setTimeContext(startTime, new Date());
            LoggingUtils.setResponseContext("0", "SDC Notification received and processed successfully",
                    this.getClass().getName());
        }
    }

    public int getNbOfNotificationsOngoing() {
        return nbOfNotificationsOngoing;
    }

    private void changeControllerStatusIdle() {
        if (this.nbOfNotificationsOngoing > 1) {
            --this.nbOfNotificationsOngoing;
        } else {
            this.nbOfNotificationsOngoing = 0;
            this.controllerStatus = SdcSingleControllerStatus.IDLE;
        }
    }

    protected final synchronized void changeControllerStatus(SdcSingleControllerStatus newControllerStatus) {
        switch (newControllerStatus) {
            case BUSY:
                ++this.nbOfNotificationsOngoing;
                this.controllerStatus = newControllerStatus;
                break;
            case IDLE:
                this.changeControllerStatusIdle();
                break;
            default:
                this.controllerStatus = newControllerStatus;
                break;
        }
    }

    public final synchronized SdcSingleControllerStatus getControllerStatus() {
        return this.controllerStatus;
    }

    public SdcSingleController(ClampProperties clampProp, CsarInstaller csarInstaller,
            SdcSingleControllerConfiguration sdcSingleConfig, boolean isClientAutoManaged) {
        this.isSdcClientAutoManaged = isClientAutoManaged;
        this.sdcConfig = sdcSingleConfig;
        this.refProp = clampProp;
        this.csarInstaller = csarInstaller;
    }

    /**
     * This method initializes the SDC Controller and the SDC Client.
     *
     * @throws SdcControllerException
     *             It throws an exception if the SDC Client cannot be
     *             instantiated or if an init attempt is done when already
     *             initialized
     * @throws SdcParametersException
     *             If there is an issue with the parameters provided
     */
    public void initSdc() throws SdcControllerException {
        logger.info("Attempt to initialize the SDC Controller: " + sdcConfig.getSdcControllerName());
        if (this.getControllerStatus() != SdcSingleControllerStatus.STOPPED) {
            throw new SdcControllerException("The controller is already initialized, call the closeSDC method first");
        }
        if (this.distributionClient == null) {
            distributionClient = DistributionClientFactory.createDistributionClient();
        }
        IDistributionClientResult result = this.distributionClient.init(sdcConfig, new SdcNotificationCallBack(this));
        if (!result.getDistributionActionResult().equals(DistributionActionResultEnum.SUCCESS)) {
            logger.error("SDC distribution client init failed with reason:" + result.getDistributionMessageResult());
            this.changeControllerStatus(SdcSingleControllerStatus.STOPPED);
            throw new SdcControllerException("Initialization of the SDC Controller failed with reason: "
                    + result.getDistributionMessageResult());
        }
        logger.info("SDC Controller successfully initialized: " + sdcConfig.getSdcControllerName());
        logger.info("Attempt to start the SDC Controller: " + sdcConfig.getSdcControllerName());
        result = this.distributionClient.start();
        if (!result.getDistributionActionResult().equals(DistributionActionResultEnum.SUCCESS)) {
            logger.error("SDC distribution client start failed with reason:" + result.getDistributionMessageResult());
            this.changeControllerStatus(SdcSingleControllerStatus.STOPPED);
            throw new SdcControllerException(
                    "Startup of the SDC Controller failed with reason: " + result.getDistributionMessageResult());
        }
        logger.info("SDC Controller successfully started: " + sdcConfig.getSdcControllerName());
        this.changeControllerStatus(SdcSingleControllerStatus.IDLE);
    }

    /**
     * This method closes the SDC Controller and the SDC Client.
     *
     * @throws SdcControllerException
     *             It throws an exception if the SDC Client cannot be closed
     *             because it's currently BUSY in processing notifications.
     */
    public void closeSdc() throws SdcControllerException {
        if (this.getControllerStatus() == SdcSingleControllerStatus.BUSY) {
            throw new SdcControllerException("Cannot close the SDC controller as it's currently in BUSY state");
        }
        if (this.distributionClient != null) {
            this.distributionClient.stop();
            // If auto managed we can set it to Null, SdcController controls it.
            // In the other case the client of this class has specified it, so
            // we can't reset it
            if (isSdcClientAutoManaged) {
                // Next init will initialize it with a new SDC Client
                this.distributionClient = null;
            }
        }
        this.changeControllerStatus(SdcSingleControllerStatus.STOPPED);
    }

    private void sendAllNotificationForCsarHandler(INotificationData iNotif, CsarHandler csar,
            NotificationType notificationType, DistributionStatusEnum distributionStatus, String errorMessage) {
        if (csar != null) {
            // Notify for the CSAR
            this.sendSdcNotification(notificationType, csar.getArtifactElement().getArtifactURL(),
                    sdcConfig.getConsumerID(), iNotif.getDistributionID(), distributionStatus, errorMessage,
                    System.currentTimeMillis());
            // Notify for all VF resources found
            for (Entry<String, BlueprintArtifact> blueprint : csar.getMapOfBlueprints().entrySet()) {
                // Normally always 1 artifact in resource for Clamp as we
                // specified
                // only VF_METADATA type
                this.sendSdcNotification(notificationType,
                        blueprint.getValue().getResourceAttached().getArtifacts().get(0).getArtifactURL(),
                        sdcConfig.getConsumerID(), iNotif.getDistributionID(), distributionStatus, errorMessage,
                        System.currentTimeMillis());
            }
        } else {
            this.sendSdcNotification(notificationType, null, sdcConfig.getConsumerID(), iNotif.getDistributionID(),
                    distributionStatus, errorMessage, System.currentTimeMillis());
        }
    }

    /**
     * This method processes the notification received from Sdc.
     * 
     * @param iNotif
     *            The INotificationData
     */
    public void treatNotification(INotificationData iNotif) {
        CsarHandler csar = null;
        try {
            // wait for a random time, so that 2 running Clamp will not treat
            // the same Notification at the same time
            Thread.sleep(ThreadLocalRandom.current().nextInt(1, 10) * 1000L);
            logger.info("Notification received for service UUID:" + iNotif.getServiceUUID());
            this.changeControllerStatus(SdcSingleControllerStatus.BUSY);
            csar = new CsarHandler(iNotif, this.sdcConfig.getSdcControllerName(),
                    refProp.getStringValue(CONFIG_SDC_FOLDER));
            csar.save(downloadTheArtifact(csar.getArtifactElement()));
            if (csarInstaller.isCsarAlreadyDeployed(csar)) {
                sendAllNotificationForCsarHandler(iNotif, csar, NotificationType.DOWNLOAD,
                        DistributionStatusEnum.ALREADY_DOWNLOADED, null);
                sendAllNotificationForCsarHandler(iNotif, csar, NotificationType.DEPLOY,
                        DistributionStatusEnum.ALREADY_DEPLOYED, null);
            } else {
                sendAllNotificationForCsarHandler(iNotif, csar, NotificationType.DOWNLOAD,
                        DistributionStatusEnum.DOWNLOAD_OK, null);
                csarInstaller.installTheCsar(csar);
                sendAllNotificationForCsarHandler(iNotif, csar, NotificationType.DEPLOY,
                        DistributionStatusEnum.DEPLOY_OK, null);
            }
        } catch (SdcArtifactInstallerException | SdcToscaParserException e) {
            logger.error("SdcArtifactInstallerException exception caught during the notification processing", e);
            sendAllNotificationForCsarHandler(iNotif, csar, NotificationType.DEPLOY,
                    DistributionStatusEnum.DEPLOY_ERROR, e.getMessage());
        } catch (SdcDownloadException | CsarHandlerException e) {
            logger.error("SdcDownloadException exception caught during the notification processing", e);
            sendAllNotificationForCsarHandler(iNotif, csar, NotificationType.DOWNLOAD,
                    DistributionStatusEnum.DOWNLOAD_ERROR, e.getMessage());
        } catch (InterruptedException e) {
            logger.error("Interrupt exception caught during the notification processing", e);
            sendAllNotificationForCsarHandler(iNotif, csar, NotificationType.DEPLOY,
                    DistributionStatusEnum.DEPLOY_ERROR, e.getMessage());
            Thread.currentThread().interrupt();
        } catch (RuntimeException e) {
            logger.error("Unexpected exception caught during the notification processing", e);
            sendAllNotificationForCsarHandler(iNotif, csar, NotificationType.DEPLOY,
                    DistributionStatusEnum.DEPLOY_ERROR, e.getMessage());
        } finally {
            this.changeControllerStatus(SdcSingleControllerStatus.IDLE);
        }
    }

    private enum NotificationType {
        DOWNLOAD, DEPLOY
    }

    private IDistributionClientDownloadResult downloadTheArtifact(IArtifactInfo artifact) throws SdcDownloadException {
        logger.info("Trying to download the artifact : " + artifact.getArtifactURL() + " UUID: "
                + artifact.getArtifactUUID());
        IDistributionClientDownloadResult downloadResult;
        try {
            downloadResult = distributionClient.download(artifact);
            if (null == downloadResult) {
                logger.info("downloadResult is Null for: " + artifact.getArtifactUUID());
                return null;
            }
        } catch (RuntimeException e) {
            throw new SdcDownloadException("Exception caught when downloading the artifact", e);
        }
        if (DistributionActionResultEnum.SUCCESS.equals(downloadResult.getDistributionActionResult())) {
            logger.info("Successfully downloaded the artifact " + artifact.getArtifactURL() + " UUID "
                    + artifact.getArtifactUUID() + "Size of payload " + downloadResult.getArtifactPayload().length);
        } else {
            throw new SdcDownloadException("Artifact " + artifact.getArtifactName()
                    + " could not be downloaded from SDC URL " + artifact.getArtifactURL() + " UUID "
                    + artifact.getArtifactUUID() + ")" + System.lineSeparator() + "Error message is "
                    + downloadResult.getDistributionMessageResult() + System.lineSeparator());
        }
        return downloadResult;
    }

    private void sendSdcNotification(NotificationType notificationType, String artifactURL, String consumerID,
            String distributionID, DistributionStatusEnum status, String errorReason, long timestamp) {
        String event = "Sending " + notificationType.name() + "(" + status.name() + ")"
                + " notification to SDC for artifact:" + artifactURL;
        if (errorReason != null) {
            event = event + "(" + errorReason + ")";
        }
        logger.info(event);
        String action = "";
        try {
            IDistributionStatusMessage message = new DistributionStatusMessage(artifactURL, consumerID, distributionID,
                    status, timestamp);
            switch (notificationType) {
                case DOWNLOAD:
                    this.sendDownloadStatus(message, errorReason);
                    action = "sendDownloadStatus";
                    break;
                case DEPLOY:
                    this.sendDeploymentStatus(message, errorReason);
                    action = "sendDeploymentdStatus";
                    break;
                default:
                    break;
            }
        } catch (RuntimeException e) {
            logger.warn("Unable to send the SDC Notification (" + action + ") due to an exception", e);
        }
        logger.info("SDC Notification sent successfully(" + action + ")");
    }

    private void sendDownloadStatus(IDistributionStatusMessage message, String errorReason) {
        if (errorReason != null) {
            this.distributionClient.sendDownloadStatus(message, errorReason);
        } else {
            this.distributionClient.sendDownloadStatus(message);
        }
    }

    private void sendDeploymentStatus(IDistributionStatusMessage message, String errorReason) {
        if (errorReason != null) {
            this.distributionClient.sendDeploymentStatus(message, errorReason);
        } else {
            this.distributionClient.sendDeploymentStatus(message);
        }
    }
}