summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/sdc/dcae/composition/restmodels/ruleeditor/ActionDeserializer.java
blob: b8bfae535da3fa24c5cdcfc1c092cb425a4635b1 (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
package org.onap.sdc.dcae.composition.restmodels.ruleeditor;

import com.google.gson.*;
import java.lang.reflect.Type;

public class ActionDeserializer implements JsonDeserializer<BaseAction> {

	@Override
	public BaseAction deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
		String action;
		try {
			action = json.getAsJsonObject().get("actionType").getAsString();
		} catch (RuntimeException e) {
			throw new JsonParseException("Missing information : action type");
		}
		ActionTypeEnum actionTypeEnum = ActionTypeEnum.getTypeByName(action);
		if (null == actionTypeEnum) {
			throw new JsonParseException("Undefined action type: " + action);
		}
		Type clazz = getActionClassByActionTypeEnum(actionTypeEnum);
		return new Gson().fromJson(json, clazz);
	}

	private Type getActionClassByActionTypeEnum(ActionTypeEnum actionTypeEnum) {
		switch (actionTypeEnum) {
		case MAP:
			return MapAction.class;
		case DATE_FORMATTER:
			return DateFormatterAction.class;
		case LOG_EVENT:
			return LogEventAction.class;
		case LOG_TEXT:
			return LogTextAction.class;
		case CLEAR:
		case CLEAR_NSF:
			return UnaryFieldAction.class;
		case REPLACE_TEXT:
			return ReplaceTextAction.class;
		case HP_METRIC:
			return HpMetricAction.class;
		case STRING_TRANSFORM:
			return StringTransformAction.class;
		case TOPO_SEARCH:
			return TopoSearchAction.class;
		default:
			// suitable for copy/regex/concat
			return BaseCopyAction.class;
		}
	}

}