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.core.utilities.CommonMethods;
24 import org.openecomp.core.utilities.file.FileUtils;
25 import org.openecomp.core.utilities.json.JsonUtil;
26 import org.openecomp.sdc.common.errors.Messages;
27 import org.openecomp.sdc.datatypes.error.ErrorLevel;
28 import org.openecomp.sdc.healing.api.HealingManager;
29 import org.openecomp.sdc.healing.interfaces.Healer;
30 import org.openecomp.sdc.healing.types.HealCode;
31 import org.openecomp.sdc.logging.context.impl.MdcDataErrorMessage;
32 import org.openecomp.sdc.logging.types.LoggerConstants;
33 import org.openecomp.sdc.logging.types.LoggerErrorCode;
34 import org.openecomp.sdc.logging.types.LoggerErrorDescription;
35 import org.openecomp.sdc.logging.types.LoggerTragetServiceName;
37 import java.lang.reflect.InvocationTargetException;
38 import java.util.ArrayList;
40 import java.util.Optional;
43 * Created by Talio on 11/29/2016.
45 public class HealingManagerImpl implements HealingManager {
46 private static String HEALING_CONF_FILE = "healingConfiguration.json";
47 private static Map<String, String> healerCodeToImplClass = initHealers();
50 public Object heal(HealCode code, Map<String, Object> healParameters) {
51 ArrayList<String> healingFailureMessages = new ArrayList<>();
54 heal(healParameters, healerCodeToImplClass.get(code.name()), healingFailureMessages);
56 if (!healingFailureMessages.isEmpty()) {
57 throw new RuntimeException(CommonMethods.listToSeparatedString(healingFailureMessages, '\n'));
63 public Optional<String> healAll(Map<String, Object> healParameters) {
64 ArrayList<String> healingFailureMessages = new ArrayList<>();
66 for (String implClassName : healerCodeToImplClass.values()) {
67 heal(healParameters, implClassName, healingFailureMessages);
70 return healingFailureMessages.isEmpty() ? Optional.empty()
71 : Optional.of(CommonMethods.listToSeparatedString(healingFailureMessages, '\n'));
74 private Object heal(Map<String, Object> healParameters, String healerImplClassName,
75 ArrayList<String> healingFailureMessages) {
78 healerImpl = getHealerImplInstance(healerImplClassName);
79 } catch (Exception e) {
80 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
81 LoggerTragetServiceName.SELF_HEALING, ErrorLevel.ERROR.name(), LoggerErrorCode
82 .DATA_ERROR.getErrorCode(), LoggerErrorDescription.CANT_HEAL);
83 healingFailureMessages
84 .add(String.format(Messages.CANT_LOAD_HEALING_CLASS.getErrorMessage(),
85 healerImplClassName));
90 return healerImpl.heal(healParameters);
91 } catch (Exception e) {
92 MdcDataErrorMessage.createErrorMessageAndUpdateMdc(LoggerConstants.TARGET_ENTITY_DB,
93 LoggerTragetServiceName.SELF_HEALING, ErrorLevel.ERROR.name(), LoggerErrorCode
94 .DATA_ERROR.getErrorCode(), LoggerErrorDescription.CANT_HEAL);
95 healingFailureMessages.add(e.getMessage() + " ,healer name :" + healerImplClassName);
100 private static Map<String, String> initHealers() {
101 return FileUtils.readViaInputStream(HEALING_CONF_FILE, stream -> JsonUtil.json2Object(stream, Map.class));
104 private Healer getHealerImplInstance(String implClassName)
105 throws InstantiationException, IllegalAccessException, InvocationTargetException,
106 NoSuchMethodException, ClassNotFoundException {
107 return (Healer) Class.forName(implClassName).getConstructor().newInstance();