2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.healing.impl;
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;
36 import java.io.InputStream;
37 import java.lang.reflect.Constructor;
41 * Created by Talio on 11/29/2016.
43 public class HealingManagerImpl implements HealingManager {
44 private static String HEALING_CONF_FILE = "healingConfiguration.json";
45 private static Map<String, String> healerCodeToImplClass = initHealers();
48 public Object heal(HealCode code, Map<String, Object> healParameters) {
49 String implClassName = healerCodeToImplClass.get(code.name());
51 Healer healerImpl = getHealerImplInstance(implClassName);
52 return healerImpl.heal(healParameters);
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()));
64 public void healAll(Map<String, Object> healParameters) {
65 for (String implClassName : healerCodeToImplClass.values()) {
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()));
79 private static Map<String, String> initHealers() {
80 InputStream healingConfigurationJson = FileUtils.getFileInputStream(HEALING_CONF_FILE);
81 return JsonUtil.json2Object(healingConfigurationJson, Map.class);
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();