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