06bda8b2d7b901fbcb385bf80ed5b61e19ffbdf7
[appc.git] / appc-sdc-listener / appc-sdc-listener-bundle / src / main / java / org / openecomp / appc / sdc / artifacts / impl / AbstractArtifactProcessor.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.openecomp.appc.sdc.artifacts.impl;
26
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;
46
47 import java.io.UnsupportedEncodingException;
48 import java.net.URI;
49 import java.text.DateFormat;
50 import java.text.SimpleDateFormat;
51 import java.util.Date;
52
53 /**
54  * Provides abstrace implementation for SDC artifact processor
55  */
56 public abstract class AbstractArtifactProcessor implements ArtifactProcessor {
57
58     public static final String PAYLOAD_CHARSET = "UTF-8";
59     private static final String DATE_FORMAT = "yyyy/MM/dd HH:mm:ss";
60
61     protected IDistributionClient client;
62     protected EventSender eventSender;
63
64     protected INotificationData notification;
65     protected IResourceInstance resource;
66     protected IArtifactInfo artifact;
67     protected URI storeUri;
68
69     private final EELFLogger logger = EELFManager.getInstance().getLogger(AbstractArtifactProcessor.class);
70
71     protected ArtifactStorageService artifactStorageService;
72
73     private AbstractArtifactProcessor(){
74         artifactStorageService = new ArtifactStorageService();
75     }
76
77     AbstractArtifactProcessor(IDistributionClient client, EventSender eventSender, INotificationData notification, IResourceInstance resource,
78                               IArtifactInfo artifact, URI storeUri){
79
80         this();
81         this.client = client;
82         this.eventSender = eventSender;
83         this.notification = notification;
84         this.resource = resource;
85         this.artifact = artifact;
86         this.storeUri = storeUri;
87     }
88
89     @Override
90     public void run(){
91
92         try{
93             logger.info(String.format("Attempting to download artifact %s", artifact));
94             // Download artifact
95             IDistributionClientDownloadResult download = client.download(artifact);
96
97             logger.info(String.format("Download of artifact %s completed with status %s", artifact.getArtifactUUID(), download));
98
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.");
104                 return;
105             }
106
107             client.sendDownloadStatus(Util.buildDistributionStatusMessage(client, notification, artifact, DistributionStatusEnum.DOWNLOAD_OK));
108
109             processArtifact(download);
110
111             client.sendDeploymentStatus(
112                     Util.buildDistributionStatusMessage(client, notification, this.artifact, DistributionStatusEnum.DEPLOY_OK));
113         }
114         catch (Exception e){
115             logger.error("Error processing artifact " + this.artifact.toString() ,e);
116
117             client.sendDeploymentStatus(Util.buildDistributionStatusMessage(client, notification, artifact,
118                     DistributionStatusEnum.DEPLOY_ERROR), e.getMessage());
119             sendDCAEEvent(notification.getDistributionID(), notification.getServiceName(), notification.getServiceVersion(), e.getMessage());
120         }
121     }
122
123
124     @Override
125     public void processArtifact(IDistributionClientDownloadResult download) throws APPCException {
126         String data = null;
127         if(logger.isDebugEnabled()){
128             logger.debug("Entry processArtifact in AbstractArtifactProcessor");
129         }
130         try {
131             if (download.getArtifactPayload() != null) {
132                 data = new String(download.getArtifactPayload(), PAYLOAD_CHARSET);
133             }
134         } catch (UnsupportedEncodingException e) {
135             logger.error("Error reading artifact with " + PAYLOAD_CHARSET + " encoding" + new String(download.getArtifactPayload()) ,e);
136             throw new APPCException(e);
137         }
138
139         SDCArtifact sdcArtifact = getArtifactObject(data);
140         logger.debug("Constructed SDCArtifact = " + sdcArtifact);
141         processArtifact(sdcArtifact);
142
143         if(logger.isDebugEnabled()){
144             logger.debug("Exit processArtifact in AbstractArtifactProcessor");
145         }
146     }
147
148     protected abstract void processArtifact(SDCArtifact artifact) throws APPCException;
149
150     protected SDCArtifact getArtifactObject(String data){
151
152         SDCArtifact sdcArtifact = new SDCArtifact();
153
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());
161
162         sdcArtifact.setDistributionId(this.notification.getDistributionID());
163         sdcArtifact.setServiceUUID(this.notification.getServiceUUID());
164         sdcArtifact.setServiceName(this.notification.getServiceName());
165         sdcArtifact.setServiceDescription(this.notification.getServiceDescription());
166
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());
172
173         return sdcArtifact;
174     }
175
176     protected String getCurrentDateTime() {
177         DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
178         Date date = new Date();
179         return dateFormat.format(date);
180     }
181
182     private void sendDCAEEvent(String distributionID, String serviceName, String serviceVersion, String errorMessage) {
183         if (null == eventSender){
184             return;
185         }
186         String errorDescription = String.format("SDC distribution of service '%s', version '%s' is failed with reason: '%s'",
187                 serviceName, serviceVersion, errorMessage);
188
189         EventMessage eventMessage = new EventMessage(
190                 new EventHeader((new Date()).toString(), serviceVersion, distributionID),
191                 new EventStatus(401, errorDescription));
192
193         eventSender.sendEvent(MessageDestination.DCAE, eventMessage);
194     }
195
196 }