summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/sdc/dcae/composition/restmodels/ruleeditor/MappingRules.java
blob: a2842935b3b9a07bf00f6d15cdde2dceefa865d6 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package org.onap.sdc.dcae.composition.restmodels.ruleeditor;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.gson.Gson;

public class MappingRules {

	private String version;
	private String eventType;
	//US405978 set notifyOID
	private String notifyId;
	//US420764 support phases configuration
	private String entryPhase;
	private String publishPhase;
	//US441901 global filtering
	private BaseCondition filter;
	private Rules rules = new Rules();

	public String getEntryPhase() {
		return entryPhase;
	}

	public void setEntryPhase(String entryPhase) {
		this.entryPhase = entryPhase;
	}

	public String getPublishPhase() {
		return publishPhase;
	}

	public void setPublishPhase(String publishPhase) {
		this.publishPhase = publishPhase;
	}

	public Map<String, Rule> getRules() {
		return rules;
	}

	public void setRules(Map<String, Rule> rules) {
		this.rules = new Rules(rules);
	}

	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	public String getEventType() {
		return eventType;
	}

	public void setEventType(String eventType) {
		this.eventType = eventType;
	}

	public String getNotifyId() {
		return notifyId;
	}

	public void setNotifyId(String notifyId) {
		this.notifyId = notifyId;
	}

	public BaseCondition getFilter() {
		return filter;
	}

	public void setFilter(BaseCondition filter) {
		this.filter = filter;
	}


	public MappingRules(Rule rule) {
		version = rule.getVersion();
		eventType = rule.getEventType();
		addOrReplaceRule(rule);
	}

	public MappingRules(ApplyFilterRequest request) {
		version = request.getVersion();
		eventType = request.getEventType();
		filter = request.getFilter();
	}

	protected MappingRules(){}

	public void addOrReplaceRule(Rule rule) {
		rule.generateUid();
		rules.put(rule.getUid(), rule);
		//US405978 set notifyOID
		notifyId = rule.getNotifyId();
		//US420764 support phases configuration
		entryPhase = rule.getEntryPhase();
		publishPhase = rule.getPublishPhase();
	}

	public boolean ruleExists(Rule rule) {
		return rules.containsKey(rule.getUid());
	}

	@JsonIgnore
	public boolean isEmpty() {
		return rules.isEmpty();
	}

	public Rule removeRule(String ruleUid) {
		return rules.remove(ruleUid);
	}

	public String describe() {
		return "{version:"+version+",eventType:"+eventType+"}";
	}

	public byte[] convertToPayload() {
		return new Gson().toJson(this).getBytes();
	}


	@Override
	public boolean equals(Object obj) {
		if (obj == this) {
			return true;
		}
		if (null == obj || getClass() != obj.getClass()) {
			return false;
		}
		MappingRules other = (MappingRules) obj;
		return Objects.equals(version, other.version) && Objects.equals(eventType, other.eventType) && Objects.equals(rules, other.rules) && globalTranslationFieldsEqual(other);

	}

	private boolean globalTranslationFieldsEqual(MappingRules other) {
		return Objects.equals(notifyId, other.notifyId) && Objects.equals(entryPhase, other.entryPhase) && Objects.equals(publishPhase, other.publishPhase) && Objects.equals(filter, other.filter);
	}

	@Override
	public int hashCode() {
		return Objects.hash(version, notifyId, eventType, rules, entryPhase, publishPhase, filter);
	}

	private static class Rules extends LinkedHashMap<String, Rule> {
		private Rules(){}
		private Rules(Map<String,Rule> rules) {
			this.putAll(rules);
		}
	}
}