2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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.
20 * ============LICENSE_END=========================================================
23 package org.onap.appc.sdc.listener;
25 import com.att.eelf.configuration.EELFLogger;
26 import com.att.eelf.configuration.EELFManager;
27 import org.onap.appc.adapter.message.EventSender;
28 import org.onap.appc.sdc.artifacts.ArtifactProcessor;
29 import org.onap.appc.sdc.artifacts.impl.ArtifactProcessorFactory;
30 import org.onap.sdc.api.IDistributionClient;
31 import org.onap.sdc.api.consumer.INotificationCallback;
32 import org.onap.sdc.api.notification.IArtifactInfo;
33 import org.onap.sdc.api.notification.INotificationData;
34 import org.onap.sdc.api.notification.IResourceInstance;
35 import org.apache.commons.lang3.concurrent.BasicThreadFactory;
36 import org.onap.sdc.utils.DistributionStatusEnum;
37 import org.osgi.framework.BundleContext;
38 import org.osgi.framework.FrameworkUtil;
39 import org.osgi.framework.ServiceReference;
42 import java.util.concurrent.ArrayBlockingQueue;
43 import java.util.concurrent.ThreadPoolExecutor;
44 import java.util.concurrent.TimeUnit;
45 import java.util.concurrent.atomic.AtomicBoolean;
47 public class SdcCallback implements INotificationCallback {
49 private final EELFLogger logger = EELFManager.getInstance().getLogger(SdcCallback.class);
50 private ArtifactProcessorFactory artifactProcessorFactory = new ArtifactProcessorFactory();
53 private IDistributionClient client;
55 private EventSender eventSender = null;
57 private ThreadPoolExecutor executor;
58 private int threadCount = 10;
60 private AtomicBoolean isRunning = new AtomicBoolean(false);
63 public SdcCallback(URI storeUri, IDistributionClient client) {
64 this.storeUri = storeUri;
67 // Create the thread pool
68 executor = new ThreadPoolExecutor(threadCount, threadCount, 1, TimeUnit.SECONDS,
69 new ArrayBlockingQueue<Runnable>(threadCount * 2));
71 // Custom Named thread factory
72 BasicThreadFactory threadFactory = new BasicThreadFactory.Builder().namingPattern("Appc-Listener-%d").build();
73 executor.setThreadFactory(threadFactory);
79 public void activateCallback(INotificationData data) {
80 if (null == eventSender) {
82 BundleContext bctx = FrameworkUtil.getBundle(EventSender.class).getBundleContext();
83 ServiceReference sref = bctx.getServiceReference(EventSender.class);
84 eventSender = (EventSender) bctx.getService(sref);
85 } catch (Exception e) {
86 logger.error("SdcCallback failed on initializing EventSender", e);
90 if (isRunning.get()) {
92 for(IArtifactInfo artifact:data.getServiceArtifacts()){
93 ArtifactProcessor artifactProcessor = artifactProcessorFactory.getArtifactProcessor(
94 client, eventSender, data, null, artifact, storeUri);
95 if(artifactProcessor!=null){
96 executor.submit(artifactProcessor);
100 for (IResourceInstance resource : data.getResources()) {
101 for (IArtifactInfo artifact : resource.getArtifacts()) {
102 logger.info(Util.toSdcStoreDocumentInput(data, resource, artifact, "abc"));
103 if (executor.getQueue().size() >= threadCount) {
104 logger.warn(String.format("excuter queue size (%d) is exceeding thread count (%s).",
105 executor.getQueue().size(), threadCount));
107 ArtifactProcessor artifactProcessor = artifactProcessorFactory.getArtifactProcessor(
108 client, eventSender, data, resource, artifact, storeUri);
109 if(artifactProcessor != null){
110 executor.submit(artifactProcessor);
113 /* Before refactoring of the DownloadAndStoreOp class, the approach was to download all the
114 artifacts, send the download status, and then perform the processing of artifact if it is
115 required. Now that we are downloading the artifacts only when its processing is required,
116 we are sending the download status as positive just to have the same behaviour as before
119 client.sendDownloadStatus(Util.buildDistributionStatusMessage(
120 client, data, artifact, DistributionStatusEnum.DOWNLOAD_OK));
121 logger.error("Artifact type not supported : " + artifact.getArtifactType());
126 // TODO - return a failed result so sdc knows we are shut down
134 public void stop(int waitSec) {
135 isRunning.set(false);
136 logger.info(String.format("Stopping the SDC listener and waiting up to %ds for %d pending jobs", waitSec,
137 executor.getQueue().size()));
138 boolean cleanShutdown = false;
141 cleanShutdown = executor.awaitTermination(waitSec, TimeUnit.SECONDS);
142 executor.shutdownNow(); // In case of timeout
143 } catch (InterruptedException e) {
144 logger.error("Error in SdcCallback for stop(int waitSec) method due to InterruptedException: reason= " + e.getMessage(), e);
146 logger.info(String.format("Attempting to shutdown cleanly: %s", cleanShutdown ? "SUCCESS" : "FAILURE"));
147 logger.info("Shutdown complete.");