summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/aai/validation/ruledriven/configuration/RuleSection.java
blob: b03bcf398c7d0a48d629d8d73ace7ff519046699 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
 * ============LICENSE_START===================================================
 * Copyright (c) 2018 Amdocs
 * ============================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=====================================================
 */
package org.onap.aai.validation.ruledriven.configuration;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.onap.aai.validation.ruledriven.configuration.build.RuleBuilder;

/**
 * Rules Configuration: rule {} section.
 */
public class RuleSection {

	private String name;
	private boolean isGenericRule;
	private String description;
	private String category;
	private String errorMessage;
	private String type;
	private String objectId;
	private String severity;
	private List<String> attributes = new ArrayList<>();
	private List<String> fields = new ArrayList<>();
	private String expression;

	/**
	 * Rules may be defined within an entity {} section.
	 */
	public RuleSection() {
		isGenericRule = false;
	}

	/**
	 * @param attribute
	 */
	public void addAttribute(String attribute) {
		if (this.attributes == null) {
			this.attributes = new ArrayList<>();
		}
		this.attributes.add(attribute);
	}

	private void addAttributeMapping(String field) {
		if (this.fields == null) {
			this.fields = new ArrayList<>();
		}
		this.fields.add(field);
	}

	private void addAttributeNames(List<String> fieldNames) {
		for (String attribute : fieldNames) {
			addAttributeMapping(attribute);
		}
	}

	private void addAttributes(List<String> attributes) {
		for (String attribute : attributes) {
			addAttribute(attribute);
		}
	}

	/**
	 * Make a copy of a generically defined rule so that we can tailor it for our specific entity.
	 *
	 * @param genericRule
	 */
	public void copyFrom(RuleSection genericRule) {
		setCategory(genericRule.getCategory());
		setDescription(genericRule.getDescription());
		setSeverity(genericRule.getSeverity());
		setExpression(genericRule.getExpression());
		setErrorMessage(genericRule.getErrorMessage());
		if (genericRule.getAttributes() != null) {
			if (getAttributes().isEmpty()) {
				addAttributes(genericRule.getAttributes());
			} else {
				// Map the attributes
				addAttributeNames(genericRule.getAttributes());
			}
		}

		if (getAttributes().isEmpty()) {
			throw new IllegalArgumentException("No attributes defined");
		}
	}

	public List<String> getAttributes() {
		return new ArrayList<>(attributes);
	}

	public String getCategory() {
		return category;
	}

	public String getDescription() {
		return description;
	}

	public String getErrorMessage() {
		return errorMessage;
	}

	public String getExpression() {
		return expression;
	}

	public List<String> getExpressionFieldNames() {
		if (fields.size() < attributes.size()) {
			return attributes;
		} else {
			return fields;
		}
	}

	public String getName() {
		return name;
	}

	public String getType() {
		return type;
	}

	public String getObjectId() {
		return objectId;
	}

	public String getSeverity() {
		return severity;
	}

	public boolean isGeneric() {
		return isGenericRule;
	}

	public void setAttributes(List<String> attributes) {
		this.attributes = attributes;
	}

	public void setAttributes(String[] attributes) {
		this.attributes = new ArrayList<>(Arrays.asList(attributes));
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}

	public void setExpression(String expression) {
		this.expression = expression;
	}

	public void setIsGeneric(boolean isGenericRule) {
		this.isGenericRule = isGenericRule;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setObject(String object) {
		this.type = object;
	}

	public void setObjectId(String objectId) {
		this.objectId = objectId;
	}

	public void setSeverity(String severity) {
		this.severity = severity;
	}

	@Override
	public String toString() {
		return new RuleBuilder(this, "").toString();
	}

}