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.onap.sdc.generator;
23 import static org.onap.sdc.generator.util.ArtifactGeneratorUtil.logError;
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;
36 import java.util.ArrayList;
37 import java.util.HashMap;
38 import java.util.List;
42 public class GeneratorManager {
44 private static Logger log = LoggerFactory.getLogger(GeneratorManager.class.getName());
45 private static Map<ArtifactType, ArtifactGenerator> generators = new HashMap<>();
48 * Gets active artifact generators.
50 * @param clientConfiguration the client configuration
51 * @return the active artifact generators
52 * @throws Exception the exception
54 public static List<ArtifactGenerator> getActiveArtifactGenerators(String clientConfiguration)
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());
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));
82 private static GeneratorConfiguration getGeneratorConfiguration(String jsonConfiguration) {
84 return new ObjectMapper().readValue(jsonConfiguration, GeneratorConfiguration.class);
85 } catch (Exception exception) {
87 .logError(GeneratorConstants.GENERATOR_ERROR_INVALID_CLIENT_CONFIGURATION, exception);
88 throw new IllegalArgumentException(
89 GeneratorConstants.GENERATOR_ERROR_INVALID_CLIENT_CONFIGURATION, exception);