Fix for radio buttons
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / distribution / engine / PublishNotificationRunnable.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.DistributionEngineConfiguration;
24 import org.openecomp.sdc.be.distribution.api.client.CambriaOperationStatus;
25 import org.openecomp.sdc.be.impl.ComponentsUtils;
26 import org.openecomp.sdc.be.model.Service;
27 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
28 import org.openecomp.sdc.common.util.ThreadLocalsHolder;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class PublishNotificationRunnable implements Runnable {
33
34         private String envName;
35         private String distributionId;
36         private Service service;
37         private INotificationData data;
38         private DistributionEngineConfiguration deConfiguration;
39         private String topicName;
40         private CambriaHandler cambriaHandler;
41         private ComponentsUtils componentUtils;
42         private String userId;
43         private String modifierName;
44         private String requestId;
45
46         private static Logger logger = LoggerFactory.getLogger(PublishNotificationRunnable.class.getName());
47
48         public PublishNotificationRunnable(String envName, String distributionId, Service service, INotificationData data, DistributionEngineConfiguration deConfiguration, String topicName, String userId, String modifierName,
49                         CambriaHandler cambriaHandler, ComponentsUtils componentUtils, String requestId) {
50                 super();
51                 this.envName = envName;
52                 this.distributionId = distributionId;
53                 this.service = service;
54                 this.data = data;
55                 this.deConfiguration = deConfiguration;
56                 this.topicName = topicName;
57                 this.cambriaHandler = cambriaHandler;
58                 this.componentUtils = componentUtils;
59                 this.userId = userId;
60                 this.modifierName = modifierName;
61                 this.requestId = requestId;
62         }
63
64         public INotificationData getData() {
65                 return data;
66         }
67
68         public void setData(INotificationData data) {
69                 this.data = data;
70         }
71
72         public DistributionEngineConfiguration getDeConfiguration() {
73                 return deConfiguration;
74         }
75
76         public void setDeConfiguration(DistributionEngineConfiguration deConfiguration) {
77                 this.deConfiguration = deConfiguration;
78         }
79
80         public String getTopicName() {
81                 return topicName;
82         }
83
84         public void setTopicName(String topicName) {
85                 this.topicName = topicName;
86         }
87
88         public String getUserId() {
89                 return userId;
90         }
91
92         public void setUserId(String userId) {
93                 this.userId = userId;
94         }
95
96         public String getModifierName() {
97                 return modifierName;
98         }
99
100         public void setModifierName(String modifierName) {
101                 this.modifierName = modifierName;
102         }
103
104         @Override
105         public void run() {
106
107                 long startTime = System.currentTimeMillis();
108                 ThreadLocalsHolder.setUuid(this.requestId);
109
110                 CambriaErrorResponse status = cambriaHandler.sendNotificationAndClose(topicName, deConfiguration.getUebPublicKey(), deConfiguration.getUebSecretKey(), deConfiguration.getUebServers(), data,
111                                 deConfiguration.getDistributionNotificationTopic().getMaxWaitingAfterSendingSeconds());
112
113                 logger.info("After publishing service {} of version {}. Status is {}", service.getName(), service.getVersion(), status.getHttpCode());
114                 auditDistributionNotification(topicName, status, service, distributionId, envName, userId, modifierName);
115
116                 long endTime = System.currentTimeMillis();
117                 logger.debug("After building and publishing artifacts object. Total took {} milliseconds", (endTime - startTime));
118
119         }
120
121         private void auditDistributionNotification(String topicName, CambriaErrorResponse status, Service service, String distributionId, String envName, String userId, String modifierName) {
122                 if (this.componentUtils != null) {
123                         Integer httpCode = status.getHttpCode();
124                         String httpCodeStr = String.valueOf(httpCode);
125
126                         String desc = getDescriptionFromErrorResponse(status);
127
128                         this.componentUtils.auditDistributionNotification(AuditingActionEnum.DISTRIBUTION_NOTIFY, service.getUUID(), service.getName(), "Service", service.getVersion(), userId, modifierName, envName, service.getLifecycleState().name(), topicName,
129                                         distributionId, desc, httpCodeStr);
130                 }
131         }
132
133         private String getDescriptionFromErrorResponse(CambriaErrorResponse status) {
134
135                 CambriaOperationStatus operationStatus = status.getOperationStatus();
136
137                 switch (operationStatus) {
138                 case OK:
139                         return "OK";
140                 case AUTHENTICATION_ERROR:
141                         return "Error: Authentication problem towards U-EB server";
142                 case INTERNAL_SERVER_ERROR:
143                         return "Error: Internal U-EB server error";
144                 case UNKNOWN_HOST_ERROR:
145                         return "Error: Cannot reach U-EB server host";
146                 case CONNNECTION_ERROR:
147                         return "Error: Cannot connect to U-EB server";
148                 case OBJECT_NOT_FOUND:
149                         return "Error: object not found in U-EB server";
150                 default:
151                         return "Error: Internal Cambria server problem";
152
153                 }
154
155         }
156 }