16a0a1dc31aea7bc249f0f96db54b4d8c975ab19
[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 java.util.concurrent.ExecutorService;
24 import java.util.concurrent.RejectedExecutionException;
25
26 import javax.annotation.PreDestroy;
27
28 import org.openecomp.sdc.be.config.ConfigurationManager;
29 import org.openecomp.sdc.be.config.DistributionEngineConfiguration;
30 import org.openecomp.sdc.be.config.DistributionEngineConfiguration.DistributionNotificationTopicConfig;
31 import org.openecomp.sdc.be.impl.ComponentsUtils;
32 import org.openecomp.sdc.be.model.Service;
33 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
34 import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation;
35 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
36 import org.openecomp.sdc.common.util.ThreadLocalsHolder;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.stereotype.Component;
40
41 @Component("distributionNotificationSender")
42 public class DistributionNotificationSender {
43
44         protected static final String DISTRIBUTION_NOTIFICATION_SENDING = "distributionNotificationSending";
45
46         private static Logger logger = LoggerFactory.getLogger(DistributionNotificationSender.class.getName());
47
48         // final String BASE_ARTIFACT_URL = "/sdc/v1/catalog/services/%s/%s/";
49         // final String RESOURCE_ARTIFACT_URL = BASE_ARTIFACT_URL
50         // + "resources/%s/%s/artifacts/%s";
51         // final String SERVICE_ARTIFACT_URL = BASE_ARTIFACT_URL + "artifacts/%s";
52
53         @javax.annotation.Resource
54         InterfaceLifecycleOperation interfaceLifecycleOperation;
55
56         @javax.annotation.Resource
57         protected ComponentsUtils componentUtils;
58
59         ExecutorService executorService = null;
60
61         CambriaHandler cambriaHandler = new CambriaHandler();
62
63         NotificationExecutorService notificationExecutorService = new NotificationExecutorService();
64
65         public DistributionNotificationSender() {
66                 super();
67
68                 DistributionNotificationTopicConfig distributionNotificationTopic = ConfigurationManager.getConfigurationManager().getDistributionEngineConfiguration().getDistributionNotificationTopic();
69
70                 executorService = notificationExecutorService.createExcecutorService(distributionNotificationTopic);
71         }
72
73         @PreDestroy
74         public void shutdown() {
75                 logger.debug("Going to close notificationExecutorService");
76                 if (executorService != null) {
77
78                         long maxWaitingTime = ConfigurationManager.getConfigurationManager().getDistributionEngineConfiguration().getDistributionNotificationTopic().getMaxWaitingAfterSendingSeconds();
79
80                         notificationExecutorService.shutdownAndAwaitTermination(executorService, maxWaitingTime + 1);
81                 }
82         }
83
84         public StorageOperationStatus sendNotification(String topicName, String distributionId, DistributionEngineConfiguration deConfiguration, String envName, INotificationData notificationData, Service service, String userId, String modifierName) {
85
86                 Runnable task = new PublishNotificationRunnable(envName, distributionId, service, notificationData, deConfiguration, topicName, userId, modifierName, cambriaHandler, componentUtils, ThreadLocalsHolder.getUuid());
87                 try {
88                         executorService.submit(task);
89                 } catch (RejectedExecutionException e) {
90                         logger.warn("Failed to submit task. Number of threads exceeeds", e);
91                         return StorageOperationStatus.OVERLOAD;
92                 }
93
94                 return StorageOperationStatus.OK;
95         }
96
97         /**
98          * Audit the publishing notification in case of internal server error.
99          * 
100          * @param topicName
101          * @param status
102          * @param distributionId
103          * @param envName
104          */
105         private void auditDistributionNotificationInternalServerError(String topicName, StorageOperationStatus status, String distributionId, String envName) {
106
107                 if (this.componentUtils != null) {
108                         this.componentUtils.auditDistributionNotification(AuditingActionEnum.DISTRIBUTION_NOTIFY, "", " ", "Service", " ", " ", " ", envName, " ", topicName, distributionId, "Error: Internal Server Error. " + status, " ");
109                 }
110         }
111
112         protected CambriaErrorResponse publishNotification(INotificationData data, DistributionEngineConfiguration deConfiguration, String topicName) {
113
114                 CambriaErrorResponse status = cambriaHandler.sendNotification(topicName, deConfiguration.getUebPublicKey(), deConfiguration.getUebSecretKey(), deConfiguration.getUebServers(), data);
115
116                 return status;
117         }
118
119 }