2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.openecomp.appc.sdc.artifacts.impl;
27 import org.openecomp.appc.adapter.message.EventSender;
28 import org.openecomp.appc.adapter.message.MessageDestination;
29 import org.openecomp.appc.adapter.message.event.EventHeader;
30 import org.openecomp.appc.adapter.message.event.EventMessage;
31 import org.openecomp.appc.adapter.message.event.EventStatus;
32 import org.openecomp.appc.sdc.listener.Util;
33 import org.openecomp.appc.exceptions.APPCException;
34 import com.att.eelf.configuration.EELFLogger;
35 import com.att.eelf.configuration.EELFManager;
36 import org.openecomp.appc.sdc.artifacts.ArtifactProcessor;
37 import org.openecomp.appc.sdc.artifacts.helper.ArtifactStorageService;
38 import org.openecomp.appc.sdc.artifacts.object.SDCArtifact;
39 import org.openecomp.sdc.api.IDistributionClient;
40 import org.openecomp.sdc.api.notification.IArtifactInfo;
41 import org.openecomp.sdc.api.notification.INotificationData;
42 import org.openecomp.sdc.api.notification.IResourceInstance;
43 import org.openecomp.sdc.api.results.IDistributionClientDownloadResult;
44 import org.openecomp.sdc.utils.DistributionActionResultEnum;
45 import org.openecomp.sdc.utils.DistributionStatusEnum;
47 import java.io.UnsupportedEncodingException;
49 import java.text.DateFormat;
50 import java.text.SimpleDateFormat;
51 import java.util.Date;
54 * Provides abstrace implementation for SDC artifact processor
56 public abstract class AbstractArtifactProcessor implements ArtifactProcessor {
58 public static final String PAYLOAD_CHARSET = "UTF-8";
59 private static final String DATE_FORMAT = "yyyy/MM/dd HH:mm:ss";
61 protected IDistributionClient client;
62 protected EventSender eventSender;
64 protected INotificationData notification;
65 protected IResourceInstance resource;
66 protected IArtifactInfo artifact;
67 protected URI storeUri;
69 private final EELFLogger logger = EELFManager.getInstance().getLogger(AbstractArtifactProcessor.class);
71 protected ArtifactStorageService artifactStorageService;
73 private AbstractArtifactProcessor(){
74 artifactStorageService = new ArtifactStorageService();
77 AbstractArtifactProcessor(IDistributionClient client, EventSender eventSender, INotificationData notification, IResourceInstance resource,
78 IArtifactInfo artifact, URI storeUri){
82 this.eventSender = eventSender;
83 this.notification = notification;
84 this.resource = resource;
85 this.artifact = artifact;
86 this.storeUri = storeUri;
93 logger.info(String.format("Attempting to download artifact %s", artifact));
95 IDistributionClientDownloadResult download = client.download(artifact);
97 logger.info(String.format("Download of artifact %s completed with status %s", artifact.getArtifactUUID(), download));
99 // Notify of download status
100 if (download.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) {
101 client.sendDownloadStatus(Util.buildDistributionStatusMessage(client, notification, artifact,
102 DistributionStatusEnum.DOWNLOAD_ERROR), download.getDistributionMessageResult());
103 sendDCAEEvent(notification.getDistributionID(), notification.getServiceName(), notification.getServiceVersion(), "Download is failed.");
107 client.sendDownloadStatus(Util.buildDistributionStatusMessage(client, notification, artifact, DistributionStatusEnum.DOWNLOAD_OK));
109 processArtifact(download);
111 client.sendDeploymentStatus(
112 Util.buildDistributionStatusMessage(client, notification, this.artifact, DistributionStatusEnum.DEPLOY_OK));
115 logger.error("Error processing artifact " + this.artifact.toString() ,e);
117 client.sendDeploymentStatus(Util.buildDistributionStatusMessage(client, notification, artifact,
118 DistributionStatusEnum.DEPLOY_ERROR), e.getMessage());
119 sendDCAEEvent(notification.getDistributionID(), notification.getServiceName(), notification.getServiceVersion(), e.getMessage());
125 public void processArtifact(IDistributionClientDownloadResult download) throws APPCException {
127 if(logger.isDebugEnabled()){
128 logger.debug("Entry processArtifact in AbstractArtifactProcessor");
131 if (download.getArtifactPayload() != null) {
132 data = new String(download.getArtifactPayload(), PAYLOAD_CHARSET);
134 } catch (UnsupportedEncodingException e) {
135 logger.error("Error reading artifact with " + PAYLOAD_CHARSET + " encoding" + new String(download.getArtifactPayload()) ,e);
136 throw new APPCException(e);
139 SDCArtifact sdcArtifact = getArtifactObject(data);
140 logger.debug("Constructed SDCArtifact = " + sdcArtifact);
141 processArtifact(sdcArtifact);
143 if(logger.isDebugEnabled()){
144 logger.debug("Exit processArtifact in AbstractArtifactProcessor");
148 protected abstract void processArtifact(SDCArtifact artifact) throws APPCException;
150 protected SDCArtifact getArtifactObject(String data){
152 SDCArtifact sdcArtifact = new SDCArtifact();
154 sdcArtifact.setArtifactUUID(this.artifact.getArtifactUUID());
155 sdcArtifact.setArtifactName(this.artifact.getArtifactName());
156 sdcArtifact.setArtifactType(this.artifact.getArtifactType());
157 sdcArtifact.setArtifactVersion(this.artifact.getArtifactVersion());
158 sdcArtifact.setArtifactDescription(this.artifact.getArtifactDescription());
159 sdcArtifact.setArtifactContent(data);
160 sdcArtifact.setCreationDate(getCurrentDateTime());
162 sdcArtifact.setDistributionId(this.notification.getDistributionID());
163 sdcArtifact.setServiceUUID(this.notification.getServiceUUID());
164 sdcArtifact.setServiceName(this.notification.getServiceName());
165 sdcArtifact.setServiceDescription(this.notification.getServiceDescription());
167 sdcArtifact.setResourceName(this.resource.getResourceName());
168 sdcArtifact.setResourceType(this.resource.getResourceType());
169 sdcArtifact.setResourceVersion(this.resource.getResourceVersion());
170 sdcArtifact.setResourceUUID(this.resource.getResourceUUID());
171 sdcArtifact.setResourceInstanceName(this.resource.getResourceInstanceName());
176 protected String getCurrentDateTime() {
177 DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
178 Date date = new Date();
179 return dateFormat.format(date);
182 private void sendDCAEEvent(String distributionID, String serviceName, String serviceVersion, String errorMessage) {
183 if (null == eventSender){
186 String errorDescription = String.format("SDC distribution of service '%s', version '%s' is failed with reason: '%s'",
187 serviceName, serviceVersion, errorMessage);
189 EventMessage eventMessage = new EventMessage(
190 new EventHeader((new Date()).toString(), serviceVersion, distributionID),
191 new EventStatus(401, errorDescription));
193 eventSender.sendEvent(MessageDestination.DCAE, eventMessage);