DCAE-D be initial commit
[sdc/dcae-d/dt-be-main.git] / dcaedt_be / src / main / java / org / onap / sdc / dcae / rule / editor / translators / RuleTranslator.java
1 package org.onap.sdc.dcae.rule.editor.translators;
2
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;
9
10 public class RuleTranslator implements IRuleElementTranslator<Rule> {
11
12         private static RuleTranslator ruleTranslator = new RuleTranslator();
13
14         public static RuleTranslator getInstance() {
15                 return ruleTranslator;
16         }
17
18         private RuleTranslator() {
19         }
20
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);
29                         }
30                 }
31         }
32
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));
37                 return translation;
38         }
39
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();
43         }
44
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();
50         }
51 }