Package: Configuration
Configuration
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Configuration() |
|
|
|
|
|
||||||||||||||||||||
getApplications() |
|
|
|
|
|
||||||||||||||||||||
lambda$validate$0(List, Configuration.Application) |
|
|
|
|
|
||||||||||||||||||||
setApplications(Map) |
|
|
|
|
|
||||||||||||||||||||
validate() |
|
|
|
|
|
Coverage
1: /*******************************************************************************
2: * Copyright (c) 2017, 2022 Red Hat Inc and others.
3: *
4: * This program and the accompanying materials are made
5: * available under the terms of the Eclipse Public License 2.0
6: * which is available at https://www.eclipse.org/legal/epl-2.0/
7: *
8: * SPDX-License-Identifier: EPL-2.0
9: *
10: * Contributors:
11: * Red Hat Inc - initial API and implementation
12: *******************************************************************************/
13: package org.eclipse.kapua.kura.simulator.simulation;
14:
15: import java.util.HashMap;
16: import java.util.LinkedList;
17: import java.util.List;
18: import java.util.Map;
19: import java.util.Objects;
20:
21: public class Configuration {
22:
23: public static final String DEFAULT_VALUE_NAME = "value";
24:
25: public static class Scheduler {
26:
27: private long period = 1000L;
28:
29: public void setPeriod(final long period) {
30: this.period = period;
31: }
32:
33: public long getPeriod() {
34: return this.period;
35: }
36:
37: private void validate(final List<Exception> violations) {
38: if (this.period <= 0) {
39: violations.add(new IllegalArgumentException(String.format("Scheduler period must be greater than zero: ", this.period)));
40: }
41: }
42: }
43:
44: public static class Topic {
45:
46: private String bodyGenerator;
47: private String positionGenerator;
48:
49: private Map<String, MetricsMapping> metrics = new HashMap<>();
50:
51: public void setBodyGenerator(final String bodyGenerator) {
52: this.bodyGenerator = bodyGenerator;
53: }
54:
55: public String getBodyGenerator() {
56: return this.bodyGenerator;
57: }
58:
59: public void setPositionGenerator(final String positionGenerator) {
60: this.positionGenerator = positionGenerator;
61: }
62:
63: public String getPositionGenerator() {
64: return this.positionGenerator;
65: }
66:
67: public void setMetrics(final Map<String, MetricsMapping> metrics) {
68: Objects.requireNonNull(metrics);
69: this.metrics = metrics;
70: }
71:
72: public Map<String, MetricsMapping> getMetrics() {
73: return this.metrics;
74: }
75:
76: private void validate(final Map<String, Map<String, Object>> generators, final String id, final List<Exception> violations) {
77:
78: if (this.bodyGenerator != null && !this.bodyGenerator.isEmpty()) {
79: validateGeneratorReference(generators, String.format("Topic %s/body", id), this.bodyGenerator, violations);
80: }
81:
82: if (this.positionGenerator != null && !this.positionGenerator.isEmpty()) {
83: validateGeneratorReference(generators, String.format("Topic %s/position", id), this.positionGenerator, violations);
84: }
85:
86: for (final Map.Entry<String, MetricsMapping> entry : this.metrics.entrySet()) {
87: final String generator = entry.getValue().getGenerator();
88: if (generator != null && !generator.isEmpty()) {
89: validateGeneratorReference(generators, String.format("Topic %s/Metric %s", id, entry.getKey()), generator, violations);
90: } else if (generators.size() != 1) {
91: violations.add(new IllegalStateException(
92: String.format("Topic %s/Metric %s uses 'default' generator which requires exactly one generation, but there are: %s", id, entry.getKey(), generators.size())));
93: }
94: }
95:
96: }
97:
98: private void validateGeneratorReference(final Map<String, Map<String, Object>> generators, final String location, final String generator, final List<Exception> violations) {
99: if (!generators.containsKey(generator)) {
100: violations.add(new IllegalStateException(String.format(" %s reference non-existing generator %s", location, generator)));
101: }
102: }
103:
104: }
105:
106: public static class MetricsMapping {
107:
108: private String generator;
109:
110: private String value = DEFAULT_VALUE_NAME;
111:
112: public MetricsMapping() {
113: }
114:
115: public MetricsMapping(final String generator, final String value) {
116: this.generator = generator;
117: this.value = value;
118: }
119:
120: public String getGenerator() {
121: return this.generator;
122: }
123:
124: public void setGenerator(final String generator) {
125: this.generator = generator;
126: }
127:
128: public String getValue() {
129: return this.value;
130: }
131:
132: public void setValue(final String value) {
133: this.value = value;
134: }
135: }
136:
137: public static class Application {
138:
139: private Scheduler scheduler = new Scheduler();
140:
141: private Map<String, Topic> topics = new HashMap<>();
142:
143: private Map<String, Map<String, Object>> generators = new HashMap<>();
144:
145: public void setScheduler(final Scheduler scheduler) {
146: Objects.requireNonNull(scheduler);
147: this.scheduler = scheduler;
148: }
149:
150: public Scheduler getScheduler() {
151: return this.scheduler;
152: }
153:
154: public void setGenerators(final Map<String, Map<String, Object>> generators) {
155: Objects.requireNonNull(generators);
156: this.generators = generators;
157: }
158:
159: public Map<String, Map<String, Object>> getGenerators() {
160: return this.generators;
161: }
162:
163: public void setTopics(final Map<String, Topic> topics) {
164: Objects.requireNonNull(topics);
165: this.topics = topics;
166: }
167:
168: public Map<String, Topic> getTopics() {
169: return this.topics;
170: }
171:
172: public void validate() throws IllegalStateException {
173: final List<Exception> violations = new LinkedList<>();
174:
175: validate(violations);
176:
177: if (!violations.isEmpty()) {
178: final IllegalStateException ex = new IllegalStateException("Configuration has validation errors");
179: violations.forEach(ex::addSuppressed);
180: throw ex;
181: }
182: }
183:
184: void validate(final List<Exception> violations) {
185: this.scheduler.validate(violations);
186: this.topics.forEach((id, topic) -> topic.validate(this.generators, id, violations));
187: }
188: }
189:
190: private Map<String, Application> applications = new HashMap<>();
191:
192: public void setApplications(final Map<String, Application> applications) {
193: Objects.requireNonNull(applications);
194: this.applications = applications;
195: }
196:
197: public Map<String, Application> getApplications() {
198: return this.applications;
199: }
200:
201: public void validate() throws IllegalStateException {
202: final List<Exception> violations = new LinkedList<>();
203:
204: this.applications.values().forEach(app -> app.validate(violations));
205:
206:• if (!violations.isEmpty()) {
207: final IllegalStateException ex = new IllegalStateException("Configuration has validation errors");
208: violations.forEach(ex::addSuppressed);
209: throw ex;
210: }
211: }
212: }