1 package org.onap.sdc.dcae.rule.editor.translators;
3 import com.google.gson.Gson;
4 import org.onap.sdc.common.onaplog.Enums.LogLevel;
5 import org.onap.sdc.dcae.composition.restmodels.ruleeditor.*;
6 import org.onap.sdc.dcae.rule.editor.enums.OperatorTypeEnum;
7 import org.onap.sdc.dcae.rule.editor.enums.RuleEditorElementType;
8 import org.onap.sdc.dcae.rule.editor.utils.ValidationUtils;
10 public class RuleTranslator implements IRuleElementTranslator<Rule> {
12 private static RuleTranslator ruleTranslator = new RuleTranslator();
14 public static RuleTranslator getInstance() {
15 return ruleTranslator;
18 private RuleTranslator() {
21 private class ActionRuleTranslation extends RuleTranslation {
22 private ActionRuleTranslation(Rule rule) {
23 phase = rule.getPhase();
24 filter = rule.isConditionalRule() ? getConditionTranslator(rule.getCondition()).translateToHpJson(rule.getCondition()) : null;
25 boolean asNewProcessor = true;
26 for (BaseAction action : rule.getActions()) {
27 // consecutive copy actions are aggregated into a single processor
28 asNewProcessor = getActionTranslator(action).addToHpJsonProcessors(action, processors, asNewProcessor);
33 public Translation translateToHpJson(Rule rule) {
34 debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Start translating rule {}", rule.getUid());
35 Translation translation = new ActionRuleTranslation(rule);
36 debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Finished translation for rule {}. Result: {}", rule.getUid(), new Gson().toJson(translation));
40 private IRuleElementTranslator getConditionTranslator(BaseCondition condition){
41 return condition instanceof ConditionGroup ? ConditionGroupTranslator.getInstance() :
42 ValidationUtils.validateNotEmpty(OperatorTypeEnum.getTypeByName(((Condition)condition).getOperator()).getModifiedType()) ? FieldConditionTranslator.getInstance() : ConditionTranslator.getInstance();
45 private CopyActionTranslator getActionTranslator(BaseAction action) {
46 ActionTypeEnum type = ActionTypeEnum.getTypeByName(action.getActionType());
47 if(ActionTypeEnum.COPY == type && ValidationUtils.validateNotEmpty(action.getRegexValue()))
48 return RegexActionTranslator.getInstance();
49 return (CopyActionTranslator)RuleEditorElementType.getElementTypeByName(type.getType()).getTranslator();