ActivitySpec - Correcting logger messages
[sdc.git] / common / openecomp-sdc-artifact-generator-lib / openecomp-sdc-artifact-generator-core / src / main / java / org / openecomp / sdc / generator / GeneratorTask.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.generator;
22
23 import org.openecomp.sdc.logging.api.Logger;
24 import org.openecomp.sdc.logging.api.LoggerFactory;
25 import org.openecomp.sdc.generator.data.Artifact;
26 import org.openecomp.sdc.generator.data.GenerationData;
27 import org.openecomp.sdc.generator.intf.ArtifactGenerator;
28
29 import java.util.LinkedList;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.concurrent.RecursiveTask;
33
34 public class GeneratorTask extends RecursiveTask<GenerationData> {
35
36   private static Logger log = LoggerFactory.getLogger(GeneratorTask.class.getName());
37
38   List<Artifact> input;
39   List<ArtifactGenerator> generators;
40   Map<String, String> additionalParams;
41
42   /**
43    * Instantiates a new Generator task.
44    *
45    * @param generators       the generators
46    * @param input            the input
47    * @param additionalParams the additional params
48    */
49   public GeneratorTask(List<ArtifactGenerator> generators, List<Artifact> input,
50                        Map<String, String> additionalParams ) {
51     this.input = input;
52     this.generators = generators;
53     this.additionalParams = additionalParams;
54   }
55
56   @Override
57   protected GenerationData compute() {
58     if (generators.size() == 1) {
59       log.debug("Instantiating Generator : " + generators.get(0).getClass().getName());
60       return generators.remove(0).generateArtifact(input, additionalParams);
61     } else {
62       LinkedList<ArtifactGenerator> generator = new LinkedList<>();
63       generator.add(generators.remove(0));
64       GeneratorTask tobeDone = new GeneratorTask(generator, input, additionalParams);
65       GeneratorTask tobeForked =
66           new GeneratorTask(new LinkedList<ArtifactGenerator>(generators), input, additionalParams);
67       tobeForked.fork();
68       GenerationData output = tobeDone.compute();
69       output.add(tobeForked.join());
70       return output;
71     }
72   }
73
74 }