aboutsummaryrefslogtreecommitdiffstats
path: root/app-c/appc/appc-asdc-listener/appc-asdc-listener-bundle/src/main/java/org/openecomp/appc/sdc/listener/DownloadAndStoreOp.java
blob: fcc5e7ddae9de2599016ec5bcd7c79e6e4112397 (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
/*-
 * ============LICENSE_START=======================================================
 * openECOMP : APP-C
 * ================================================================================
 * Copyright (C) 2017 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.openecomp.appc.sdc.listener;

import org.openecomp.appc.adapter.dmaap.EventSender;
import org.openecomp.appc.adapter.dmaap.DmaapDestination;
import org.openecomp.appc.adapter.dmaap.event.EventHeader;
import org.openecomp.appc.adapter.dmaap.event.EventMessage;
import org.openecomp.appc.adapter.dmaap.event.EventStatus;
import org.openecomp.appc.exceptions.APPCException;
import org.openecomp.appc.licmgr.Constants;
import org.openecomp.appc.licmgr.LicenseManager;
import org.openecomp.sdc.api.IDistributionClient;
import org.openecomp.sdc.api.notification.IArtifactInfo;
import org.openecomp.sdc.api.notification.INotificationData;
import org.openecomp.sdc.api.notification.IResourceInstance;
import org.openecomp.sdc.api.results.IDistributionClientDownloadResult;
import org.openecomp.sdc.utils.DistributionActionResultEnum;
import org.openecomp.sdc.utils.DistributionStatusEnum;
import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.ServiceReference;

import static org.openecomp.appc.licmgr.Constants.ASDC_ARTIFACTS_FIELDS.*;

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URI;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@SuppressWarnings("JavaDoc")
public class DownloadAndStoreOp implements Runnable {

    public static final String PAYLOAD_CHARSET = "UTF-8";

    private static final String DATE_FORMAT = "yyyy/MM/dd HH:mm:ss";

    private final EELFLogger logger = EELFManager.getInstance().getLogger(DownloadAndStoreOp.class);

    private IDistributionClient client;
    private EventSender eventSender;

    private INotificationData notification;
    private IResourceInstance resource;
    private IArtifactInfo artifact;

    private URI storeUri;

    public DownloadAndStoreOp(IDistributionClient client, EventSender eventSender, INotificationData notification, IResourceInstance resource,
                              IArtifactInfo artifact, URI storeUri) {
        this.client = client;
        this.eventSender = eventSender;
        this.notification = notification;
        this.resource = resource;
        this.artifact = artifact;
        this.storeUri = storeUri;
    }

    @Override
    public void run() {
        logger.info(String.format("Attempting to download artifact %s", artifact));
        // Download artifact
        IDistributionClientDownloadResult download = client.download(artifact);
        logger.info(String.format("Download of artifact %s completed with status %s", artifact.getArtifactUUID(), download));

        // Notify of download status
        if (download.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) {
            client.sendDownloadStatus(Util.buildDistributionStatusMessage(client, notification, artifact,
                            DistributionStatusEnum.DOWNLOAD_ERROR), download.getDistributionMessageResult());
            sendDCAEEvent(notification.getDistributionID(), notification.getServiceName(), notification.getServiceVersion(), "Download is failed.");
            return;
        }

        client.sendDownloadStatus(Util.buildDistributionStatusMessage(client, notification, artifact, DistributionStatusEnum.DOWNLOAD_OK));

        String data = null;
        try {
            if (download.getArtifactPayload() != null) {
                data = new String(download.getArtifactPayload(), PAYLOAD_CHARSET);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        boolean providerSuccess = false;
        String providerReason = "Unknown Error";
        // Send data to provider
        if (data != null && artifact != null) {
            switch(artifact.getArtifactType()) {
                case "APPC_CONFIG":
                    String postData = Util.toAsdcStoreDocumentInput(notification, resource, artifact, data);
                    try {
                        ProviderResponse result = ProviderOperations.post(storeUri.toURL(), postData, null);
                        if (result.getStatus() == 200) {
                            providerSuccess = Util.parseResponse(result.getBody());
                            providerReason = "Success";
                        }
                    } catch (MalformedURLException | APPCException e) {
                        providerReason = e.getMessage();
                        e.printStackTrace();
                    }
                    break;

                case "VF_LICENSE":
                    BundleContext bctx = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
                    ServiceReference srefLicenseService = bctx.getServiceReference(LicenseManager.class);
                    LicenseManager licenseService = (LicenseManager) bctx.getService(srefLicenseService);

                    Map<String, String> artifactPayload = prepareArtifactPayloadParamsMap(data);

                    String vnfType = artifactPayload.get(RESOURCE_NAME.name());
                    String version = artifactPayload.get(RESOURCE_VERSION.name());
                    String packageArtifactID = artifactPayload.get(ARTIFACT_UUID.name());
                    String packageArtifactVersion = artifactPayload.get(INTERNAL_VERSION.name());

                    try {
                        if (null == vnfType || null == version || null == packageArtifactID || null == packageArtifactVersion || vnfType.isEmpty() || version.isEmpty() || packageArtifactID.isEmpty() || packageArtifactVersion.isEmpty()) {
                            throw new APPCException(String.format("Missing information in ASDC request. Details: resource_type='%s', resource_version='%s', artifactID='%s', artifactVersion='%s'", vnfType, version, packageArtifactID, packageArtifactVersion));
                        }

                        Map<String, String> existingArtifactPayload = licenseService.retrieveLicenseModelData(vnfType, version);

                        if (existingArtifactPayload.isEmpty()) { // new resource
                            licenseService.storeArtifactPayload(artifactPayload);
                        } else { // duplicate
                            logger.warn(String.format("Artifact of type '%s' already deployed for resource_type='%s' and resource_version='%s'", Constants.VF_LICENSE, vnfType, version));
                        }

                        providerSuccess = true;

                    } catch (Exception e) {
                        providerSuccess = false;
                        providerReason = e.getMessage();
                    }
                    break;

                default:
                    throw new UnsupportedOperationException("Artifact type " + artifact.getArtifactType() + " is not supported");
            }

        }

        // Notify of provider's response
        if (providerSuccess) {
            client.sendDeploymentStatus(
                            Util.buildDistributionStatusMessage(client, notification, artifact, DistributionStatusEnum.DEPLOY_OK));
        } else {
            client.sendDeploymentStatus(Util.buildDistributionStatusMessage(client, notification, artifact,
                            DistributionStatusEnum.DEPLOY_ERROR), providerReason);
            sendDCAEEvent(notification.getDistributionID(), notification.getServiceName(), notification.getServiceVersion(), providerReason);
        }

    }

    /**
     * Prepares Artifact Payload params map
     * @param data
     * @return Map<String,String>
     */
    private Map<String, String> prepareArtifactPayloadParamsMap(String data) {
        Map<String, String> paramsMap = new HashMap<>();

        paramsMap.put(SERVICE_UUID.name(), this.notification.getServiceUUID());
        paramsMap.put(DISTRIBUTION_ID.name(), this.notification.getDistributionID());
        paramsMap.put(SERVICE_NAME.name(), this.notification.getServiceName());
        paramsMap.put(SERVICE_DESCRIPTION.name(), this.notification.getServiceDescription());
        paramsMap.put(RESOURCE_UUID.name(), this.resource.getResourceUUID());
        paramsMap.put(RESOURCE_INSTANCE_NAME.name(), this.resource.getResourceInstanceName());
        paramsMap.put(RESOURCE_NAME.name(), this.resource.getResourceName());
        paramsMap.put(RESOURCE_VERSION.name(), this.resource.getResourceVersion());
        paramsMap.put(RESOURCE_TYPE.name(), this.resource.getResourceType());
        paramsMap.put(ARTIFACT_UUID.name(), this.artifact.getArtifactUUID());
        paramsMap.put(ARTIFACT_TYPE.name(), this.artifact.getArtifactType());
        paramsMap.put(ARTIFACT_VERSION.name(), this.artifact.getArtifactVersion());
        paramsMap.put(ARTIFACT_DESCRIPTION.name(), this.artifact.getArtifactDescription());
        paramsMap.put(CREATION_DATE.name(), getCurrentDateTime());
        paramsMap.put(ARTIFACT_NAME.name(), this.artifact.getArtifactName());
        paramsMap.put(ARTIFACT_CONTENT.name(), data);

        return paramsMap;
    }


    private String getCurrentDateTime() {
        DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
        Date date = new Date();
        return dateFormat.format(date);
    }

    private void sendDCAEEvent(String distributionID, String serviceName, String serviceVersion, String errorMessage) {
        if (null == eventSender) return;
        String errorDescription = String.format("ASDC distribution of service '%s', version '%s' is failed with reason: '%s'",
                        serviceName, serviceVersion, errorMessage);

        EventMessage eventMessage = new EventMessage(
                        new EventHeader((new java.util.Date()).toString(), serviceVersion, distributionID),
                        new EventStatus(401, errorDescription));

        eventSender.sendEvent(DmaapDestination.DCAE, eventMessage);
    }

}