2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.generator;
23 import static org.openecomp.sdc.generator.data.GeneratorConstants.GENERATOR_ERROR_INVALID_CLIENT_CONFIGURATION;
24 import static org.openecomp.sdc.generator.util.ArtifactGeneratorUtil.logError;
26 import com.fasterxml.jackson.databind.ObjectMapper;
27 import org.openecomp.core.logging.api.Logger;
28 import org.openecomp.core.logging.api.LoggerFactory;
29 import org.openecomp.sdc.generator.data.ArtifactType;
30 import org.openecomp.sdc.generator.data.GeneratorConfiguration;
31 import org.openecomp.sdc.generator.intf.ArtifactGenerator;
32 import org.openecomp.sdc.generator.intf.Generator;
33 import org.reflections.Reflections;
35 import java.util.ArrayList;
36 import java.util.HashMap;
37 import java.util.List;
41 public class GeneratorManager {
43 private static Logger log = LoggerFactory.getLogger(GeneratorManager.class.getName());
44 private static Map<ArtifactType, ArtifactGenerator> generators = new HashMap<>();
47 * Gets active artifact generators.
49 * @param clientConfiguration the client configuration
50 * @return the active artifact generators
51 * @throws Exception the exception
53 public static List<ArtifactGenerator> getActiveArtifactGenerators(String clientConfiguration)
56 if (generators.isEmpty()) {
57 log.debug("Getting list of active generators");
58 Reflections reflections = new Reflections("org.openecomp.sdc.generator");
59 Set<Class<?>> annotated = reflections.getTypesAnnotatedWith(Generator.class);
60 for (Class<?> clazz : annotated) {
61 Generator generator = clazz.getAnnotation(Generator.class);
62 generators.put(generator.artifactType(), (ArtifactGenerator) clazz.newInstance());
66 log.debug("Parsing generator configuration from the client configuration : "
67 + clientConfiguration);
68 GeneratorConfiguration gf = getGeneratorConfiguration(clientConfiguration);
69 List<ArtifactGenerator> generatorList = new ArrayList<>();
70 if (gf.getArtifactTypes() != null && !gf.getArtifactTypes().isEmpty()) {
71 for (ArtifactType type : gf.getArtifactTypes()) {
72 if (generators.get(type) != null) {
73 generatorList.add(generators.get(type));
81 private static GeneratorConfiguration getGeneratorConfiguration(String jsonConfiguration) {
83 return new ObjectMapper().readValue(jsonConfiguration, GeneratorConfiguration.class);
84 } catch (Exception exception) {
85 logError(GENERATOR_ERROR_INVALID_CLIENT_CONFIGURATION, exception);
86 throw new IllegalArgumentException(
87 GENERATOR_ERROR_INVALID_CLIENT_CONFIGURATION, exception);