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.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;
 
  36 import java.lang.reflect.Constructor;
 
  40  * Created by Talio on 11/29/2016.
 
  42 public class HealingManagerImpl implements HealingManager {
 
  43   private static String HEALING_CONF_FILE = "healingConfiguration.json";
 
  44   private static Map<String, String> healerCodeToImplClass = initHealers();
 
  47   public Object heal(HealCode code, Map<String, Object> healParameters) {
 
  48     String implClassName = healerCodeToImplClass.get(code.name());
 
  50       Healer healerImpl = getHealerImplInstance(implClassName);
 
  51       return healerImpl.heal(healParameters);
 
  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()));
 
  63   public void healAll(Map<String, Object> healParameters) {
 
  64     for (String implClassName : healerCodeToImplClass.values()) {
 
  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()));
 
  78   private static Map<String, String> initHealers() {
 
  79     return FileUtils.readViaInputStream(HEALING_CONF_FILE, stream -> JsonUtil.json2Object(stream, Map.class));
 
  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();