Sync Integ to Master
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / distribution / engine / DistributionNotificationSender.java
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.resources.data.auditing.AuditingActionEnum;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.stereotype.Component;
33
34 @Component("distributionNotificationSender")
35 public class DistributionNotificationSender {
36
37     protected static final String DISTRIBUTION_NOTIFICATION_SENDING = "distributionNotificationSending";
38
39     private static final Logger logger = LoggerFactory.getLogger(DistributionNotificationSender.class);
40
41     @javax.annotation.Resource
42     protected ComponentsUtils componentUtils;
43     private CambriaHandler cambriaHandler = new CambriaHandler();
44     private DistributionEngineConfiguration deConfiguration = ConfigurationManager.getConfigurationManager().getDistributionEngineConfiguration();
45
46     public ActionStatus sendNotification(String topicName, String distributionId, EnvironmentMessageBusData messageBusData, INotificationData notificationData, Service service, String userId, String modifierName) {
47         long startTime = System.currentTimeMillis();
48         CambriaErrorResponse status = cambriaHandler.sendNotificationAndClose(topicName, messageBusData.getUebPublicKey(), messageBusData.getUebPrivateKey(), messageBusData.getDmaaPuebEndpoints(), notificationData,
49                 deConfiguration.getDistributionNotificationTopic().getMaxWaitingAfterSendingSeconds());
50         logger.info("After publishing service {} of version {}. Status is {}", service.getName(), service.getVersion(), status.getHttpCode());
51         auditDistributionNotification(topicName, distributionId, status, service, messageBusData.getEnvId(), userId, modifierName, notificationData.getWorkloadContext(), messageBusData.getTenant());
52         long endTime = System.currentTimeMillis();
53         logger.debug("After building and publishing artifacts object. Total took {} milliseconds", (endTime - startTime));
54         return convertCambriaResponse(status);
55     }
56
57     private void auditDistributionNotification(String topicName, String distributionId, CambriaErrorResponse status, Service service, String envId, String userId, String modifierName, String workloadContext, String tenant) {
58         if (this.componentUtils != null) {
59             Integer httpCode = status.getHttpCode();
60             String httpCodeStr = String.valueOf(httpCode);
61
62             String desc = getDescriptionFromErrorResponse(status);
63
64             this.componentUtils.auditDistributionNotification(AuditingActionEnum.DISTRIBUTION_NOTIFY, service.getUUID(), service.getName(), "Service", service.getVersion(), userId, modifierName, envId, service.getLifecycleState().name(), topicName,
65                     distributionId, desc, httpCodeStr, workloadContext, tenant);
66         }
67     }
68
69     private String getDescriptionFromErrorResponse(CambriaErrorResponse status) {
70
71         CambriaOperationStatus operationStatus = status.getOperationStatus();
72
73         switch (operationStatus) {
74             case OK:
75                 return "OK";
76             case AUTHENTICATION_ERROR:
77                 return "Error: Authentication problem towards U-EB server";
78             case INTERNAL_SERVER_ERROR:
79                 return "Error: Internal U-EB server error";
80             case UNKNOWN_HOST_ERROR:
81                 return "Error: Cannot reach U-EB server host";
82             case CONNNECTION_ERROR:
83                 return "Error: Cannot connect to U-EB server";
84             case OBJECT_NOT_FOUND:
85                 return "Error: object not found in U-EB server";
86             default:
87                 return "Error: Internal Cambria server problem";
88
89         }
90
91     }
92
93     private ActionStatus convertCambriaResponse(CambriaErrorResponse status) {
94         CambriaOperationStatus operationStatus = status.getOperationStatus();
95
96         switch (operationStatus) {
97             case OK:
98                 return ActionStatus.OK;
99             case AUTHENTICATION_ERROR:
100                 return ActionStatus.AUTHENTICATION_ERROR;
101             case INTERNAL_SERVER_ERROR:
102                 return ActionStatus.GENERAL_ERROR;
103             case UNKNOWN_HOST_ERROR:
104                 return ActionStatus.UNKNOWN_HOST;
105             case CONNNECTION_ERROR:
106                 return ActionStatus.CONNNECTION_ERROR;
107             case OBJECT_NOT_FOUND:
108                 return ActionStatus.OBJECT_NOT_FOUND;
109             default:
110                 return ActionStatus.GENERAL_ERROR;
111
112         }
113     }
114
115
116 }