Skip to content

Package: ConfiguredSimulation

ConfiguredSimulation

nameinstructionbranchcomplexitylinemethod
ConfiguredSimulation(String, GeneratorScheduler, Map, Map)
M: 18 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
close()
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
convertTopic(Configuration.Topic)
M: 14 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
createApplication(String)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
from(Configuration.Application, String)
M: 85 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 13 C: 0
0%
M: 1 C: 0
0%
lambda$from$0(Map, String, Configuration.Topic)
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%

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.time.Duration;
16: import java.util.HashMap;
17: import java.util.Map;
18: import java.util.Objects;
19: import java.util.Optional;
20:
21: import org.eclipse.kapua.kura.simulator.app.Application;
22: import org.eclipse.kapua.kura.simulator.app.ApplicationContext;
23: import org.eclipse.kapua.kura.simulator.app.Descriptor;
24: import org.eclipse.kapua.kura.simulator.app.Handler;
25: import org.eclipse.kapua.kura.simulator.app.data.PeriodicGenerator;
26: import org.eclipse.kapua.kura.simulator.app.data.PeriodicGenerator.Topic;
27: import org.eclipse.kapua.kura.simulator.generator.Generator;
28: import org.eclipse.kapua.kura.simulator.generator.GeneratorFactories;
29: import org.eclipse.kapua.kura.simulator.generator.GeneratorScheduler;
30:
31: public class ConfiguredSimulation implements Simulation {
32:
33: public static ConfiguredSimulation from(final Configuration.Application configuration, final String applicationId) {
34: Objects.requireNonNull(configuration);
35: Objects.requireNonNull(applicationId);
36:
37: // create generators
38:
39: final Map<String, Generator> generators = new HashMap<>(configuration.getGenerators().size());
40:
41:• for (final Map.Entry<String, Map<String, Object>> entry : configuration.getGenerators().entrySet()) {
42: final Optional<Generator> generator = GeneratorFactories.create(entry.getValue());
43:• if (!generator.isPresent()) {
44: throw new IllegalStateException(String.format("Unable to find generator factory for: " + entry.getValue()));
45: }
46: generators.put(entry.getKey(), generator.get());
47: }
48:
49: // create metric mappings
50:
51: final Map<String, Topic> topics = new HashMap<>(configuration.getTopics().size());
52: configuration.getTopics().forEach((key, topic) -> topics.put(key, convertTopic(topic)));
53:
54: // create scheduler
55:
56: final GeneratorScheduler scheduler = new GeneratorScheduler(Duration.ofMillis(configuration.getScheduler().getPeriod()));
57:
58: // return result
59:
60: return new ConfiguredSimulation(applicationId, scheduler, generators, topics);
61: }
62:
63: private static Topic convertTopic(final Configuration.Topic value) {
64:• if (value == null) {
65: return null;
66: }
67:
68: return new Topic(value.getPositionGenerator(), value.getBodyGenerator(), value.getMetrics());
69: }
70:
71: private final Descriptor descriptor;
72: private final GeneratorScheduler scheduler;
73:
74: private final Map<String, Generator> generators;
75: private final Map<String, Topic> topics;
76:
77: private ConfiguredSimulation(final String applicationId, final GeneratorScheduler scheduler, final Map<String, Generator> generators, final Map<String, Topic> topics) {
78: this.descriptor = new Descriptor(applicationId);
79: this.scheduler = scheduler;
80: this.generators = generators;
81: this.topics = topics;
82: }
83:
84: @Override
85: public void close() throws Exception {
86: this.scheduler.close();
87: }
88:
89: @Override
90: public Application createApplication(final String simulatorId) {
91: return new Application() {
92:
93: @Override
94: public Descriptor getDescriptor() {
95: return ConfiguredSimulation.this.descriptor;
96: }
97:
98: @Override
99: public Handler createHandler(final ApplicationContext context) {
100: return new PeriodicGenerator(context, ConfiguredSimulation.this.scheduler, ConfiguredSimulation.this.generators, ConfiguredSimulation.this.topics);
101: }
102: };
103: }
104:
105: }