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