Fix simulator topics for lower case
[policy/models.git] / models-sim / policy-models-simulators / src / main / java / org / onap / policy / models / simulators / Main.java
index a0b1655..a501d52 100644 (file)
@@ -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,6 +23,7 @@
 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;
@@ -29,6 +32,7 @@ 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;
@@ -43,11 +47,13 @@ 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.CdsSimulator;
 import org.onap.policy.simulators.TopicServer;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -80,17 +86,33 @@ public class Main extends ServiceManagerContainer {
         }
 
         DmaapSimParameterGroup dmaapProv = params.getDmaapProvider();
-        String dmaapName = dmaapProv.getName();
-        String provName = dmaapName.replace("simulator", "provider");
+        String dmaapName = (dmaapProv != null ? dmaapProv.getName() : null);
 
         // dmaap provider
-        AtomicReference<DmaapSimProvider> provRef = new AtomicReference<>();
-        addAction(provName, () -> provRef.set(buildDmaapProvider(dmaapProv)), () -> provRef.get().shutdown());
+        if (dmaapProv != null) {
+            String provName = dmaapName.replace("simulator", "provider");
+            AtomicReference<DmaapSimProvider> provRef = new AtomicReference<>();
+            addAction(provName, () -> provRef.set(buildDmaapProvider(dmaapProv)), () -> provRef.get().shutdown());
+        }
+
+        CdsServerParameters cdsServer = params.getGrpcServer();
+
+        // Cds Simulator
+        if (cdsServer != null) {
+            AtomicReference<CdsSimulator> cdsSim = new AtomicReference<>();
+            addAction(cdsServer.getName(), () -> cdsSim.set(buildCdsSimulator(cdsServer)), () -> cdsSim.get().stop());
+        }
 
         // REST server simulators
         // @formatter:off
         for (ClassRestServerParameters restsim : params.getRestServers()) {
             AtomicReference<HttpServletServer> 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());
@@ -127,11 +149,16 @@ public class Main extends ServiceManagerContainer {
     }
 
     /**
-     * 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");
@@ -147,7 +174,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));
             }
@@ -165,13 +192,20 @@ public class Main extends ServiceManagerContainer {
     }
 
     private DmaapSimProvider buildDmaapProvider(DmaapSimParameterGroup params) {
-        DmaapSimProvider prov = new DmaapSimProvider(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());
+        cdsSimulator.start();
+        return cdsSimulator;
+    }
+
+
     private TopicSink startSink(TopicParameters params) {
         TopicSink sink = TopicEndpointManager.getManager().addTopicSinks(List.of(params)).get(0);
         sink.start();
@@ -186,7 +220,7 @@ public class Main extends ServiceManagerContainer {
 
     private HttpServletServer buildRestServer(String dmaapName, ClassRestServerParameters params) {
         try {
-            Properties props = getServerProperties(dmaapName, params);
+            var props = getServerProperties(dmaapName, params);
             HttpServletServer testServer = makeServer(props);
             testServer.waitedStart(5000);
 
@@ -235,10 +269,10 @@ public class Main extends ServiceManagerContainer {
      * 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();
+        final var props = new Properties();
         props.setProperty(PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES, params.getName());
 
         final String svcpfx = PolicyEndPointProperties.PROPERTY_HTTP_SERVER_SERVICES + "." + params.getName();
@@ -253,9 +287,10 @@ 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())) {
+        if (dmaapName != null && dmaapName.equals(params.getName())) {
             props.setProperty(svcpfx + PolicyEndPointProperties.PROPERTY_HTTP_SERIALIZATION_PROVIDER,
                             String.join(",", CambriaMessageBodyHandler.class.getName(),
                                             GsonMessageBodyHandler.class.getName(),