summaryrefslogtreecommitdiffstats
path: root/dcaedt_be/src/main/java/org/onap/sdc/dcae/rule/editor/translators/RuleTranslator.java
blob: bce39803dcc3d5b718037c97b8cbd39e1d8d1901 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package org.onap.sdc.dcae.rule.editor.translators;

import com.google.gson.Gson;
import org.onap.sdc.common.onaplog.Enums.LogLevel;
import org.onap.sdc.dcae.composition.restmodels.ruleeditor.*;
import org.onap.sdc.dcae.rule.editor.enums.OperatorTypeEnum;
import org.onap.sdc.dcae.rule.editor.enums.RuleEditorElementType;
import org.onap.sdc.dcae.rule.editor.utils.ValidationUtils;

public class RuleTranslator implements IRuleElementTranslator<Rule> {

	private static RuleTranslator ruleTranslator = new RuleTranslator();

	public static RuleTranslator getInstance() {
		return ruleTranslator;
	}

	private RuleTranslator() {
	}

	private class ActionRuleTranslation extends RuleTranslation {
		private ActionRuleTranslation(Rule rule) {
			phase = rule.getPhase();
			filter = rule.isConditionalRule() ? getConditionTranslator(rule.getCondition()).translateToHpJson(rule.getCondition()) : null;
			boolean asNewProcessor = true;
			for (BaseAction action : rule.getActions()) {
				// consecutive copy actions are aggregated into a single processor
				asNewProcessor = getActionTranslator(action).addToHpJsonProcessors(action, processors, asNewProcessor);
			}
		}
	}

	public Object translateToHpJson(Rule rule) {
		debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Start translating rule {}", rule.getUid());
		Object translation = new ActionRuleTranslation(rule);
		debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Finished translation for rule {}. Result: {}", rule.getUid(), new Gson().toJson(translation));
		return translation;
	}

	private IRuleElementTranslator getConditionTranslator(BaseCondition condition){
		return condition instanceof ConditionGroup ? ConditionGroupTranslator.getInstance() :
				getSimpleConditionTranslator((Condition) condition);
	}

	private IRuleElementTranslator getSimpleConditionTranslator(Condition condition) {
		String conditionType = OperatorTypeEnum.getTypeByName(condition.getOperator()).getConditionType();
		return RuleEditorElementType.getElementTypeByName(conditionType).getTranslator();
	}


	private ActionTranslator getActionTranslator(BaseAction action) {
		ActionTypeEnum type = ActionTypeEnum.getTypeByName(action.getActionType());
		if(ActionTypeEnum.COPY == type && ValidationUtils.validateNotEmpty(((BaseCopyAction)action).regexValue())) {
			return RegexActionTranslator.getInstance();
		}
		return (ActionTranslator) RuleEditorElementType.getElementTypeByName(type.getType()).getTranslator();
	}
}