[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / openecomp-be / lib / openecomp-healing-lib / openecomp-sdc-healing-core / src / main / java / org / openecomp / sdc / healing / impl / HealingManagerImpl.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.healing.impl;
22
23 import org.openecomp.sdc.datatypes.error.ErrorLevel;
24 import org.openecomp.sdc.healing.interfaces.Healer;
25 import org.openecomp.core.utilities.file.FileUtils;
26 import org.openecomp.sdc.healing.types.HealCode;
27 import org.openecomp.core.utilities.json.JsonUtil;
28 import org.openecomp.sdc.healing.api.HealingManager;
29 import org.openecomp.sdc.common.errors.Messages;
30 import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage;
31 import org.openecomp.sdc.logging.types.LoggerConstants;
32 import org.openecomp.sdc.logging.types.LoggerErrorCode;
33 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
34 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
35
36 import java.io.InputStream;
37 import java.lang.reflect.Constructor;
38 import java.util.Map;
39
40 /**
41  * Created by Talio on 11/29/2016.
42  */
43 public class HealingManagerImpl implements HealingManager {
44   private static String HEALING_CONF_FILE = "healingConfiguration.json";
45   private static Map<String, String> healerCodeToImplClass = initHealers();
46
47   @Override
48   public Object heal(HealCode code, Map<String, Object> healParameters) {
49     String implClassName = healerCodeToImplClass.get(code.name());
50     try {
51       Healer healerImpl = getHealerImplInstance(implClassName);
52       return healerImpl.heal(healParameters);
53
54     } catch (Exception e) {
55       MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
56           LoggerTragetServiceName.SELF_HEALING, ErrorLevel.ERROR.name(), LoggerErrorCode
57               .DATA_ERROR.getErrorCode(), LoggerErrorDescription.CANT_HEAL);
58       throw new RuntimeException(String.format(Messages.CANT_LOAD_CLASS.getErrorMessage(),
59           implClassName, e.getMessage()));
60     }
61   }
62
63   @Override
64   public void healAll(Map<String, Object> healParameters) {
65     for (String implClassName : healerCodeToImplClass.values()) {
66       try {
67         Healer healerImpl = getHealerImplInstance(implClassName);
68         healerImpl.heal(healParameters);
69       } catch (Exception e) {
70         MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
71             LoggerTragetServiceName.SELF_HEALING, ErrorLevel.ERROR.name(), LoggerErrorCode
72                 .DATA_ERROR.getErrorCode(), LoggerErrorDescription.CANT_HEAL);
73         throw new RuntimeException(String.format(Messages.CANT_LOAD_CLASS.getErrorMessage(),
74             implClassName, e.getMessage()));
75       }
76     }
77   }
78
79   private static Map<String, String> initHealers() {
80     InputStream healingConfigurationJson = FileUtils.getFileInputStream(HEALING_CONF_FILE);
81     return JsonUtil.json2Object(healingConfigurationJson, Map.class);
82   }
83
84   private Healer getHealerImplInstance(String implClassName)
85       throws ClassNotFoundException, NoSuchMethodException, InstantiationException,
86       IllegalAccessException, java.lang.reflect.InvocationTargetException {
87     Class<?> clazz = Class.forName(implClassName);
88     Constructor<?> constructor = clazz.getConstructor();
89     return (Healer) constructor.newInstance();
90   }
91 }