3f6148f0e73db05b2447434beb1e2819ddc586b2
[sdc.git] / common / openecomp-sdc-artifact-generator-lib / openecomp-sdc-artifact-generator-core / src / main / java / org / openecomp / sdc / generator / impl / ArtifactGenerationServiceImpl.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.impl;
22
23 import static org.openecomp.sdc.generator.data.GeneratorConstants.GENERATOR_ERROR_ARTIFACT_GENERATION_FAILED;
24 import static org.openecomp.sdc.generator.data.GeneratorConstants.GENERATOR_INVOCATION_ERROR_CODE;
25 import static org.openecomp.sdc.generator.util.ArtifactGeneratorUtil.logError;
26
27 import org.openecomp.sdc.logging.api.Logger;
28 import org.openecomp.sdc.logging.api.LoggerFactory;
29 import org.openecomp.sdc.generator.GeneratorManager;
30 import org.openecomp.sdc.generator.GeneratorTask;
31 import org.openecomp.sdc.generator.data.Artifact;
32 import org.openecomp.sdc.generator.data.GenerationData;
33 import org.openecomp.sdc.generator.intf.ArtifactGenerator;
34 import org.openecomp.sdc.generator.service.ArtifactGenerationService;
35 import org.openecomp.sdc.generator.util.ArtifactGeneratorUtil;
36
37 import java.util.List;
38 import java.util.Map;
39 import java.util.concurrent.ForkJoinPool;
40
41 /**
42  * Artifact Generation Service implementation class.
43  */
44 public class ArtifactGenerationServiceImpl implements ArtifactGenerationService {
45
46   private static Logger log =
47       LoggerFactory.getLogger(ArtifactGenerationServiceImpl.class.getName());
48
49   /**
50    * Artifact generator method.
51    *
52    * @param input                   List of input files as {@link Artifact} models
53    * @param overridingConfiguration Configuration data for invoking generators
54    * @param additionalParams Additional Parameters
55    * @return Generated artifacts/Error data in a {@link GenerationData} object
56    */
57   @Override
58   public GenerationData generateArtifact(List<Artifact> input, String overridingConfiguration,
59                                          Map<String, String> additionalParams) {
60     try {
61       //Initialize artifact generation logging context
62       ArtifactGeneratorUtil.initializeLoggingContext();
63
64       List<ArtifactGenerator> generatorsToBeUsed =
65           GeneratorManager.getActiveArtifactGenerators(overridingConfiguration);
66       if (generatorsToBeUsed.size() > 0) {
67         return ForkJoinPool.commonPool().invoke(new GeneratorTask(generatorsToBeUsed, input,
68             additionalParams));
69       } else {
70         return new GenerationData();
71       }
72     } catch (IllegalArgumentException iae) {
73       //Invalid client configuration
74       logError(GENERATOR_ERROR_ARTIFACT_GENERATION_FAILED, iae);
75       GenerationData errorData = new GenerationData();
76       errorData.add(GENERATOR_INVOCATION_ERROR_CODE, iae.getMessage());
77       return errorData;
78     } catch (Exception ex) {
79       logError(GENERATOR_ERROR_ARTIFACT_GENERATION_FAILED, ex);
80       GenerationData errorData = new GenerationData();
81       errorData.add(GENERATOR_INVOCATION_ERROR_CODE,
82                     GENERATOR_ERROR_ARTIFACT_GENERATION_FAILED);
83       return errorData;
84     }
85   }
86 }