summaryrefslogtreecommitdiffstats
path: root/dcaedt_be/src/main/java/org/onap/sdc/dcae/rule/editor/translators/RuleTranslator.java
blob: c652923d9f0aa4cfee5fae86ab95cc62819ea623 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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);
			}
		}
	}

	private class EntryPhaseTranslation extends RuleTranslation {

		private EntryPhaseTranslation(String phaseName, String runPhase) {
			phase = phaseName;
			processors.add(new RunPhaseProcessorsTranslation(runPhase));
		}

		private EntryPhaseTranslation(String phaseName, String runPhase, BaseCondition entryPhaseFilter) {
			this(phaseName, runPhase);
			if("snmp_map".equals(phaseName)) {
				processors.add(0, new SnmpConvertor());
			}
			if(null != entryPhaseFilter) {
				filter = getConditionTranslator(entryPhaseFilter).translateToHpJson(entryPhaseFilter);
			}
		}
	}

		// hardcoded SNMP processor

	private class SnmpConvertor extends ProcessorTranslation {
		private String array = "varbinds";
		private String datacolumn = "varbind_value";
		private String keycolumn = "varbind_oid";

		private SnmpConvertor() {
			clazz = "SnmpConvertor";
		}
	}

	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;
	}

	public Object entryPhaseTranslation(String entryPhase, String runPhase, BaseCondition entryPhaseFilter) {
		debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Start translating entry phase {}", entryPhase);
		Object translation = new EntryPhaseTranslation(entryPhase, runPhase, entryPhaseFilter);
		debugLogger.log(LogLevel.DEBUG, this.getClass().getName(), "Finished translation for entry phase {}. Result: {}", entryPhase, 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();
	}
}