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