49ba6148c0491b39dba8c48a6b422686e16814b6
[sdc.git] /
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.enrichment.impl.external.artifact;
22
23 import org.openecomp.core.utilities.file.FileUtils;
24 import org.openecomp.core.utilities.json.JsonUtil;
25 import org.openecomp.sdc.datatypes.error.ErrorMessage;
26 import org.openecomp.sdc.enrichment.inter.Enricher;
27 import org.openecomp.sdc.enrichment.inter.ExternalArtifactEnricherInterface;
28 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
29
30 import java.lang.reflect.Constructor;
31 import java.util.Collection;
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.Map;
35
36 public class ExternalArtifactEnricher extends Enricher {
37   private static final String EXTERNAL_ARTIFACT_ENRICH_CONF_FILE = "ExternalArtifactConfiguration"
38       + ".json";
39   private static Collection<String> implementingClasses =
40       getExternalArtifactEnrichedImplClassesList();
41
42   private static Collection<String> getExternalArtifactEnrichedImplClassesList() {
43     @SuppressWarnings("unchecked")
44     Map<String, String> confFileAsMap = FileUtils.readViaInputStream(EXTERNAL_ARTIFACT_ENRICH_CONF_FILE,
45         stream -> JsonUtil.json2Object(stream, Map.class));
46
47     return confFileAsMap.values();
48   }
49
50   @Override
51   public Map<String, List<ErrorMessage>> enrich() throws Exception{
52     Map<String, List<ErrorMessage>> errors = new HashMap<>();
53
54             for (String implementingClassName : implementingClasses) {
55                 ExternalArtifactEnricherInterface externalArtifactEnricherInstance = getExternalArtifactEnricherInstance(implementingClassName);
56                 errors.putAll(externalArtifactEnricherInstance.enrich(this.data, (ToscaServiceModel) this.model));
57             }
58     return errors;
59   }
60
61   private ExternalArtifactEnricherInterface getExternalArtifactEnricherInstance(
62       String implementingClassName) throws Exception {
63     Class<?> clazz = Class.forName(implementingClassName);
64     Constructor<?> constructor = clazz.getConstructor();
65     return (ExternalArtifactEnricherInterface) constructor.newInstance();
66   }
67 }