X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=models-sim%2Fpolicy-models-simulators%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fpolicy%2Fmodels%2Fsimulators%2FMain.java;h=b1ee739428a28ee418d1aa61b2384c4a0c5ea84b;hb=49f07db935d114b72a44e446867b16262dd552aa;hp=8333800f353a410c016a7b7888fc5fec61782fb1;hpb=3dfe17c58ef6980014da187a9e915841882ce01b;p=policy%2Fmodels.git 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 8333800f3..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 @@ -1,6 +1,8 @@ /*- * ============LICENSE_START======================================================= - * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2020-2021 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2020-2021 Bell Canada. All rights reserved. + * Modifications Copyright 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. @@ -21,18 +23,14 @@ package org.onap.policy.models.simulators; import java.io.FileNotFoundException; -import java.lang.reflect.InvocationTargetException; -import java.util.List; +import java.io.IOException; import java.util.Properties; import java.util.concurrent.atomic.AtomicReference; import lombok.AccessLevel; import lombok.Getter; -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.apache.commons.lang3.StringUtils; 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; @@ -41,12 +39,9 @@ import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.common.utils.coder.StandardCoder; 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.TopicServer; +import org.onap.policy.simulators.CdsSimulator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -71,59 +66,44 @@ 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.getName(); - String provName = dmaapName.replace("simulator", "provider"); + String messageBroker = "models-sim"; - // dmaap provider - AtomicReference provRef = new AtomicReference<>(); - addAction(provName, () -> provRef.set(buildDmaapProvider(dmaapProv)), () -> provRef.get().shutdown()); + CdsServerParameters cdsServer = params.getGrpcServer(); - // @formatter:off + // Cds Simulator + if (cdsServer != null) { + AtomicReference cdsSim = new AtomicReference<>(); + addAction(cdsServer.getName(), () -> cdsSim.set(buildCdsSimulator(cdsServer)), () -> cdsSim.get().stop()); + } // REST server simulators + // @formatter:off for (ClassRestServerParameters restsim : params.getRestServers()) { AtomicReference ref = new AtomicReference<>(); + if (StringUtils.isNotBlank(restsim.getResourceLocation())) { + String resourceLocationId = restsim.getProviderClass() + "_RESOURCE_LOCATION"; + addAction(resourceLocationId, + () -> Registry.register(resourceLocationId, restsim.getResourceLocation()), + () -> 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 - AtomicReference> sinkRef = new AtomicReference<>(); - addAction("topic sinks", () -> sinkRef.set(buildSinks(params.getTopicSinks())), - () -> shutdownSinks(sinkRef.get())); - - // topic sources - AtomicReference> sourceRef = new AtomicReference<>(); - addAction("topic sources", () -> sourceRef.set(buildSources(params.getTopicSources())), - () -> shutdownSources(sourceRef.get())); - - // topic server simulators - for (TopicServerParameters topicsim : params.getTopicServers()) { - AtomicReference> ref = new AtomicReference<>(); - addAction(topicsim.getName(), - () -> ref.set(buildTopicServer(topicsim, sinkRef.get(), sourceRef.get())), + () -> ref.set(buildRestServer(messageBroker, restsim)), () -> ref.get().shutdown()); } - // @formatter:on } /** - * The main method. + * The main method. The arguments are validated, thus adding the NOSONAR. * * @param args the arguments, the first of which is the name of the parameter file */ - public static void main(final String[] args) { + public static void main(final String[] args) { // NOSONAR + /* + * Only one argument is used and is validated implicitly by the constructor (i.e., + * file-not-found), thus sonar is disabled. + */ + try { if (args.length != 1) { throw new IllegalArgumentException("arg(s): parameter-file-name"); @@ -139,7 +119,7 @@ public class Main extends ServiceManagerContainer { private SimulatorParameters readParameters(String paramFile) { try { - String paramsJson = getResourceAsString(paramFile); + var paramsJson = getResourceAsString(paramFile); if (paramsJson == null) { throw new IllegalArgumentException(new FileNotFoundException(paramFile)); } @@ -156,33 +136,17 @@ public class Main extends ServiceManagerContainer { } } - private DmaapSimProvider buildDmaapProvider(DmaapSimParameterGroup params) { - DmaapSimProvider prov = new DmaapSimProvider(params); - DmaapSimProvider.setInstance(prov); - prov.start(); - - return prov; - } - - protected List buildSinks(List params) { - return TopicEndpointManager.getManager().addTopicSinks(params); + private CdsSimulator buildCdsSimulator(CdsServerParameters params) throws IOException { + var cdsSimulator = new CdsSimulator(params.getHost(), params.getPort(), params.getResourceLocation(), + params.getSuccessRepeatCount(), params.getRequestedResponseDelayMs()); + cdsSimulator.start(); + return cdsSimulator; } - private void shutdownSinks(List sinks) { - sinks.forEach(TopicSink::shutdown); - } - protected List buildSources(List params) { - return TopicEndpointManager.getManager().addTopicSources(params); - } - - private void shutdownSources(List sources) { - sources.forEach(TopicSource::shutdown); - } - - private HttpServletServer buildRestServer(String dmaapName, ClassRestServerParameters params) { + private HttpServletServer buildRestServer(String messageBroker, ClassRestServerParameters params) { try { - Properties props = getServerProperties(dmaapName, params); + var props = getServerProperties(messageBroker, params); HttpServletServer testServer = makeServer(props); testServer.waitedStart(5000); @@ -201,37 +165,16 @@ public class Main extends ServiceManagerContainer { } } - private TopicServer buildTopicServer(TopicServerParameters params, List sinks, - List sources) { - try { - // find the desired sink - TopicSink sink = sinks.stream().filter(sink2 -> sink2.getTopic().equals(params.getSink())).findAny() - .orElseThrow(() -> new IllegalArgumentException("invalid sink topic " + params.getSink())); - - // find the desired source - TopicSource source = sources.stream().filter(source2 -> source2.getTopic().equals(params.getSource())) - .findAny().orElseThrow(() -> 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 * parameters. * * @param params parameters from which to build the properties - * @return a set of properties representing the given parameters + * @return a Map of properties representing the given parameters */ - private static Properties getServerProperties(String dmaapName, ClassRestServerParameters params) { - final Properties props = new Properties(); + private static Properties getServerProperties(String messageBroker, ClassRestServerParameters params) { + final var props = new Properties(); props.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES, params.getName()); final String svcpfx = PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + params.getName(); @@ -246,17 +189,12 @@ public class Main extends ServiceManagerContainer { params.getProviderClass()); props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "false"); props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SWAGGER_SUFFIX, "false"); + props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SNI_HOST_CHECK_SUFFIX, "false"); props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX, "true"); - if (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; }