Rename packages from openecomp to onap.
[sdc.git] / common / onap-sdc-artifact-generator-lib / onap-sdc-artifact-generator-core / src / main / java / org / onap / sdc / generator / GeneratorManager.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;
22
23 import static org.onap.sdc.generator.util.ArtifactGeneratorUtil.logError;
24
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import org.onap.sdc.generator.data.ArtifactType;
27 import org.onap.sdc.generator.data.GeneratorConfiguration;
28 import org.onap.sdc.generator.data.GeneratorConstants;
29 import org.onap.sdc.generator.intf.ArtifactGenerator;
30 import org.onap.sdc.generator.intf.Generator;
31 import org.onap.sdc.generator.util.ArtifactGeneratorUtil;
32 import org.openecomp.sdc.logging.api.Logger;
33 import org.openecomp.sdc.logging.api.LoggerFactory;
34 import org.reflections.Reflections;
35
36 import java.util.ArrayList;
37 import java.util.HashMap;
38 import java.util.List;
39 import java.util.Map;
40 import java.util.Set;
41
42 public class GeneratorManager {
43
44   private static Logger log = LoggerFactory.getLogger(GeneratorManager.class.getName());
45   private static Map<ArtifactType, ArtifactGenerator> generators = new HashMap<>();
46
47   /**
48    * Gets active artifact generators.
49    *
50    * @param clientConfiguration the client configuration
51    * @return the active artifact generators
52    * @throws Exception the exception
53    */
54   public static List<ArtifactGenerator> getActiveArtifactGenerators(String clientConfiguration)
55       throws Exception {
56
57     if (generators.isEmpty()) {
58       log.debug("Getting list of active generators");
59       Reflections reflections = new Reflections("org.onap.sdc.generator");
60       Set<Class<?>> annotated = reflections.getTypesAnnotatedWith(Generator.class);
61       for (Class<?> clazz : annotated) {
62         Generator generator = clazz.getAnnotation(Generator.class);
63         generators.put(generator.artifactType(), (ArtifactGenerator) clazz.newInstance());
64       }
65     }
66
67     log.debug("Parsing generator configuration from the client configuration : "
68         + clientConfiguration);
69     GeneratorConfiguration gf = getGeneratorConfiguration(clientConfiguration);
70     List<ArtifactGenerator> generatorList = new ArrayList<>();
71     if (gf.getArtifactTypes() != null && !gf.getArtifactTypes().isEmpty()) {
72       for (ArtifactType type : gf.getArtifactTypes()) {
73         if (generators.get(type) != null) {
74           generatorList.add(generators.get(type));
75         }
76       }
77     }
78
79     return generatorList;
80   }
81
82   private static GeneratorConfiguration getGeneratorConfiguration(String jsonConfiguration) {
83     try {
84       return new ObjectMapper().readValue(jsonConfiguration, GeneratorConfiguration.class);
85     } catch (Exception exception) {
86       ArtifactGeneratorUtil
87           .logError(GeneratorConstants.GENERATOR_ERROR_INVALID_CLIENT_CONFIGURATION, exception);
88       throw new IllegalArgumentException(
89           GeneratorConstants.GENERATOR_ERROR_INVALID_CLIENT_CONFIGURATION, exception);
90     }
91   }
92
93 }