d721e13a07a6d66da40dee0cba61102a5b81761f
[appc.git] / appc-sdc-listener / appc-sdc-listener-bundle / src / main / java / org / openecomp / appc / sdc / listener / SdcCallback.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
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  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.openecomp.appc.sdc.listener;
26
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.appc.sdc.artifacts.ArtifactProcessor;
31 import org.openecomp.appc.sdc.artifacts.impl.ArtifactProcessorFactory;
32 import org.openecomp.sdc.api.IDistributionClient;
33 import org.openecomp.sdc.api.consumer.INotificationCallback;
34 import org.openecomp.sdc.api.notification.IArtifactInfo;
35 import org.openecomp.sdc.api.notification.INotificationData;
36 import org.openecomp.sdc.api.notification.IResourceInstance;
37 import org.apache.commons.lang3.concurrent.BasicThreadFactory;
38 import org.openecomp.sdc.utils.DistributionStatusEnum;
39 import org.osgi.framework.BundleContext;
40 import org.osgi.framework.FrameworkUtil;
41 import org.osgi.framework.ServiceReference;
42
43 import java.net.URI;
44 import java.util.concurrent.ArrayBlockingQueue;
45 import java.util.concurrent.ThreadPoolExecutor;
46 import java.util.concurrent.TimeUnit;
47 import java.util.concurrent.atomic.AtomicBoolean;
48
49 public class SdcCallback implements INotificationCallback {
50
51     private final EELFLogger logger = EELFManager.getInstance().getLogger(SdcCallback.class);
52
53     private URI storeUri;
54     private IDistributionClient client;
55
56     private EventSender eventSender = null;
57
58     private ThreadPoolExecutor executor;
59     private int threadCount = 10;
60
61     private AtomicBoolean isRunning = new AtomicBoolean(false);
62
63
64     public SdcCallback(URI storeUri, IDistributionClient client) {
65         this.storeUri = storeUri;
66         this.client = client;
67
68         // Create the thread pool
69         executor = new ThreadPoolExecutor(threadCount, threadCount, 1, TimeUnit.SECONDS,
70             new ArrayBlockingQueue<Runnable>(threadCount * 2));
71
72         // Custom Named thread factory
73         BasicThreadFactory threadFactory = new BasicThreadFactory.Builder().namingPattern("Appc-Listener-%d").build();
74         executor.setThreadFactory(threadFactory);
75
76         isRunning.set(true);
77     }
78
79     @Override
80     public void activateCallback(INotificationData data) {
81         if (null == eventSender) {
82             try {
83                 BundleContext bctx = FrameworkUtil.getBundle(EventSender.class).getBundleContext();
84                 ServiceReference sref = bctx.getServiceReference(EventSender.class);
85                 eventSender = (EventSender) bctx.getService(sref);
86             } catch (Throwable t) {
87                 logger.error("SdcCallback failed on initializing EventSender", t);
88             }
89         }
90
91         if (isRunning.get()) {
92
93             for(IArtifactInfo artifact:data.getServiceArtifacts()){
94                 ArtifactProcessor artifactProcessor = ArtifactProcessorFactory.getArtifactProcessor(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                         // log warning about job backlog
105                     }
106                     ArtifactProcessor artifactProcessor = ArtifactProcessorFactory.getArtifactProcessor(client, eventSender, data, resource, artifact, storeUri);
107                     if(artifactProcessor != null){
108                         executor.submit(artifactProcessor);
109                     }
110                     else{
111                         /* Before refactoring of the DownloadAndStoreOp class, the approach was to download all the
112                             artifacts, send the download status, and then perform the processing of artifact if it is
113                             required. Now that we are downloading the artifacts only when its processing is required,
114                             we are sending the download status as positive just to have the same behaviour as before
115                             refactoring.
116                          */
117                         client.sendDownloadStatus(Util.buildDistributionStatusMessage(client, data, artifact, DistributionStatusEnum.DOWNLOAD_OK));
118                         logger.error("Artifact type not supported : " + artifact.getArtifactType());
119                     }
120                 }
121             }
122         } else {
123             // TODO - return a failed result so sdc knows we are shut down
124         }
125     }
126
127     public void stop() {
128         stop(10);
129     }
130
131     public void stop(int waitSec) {
132         isRunning.set(false);
133         logger.info(String.format("Stopping the SDC listener and waiting up to %ds for %d pending jobs", waitSec,
134             executor.getQueue().size()));
135         boolean cleanShutdown = false;
136         executor.shutdown();
137         try {
138             cleanShutdown = executor.awaitTermination(waitSec, TimeUnit.SECONDS);
139             executor.shutdownNow(); // In case of timeout
140         } catch (InterruptedException e) {
141             e.printStackTrace();
142         }
143         logger.info(String.format("Attempting to shutdown cleanly: %s", cleanShutdown ? "SUCCESS" : "FAILURE"));
144         logger.info("Shutdown complete.");
145     }
146
147 }