1c895ffc6f12b28ca9f212b6a97cc875aa5c9af8
[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.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;
36
37 import java.lang.reflect.InvocationTargetException;
38 import java.util.ArrayList;
39 import java.util.Map;
40 import java.util.Optional;
41
42 /**
43  * Created by Talio on 11/29/2016.
44  */
45 public class HealingManagerImpl implements HealingManager {
46   private static String HEALING_CONF_FILE = "healingConfiguration.json";
47   private static Map<String, String> healerCodeToImplClass = initHealers();
48
49   @Override
50   public Object heal(HealCode code, Map<String, Object> healParameters) {
51     ArrayList<String> healingFailureMessages = new ArrayList<>();
52
53     Object result =
54         heal(healParameters, healerCodeToImplClass.get(code.name()), healingFailureMessages);
55
56     if (!healingFailureMessages.isEmpty()) {
57       throw new RuntimeException(CommonMethods.listToSeparatedString(healingFailureMessages, '\n'));
58     }
59     return result;
60   }
61
62   @Override
63   public Optional<String> healAll(Map<String, Object> healParameters) {
64     ArrayList<String> healingFailureMessages = new ArrayList<>();
65
66     for (String implClassName : healerCodeToImplClass.values()) {
67       heal(healParameters, implClassName, healingFailureMessages);
68     }
69
70     return healingFailureMessages.isEmpty() ? Optional.empty()
71         : Optional.of(CommonMethods.listToSeparatedString(healingFailureMessages, '\n'));
72   }
73
74   private Object heal(Map<String, Object> healParameters, String healerImplClassName,
75                       ArrayList<String> healingFailureMessages) {
76     Healer healerImpl;
77     try {
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));
86       return null;
87     }
88
89     try {
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);
96     }
97     return null;
98   }
99
100   private static Map<String, String> initHealers() {
101     return FileUtils.readViaInputStream(HEALING_CONF_FILE, stream -> JsonUtil.json2Object(stream, Map.class));
102   }
103
104   private Healer getHealerImplInstance(String implClassName)
105       throws InstantiationException, IllegalAccessException, InvocationTargetException,
106       NoSuchMethodException, ClassNotFoundException {
107     return (Healer) Class.forName(implClassName).getConstructor().newInstance();
108   }
109 }