0098eac7d93a78be8a4ce6384e626e42002db73a
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.openecomp.sdc.be.components.distribution.engine;
21
22 import org.openecomp.sdc.be.config.ConfigurationManager;
23 import org.openecomp.sdc.be.config.DistributionEngineConfiguration;
24 import org.openecomp.sdc.be.dao.api.ActionStatus;
25 import org.openecomp.sdc.be.distribution.api.client.CambriaOperationStatus;
26 import org.openecomp.sdc.be.impl.ComponentsUtils;
27 import org.openecomp.sdc.be.model.Service;
28 import org.openecomp.sdc.be.model.User;
29 import org.openecomp.sdc.common.log.wrappers.Logger;
30 import org.springframework.stereotype.Component;
31
32 @Component("distributionNotificationSender")
33 public class DistributionNotificationSender {
34
35     protected static final String DISTRIBUTION_NOTIFICATION_SENDING = "distributionNotificationSending";
36     private static final Logger logger = Logger.getLogger(DistributionNotificationSender.class.getName());
37     @javax.annotation.Resource
38     protected ComponentsUtils componentUtils;
39     private CambriaHandler cambriaHandler = new CambriaHandler();
40     private DistributionEngineConfiguration deConfiguration = ConfigurationManager.getConfigurationManager().getDistributionEngineConfiguration();
41
42     public ActionStatus sendNotification(String topicName, String distributionId, EnvironmentMessageBusData messageBusData,
43                                          INotificationData notificationData, Service service, User modifier) {
44         long startTime = System.currentTimeMillis();
45         CambriaErrorResponse status = cambriaHandler
46             .sendNotificationAndClose(topicName, messageBusData.getUebPublicKey(), messageBusData.getUebPrivateKey(),
47                 messageBusData.getDmaaPuebEndpoints(), notificationData,
48                 deConfiguration.getDistributionNotificationTopic().getMaxWaitingAfterSendingSeconds());
49         logger.info("After publishing service {} of version {}. Status is {}", service.getName(), service.getVersion(), status.getHttpCode());
50         auditDistributionNotification(
51             new AuditDistributionNotificationBuilder().setTopicName(topicName).setDistributionId(distributionId).setStatus(status).setService(service)
52                 .setEnvId(messageBusData.getEnvId()).setModifier(modifier).setWorkloadContext(notificationData.getWorkloadContext())
53                 .setTenant(messageBusData.getTenant()));
54         long endTime = System.currentTimeMillis();
55         if (logger.isDebugEnabled()) {
56             logger.debug("After building and publishing artifacts object. Total took {} milliseconds", endTime - startTime);
57         }
58         return convertCambriaResponse(status);
59     }
60
61     private void auditDistributionNotification(AuditDistributionNotificationBuilder builder) {
62         if (this.componentUtils != null) {
63             Integer httpCode = builder.getStatus().getHttpCode();
64             String httpCodeStr = String.valueOf(httpCode);
65             String desc = getDescriptionFromErrorResponse(builder.getStatus());
66             this.componentUtils.auditDistributionNotification(builder.getService().getUUID(), builder.getService().getName(), "Service",
67                 builder.getService().getVersion(), builder.getModifier(), builder.getEnvId(), builder.getService().getLifecycleState().name(),
68                 builder.getTopicName(), builder.getDistributionId(), desc, httpCodeStr, builder.getWorkloadContext(), builder.getTenant());
69         }
70     }
71
72     private String getDescriptionFromErrorResponse(CambriaErrorResponse status) {
73         CambriaOperationStatus operationStatus = status.getOperationStatus();
74         switch (operationStatus) {
75             case OK:
76                 return "OK";
77             case AUTHENTICATION_ERROR:
78                 return "Error: Authentication problem towards U-EB server";
79             case INTERNAL_SERVER_ERROR:
80                 return "Error: Internal U-EB server error";
81             case UNKNOWN_HOST_ERROR:
82                 return "Error: Cannot reach U-EB server host";
83             case CONNNECTION_ERROR:
84                 return "Error: Cannot connect to U-EB server";
85             case OBJECT_NOT_FOUND:
86                 return "Error: object not found in U-EB server";
87             default:
88                 return "Error: Internal Cambria server problem";
89         }
90     }
91
92     private ActionStatus convertCambriaResponse(CambriaErrorResponse status) {
93         CambriaOperationStatus operationStatus = status.getOperationStatus();
94         switch (operationStatus) {
95             case OK:
96                 return ActionStatus.OK;
97             case AUTHENTICATION_ERROR:
98                 return ActionStatus.AUTHENTICATION_ERROR;
99             case INTERNAL_SERVER_ERROR:
100                 return ActionStatus.GENERAL_ERROR;
101             case UNKNOWN_HOST_ERROR:
102                 return ActionStatus.UNKNOWN_HOST;
103             case CONNNECTION_ERROR:
104                 return ActionStatus.CONNNECTION_ERROR;
105             case OBJECT_NOT_FOUND:
106                 return ActionStatus.OBJECT_NOT_FOUND;
107             default:
108                 return ActionStatus.GENERAL_ERROR;
109         }
110     }
111 }