2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.openecomp.appc.sdc.listener;
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;
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;
46 public class AsdcCallback implements INotificationCallback {
48 private final EELFLogger logger = EELFManager.getInstance().getLogger(AsdcCallback.class);
51 private IDistributionClient client;
53 private EventSender eventSender = null;
55 private ThreadPoolExecutor executor;
56 private int threadCount = 10;
58 private AtomicBoolean isRunning = new AtomicBoolean(false);
61 public AsdcCallback(URI storeUri, IDistributionClient client) {
62 this.storeUri = storeUri;
65 // Create the thread pool
66 executor = new ThreadPoolExecutor(threadCount, threadCount, 1, TimeUnit.SECONDS,
67 new ArrayBlockingQueue<Runnable>(threadCount * 2));
69 // Custom Named thread factory
70 BasicThreadFactory threadFactory = new BasicThreadFactory.Builder().namingPattern("Appc-Listener-%d").build();
71 executor.setThreadFactory(threadFactory);
77 public void activateCallback(INotificationData data) {
78 if (null == eventSender) {
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);
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
95 executor.submit(new DownloadAndStoreOp(client, eventSender, data, resource, artifact, storeUri));
99 // TODO - return a failed result so asdc knows we are shut down
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;
114 cleanShutdown = executor.awaitTermination(waitSec, TimeUnit.SECONDS);
115 executor.shutdownNow(); // In case of timeout
116 } catch (InterruptedException e) {
119 logger.info(String.format("Attempting to shutdown cleanly: %s", cleanShutdown ? "SUCCESS" : "FAILURE"));
120 logger.info("Shutdown complete.");