2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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.
20 * ============LICENSE_END=========================================================
23 package org.onap.appc.sdc.artifacts.impl;
25 import org.onap.appc.adapter.message.EventSender;
26 import org.onap.appc.adapter.message.MessageDestination;
27 import org.onap.appc.adapter.message.event.EventHeader;
28 import org.onap.appc.adapter.message.event.EventMessage;
29 import org.onap.appc.adapter.message.event.EventStatus;
30 import org.onap.appc.sdc.listener.Util;
31 import org.onap.appc.exceptions.APPCException;
32 import com.att.eelf.configuration.EELFLogger;
33 import com.att.eelf.configuration.EELFManager;
34 import org.onap.appc.sdc.artifacts.ArtifactProcessor;
35 import org.onap.appc.sdc.artifacts.helper.ArtifactStorageService;
36 import org.onap.appc.sdc.artifacts.object.SDCArtifact;
37 import org.onap.sdc.api.IDistributionClient;
38 import org.onap.sdc.api.notification.IArtifactInfo;
39 import org.onap.sdc.api.notification.INotificationData;
40 import org.onap.sdc.api.notification.IResourceInstance;
41 import org.onap.sdc.api.results.IDistributionClientDownloadResult;
42 import org.onap.sdc.utils.DistributionActionResultEnum;
43 import org.onap.sdc.utils.DistributionStatusEnum;
45 import java.io.UnsupportedEncodingException;
47 import java.text.DateFormat;
48 import java.text.SimpleDateFormat;
49 import java.util.Date;
52 * Provides abstrace implementation for SDC artifact processor
54 public abstract class AbstractArtifactProcessor implements ArtifactProcessor {
56 public static final String PAYLOAD_CHARSET = "UTF-8";
57 private static final String DATE_FORMAT = "yyyy/MM/dd HH:mm:ss";
59 protected IDistributionClient client;
60 protected EventSender eventSender;
62 protected INotificationData notification;
63 protected IResourceInstance resource;
64 protected IArtifactInfo artifact;
65 protected URI storeUri;
67 private final EELFLogger logger = EELFManager.getInstance().getLogger(AbstractArtifactProcessor.class);
69 protected ArtifactStorageService artifactStorageService;
71 private AbstractArtifactProcessor(){
72 artifactStorageService = new ArtifactStorageService();
75 AbstractArtifactProcessor(IDistributionClient client, EventSender eventSender, INotificationData notification, IResourceInstance resource,
76 IArtifactInfo artifact, URI storeUri){
80 this.eventSender = eventSender;
81 this.notification = notification;
82 this.resource = resource;
83 this.artifact = artifact;
84 this.storeUri = storeUri;
91 logger.info(String.format("Attempting to download artifact %s", artifact));
93 IDistributionClientDownloadResult download = client.download(artifact);
95 logger.info(String.format("Download of artifact %s completed with status %s", artifact.getArtifactUUID(), download));
97 // Notify of download status
98 if (download.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) {
99 client.sendDownloadStatus(Util.buildDistributionStatusMessage(client, notification, artifact,
100 DistributionStatusEnum.DOWNLOAD_ERROR), download.getDistributionMessageResult());
101 sendDCAEEvent(notification.getDistributionID(), notification.getServiceName(), notification.getServiceVersion(), "Download is failed.");
105 client.sendDownloadStatus(Util.buildDistributionStatusMessage(client, notification, artifact, DistributionStatusEnum.DOWNLOAD_OK));
107 processArtifact(download);
109 client.sendDeploymentStatus(
110 Util.buildDistributionStatusMessage(client, notification, this.artifact, DistributionStatusEnum.DEPLOY_OK));
113 logger.error("Error processing artifact " + this.artifact.toString() ,e);
115 client.sendDeploymentStatus(Util.buildDistributionStatusMessage(client, notification, artifact,
116 DistributionStatusEnum.DEPLOY_ERROR), e.getMessage());
117 sendDCAEEvent(notification.getDistributionID(), notification.getServiceName(), notification.getServiceVersion(), e.getMessage());
123 public void processArtifact(IDistributionClientDownloadResult download) throws APPCException {
125 if(logger.isDebugEnabled()){
126 logger.debug("Entry processArtifact in AbstractArtifactProcessor");
129 if (download.getArtifactPayload() != null) {
130 data = new String(download.getArtifactPayload(), PAYLOAD_CHARSET);
132 } catch (UnsupportedEncodingException e) {
133 logger.error("Error reading artifact with " + PAYLOAD_CHARSET + " encoding" + new String(download.getArtifactPayload()) ,e);
134 throw new APPCException(e);
137 SDCArtifact sdcArtifact = getArtifactObject(data);
138 logger.debug("Constructed SDCArtifact = " + sdcArtifact);
139 processArtifact(sdcArtifact);
141 if(logger.isDebugEnabled()){
142 logger.debug("Exit processArtifact in AbstractArtifactProcessor");
146 protected abstract void processArtifact(SDCArtifact artifact) throws APPCException;
148 protected SDCArtifact getArtifactObject(String data){
150 SDCArtifact sdcArtifact = new SDCArtifact();
152 sdcArtifact.setArtifactUUID(this.artifact.getArtifactUUID());
153 sdcArtifact.setArtifactName(this.artifact.getArtifactName());
154 sdcArtifact.setArtifactType(this.artifact.getArtifactType());
155 sdcArtifact.setArtifactVersion(this.artifact.getArtifactVersion());
156 sdcArtifact.setArtifactDescription(this.artifact.getArtifactDescription());
157 sdcArtifact.setArtifactContent(data);
158 sdcArtifact.setCreationDate(getCurrentDateTime());
160 sdcArtifact.setDistributionId(this.notification.getDistributionID());
161 sdcArtifact.setServiceUUID(this.notification.getServiceUUID());
162 sdcArtifact.setServiceName(this.notification.getServiceName());
163 sdcArtifact.setServiceDescription(this.notification.getServiceDescription());
165 sdcArtifact.setResourceName(this.resource.getResourceName());
166 sdcArtifact.setResourceType(this.resource.getResourceType());
167 sdcArtifact.setResourceVersion(this.resource.getResourceVersion());
168 sdcArtifact.setResourceUUID(this.resource.getResourceUUID());
169 sdcArtifact.setResourceInstanceName(this.resource.getResourceInstanceName());
174 protected String getCurrentDateTime() {
175 DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
176 Date date = new Date();
177 return dateFormat.format(date);
180 private void sendDCAEEvent(String distributionID, String serviceName, String serviceVersion, String errorMessage) {
181 if (null == eventSender){
184 String errorDescription = String.format("SDC distribution of service '%s', version '%s' is failed with reason: '%s'",
185 serviceName, serviceVersion, errorMessage);
187 EventMessage eventMessage = new EventMessage(
188 new EventHeader((new Date()).toString(), serviceVersion, distributionID),
189 new EventStatus(401, errorDescription));
191 eventSender.sendEvent(MessageDestination.DCAE, eventMessage);