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;
 
  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;
 
  30 import org.apache.commons.lang3.concurrent.BasicThreadFactory;
 
  31 import org.openecomp.appc.adapter.dmaap.EventSender;
 
  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;
 
  44 public class AsdcCallback implements INotificationCallback {
 
  46     private final EELFLogger logger = EELFManager.getInstance().getLogger(AsdcCallback.class);
 
  49     private IDistributionClient client;
 
  51     private EventSender eventSender = null;
 
  53     private ThreadPoolExecutor executor;
 
  54     private int threadCount = 10;
 
  56     private AtomicBoolean isRunning = new AtomicBoolean(false);
 
  59     public AsdcCallback(URI storeUri, IDistributionClient client) {
 
  60         this.storeUri = storeUri;
 
  63         // Create the thread pool
 
  64         executor = new ThreadPoolExecutor(threadCount, threadCount, 1, TimeUnit.SECONDS,
 
  65             new ArrayBlockingQueue<Runnable>(threadCount * 2));
 
  67         // Custom Named thread factory
 
  68         BasicThreadFactory threadFactory = new BasicThreadFactory.Builder().namingPattern("Appc-Listener-%d").build();
 
  69         executor.setThreadFactory(threadFactory);
 
  75     public void activateCallback(INotificationData data) {
 
  76         if (null == eventSender) {
 
  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);
 
  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
 
  93                     executor.submit(new DownloadAndStoreOp(client, eventSender, data, resource, artifact, storeUri));
 
  97             // TODO - return a failed result so asdc knows we are shut down
 
 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;
 
 112             cleanShutdown = executor.awaitTermination(waitSec, TimeUnit.SECONDS);
 
 113             executor.shutdownNow(); // In case of timeout
 
 114         } catch (InterruptedException e) {
 
 117         logger.info(String.format("Attempting to shutdown cleanly: %s", cleanShutdown ? "SUCCESS" : "FAILURE"));
 
 118         logger.info("Shutdown complete.");