Implement support for v10 model entities.
[aai/model-loader.git] / src / main / java / org / openecomp / modelloader / notification / EventCallback.java
1 /**\r
2  * ============LICENSE_START=======================================================\r
3  * Model Loader\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
19  *\r
20  * ECOMP and OpenECOMP are trademarks\r
21  * and service marks of AT&T Intellectual Property.\r
22  */\r
23 package org.openecomp.modelloader.notification;\r
24 \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
36 \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
49 \r
50 import java.text.SimpleDateFormat;\r
51 import java.util.ArrayList;\r
52 import java.util.List;\r
53 \r
54 public class EventCallback implements INotificationCallback {\r
55 \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
64 \r
65   private static SimpleDateFormat dateFormatter = new SimpleDateFormat(\r
66       "yyyy-MM-dd'T'HH:mm:ss.SSSXXX");\r
67 \r
68   public EventCallback(IDistributionClient client, ModelLoaderConfig config) {\r
69     this.client = client;\r
70     this.config = config;\r
71   }\r
72 \r
73   @Override\r
74   public void activateCallback(INotificationData data) {\r
75     // Init MDC\r
76     MdcContext.initialize(data.getDistributionID(), "ModelLoader", "", "Event-Bus", "");\r
77 \r
78     logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT,\r
79         "Received distribution " + data.getDistributionID());\r
80 \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
86 \r
87     for (IArtifactInfo artifact : artifacts) {\r
88       // Grab the current time so we can measure the download time for the\r
89       // metrics log\r
90       long startTimeInMs = System.currentTimeMillis();\r
91       MdcOverride override = new MdcOverride();\r
92       override.addAttribute(MdcContext.MDC_START_TIME, dateFormatter.format(startTimeInMs));\r
93 \r
94       // Download Artifact\r
95       IDistributionClientDownloadResult downloadResult = client.download(artifact);\r
96 \r
97       // Generate metrics log\r
98       metricsLogger.info(ModelLoaderMsgs.DOWNLOAD_COMPLETE, null, override,\r
99           artifact.getArtifactName(), downloadResult.getDistributionActionResult().toString());\r
100 \r
101       if (downloadResult.getDistributionActionResult() != DistributionActionResultEnum.SUCCESS) {\r
102         publishDownloadFailure(data, artifact, downloadResult.getDistributionMessageResult());\r
103         success = false;\r
104         break;\r
105       }\r
106 \r
107       logger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT,\r
108                 "Downloaded artifact: " + artifact.getArtifactName() + "  Payload:\n" + new String(downloadResult.getArtifactPayload()));\r
109       \r
110       publishDownloadSuccess(data, artifact, downloadResult);\r
111 \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
119         } else {\r
120           success = false;\r
121           publishDeployFailure(data, artifact);\r
122           break;\r
123         }\r
124       } else if (artifact.getArtifactType()\r
125           .compareToIgnoreCase(ArtifactTypeEnum.VNF_CATALOG.toString()) == 0) {\r
126         catalogArtifacts\r
127             .add(new VnfCatalogArtifact(new String(downloadResult.getArtifactPayload())));\r
128       }\r
129     }\r
130 \r
131     String statusString = "SUCCESS";\r
132     if (success) {\r
133       ModelArtifactHandler modelHandler = new ModelArtifactHandler(config);\r
134       boolean modelDeploySuccess = modelHandler.pushArtifacts(modelArtifacts,\r
135           data.getDistributionID());\r
136 \r
137       VnfCatalogArtifactHandler catalogHandler = new VnfCatalogArtifactHandler(config);\r
138       boolean catalogDeploySuccess = catalogHandler.pushArtifacts(catalogArtifacts,\r
139           data.getDistributionID());\r
140 \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
148           } else {\r
149             publishDeployFailure(data, artifact);\r
150             statusString = "FAILURE";\r
151           }\r
152         } else if (artifact.getArtifactType()\r
153             .compareToIgnoreCase(ArtifactTypeEnum.VNF_CATALOG.toString()) == 0) {\r
154           if (catalogDeploySuccess) {\r
155             publishDeploySuccess(data, artifact);\r
156           } else {\r
157             publishDeployFailure(data, artifact);\r
158             statusString = "FAILURE";\r
159           }\r
160         }\r
161       }\r
162     } else {\r
163       statusString = "FAILURE";\r
164     }\r
165 \r
166     auditLogger.info(ModelLoaderMsgs.DISTRIBUTION_EVENT,\r
167         "Processed distribution " + data.getDistributionID() + "  (" + statusString + ")");\r
168     MDC.clear();\r
169   }\r
170 \r
171   private List<IArtifactInfo> getArtifacts(INotificationData data) {\r
172     List<IArtifactInfo> artifacts = new ArrayList<IArtifactInfo>();\r
173     List<IResourceInstance> resources = data.getResources();\r
174 \r
175     if (data.getServiceArtifacts() != null) {\r
176       artifacts.addAll(data.getServiceArtifacts());\r
177     }\r
178 \r
179     if (resources != null) {\r
180       for (IResourceInstance resource : resources) {\r
181         if (resource.getArtifacts() != null) {\r
182           artifacts.addAll(resource.getArtifacts());\r
183         }\r
184       }\r
185     }\r
186 \r
187     return artifacts;\r
188   }\r
189 \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
193     // log\r
194     long startTimeInMs = System.currentTimeMillis();\r
195     MdcOverride override = new MdcOverride();\r
196     override.addAttribute(MdcContext.MDC_START_TIME, dateFormatter.format(startTimeInMs));\r
197 \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
202 \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
207     }\r
208 \r
209     logger.error(ModelLoaderMsgs.DISTRIBUTION_EVENT_ERROR,\r
210         "Failed to download artifact " + artifact.getArtifactName() + ": " + errorMessage);\r
211   }\r
212 \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
216     // log\r
217     long startTimeInMs = System.currentTimeMillis();\r
218     MdcOverride override = new MdcOverride();\r
219     override.addAttribute(MdcContext.MDC_START_TIME, dateFormatter.format(startTimeInMs));\r
220 \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
225 \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
230     }\r
231 \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
246     }\r
247   }\r
248 \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
251     // log\r
252     long startTimeInMs = System.currentTimeMillis();\r
253     MdcOverride override = new MdcOverride();\r
254     override.addAttribute(MdcContext.MDC_START_TIME, dateFormatter.format(startTimeInMs));\r
255 \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
260 \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
264     }\r
265   }\r
266 \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
269     // log\r
270     long startTimeInMs = System.currentTimeMillis();\r
271     MdcOverride override = new MdcOverride();\r
272     override.addAttribute(MdcContext.MDC_START_TIME, dateFormatter.format(startTimeInMs));\r
273 \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
278 \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
282     }\r
283   }\r
284 \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
290 \r
291     return statusMessage;\r
292   }\r
293 \r
294 }\r