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