1c6afe5a9854811a3d9575e587462dd5380a9cd8
[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
21 package org.openecomp.sdc.be.components.distribution.engine;
22
23 import org.openecomp.sdc.be.config.ConfigurationManager;
24 import org.openecomp.sdc.be.config.DistributionEngineConfiguration;
25 import org.openecomp.sdc.be.dao.api.ActionStatus;
26 import org.openecomp.sdc.be.distribution.api.client.CambriaOperationStatus;
27 import org.openecomp.sdc.be.impl.ComponentsUtils;
28 import org.openecomp.sdc.be.model.Service;
29 import org.openecomp.sdc.be.model.User;
30 import org.openecomp.sdc.common.log.wrappers.Logger;
31 import org.springframework.stereotype.Component;
32
33 @Component("distributionNotificationSender")
34 public class DistributionNotificationSender {
35
36     protected static final String DISTRIBUTION_NOTIFICATION_SENDING = "distributionNotificationSending";
37
38     private static final Logger logger = Logger.getLogger(DistributionNotificationSender.class.getName());
39
40     @javax.annotation.Resource
41     protected ComponentsUtils componentUtils;
42     private CambriaHandler cambriaHandler = new CambriaHandler();
43     private DistributionEngineConfiguration deConfiguration =
44             ConfigurationManager.getConfigurationManager().getDistributionEngineConfiguration();
45
46     public ActionStatus sendNotification(String topicName, String distributionId,
47             EnvironmentMessageBusData messageBusData, INotificationData notificationData, Service service,
48             User modifier) {
49         long startTime = System.currentTimeMillis();
50         CambriaErrorResponse status =
51                 cambriaHandler.sendNotificationAndClose(topicName, messageBusData.getUebPublicKey(),
52                         messageBusData.getUebPrivateKey(), messageBusData.getDmaaPuebEndpoints(), notificationData,
53                         deConfiguration.getDistributionNotificationTopic().getMaxWaitingAfterSendingSeconds());
54
55         logger.info("After publishing service {} of version {}. Status is {}", service.getName(), service.getVersion(),
56                 status.getHttpCode());
57
58         auditDistributionNotification(new AuditDistributionNotificationBuilder().setTopicName(topicName)
59                 .setDistributionId(distributionId).setStatus(status).setService(service)
60                 .setEnvId(messageBusData.getEnvId()).setModifier(modifier)
61                 .setWorkloadContext(notificationData.getWorkloadContext()).setTenant(messageBusData.getTenant()));
62
63         long endTime = System.currentTimeMillis();
64
65         if (logger.isDebugEnabled()) {
66             logger.debug("After building and publishing artifacts object. Total took {} milliseconds",
67                     endTime - startTime);
68         }
69
70         return convertCambriaResponse(status);
71     }
72
73     private void auditDistributionNotification(AuditDistributionNotificationBuilder builder) {
74         if (this.componentUtils != null) {
75             Integer httpCode = builder.getStatus().getHttpCode();
76             String httpCodeStr = String.valueOf(httpCode);
77
78             String desc = getDescriptionFromErrorResponse(builder.getStatus());
79
80             this.componentUtils.auditDistributionNotification(builder.getService().getUUID(),
81                     builder.getService().getName(), "Service", builder.getService().getVersion(), builder.getModifier(),
82                     builder.getEnvId(), builder.getService().getLifecycleState().name(), builder.getTopicName(),
83                     builder.getDistributionId(), desc, httpCodeStr, builder.getWorkloadContext(), builder.getTenant());
84         }
85     }
86
87     private String getDescriptionFromErrorResponse(CambriaErrorResponse status) {
88
89         CambriaOperationStatus operationStatus = status.getOperationStatus();
90
91         switch (operationStatus) {
92             case OK:
93                 return "OK";
94             case AUTHENTICATION_ERROR:
95                 return "Error: Authentication problem towards U-EB server";
96             case INTERNAL_SERVER_ERROR:
97                 return "Error: Internal U-EB server error";
98             case UNKNOWN_HOST_ERROR:
99                 return "Error: Cannot reach U-EB server host";
100             case CONNNECTION_ERROR:
101                 return "Error: Cannot connect to U-EB server";
102             case OBJECT_NOT_FOUND:
103                 return "Error: object not found in U-EB server";
104             default:
105                 return "Error: Internal Cambria server problem";
106
107         }
108
109     }
110
111     private ActionStatus convertCambriaResponse(CambriaErrorResponse status) {
112         CambriaOperationStatus operationStatus = status.getOperationStatus();
113
114         switch (operationStatus) {
115             case OK:
116                 return ActionStatus.OK;
117             case AUTHENTICATION_ERROR:
118                 return ActionStatus.AUTHENTICATION_ERROR;
119             case INTERNAL_SERVER_ERROR:
120                 return ActionStatus.GENERAL_ERROR;
121             case UNKNOWN_HOST_ERROR:
122                 return ActionStatus.UNKNOWN_HOST;
123             case CONNNECTION_ERROR:
124                 return ActionStatus.CONNNECTION_ERROR;
125             case OBJECT_NOT_FOUND:
126                 return ActionStatus.OBJECT_NOT_FOUND;
127             default:
128                 return ActionStatus.GENERAL_ERROR;
129
130         }
131     }
132
133
134 }