re base code
[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.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 = ConfigurationManager.getConfigurationManager().getDistributionEngineConfiguration();
44
45     public ActionStatus sendNotification(String topicName, String distributionId, EnvironmentMessageBusData messageBusData, INotificationData notificationData, Service service, User modifier) {
46         long startTime = System.currentTimeMillis();
47         CambriaErrorResponse status = cambriaHandler.sendNotificationAndClose(topicName, messageBusData.getUebPublicKey(), messageBusData.getUebPrivateKey(), 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(topicName, distributionId, status, service, messageBusData.getEnvId(), modifier, notificationData.getWorkloadContext(), messageBusData.getTenant());
51         long endTime = System.currentTimeMillis();
52         logger.debug("After building and publishing artifacts object. Total took {} milliseconds", (endTime - startTime));
53         return convertCambriaResponse(status);
54     }
55
56     private void auditDistributionNotification(String topicName, String distributionId, CambriaErrorResponse status, Service service, String envId, User modifier
57             , 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(service.getUUID(), service.getName(), "Service", service.getVersion(), modifier, 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 }