2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.openecomp.appc.sdc.listener;
24 import com.att.eelf.configuration.EELFLogger;
25 import com.att.eelf.configuration.EELFManager;
26 import org.openecomp.appc.adapter.message.EventSender;
27 import org.openecomp.sdc.api.IDistributionClient;
28 import org.openecomp.sdc.api.consumer.INotificationCallback;
29 import org.openecomp.sdc.api.notification.IArtifactInfo;
30 import org.openecomp.sdc.api.notification.INotificationData;
31 import org.openecomp.sdc.api.notification.IResourceInstance;
32 import org.apache.commons.lang3.concurrent.BasicThreadFactory;
33 import org.osgi.framework.BundleContext;
34 import org.osgi.framework.FrameworkUtil;
35 import org.osgi.framework.ServiceReference;
38 import java.util.concurrent.ArrayBlockingQueue;
39 import java.util.concurrent.ThreadPoolExecutor;
40 import java.util.concurrent.TimeUnit;
41 import java.util.concurrent.atomic.AtomicBoolean;
43 public class AsdcCallback implements INotificationCallback {
45 private final EELFLogger logger = EELFManager.getInstance().getLogger(AsdcCallback.class);
48 private IDistributionClient client;
50 private EventSender eventSender = null;
52 private ThreadPoolExecutor executor;
53 private int threadCount = 10;
55 private AtomicBoolean isRunning = new AtomicBoolean(false);
58 public AsdcCallback(URI storeUri, IDistributionClient client) {
59 this.storeUri = storeUri;
62 // Create the thread pool
63 executor = new ThreadPoolExecutor(threadCount, threadCount, 1, TimeUnit.SECONDS,
64 new ArrayBlockingQueue<Runnable>(threadCount * 2));
66 // Custom Named thread factory
67 BasicThreadFactory threadFactory = new BasicThreadFactory.Builder().namingPattern("Appc-Listener-%d").build();
68 executor.setThreadFactory(threadFactory);
74 public void activateCallback(INotificationData data) {
75 if (null == eventSender) {
77 BundleContext bctx = FrameworkUtil.getBundle(EventSender.class).getBundleContext();
78 ServiceReference sref = bctx.getServiceReference(EventSender.class);
79 eventSender = (EventSender) bctx.getService(sref);
80 } catch (Throwable t) {
81 logger.error("AsdcCallback failed on initializing EventSender", t);
85 if (isRunning.get()) {
86 for (IResourceInstance resource : data.getResources()) {
87 for (IArtifactInfo artifact : resource.getArtifacts()) {
88 logger.info(Util.toAsdcStoreDocumentInput(data, resource, artifact, "abc"));
89 if (executor.getQueue().size() >= threadCount) {
90 // log warning about job backlog
92 executor.submit(new DownloadAndStoreOp(client, eventSender, data, resource, artifact, storeUri));
96 // TODO - return a failed result so asdc knows we are shut down
104 public void stop(int waitSec) {
105 isRunning.set(false);
106 logger.info(String.format("Stopping the ASDC listener and waiting up to %ds for %d pending jobs", waitSec,
107 executor.getQueue().size()));
108 boolean cleanShutdown = false;
111 cleanShutdown = executor.awaitTermination(waitSec, TimeUnit.SECONDS);
112 executor.shutdownNow(); // In case of timeout
113 } catch (InterruptedException e) {
116 logger.info(String.format("Attempting to shutdown cleanly: %s", cleanShutdown ? "SUCCESS" : "FAILURE"));
117 logger.info("Shutdown complete.");