Change nexus values to properties
[appc.git] / app-c / appc / appc-asdc-listener / appc-asdc-listener-bundle / src / main / java / org / openecomp / appc / sdc / listener / AsdcCallback.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.appc.sdc.listener;
23
24 import java.net.URI;
25 import java.util.concurrent.ArrayBlockingQueue;
26 import java.util.concurrent.ThreadPoolExecutor;
27 import java.util.concurrent.TimeUnit;
28 import java.util.concurrent.atomic.AtomicBoolean;
29
30 import org.apache.commons.lang3.concurrent.BasicThreadFactory;
31 import org.openecomp.appc.adapter.dmaap.EventSender;
32
33 import org.openecomp.sdc.api.IDistributionClient;
34 import org.openecomp.sdc.api.consumer.INotificationCallback;
35 import org.openecomp.sdc.api.notification.IArtifactInfo;
36 import org.openecomp.sdc.api.notification.INotificationData;
37 import org.openecomp.sdc.api.notification.IResourceInstance;
38 import com.att.eelf.configuration.EELFLogger;
39 import com.att.eelf.configuration.EELFManager;
40 import org.osgi.framework.BundleContext;
41 import org.osgi.framework.FrameworkUtil;
42 import org.osgi.framework.ServiceReference;
43
44 public class AsdcCallback implements INotificationCallback {
45
46     private final EELFLogger logger = EELFManager.getInstance().getLogger(AsdcCallback.class);
47
48     private URI storeUri;
49     private IDistributionClient client;
50
51     private EventSender eventSender = null;
52
53     private ThreadPoolExecutor executor;
54     private int threadCount = 10;
55
56     private AtomicBoolean isRunning = new AtomicBoolean(false);
57
58
59     public AsdcCallback(URI storeUri, IDistributionClient client) {
60         this.storeUri = storeUri;
61         this.client = client;
62
63         // Create the thread pool
64         executor = new ThreadPoolExecutor(threadCount, threadCount, 1, TimeUnit.SECONDS,
65             new ArrayBlockingQueue<Runnable>(threadCount * 2));
66
67         // Custom Named thread factory
68         BasicThreadFactory threadFactory = new BasicThreadFactory.Builder().namingPattern("Appc-Listener-%d").build();
69         executor.setThreadFactory(threadFactory);
70
71         isRunning.set(true);
72     }
73
74     @Override
75     public void activateCallback(INotificationData data) {
76         if (null == eventSender) {
77             try {
78                 BundleContext bctx = FrameworkUtil.getBundle(EventSender.class).getBundleContext();
79                 ServiceReference sref = bctx.getServiceReference(EventSender.class);
80                 eventSender = (EventSender) bctx.getService(sref);
81             } catch (Throwable t) {
82                 logger.error("AsdcCallback failed on initializing EventSender", t);
83             }
84         }
85
86         if (isRunning.get()) {
87             for (IResourceInstance resource : data.getResources()) {
88                 for (IArtifactInfo artifact : resource.getArtifacts()) {
89                     logger.info(Util.toAsdcStoreDocumentInput(data, resource, artifact, "abc"));
90                     if (executor.getQueue().size() >= threadCount) {
91                         // log warning about job backlog
92                     }
93                     executor.submit(new DownloadAndStoreOp(client, eventSender, data, resource, artifact, storeUri));
94                 }
95             }
96         } else {
97             // TODO - return a failed result so asdc knows we are shut down
98         }
99     }
100
101     public void stop() {
102         stop(10);
103     }
104
105     public void stop(int waitSec) {
106         isRunning.set(false);
107         logger.info(String.format("Stopping the ASDC listener and waiting up to %ds for %d pending jobs", waitSec,
108             executor.getQueue().size()));
109         boolean cleanShutdown = false;
110         executor.shutdown();
111         try {
112             cleanShutdown = executor.awaitTermination(waitSec, TimeUnit.SECONDS);
113             executor.shutdownNow(); // In case of timeout
114         } catch (InterruptedException e) {
115             e.printStackTrace();
116         }
117         logger.info(String.format("Attempting to shutdown cleanly: %s", cleanShutdown ? "SUCCESS" : "FAILURE"));
118         logger.info("Shutdown complete.");
119     }
120
121 }