aboutsummaryrefslogtreecommitdiffstats
path: root/models-sim/policy-models-simulators/src/main/java
diff options
context:
space:
mode:
Diffstat (limited to 'models-sim/policy-models-simulators/src/main/java')
-rw-r--r--models-sim/policy-models-simulators/src/main/java/org/onap/policy/models/simulators/Main.java118
-rw-r--r--models-sim/policy-models-simulators/src/main/java/org/onap/policy/models/simulators/SimulatorParameters.java52
-rw-r--r--models-sim/policy-models-simulators/src/main/java/org/onap/policy/models/simulators/TextMessageBodyHandler.java66
-rw-r--r--models-sim/policy-models-simulators/src/main/java/org/onap/policy/models/simulators/TopicServerParameters.java48
4 files changed, 74 insertions, 210 deletions
diff --git a/models-sim/policy-models-simulators/src/main/java/org/onap/policy/models/simulators/Main.java b/models-sim/policy-models-simulators/src/main/java/org/onap/policy/models/simulators/Main.java
index a501d5253..b1ee73942 100644
--- a/models-sim/policy-models-simulators/src/main/java/org/onap/policy/models/simulators/Main.java
+++ b/models-sim/policy-models-simulators/src/main/java/org/onap/policy/models/simulators/Main.java
@@ -24,21 +24,13 @@ package org.onap.policy.models.simulators;
import java.io.FileNotFoundException;
import java.io.IOException;
-import java.lang.reflect.InvocationTargetException;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicReference;
import lombok.AccessLevel;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;
-import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
-import org.onap.policy.common.endpoints.event.comm.TopicSink;
-import org.onap.policy.common.endpoints.event.comm.TopicSource;
import org.onap.policy.common.endpoints.http.server.HttpServletServer;
import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
-import org.onap.policy.common.endpoints.parameters.TopicParameters;
import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
import org.onap.policy.common.gson.GsonMessageBodyHandler;
import org.onap.policy.common.parameters.BeanValidationResult;
@@ -49,12 +41,7 @@ import org.onap.policy.common.utils.network.NetworkUtil;
import org.onap.policy.common.utils.resources.ResourceUtils;
import org.onap.policy.common.utils.services.Registry;
import org.onap.policy.common.utils.services.ServiceManagerContainer;
-import org.onap.policy.models.sim.dmaap.parameters.DmaapSimParameterGroup;
-import org.onap.policy.models.sim.dmaap.provider.DmaapSimProvider;
-import org.onap.policy.models.sim.dmaap.rest.CambriaMessageBodyHandler;
-import org.onap.policy.models.sim.dmaap.rest.TextMessageBodyHandler;
import org.onap.policy.simulators.CdsSimulator;
-import org.onap.policy.simulators.TopicServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -79,21 +66,7 @@ public class Main extends ServiceManagerContainer {
super(Main.class.getPackage().getName());
SimulatorParameters params = readParameters(paramFile);
- BeanValidationResult result = params.validate("simulators");
- if (!result.isValid()) {
- logger.error("invalid parameters:\n{}", result.getResult());
- throw new IllegalArgumentException("invalid simulator parameters");
- }
-
- DmaapSimParameterGroup dmaapProv = params.getDmaapProvider();
- String dmaapName = (dmaapProv != null ? dmaapProv.getName() : null);
-
- // dmaap provider
- if (dmaapProv != null) {
- String provName = dmaapName.replace("simulator", "provider");
- AtomicReference<DmaapSimProvider> provRef = new AtomicReference<>();
- addAction(provName, () -> provRef.set(buildDmaapProvider(dmaapProv)), () -> provRef.get().shutdown());
- }
+ String messageBroker = "models-sim";
CdsServerParameters cdsServer = params.getGrpcServer();
@@ -114,35 +87,7 @@ public class Main extends ServiceManagerContainer {
() -> Registry.unregister(resourceLocationId));
}
addAction(restsim.getName(),
- () -> ref.set(buildRestServer(dmaapName, restsim)),
- () -> ref.get().shutdown());
- }
-
- // NOTE: topics must be started AFTER the (dmaap) rest servers
-
- // topic sinks
- Map<String, TopicSink> sinks = new HashMap<>();
- for (TopicParameters topicParams : params.getTopicSinks()) {
- String topic = topicParams.getTopic();
- addAction("Sink " + topic,
- () -> sinks.put(topic, startSink(topicParams)),
- () -> sinks.get(topic).shutdown());
- }
-
- // topic sources
- Map<String, TopicSource> sources = new HashMap<>();
- for (TopicParameters topicParams : params.getTopicSources()) {
- String topic = topicParams.getTopic();
- addAction("Source " + topic,
- () -> sources.put(topic, startSource(topicParams)),
- () -> sources.get(topic).shutdown());
- }
-
- // topic server simulators
- for (TopicServerParameters topicsim : params.getTopicServers()) {
- AtomicReference<TopicServer<?>> ref = new AtomicReference<>();
- addAction(topicsim.getName(),
- () -> ref.set(buildTopicServer(topicsim, sinks, sources)),
+ () -> ref.set(buildRestServer(messageBroker, restsim)),
() -> ref.get().shutdown());
}
// @formatter:on
@@ -191,13 +136,6 @@ public class Main extends ServiceManagerContainer {
}
}
- private DmaapSimProvider buildDmaapProvider(DmaapSimParameterGroup params) {
- var prov = new DmaapSimProvider(params);
- DmaapSimProvider.setInstance(prov);
- prov.start();
- return prov;
- }
-
private CdsSimulator buildCdsSimulator(CdsServerParameters params) throws IOException {
var cdsSimulator = new CdsSimulator(params.getHost(), params.getPort(), params.getResourceLocation(),
params.getSuccessRepeatCount(), params.getRequestedResponseDelayMs());
@@ -206,21 +144,9 @@ public class Main extends ServiceManagerContainer {
}
- private TopicSink startSink(TopicParameters params) {
- TopicSink sink = TopicEndpointManager.getManager().addTopicSinks(List.of(params)).get(0);
- sink.start();
- return sink;
- }
-
- private TopicSource startSource(TopicParameters params) {
- TopicSource source = TopicEndpointManager.getManager().addTopicSources(List.of(params)).get(0);
- source.start();
- return source;
- }
-
- private HttpServletServer buildRestServer(String dmaapName, ClassRestServerParameters params) {
+ private HttpServletServer buildRestServer(String messageBroker, ClassRestServerParameters params) {
try {
- var props = getServerProperties(dmaapName, params);
+ var props = getServerProperties(messageBroker, params);
HttpServletServer testServer = makeServer(props);
testServer.waitedStart(5000);
@@ -239,30 +165,6 @@ public class Main extends ServiceManagerContainer {
}
}
- private TopicServer<?> buildTopicServer(TopicServerParameters params, Map<String, TopicSink> sinks,
- Map<String, TopicSource> sources) {
- try {
- // find the desired sink
- TopicSink sink = sinks.get(params.getSink());
- if (sink == null) {
- throw new IllegalArgumentException("invalid sink topic " + params.getSink());
- }
-
- // find the desired source
- TopicSource source = sources.get(params.getSource());
- if (source == null) {
- throw new IllegalArgumentException("invalid source topic " + params.getSource());
- }
-
- // create the topic server
- return (TopicServer<?>) Class.forName(params.getProviderClass())
- .getDeclaredConstructor(TopicSink.class, TopicSource.class).newInstance(sink, source);
-
- } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException
- | SecurityException | ClassNotFoundException e) {
- throw new IllegalArgumentException("cannot create TopicServer: " + params.getName(), e);
- }
- }
/**
* Creates a set of properties, suitable for building a REST server, from the
@@ -271,7 +173,7 @@ public class Main extends ServiceManagerContainer {
* @param params parameters from which to build the properties
* @return a Map of properties representing the given parameters
*/
- private static Properties getServerProperties(String dmaapName, ClassRestServerParameters params) {
+ private static Properties getServerProperties(String messageBroker, ClassRestServerParameters params) {
final var props = new Properties();
props.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES, params.getName());
@@ -290,15 +192,9 @@ public class Main extends ServiceManagerContainer {
props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SNI_HOST_CHECK_SUFFIX, "false");
props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "true");
- if (dmaapName != null && dmaapName.equals(params.getName())) {
- props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER,
- String.join(",", CambriaMessageBodyHandler.class.getName(),
- GsonMessageBodyHandler.class.getName(),
- TextMessageBodyHandler.class.getName()));
- } else {
- props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER, String.join(",",
+ props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER, String.join(",",
GsonMessageBodyHandler.class.getName(), TextMessageBodyHandler.class.getName()));
- }
+
return props;
}
diff --git a/models-sim/policy-models-simulators/src/main/java/org/onap/policy/models/simulators/SimulatorParameters.java b/models-sim/policy-models-simulators/src/main/java/org/onap/policy/models/simulators/SimulatorParameters.java
index 28c4f42d2..0ae3a17e7 100644
--- a/models-sim/policy-models-simulators/src/main/java/org/onap/policy/models/simulators/SimulatorParameters.java
+++ b/models-sim/policy-models-simulators/src/main/java/org/onap/policy/models/simulators/SimulatorParameters.java
@@ -4,6 +4,7 @@
* ================================================================================
* Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved.
* Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
+ * Modifications Copyright (C) 2024 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -24,12 +25,7 @@ package org.onap.policy.models.simulators;
import java.util.LinkedList;
import java.util.List;
import lombok.Getter;
-import org.onap.policy.common.endpoints.parameters.TopicParameters;
-import org.onap.policy.common.parameters.BeanValidationResult;
-import org.onap.policy.common.parameters.BeanValidator;
-import org.onap.policy.common.parameters.ValidationStatus;
import org.onap.policy.common.parameters.annotations.Valid;
-import org.onap.policy.models.sim.dmaap.parameters.DmaapSimParameterGroup;
/**
* Simulator parameters.
@@ -37,14 +33,6 @@ import org.onap.policy.models.sim.dmaap.parameters.DmaapSimParameterGroup;
@Getter
public class SimulatorParameters {
- /**
- * Note: this is only used to capture the provider's parameters; the rest server
- * parameters that it contains are ignored. Instead, the parameters for the rest
- * server are contained within the {@link #restServers} entry having the same name as
- * the provider parameters.
- */
- private DmaapSimParameterGroup dmaapProvider;
-
private @Valid CdsServerParameters grpcServer;
/**
@@ -52,43 +40,5 @@ public class SimulatorParameters {
*/
private List<@Valid ClassRestServerParameters> restServers = new LinkedList<>();
- /**
- * Topic sinks that are used by {@link #topicServers}.
- */
- private List<@Valid TopicParameters> topicSinks = new LinkedList<>();
-
- /**
- * Topic sources that are used by {@link #topicServers}.
- */
- private List<@Valid TopicParameters> topicSources = new LinkedList<>();
-
- /**
- * Parameters for the TOPIC server simulators that are to be started.
- */
- private List<@Valid TopicServerParameters> topicServers = new LinkedList<>();
-
-
- /**
- * Validates the parameters.
- *
- * @param containerName name of the parameter container
- * @return the validation result
- */
- public BeanValidationResult validate(String containerName) {
- BeanValidationResult result = new BeanValidator().validateTop(containerName, this);
-
- if (dmaapProvider != null) {
- // do not want full validation of the provider, so validate the relevant
- // fields ourselves
- var subResult = new BeanValidationResult("dmaapProvider", dmaapProvider);
- subResult.validateNotNull("name", dmaapProvider.getName());
- if (dmaapProvider.getTopicSweepSec() < 1) {
- subResult.addResult("topicSweepSec", dmaapProvider.getTopicSweepSec(),
- ValidationStatus.INVALID, "is below the minimum value: 1");
- }
- result.addResult(subResult);
- }
- return result;
- }
}
diff --git a/models-sim/policy-models-simulators/src/main/java/org/onap/policy/models/simulators/TextMessageBodyHandler.java b/models-sim/policy-models-simulators/src/main/java/org/onap/policy/models/simulators/TextMessageBodyHandler.java
new file mode 100644
index 000000000..a99a9383d
--- /dev/null
+++ b/models-sim/policy-models-simulators/src/main/java/org/onap/policy/models/simulators/TextMessageBodyHandler.java
@@ -0,0 +1,66 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP Policy Models
+ * ================================================================================
+ * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2023-2024 Nordix Foundation.
+ * ================================================================================
+ * 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.policy.models.simulators;
+
+import jakarta.ws.rs.Consumes;
+import jakarta.ws.rs.core.MediaType;
+import jakarta.ws.rs.core.MultivaluedMap;
+import jakarta.ws.rs.ext.MessageBodyReader;
+import jakarta.ws.rs.ext.Provider;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+import java.nio.charset.StandardCharsets;
+import java.util.LinkedList;
+import java.util.List;
+
+/**
+ * Provider that decodes "text/plain" messages.
+ */
+@Provider
+@Consumes(TextMessageBodyHandler.MEDIA_TYPE_TEXT_PLAIN)
+public class TextMessageBodyHandler implements MessageBodyReader<Object> {
+ public static final String MEDIA_TYPE_TEXT_PLAIN = "text/plain";
+
+ @Override
+ public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
+ return (mediaType != null && MEDIA_TYPE_TEXT_PLAIN.equals(mediaType.toString()));
+ }
+
+ @Override
+ public List<Object> readFrom(Class<Object> type, Type genericType, Annotation[] annotations, MediaType mediaType,
+ MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException {
+
+ try (var bufferedReader = new BufferedReader(new InputStreamReader(entityStream, StandardCharsets.UTF_8))) {
+ List<Object> messages = new LinkedList<>();
+ String msg;
+ while ((msg = bufferedReader.readLine()) != null) {
+ messages.add(msg);
+ }
+
+ return messages;
+ }
+ }
+}
diff --git a/models-sim/policy-models-simulators/src/main/java/org/onap/policy/models/simulators/TopicServerParameters.java b/models-sim/policy-models-simulators/src/main/java/org/onap/policy/models/simulators/TopicServerParameters.java
deleted file mode 100644
index 8a477b232..000000000
--- a/models-sim/policy-models-simulators/src/main/java/org/onap/policy/models/simulators/TopicServerParameters.java
+++ /dev/null
@@ -1,48 +0,0 @@
-/*-
- * ============LICENSE_START=======================================================
- * ONAP
- * ================================================================================
- * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
- * ================================================================================
- * 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.policy.models.simulators;
-
-import lombok.Getter;
-import org.onap.policy.common.parameters.BeanValidationResult;
-import org.onap.policy.common.parameters.BeanValidator;
-import org.onap.policy.common.parameters.annotations.NotBlank;
-import org.onap.policy.common.parameters.annotations.NotNull;
-
-@Getter
-@NotNull
-@NotBlank
-public class TopicServerParameters {
- private String name;
- private String providerClass;
- private String sink;
- private String source;
-
-
- /**
- * Validates the parameters.
- *
- * @param containerName name of the parameter container
- * @return the validation result
- */
- public BeanValidationResult validate(String containerName) {
- return new BeanValidator().validateTop(containerName, this);
- }
-}