2 * ============LICENSE_START=======================================================
\r
4 * ================================================================================
\r
5 * Copyright © 2017 AT&T Intellectual Property.
\r
6 * Copyright © 2017 Amdocs
\r
7 * All rights reserved.
\r
8 * ================================================================================
\r
9 * Licensed under the Apache License, Version 2.0 (the "License");
\r
10 * you may not use this file except in compliance with the License.
\r
11 * You may obtain a copy of the License at
\r
12 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * Unless required by applicable law or agreed to in writing, software
\r
14 * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * See the License for the specific language governing permissions and
\r
17 * limitations under the License.
\r
18 * ============LICENSE_END=========================================================
\r
20 * ECOMP and OpenECOMP are trademarks
\r
21 * and service marks of AT&T Intellectual Property.
\r
23 package org.openecomp.modelloader.notification;
\r
25 import org.openecomp.sdc.api.IDistributionClient;
\r
26 import org.openecomp.sdc.api.consumer.IDistributionStatusMessage;
\r
27 import org.openecomp.sdc.api.consumer.INotificationCallback;
\r
28 import org.openecomp.sdc.api.notification.IArtifactInfo;
\r
29 import org.openecomp.sdc.api.notification.INotificationData;
\r
30 import org.openecomp.sdc.api.notification.IResourceInstance;
\r
31 import org.openecomp.sdc.api.results.IDistributionClientDownloadResult;
\r
32 import org.openecomp.sdc.api.results.IDistributionClientResult;
\r
33 import org.openecomp.sdc.utils.ArtifactTypeEnum;
\r
34 import org.openecomp.sdc.utils.DistributionActionResultEnum;
\r
35 import org.openecomp.sdc.utils.DistributionStatusEnum;
\r
37 import org.openecomp.cl.api.Logger;
\r
38 import org.openecomp.cl.eelf.LoggerFactory;
\r
39 import org.openecomp.cl.mdc.MdcContext;
\r
40 import org.openecomp.cl.mdc.MdcOverride;
\r
41 import org.openecomp.modelloader.config.ModelLoaderConfig;
\r
42 import org.openecomp.modelloader.entity.Artifact;
\r
43 import org.openecomp.modelloader.entity.catalog.VnfCatalogArtifact;
\r
44 import org.openecomp.modelloader.entity.catalog.VnfCatalogArtifactHandler;
\r
45 import org.openecomp.modelloader.entity.model.ModelArtifactHandler;
\r
46 import org.openecomp.modelloader.entity.model.ModelArtifactParser;
\r
47 import org.openecomp.modelloader.service.ModelLoaderMsgs;
\r
48 import org.slf4j.MDC;
\r
50 import java.text.SimpleDateFormat;
\r
51 import java.util.ArrayList;
\r
52 import java.util.List;
\r
54 public class EventCallback implements INotificationCallback {
\r
56 private IDistributionClient client;
\r
57 private ModelLoaderConfig config;
\r
58 private static Logger logger = LoggerFactory.getInstance()
\r
59 .getLogger(EventCallback.class.getName());
\r
60 private static Logger auditLogger = LoggerFactory.getInstance()
\r
61 .getAuditLogger(EventCallback.class.getName());
\r
62 private static Logger metricsLogger = LoggerFactory.getInstance()
\r
63 .getMetricsLogger(EventCallback.class.getName());
\r
65 private static SimpleDateFormat dateFormatter = new SimpleDateFormat(
\r
66 "yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
\r
68 public EventCallback(IDistributionClient client, ModelLoaderConfig config) {
\r
69 this.client = client;
\r
70 this.config = config;
\r
74 public void activateCallback(INotificationData data) {
\r
76 MdcContext.initialize(data.getDistributionID(), "ModelLoader", "", "Event-Bus", "");
\r
78 logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT,
\r
79 "Received distribution " + data.getDistributionID());
\r
81 boolean success = true;
\r
82 List<IArtifactInfo> artifacts = getArtifacts(data);
\r
83 List<Artifact> modelArtifacts = new ArrayList<Artifact>();
\r
84 List<Artifact> catalogArtifacts = new ArrayList<Artifact>();
\r
85 ModelArtifactParser modelArtParser = new ModelArtifactParser();
\r
87 for (IArtifactInfo artifact : artifacts) {
\r
88 // Grab the current time so we can measure the download time for the
\r
90 long startTimeInMs = System.currentTimeMillis();
\r
91 MdcOverride override = new MdcOverride();
\r
92 override.addAttribute(MdcContext.MDC_START_TIME, dateFormatter.format(startTimeInMs));
\r
94 // Download Artifact
\r
95 IDistributionClientDownloadResult downloadResult = client.download(artifact);
\r
97 // Generate metrics log
\r
98 metricsLogger.info(ModelLoaderMsgs.DOWNLOAD_COMPLETE, null, override,
\r
99 artifact.getArtifactName(), downloadResult.getDistributionActionResult().toString());
\r
101 if (downloadResult.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) {
\r
102 publishDownloadFailure(data, artifact, downloadResult.getDistributionMessageResult());
\r
107 logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT,
\r
108 "Downloaded artifact: " + artifact.getArtifactName() + " Payload:\n" + new String(downloadResult.getArtifactPayload()));
\r
110 publishDownloadSuccess(data, artifact, downloadResult);
\r
112 if ((artifact.getArtifactType()
\r
113 .compareToIgnoreCase(ArtifactTypeEnum.MODEL_INVENTORY_PROFILE.toString()) == 0)
\r
114 || (artifact.getArtifactType()
\r
115 .compareToIgnoreCase(ArtifactTypeEnum.MODEL_QUERY_SPEC.toString()) == 0)) {
\r
116 List<Artifact> parsedArtifacts = modelArtParser.parse(downloadResult.getArtifactPayload(), downloadResult.getArtifactName());
\r
117 if (parsedArtifacts != null && !parsedArtifacts.isEmpty()) {
\r
118 modelArtifacts.addAll(parsedArtifacts);
\r
121 publishDeployFailure(data, artifact);
\r
124 } else if (artifact.getArtifactType()
\r
125 .compareToIgnoreCase(ArtifactTypeEnum.VNF_CATALOG.toString()) == 0) {
\r
127 .add(new VnfCatalogArtifact(new String(downloadResult.getArtifactPayload())));
\r
131 String statusString = "SUCCESS";
\r
133 ModelArtifactHandler modelHandler = new ModelArtifactHandler(config);
\r
134 boolean modelDeploySuccess = modelHandler.pushArtifacts(modelArtifacts,
\r
135 data.getDistributionID());
\r
137 VnfCatalogArtifactHandler catalogHandler = new VnfCatalogArtifactHandler(config);
\r
138 boolean catalogDeploySuccess = catalogHandler.pushArtifacts(catalogArtifacts,
\r
139 data.getDistributionID());
\r
141 for (IArtifactInfo artifact : artifacts) {
\r
142 if ((artifact.getArtifactType()
\r
143 .compareToIgnoreCase(ArtifactTypeEnum.MODEL_INVENTORY_PROFILE.toString()) == 0)
\r
144 || (artifact.getArtifactType()
\r
145 .compareToIgnoreCase(ArtifactTypeEnum.MODEL_QUERY_SPEC.toString()) == 0)) {
\r
146 if (modelDeploySuccess) {
\r
147 publishDeploySuccess(data, artifact);
\r
149 publishDeployFailure(data, artifact);
\r
150 statusString = "FAILURE";
\r
152 } else if (artifact.getArtifactType()
\r
153 .compareToIgnoreCase(ArtifactTypeEnum.VNF_CATALOG.toString()) == 0) {
\r
154 if (catalogDeploySuccess) {
\r
155 publishDeploySuccess(data, artifact);
\r
157 publishDeployFailure(data, artifact);
\r
158 statusString = "FAILURE";
\r
163 statusString = "FAILURE";
\r
166 auditLogger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT,
\r
167 "Processed distribution " + data.getDistributionID() + " (" + statusString + ")");
\r
171 private List<IArtifactInfo> getArtifacts(INotificationData data) {
\r
172 List<IArtifactInfo> artifacts = new ArrayList<IArtifactInfo>();
\r
173 List<IResourceInstance> resources = data.getResources();
\r
175 if (data.getServiceArtifacts() != null) {
\r
176 artifacts.addAll(data.getServiceArtifacts());
\r
179 if (resources != null) {
\r
180 for (IResourceInstance resource : resources) {
\r
181 if (resource.getArtifacts() != null) {
\r
182 artifacts.addAll(resource.getArtifacts());
\r
190 private void publishDownloadFailure(INotificationData data, IArtifactInfo artifact,
\r
191 String errorMessage) {
\r
192 // Grab the current time so we can measure the download time for the metrics
\r
194 long startTimeInMs = System.currentTimeMillis();
\r
195 MdcOverride override = new MdcOverride();
\r
196 override.addAttribute(MdcContext.MDC_START_TIME, dateFormatter.format(startTimeInMs));
\r
198 IDistributionClientResult sendDownloadStatus = client.sendDownloadStatus(
\r
199 buildStatusMessage(client, data, artifact, DistributionStatusEnum.DOWNLOAD_ERROR));
\r
200 metricsLogger.info(ModelLoaderMsgs.EVENT_PUBLISHED, null, override, "download failure",
\r
201 artifact.getArtifactName(), sendDownloadStatus.getDistributionActionResult().toString());
\r
203 if (sendDownloadStatus.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) {
\r
204 logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR,
\r
205 "Failed to publish download failure status: "
\r
206 + sendDownloadStatus.getDistributionMessageResult());
\r
209 logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR,
\r
210 "Failed to download artifact " + artifact.getArtifactName() + ": " + errorMessage);
\r
213 private void publishDownloadSuccess(INotificationData data, IArtifactInfo artifact,
\r
214 IDistributionClientDownloadResult downloadResult) {
\r
215 // Grab the current time so we can measure the download time for the metrics
\r
217 long startTimeInMs = System.currentTimeMillis();
\r
218 MdcOverride override = new MdcOverride();
\r
219 override.addAttribute(MdcContext.MDC_START_TIME, dateFormatter.format(startTimeInMs));
\r
221 IDistributionClientResult sendDownloadStatus = client.sendDownloadStatus(
\r
222 buildStatusMessage(client, data, artifact, DistributionStatusEnum.DOWNLOAD_OK));
\r
223 metricsLogger.info(ModelLoaderMsgs.EVENT_PUBLISHED, null, override, "download success",
\r
224 artifact.getArtifactName(), sendDownloadStatus.getDistributionActionResult().toString());
\r
226 if (sendDownloadStatus.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) {
\r
227 logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR,
\r
228 "Failed to publish download success status: "
\r
229 + sendDownloadStatus.getDistributionMessageResult());
\r
232 if (logger.isDebugEnabled()) {
\r
233 StringBuilder sb = new StringBuilder();
\r
234 sb.append("Downloaded artifact:\n");
\r
235 sb.append("ArtInfo_Art_Name: " + artifact.getArtifactName());
\r
236 sb.append("\nArtInfo_Art_description: " + artifact.getArtifactDescription());
\r
237 sb.append("\nArtInfo_Art_CheckSum: " + artifact.getArtifactChecksum());
\r
238 sb.append("\nArtInfo_Art_Url: " + artifact.getArtifactURL());
\r
239 sb.append("\nArtInfo_Art_Type: " + artifact.getArtifactType());
\r
240 sb.append("\nArtInfo_Serv_description: " + data.getServiceDescription());
\r
241 sb.append("\nArtInfo_Serv_Name: " + data.getServiceName());
\r
242 sb.append("\nGet_serviceVersion: " + data.getServiceVersion());
\r
243 sb.append("\nGet_Service_UUID: " + data.getServiceUUID());
\r
244 sb.append("\nArtInfo_DistributionId: " + data.getDistributionID());
\r
245 logger.debug(sb.toString());
\r
249 private void publishDeployFailure(INotificationData data, IArtifactInfo artifact) {
\r
250 // Grab the current time so we can measure the download time for the metrics
\r
252 long startTimeInMs = System.currentTimeMillis();
\r
253 MdcOverride override = new MdcOverride();
\r
254 override.addAttribute(MdcContext.MDC_START_TIME, dateFormatter.format(startTimeInMs));
\r
256 IDistributionClientResult sendStatus = client.sendDeploymentStatus(
\r
257 buildStatusMessage(client, data, artifact, DistributionStatusEnum.DEPLOY_ERROR));
\r
258 metricsLogger.info(ModelLoaderMsgs.EVENT_PUBLISHED, null, override, "deploy failure",
\r
259 artifact.getArtifactName(), sendStatus.getDistributionActionResult().toString());
\r
261 if (sendStatus.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) {
\r
262 logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR,
\r
263 "Failed to publish deploy failure status: " + sendStatus.getDistributionMessageResult());
\r
267 private void publishDeploySuccess(INotificationData data, IArtifactInfo artifact) {
\r
268 // Grab the current time so we can measure the download time for the metrics
\r
270 long startTimeInMs = System.currentTimeMillis();
\r
271 MdcOverride override = new MdcOverride();
\r
272 override.addAttribute(MdcContext.MDC_START_TIME, dateFormatter.format(startTimeInMs));
\r
274 IDistributionClientResult sendStatus = client.sendDownloadStatus(
\r
275 buildStatusMessage(client, data, artifact, DistributionStatusEnum.DEPLOY_OK));
\r
276 metricsLogger.info(ModelLoaderMsgs.EVENT_PUBLISHED, null, override, "deploy success",
\r
277 artifact.getArtifactName(), sendStatus.getDistributionActionResult().toString());
\r
279 if (sendStatus.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) {
\r
280 logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR,
\r
281 "Failed to publish deploy success status: " + sendStatus.getDistributionMessageResult());
\r
285 private IDistributionStatusMessage buildStatusMessage(IDistributionClient client,
\r
286 INotificationData data, IArtifactInfo artifact, DistributionStatusEnum status) {
\r
287 IDistributionStatusMessage statusMessage = new DistributionStatusMsg(status,
\r
288 data.getDistributionID(), client.getConfiguration().getConsumerID(),
\r
289 artifact.getArtifactURL());
\r
291 return statusMessage;
\r