e55ad880e96d36414aed880ce29bd8410ed153b9
[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.logging.api.Logger;
29 import org.openecomp.sdc.logging.api.LoggerFactory;
30 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
31 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
32
33 import java.lang.reflect.Constructor;
34 import java.util.Collection;
35 import java.util.HashMap;
36 import java.util.List;
37 import java.util.Map;
38
39 public class ExternalArtifactEnricher extends Enricher {
40   private MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
41   private static String EXTERNAL_ARTIFACT_ENRICH_CONF_FILE = "ExternalArtifactConfiguration.json";
42   private static Collection<String> implementingClasses =
43       getExternalArtifactEnrichedImplClassesList();
44   private static Logger logger = LoggerFactory.getLogger(ExternalArtifactEnricher.class);
45
46   private static Collection<String> getExternalArtifactEnrichedImplClassesList() {
47     @SuppressWarnings("unchecked")
48     Map<String, String> confFileAsMap = FileUtils.readViaInputStream(EXTERNAL_ARTIFACT_ENRICH_CONF_FILE,
49         stream -> JsonUtil.json2Object(stream, Map.class));
50
51     return confFileAsMap.values();
52   }
53
54   @Override
55   public Map<String, List<ErrorMessage>> enrich() {
56
57
58     mdcDataDebugMessage.debugEntryMessage(null);
59
60     Map<String, List<ErrorMessage>> errors = new HashMap<>();
61
62         try {
63             for (String implementingClassName : implementingClasses) {
64                 ExternalArtifactEnricherInterface externalArtifactEnricherInstance = getExternalArtifactEnricherInstance(implementingClassName);
65                 externalArtifactEnricherInstance.enrich(this.data, (ToscaServiceModel) this.model);
66             }
67         } catch (Exception e) {
68           logger.debug("",e);
69           logger.error(e.getMessage());
70         }
71
72     mdcDataDebugMessage.debugExitMessage(null);
73     return errors;
74   }
75
76   private ExternalArtifactEnricherInterface getExternalArtifactEnricherInstance(
77       String implementingClassName) throws Exception {
78     Class<?> clazz = Class.forName(implementingClassName);
79     Constructor<?> constructor = clazz.getConstructor();
80     return (ExternalArtifactEnricherInterface) constructor.newInstance();
81   }
82 }