fb0622cd679fcf439891bc081dcd1224ea236dbf
[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.common.utils.CommonUtil;
26 import org.openecomp.sdc.datatypes.error.ErrorMessage;
27 import org.openecomp.sdc.enrichment.inter.Enricher;
28 import org.openecomp.sdc.enrichment.inter.ExternalArtifactEnricherInterface;
29 import org.openecomp.sdc.logging.api.Logger;
30 import org.openecomp.sdc.logging.api.LoggerFactory;
31 import org.openecomp.sdc.logging.context.impl.MdcDataDebugMessage;
32
33 import java.io.InputStream;
34 import java.lang.reflect.Constructor;
35 import java.util.Collection;
36 import java.util.HashMap;
37 import java.util.List;
38 import java.util.Map;
39
40 public class ExternalArtifactEnricher extends Enricher {
41   private MdcDataDebugMessage mdcDataDebugMessage = new MdcDataDebugMessage();
42   private static String EXTERNAL_ARTIFACT_ENRICH_CONF_FILE = "ExternalArtifactConfiguration.json";
43   private static final String EXTERNAL_ARTIFACT_ENRICH_ERROR = "ERROR_CREATING_EXTERNAL_ARTIFACTS";
44   private static final String EXTERNAL_ARTIFACT_ENRICH_ERROR_MSG =
45       "An Error has occured during enrichment of external artifacts ";
46   private static Collection<String> implementingClasses =
47       getExternalArtifactEnrichedImplClassesList();
48   private static Logger logger = LoggerFactory.getLogger(ExternalArtifactEnricher.class);
49
50   private static Collection<String> getExternalArtifactEnrichedImplClassesList() {
51     InputStream externalArtifactEnrichConfigurationJson =
52         FileUtils.getFileInputStream(EXTERNAL_ARTIFACT_ENRICH_CONF_FILE);
53     @SuppressWarnings("unchecked")
54     Map<String, String> confFileAsMap =
55         JsonUtil.json2Object(externalArtifactEnrichConfigurationJson, Map.class);
56
57     return confFileAsMap.values();
58   }
59
60   @Override
61   public Map<String, List<ErrorMessage>> enrich() {
62
63
64     mdcDataDebugMessage.debugEntryMessage(null, null);
65
66     Map<String, List<ErrorMessage>> errors = new HashMap<>();
67
68         try {
69             for (String implementingClassName : implementingClasses) {
70                 ExternalArtifactEnricherInterface externalArtifactEnricherInstance = getExternalArtifactEnricherInstance(implementingClassName);
71                 externalArtifactEnricherInstance.enrich(this.data);
72             }
73         } catch (Exception e) {
74             e.printStackTrace();
75           logger.error(e.getMessage());
76         }
77
78     mdcDataDebugMessage.debugExitMessage(null, null);
79     return errors;
80   }
81
82   private ExternalArtifactEnricherInterface getExternalArtifactEnricherInstance(
83       String implementingClassName) throws Exception {
84     Class<?> clazz = Class.forName(implementingClassName);
85     Constructor<?> constructor = clazz.getConstructor();
86     return (ExternalArtifactEnricherInterface) constructor.newInstance();
87   }
88 }