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