541418fe8f6d6b5c083e1eb7496161c5fd1535f9
[appc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
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  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.appc.sdc.listener;
24
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;
40
41 import java.net.URI;
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;
46
47 public class SdcCallback implements INotificationCallback {
48
49     private final EELFLogger logger = EELFManager.getInstance().getLogger(SdcCallback.class);
50     private ArtifactProcessorFactory artifactProcessorFactory = new ArtifactProcessorFactory();
51
52     private URI storeUri;
53     private IDistributionClient client;
54
55     private EventSender eventSender = null;
56
57     private ThreadPoolExecutor executor;
58     private int threadCount = 10;
59
60     private AtomicBoolean isRunning = new AtomicBoolean(false);
61
62
63     public SdcCallback(URI storeUri, IDistributionClient client) {
64         this.storeUri = storeUri;
65         this.client = client;
66
67         // Create the thread pool
68         executor = new ThreadPoolExecutor(threadCount, threadCount, 1, TimeUnit.SECONDS,
69             new ArrayBlockingQueue<Runnable>(threadCount * 2));
70
71         // Custom Named thread factory
72         BasicThreadFactory threadFactory = new BasicThreadFactory.Builder().namingPattern("Appc-Listener-%d").build();
73         executor.setThreadFactory(threadFactory);
74
75         isRunning.set(true);
76     }
77
78     @Override
79     public void activateCallback(INotificationData data) {
80         if (null == eventSender) {
81             try {
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);
87             }
88         }
89
90         if (isRunning.get()) {
91
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);
97                 }
98             }
99
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));
106                     }
107                     ArtifactProcessor artifactProcessor = artifactProcessorFactory.getArtifactProcessor(
108                         client, eventSender, data, resource, artifact, storeUri);
109                     if(artifactProcessor != null){
110                         executor.submit(artifactProcessor);
111                     }
112                     else{
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
117                             refactoring.
118                          */
119                         client.sendDownloadStatus(Util.buildDistributionStatusMessage(
120                             client, data, artifact, DistributionStatusEnum.DOWNLOAD_OK));
121                         logger.error("Artifact type not supported : " + artifact.getArtifactType());
122                     }
123                 }
124             }
125         } else {
126             // TODO - return a failed result so sdc knows we are shut down
127         }
128     }
129
130     public void stop() {
131         stop(10);
132     }
133
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;
139         executor.shutdown();
140         try {
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);
145         }
146         logger.info(String.format("Attempting to shutdown cleanly: %s", cleanShutdown ? "SUCCESS" : "FAILURE"));
147         logger.info("Shutdown complete.");
148     }
149
150 }