From 7fb6242e113efb3653c7846ffe7ac2959612daf3 Mon Sep 17 00:00:00 2001 From: "puthuparambil.aditya" Date: Wed, 23 Sep 2020 12:39:46 +0100 Subject: Enhance gRPC Simulator: 1.Make gRPC Simulator respond to different actions by generating the response on the basis of received request. 2.Include CDS Simulator in policy-models-simulator 3.Introduce a packages module in models-sim to package the docker image of policy-models-simulator which includes all the simulators(pdp-simulator to be added in future) in models. 4.The Jenkins job changes to push this newly created Docker image may be taken in the next release. Issue-ID: POLICY-2828 Signed-off-by: puthuparambil.aditya Change-Id: Ic1663bbe8205c64a8133ea0b0c28152f21d0732a --- .../org/onap/policy/simulators/CdsSimulator.java | 62 +++++++- .../simulators/cds/DefaultResponseEvent-error.json | 22 +++ .../simulators/cds/DefaultResponseEvent.json | 29 ++++ .../cds/pm_control-create-subscription-error.json | 22 +++ .../cds/pm_control-create-subscription.json | 29 ++++ .../onap/policy/simulators/CdsSimulatorTest.java | 17 +- .../simulators/cds/DefaultResponseEvent-error.json | 22 +++ .../simulators/cds/DefaultResponseEvent.json | 29 ++++ .../policy/simulators/cds/create-subscription.json | 29 ++++ .../cds/pm_control-create-subscription-error.json | 22 +++ .../cds/pm_control-create-subscription.json | 29 ++++ .../packages/models-simulator-docker/pom.xml | 174 +++++++++++++++++++++ .../src/main/docker/Dockerfile | 60 +++++++ .../src/main/docker/simulators.sh | 43 +++++ .../packages/models-simulator-tarball/pom.xml | 88 +++++++++++ .../models-simulator-tarball/src/main/assembly.xml | 50 ++++++ .../src/main/resources/ssl/policy-keystore | Bin 0 -> 4431 bytes .../src/main/resources/ssl/policy-truststore | Bin 0 -> 124180 bytes models-sim/packages/pom.xml | 62 ++++++++ .../models/simulators/CdsServerParameters.java | 97 ++++++++++++ .../org/onap/policy/models/simulators/Main.java | 18 ++- .../models/simulators/SimulatorParameters.java | 3 + .../models/simulators/CdsServerParametersTest.java | 46 ++++++ .../src/test/resources/simParameters.json | 14 +- models-sim/pom.xml | 2 + 25 files changed, 959 insertions(+), 10 deletions(-) create mode 100644 models-interactions/model-simulators/src/main/resources/org/onap/policy/simulators/cds/DefaultResponseEvent-error.json create mode 100644 models-interactions/model-simulators/src/main/resources/org/onap/policy/simulators/cds/DefaultResponseEvent.json create mode 100644 models-interactions/model-simulators/src/main/resources/org/onap/policy/simulators/cds/pm_control-create-subscription-error.json create mode 100644 models-interactions/model-simulators/src/main/resources/org/onap/policy/simulators/cds/pm_control-create-subscription.json create mode 100644 models-interactions/model-simulators/src/test/resources/org/onap/policy/simulators/cds/DefaultResponseEvent-error.json create mode 100644 models-interactions/model-simulators/src/test/resources/org/onap/policy/simulators/cds/DefaultResponseEvent.json create mode 100644 models-interactions/model-simulators/src/test/resources/org/onap/policy/simulators/cds/create-subscription.json create mode 100644 models-interactions/model-simulators/src/test/resources/org/onap/policy/simulators/cds/pm_control-create-subscription-error.json create mode 100644 models-interactions/model-simulators/src/test/resources/org/onap/policy/simulators/cds/pm_control-create-subscription.json create mode 100644 models-sim/packages/models-simulator-docker/pom.xml create mode 100644 models-sim/packages/models-simulator-docker/src/main/docker/Dockerfile create mode 100644 models-sim/packages/models-simulator-docker/src/main/docker/simulators.sh create mode 100644 models-sim/packages/models-simulator-tarball/pom.xml create mode 100644 models-sim/packages/models-simulator-tarball/src/main/assembly.xml create mode 100644 models-sim/packages/models-simulator-tarball/src/main/resources/ssl/policy-keystore create mode 100644 models-sim/packages/models-simulator-tarball/src/main/resources/ssl/policy-truststore create mode 100644 models-sim/packages/pom.xml create mode 100644 models-sim/policy-models-simulators/src/main/java/org/onap/policy/models/simulators/CdsServerParameters.java create mode 100644 models-sim/policy-models-simulators/src/test/java/org/onap/policy/models/simulators/CdsServerParametersTest.java diff --git a/models-interactions/model-simulators/src/main/java/org/onap/policy/simulators/CdsSimulator.java b/models-interactions/model-simulators/src/main/java/org/onap/policy/simulators/CdsSimulator.java index dbbaa1681..f1785c8ee 100644 --- a/models-interactions/model-simulators/src/main/java/org/onap/policy/simulators/CdsSimulator.java +++ b/models-interactions/model-simulators/src/main/java/org/onap/policy/simulators/CdsSimulator.java @@ -2,6 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2020 Nordix Foundation. * Modifications Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2020 Bell Canada. 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. @@ -28,13 +29,16 @@ import io.grpc.netty.NettyServerBuilder; import io.grpc.stub.StreamObserver; import java.io.IOException; import java.net.InetSocketAddress; -import java.nio.charset.StandardCharsets; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; import lombok.Getter; -import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; +import org.onap.ccsdk.cds.controllerblueprints.common.api.ActionIdentifiers; import org.onap.ccsdk.cds.controllerblueprints.processing.api.BluePrintProcessingServiceGrpc.BluePrintProcessingServiceImplBase; import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput; import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput; import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput.Builder; +import org.onap.policy.common.utils.resources.ResourceUtils; public class CdsSimulator { @Getter @@ -42,6 +46,10 @@ public class CdsSimulator { private final Server server; + private final String resourceLocation; + + private AtomicInteger countOfEvents = new AtomicInteger(1); + /** * Constructs the object, but does not start it. * @@ -49,7 +57,21 @@ public class CdsSimulator { * @param port port of the server */ public CdsSimulator(String host, int port) { + this(host, port, "org/onap/policy/simulators/cds/", 0, 0); + } + + /** + * Constructs the object, but does not start it. + * + * @param host host name of the server + * @param port port of the server + * @param countOfSuccesfulEvents number of successive successful events + * @param requestedResponseDelayMs time for the request to be processed + */ + public CdsSimulator(String host, int port, String resourceLocation, int countOfSuccesfulEvents, + long requestedResponseDelayMs) { this.port = port; + this.resourceLocation = resourceLocation; BluePrintProcessingServiceImplBase testCdsBlueprintServerImpl = new BluePrintProcessingServiceImplBase() { @@ -62,18 +84,17 @@ public class CdsSimulator { @Override public void onNext(final ExecutionServiceInput executionServiceInput) { try { - String responseString = IOUtils.toString( - getClass().getResource("cds/CreateSubscriptionResponseEvent.json"), - StandardCharsets.UTF_8); + String responseString = getResponseString(executionServiceInput, countOfSuccesfulEvents); Builder builder = ExecutionServiceOutput.newBuilder(); JsonFormat.parser().ignoringUnknownFields().merge(responseString, builder); + TimeUnit.MILLISECONDS.sleep(requestedResponseDelayMs); responseObserver.onNext(builder.build()); - } catch (InvalidProtocolBufferException e) { throw new SimulatorRuntimeException("Cannot convert ExecutionServiceOutput output", e); - } catch (IOException e) { throw new SimulatorRuntimeException("Cannot read ExecutionServiceOutput from file", e); + } catch (InterruptedException e) { + throw new SimulatorRuntimeException("Execution Interrupted", e); } } @@ -101,4 +122,31 @@ public class CdsSimulator { public void stop() { server.shutdown(); } + + /** + * Constructs the ResponseString on the basis of request. + * + * @param executionServiceInput service input + * @param countOfSuccesfulEvents number of successive successful events + * @return responseString + */ + public String getResponseString(ExecutionServiceInput executionServiceInput, int countOfSuccesfulEvents) { + String resourceName = "DefaultResponseEvent"; + if (!StringUtils.isBlank(executionServiceInput.getActionIdentifiers().getActionName())) { + ActionIdentifiers actionIdentifiers = executionServiceInput.getActionIdentifiers(); + resourceName = actionIdentifiers.getBlueprintName() + "-" + actionIdentifiers.getActionName(); + } + if (countOfSuccesfulEvents > 0 && countOfEvents.getAndIncrement() % countOfSuccesfulEvents == 0) { + // generating the failure response + resourceName = resourceName + "-error.json"; + } else { + resourceName = resourceName + ".json"; + } + String responseString = ResourceUtils.getResourceAsString(resourceLocation + resourceName); + if (responseString == null) { + responseString = ResourceUtils.getResourceAsString(resourceLocation + + "DefaultResponseEvent.json"); + } + return responseString; + } } diff --git a/models-interactions/model-simulators/src/main/resources/org/onap/policy/simulators/cds/DefaultResponseEvent-error.json b/models-interactions/model-simulators/src/main/resources/org/onap/policy/simulators/cds/DefaultResponseEvent-error.json new file mode 100644 index 000000000..a66b1114f --- /dev/null +++ b/models-interactions/model-simulators/src/main/resources/org/onap/policy/simulators/cds/DefaultResponseEvent-error.json @@ -0,0 +1,22 @@ +{ + "commonHeader": { + "timestamp": "2020-03-20T14:00:25.217Z", + "requestId": "123456-1000", + "subRequestId": "sub-123456-1000", + "flag": { + }, + "originatorId": "sdnc" + }, + "actionIdentifiers": { + "blueprintName": "pm_control", + "blueprintVersion": "1.0.0", + "actionName": "create-subscription", + "mode": "sync" + }, + "status": { + "code": 500, + "message": "failure", + "eventType": "EVENT_COMPONENT_FAILURE", + "timestamp": "Fri Mar 20 14:00:26 GMT 2020" + } +} \ No newline at end of file diff --git a/models-interactions/model-simulators/src/main/resources/org/onap/policy/simulators/cds/DefaultResponseEvent.json b/models-interactions/model-simulators/src/main/resources/org/onap/policy/simulators/cds/DefaultResponseEvent.json new file mode 100644 index 000000000..adb51adcb --- /dev/null +++ b/models-interactions/model-simulators/src/main/resources/org/onap/policy/simulators/cds/DefaultResponseEvent.json @@ -0,0 +1,29 @@ +{ + "commonHeader": { + "timestamp": "2020-03-20T14:00:25.217Z", + "requestId": "123456-1000", + "subRequestId": "sub-123456-1000", + "flag": { + }, + "originatorId": "sdnc" + }, + "actionIdentifiers": { + "blueprintName": "pm_control", + "blueprintVersion": "1.0.0", + "actionName": "create-subscription", + "mode": "sync" + }, + "status": { + "code": 200, + "message": "success", + "eventType": "EVENT_COMPONENT_EXECUTED", + "timestamp": "Fri Mar 20 14:00:26 GMT 2020" + }, + "payload": { + "create-subscription-response": { + "odl-response": { + "status": "success" + } + } + } +} \ No newline at end of file diff --git a/models-interactions/model-simulators/src/main/resources/org/onap/policy/simulators/cds/pm_control-create-subscription-error.json b/models-interactions/model-simulators/src/main/resources/org/onap/policy/simulators/cds/pm_control-create-subscription-error.json new file mode 100644 index 000000000..a66b1114f --- /dev/null +++ b/models-interactions/model-simulators/src/main/resources/org/onap/policy/simulators/cds/pm_control-create-subscription-error.json @@ -0,0 +1,22 @@ +{ + "commonHeader": { + "timestamp": "2020-03-20T14:00:25.217Z", + "requestId": "123456-1000", + "subRequestId": "sub-123456-1000", + "flag": { + }, + "originatorId": "sdnc" + }, + "actionIdentifiers": { + "blueprintName": "pm_control", + "blueprintVersion": "1.0.0", + "actionName": "create-subscription", + "mode": "sync" + }, + "status": { + "code": 500, + "message": "failure", + "eventType": "EVENT_COMPONENT_FAILURE", + "timestamp": "Fri Mar 20 14:00:26 GMT 2020" + } +} \ No newline at end of file diff --git a/models-interactions/model-simulators/src/main/resources/org/onap/policy/simulators/cds/pm_control-create-subscription.json b/models-interactions/model-simulators/src/main/resources/org/onap/policy/simulators/cds/pm_control-create-subscription.json new file mode 100644 index 000000000..adb51adcb --- /dev/null +++ b/models-interactions/model-simulators/src/main/resources/org/onap/policy/simulators/cds/pm_control-create-subscription.json @@ -0,0 +1,29 @@ +{ + "commonHeader": { + "timestamp": "2020-03-20T14:00:25.217Z", + "requestId": "123456-1000", + "subRequestId": "sub-123456-1000", + "flag": { + }, + "originatorId": "sdnc" + }, + "actionIdentifiers": { + "blueprintName": "pm_control", + "blueprintVersion": "1.0.0", + "actionName": "create-subscription", + "mode": "sync" + }, + "status": { + "code": 200, + "message": "success", + "eventType": "EVENT_COMPONENT_EXECUTED", + "timestamp": "Fri Mar 20 14:00:26 GMT 2020" + }, + "payload": { + "create-subscription-response": { + "odl-response": { + "status": "success" + } + } + } +} \ No newline at end of file diff --git a/models-interactions/model-simulators/src/test/java/org/onap/policy/simulators/CdsSimulatorTest.java b/models-interactions/model-simulators/src/test/java/org/onap/policy/simulators/CdsSimulatorTest.java index 5f82b7e94..fea03e3e4 100644 --- a/models-interactions/model-simulators/src/test/java/org/onap/policy/simulators/CdsSimulatorTest.java +++ b/models-interactions/model-simulators/src/test/java/org/onap/policy/simulators/CdsSimulatorTest.java @@ -3,6 +3,7 @@ * ONAP * ================================================================================ * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2020 Bell Canada. 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. @@ -28,11 +29,13 @@ import io.grpc.internal.DnsNameResolverProvider; import io.grpc.internal.PickFirstLoadBalancerProvider; import io.grpc.netty.NettyChannelBuilder; import io.grpc.stub.StreamObserver; +import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import org.apache.commons.io.IOUtils; +import org.json.simple.parser.ParseException; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -40,7 +43,9 @@ import org.onap.ccsdk.cds.controllerblueprints.processing.api.BluePrintProcessin import org.onap.ccsdk.cds.controllerblueprints.processing.api.BluePrintProcessingServiceGrpc.BluePrintProcessingServiceStub; import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput; import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput; +import org.onap.policy.common.utils.coder.CoderException; import org.onap.policy.common.utils.coder.StandardCoder; +import org.onap.policy.common.utils.resources.ResourceUtils; public class CdsSimulatorTest { private static final StandardCoder coder = new StandardCoder(); @@ -61,7 +66,6 @@ public class CdsSimulatorTest { public void test() throws Exception { String reqstr = IOUtils.toString(getClass().getResource("cds/cds.request.json"), StandardCharsets.UTF_8); ExecutionServiceInput request = coder.decode(reqstr, ExecutionServiceInput.class); - ManagedChannel channel = NettyChannelBuilder.forAddress("localhost", sim.getPort()) .nameResolverFactory(new DnsNameResolverProvider()) .loadBalancerFactory(new PickFirstLoadBalancerProvider()).usePlaintext().build(); @@ -111,4 +115,15 @@ public class CdsSimulatorTest { channel.shutdown(); } } + + @Test + public void testGetResponseString() throws IOException, CoderException, ParseException { + CdsSimulator cdsSimulator = new CdsSimulator("localhost", sim.getPort()); + String reqstr = ResourceUtils.getResourceAsString( + "org/onap/policy/simulators/cds/cds.request.json"); + String responseqstr = ResourceUtils.getResourceAsString( + "org/onap/policy/simulators/cds/pm_control-create-subscription.json"); + ExecutionServiceInput request = coder.decode(reqstr, ExecutionServiceInput.class); + assertEquals(responseqstr, cdsSimulator.getResponseString(request, 0)); + } } diff --git a/models-interactions/model-simulators/src/test/resources/org/onap/policy/simulators/cds/DefaultResponseEvent-error.json b/models-interactions/model-simulators/src/test/resources/org/onap/policy/simulators/cds/DefaultResponseEvent-error.json new file mode 100644 index 000000000..a66b1114f --- /dev/null +++ b/models-interactions/model-simulators/src/test/resources/org/onap/policy/simulators/cds/DefaultResponseEvent-error.json @@ -0,0 +1,22 @@ +{ + "commonHeader": { + "timestamp": "2020-03-20T14:00:25.217Z", + "requestId": "123456-1000", + "subRequestId": "sub-123456-1000", + "flag": { + }, + "originatorId": "sdnc" + }, + "actionIdentifiers": { + "blueprintName": "pm_control", + "blueprintVersion": "1.0.0", + "actionName": "create-subscription", + "mode": "sync" + }, + "status": { + "code": 500, + "message": "failure", + "eventType": "EVENT_COMPONENT_FAILURE", + "timestamp": "Fri Mar 20 14:00:26 GMT 2020" + } +} \ No newline at end of file diff --git a/models-interactions/model-simulators/src/test/resources/org/onap/policy/simulators/cds/DefaultResponseEvent.json b/models-interactions/model-simulators/src/test/resources/org/onap/policy/simulators/cds/DefaultResponseEvent.json new file mode 100644 index 000000000..adb51adcb --- /dev/null +++ b/models-interactions/model-simulators/src/test/resources/org/onap/policy/simulators/cds/DefaultResponseEvent.json @@ -0,0 +1,29 @@ +{ + "commonHeader": { + "timestamp": "2020-03-20T14:00:25.217Z", + "requestId": "123456-1000", + "subRequestId": "sub-123456-1000", + "flag": { + }, + "originatorId": "sdnc" + }, + "actionIdentifiers": { + "blueprintName": "pm_control", + "blueprintVersion": "1.0.0", + "actionName": "create-subscription", + "mode": "sync" + }, + "status": { + "code": 200, + "message": "success", + "eventType": "EVENT_COMPONENT_EXECUTED", + "timestamp": "Fri Mar 20 14:00:26 GMT 2020" + }, + "payload": { + "create-subscription-response": { + "odl-response": { + "status": "success" + } + } + } +} \ No newline at end of file diff --git a/models-interactions/model-simulators/src/test/resources/org/onap/policy/simulators/cds/create-subscription.json b/models-interactions/model-simulators/src/test/resources/org/onap/policy/simulators/cds/create-subscription.json new file mode 100644 index 000000000..adb51adcb --- /dev/null +++ b/models-interactions/model-simulators/src/test/resources/org/onap/policy/simulators/cds/create-subscription.json @@ -0,0 +1,29 @@ +{ + "commonHeader": { + "timestamp": "2020-03-20T14:00:25.217Z", + "requestId": "123456-1000", + "subRequestId": "sub-123456-1000", + "flag": { + }, + "originatorId": "sdnc" + }, + "actionIdentifiers": { + "blueprintName": "pm_control", + "blueprintVersion": "1.0.0", + "actionName": "create-subscription", + "mode": "sync" + }, + "status": { + "code": 200, + "message": "success", + "eventType": "EVENT_COMPONENT_EXECUTED", + "timestamp": "Fri Mar 20 14:00:26 GMT 2020" + }, + "payload": { + "create-subscription-response": { + "odl-response": { + "status": "success" + } + } + } +} \ No newline at end of file diff --git a/models-interactions/model-simulators/src/test/resources/org/onap/policy/simulators/cds/pm_control-create-subscription-error.json b/models-interactions/model-simulators/src/test/resources/org/onap/policy/simulators/cds/pm_control-create-subscription-error.json new file mode 100644 index 000000000..a66b1114f --- /dev/null +++ b/models-interactions/model-simulators/src/test/resources/org/onap/policy/simulators/cds/pm_control-create-subscription-error.json @@ -0,0 +1,22 @@ +{ + "commonHeader": { + "timestamp": "2020-03-20T14:00:25.217Z", + "requestId": "123456-1000", + "subRequestId": "sub-123456-1000", + "flag": { + }, + "originatorId": "sdnc" + }, + "actionIdentifiers": { + "blueprintName": "pm_control", + "blueprintVersion": "1.0.0", + "actionName": "create-subscription", + "mode": "sync" + }, + "status": { + "code": 500, + "message": "failure", + "eventType": "EVENT_COMPONENT_FAILURE", + "timestamp": "Fri Mar 20 14:00:26 GMT 2020" + } +} \ No newline at end of file diff --git a/models-interactions/model-simulators/src/test/resources/org/onap/policy/simulators/cds/pm_control-create-subscription.json b/models-interactions/model-simulators/src/test/resources/org/onap/policy/simulators/cds/pm_control-create-subscription.json new file mode 100644 index 000000000..adb51adcb --- /dev/null +++ b/models-interactions/model-simulators/src/test/resources/org/onap/policy/simulators/cds/pm_control-create-subscription.json @@ -0,0 +1,29 @@ +{ + "commonHeader": { + "timestamp": "2020-03-20T14:00:25.217Z", + "requestId": "123456-1000", + "subRequestId": "sub-123456-1000", + "flag": { + }, + "originatorId": "sdnc" + }, + "actionIdentifiers": { + "blueprintName": "pm_control", + "blueprintVersion": "1.0.0", + "actionName": "create-subscription", + "mode": "sync" + }, + "status": { + "code": 200, + "message": "success", + "eventType": "EVENT_COMPONENT_EXECUTED", + "timestamp": "Fri Mar 20 14:00:26 GMT 2020" + }, + "payload": { + "create-subscription-response": { + "odl-response": { + "status": "success" + } + } + } +} \ No newline at end of file diff --git a/models-sim/packages/models-simulator-docker/pom.xml b/models-sim/packages/models-simulator-docker/pom.xml new file mode 100644 index 000000000..c6f4717c0 --- /dev/null +++ b/models-sim/packages/models-simulator-docker/pom.xml @@ -0,0 +1,174 @@ + + + + 4.0.0 + + org.onap.policy.models.sim + policy-models-sim + 2.3.2-SNAPSHOT + + + models-simulator-docker + pom + + ${project.artifactId} + The module for creating docker images of Simulators component. + + + UTF-8 + UTF-8 + ${project.version} + false + false + false + nexus3.onap.org:10001 + nexus3.onap.org:10003 + yyyyMMdd'T'HHmm + + + + ${project.artifactId}-${project.version} + + + org.codehaus.groovy.maven + gmaven-plugin + 1.0 + + + validate + + execute + + + + println 'Project version: ' + project.properties['dist.project.version'] + if (project.properties['dist.project.version'] != null) { + def versionArray = project.properties['dist.project.version'].split('-') + def minMaxVersionArray = versionArray[0].tokenize('.') + if (project.properties['dist.project.version'].endsWith("-SNAPSHOT")) { + project.properties['project.docker.latest.minmax.tag.version'] = + minMaxVersionArray[0] + "." + minMaxVersionArray[1] + "-SNAPSHOT-latest" + } else { + project.properties['project.docker.latest.minmax.tag.version'] = + minMaxVersionArray[0] + "." + minMaxVersionArray[1] + "-STAGING-latest" + } + println 'New tag for docker: ' + project.properties['project.docker.latest.minmax.tag.version'] + } + + + + + + + + io.fabric8 + docker-maven-plugin + + + true + 1.23 + ${docker.pull.registry} + ${docker.push.registry} + + + + onap/policy-models-simulator + + try + Dockerfile + + ${project.version} + ${project.version}-${maven.build.timestamp} + ${project.docker.latest.minmax.tag.version} + + + + + + + org.onap.policy.models.sim:models-simulator-tarball + + /lib + models-simulator.tar.gz + + + + + + + + + + + + clean-images + pre-clean + + remove + + + true + + + + + generate-images + generate-sources + + build + + + + + push-images + deploy + + build + push + + + onap/policy-models-simulator + + + + + + + org.apache.maven.plugins + maven-deploy-plugin + + true + + + + + + + + org.onap.policy.models.sim + models-simulator-tarball + 2.3.2-SNAPSHOT + tarball + tar.gz + + + diff --git a/models-sim/packages/models-simulator-docker/src/main/docker/Dockerfile b/models-sim/packages/models-simulator-docker/src/main/docker/Dockerfile new file mode 100644 index 000000000..a4420b343 --- /dev/null +++ b/models-sim/packages/models-simulator-docker/src/main/docker/Dockerfile @@ -0,0 +1,60 @@ +# +# ============LICENSE_START======================================================= +# ONAP +# ================================================================================ +# Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. +# Modifications Copyright (C) 2020 Bell Canada. 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. +# +# SPDX-License-Identifier: Apache-2.0 +# ============LICENSE_END========================================================= +# + +# +# Docker file to build an image that runs the simulators +# + +FROM onap/policy-jre-alpine:2.1.1 + +LABEL maintainer="Policy Team" + +ARG POLICY_LOGS=/var/log/onap/policy/simulators + +ENV POLICY_HOME /opt/app/policy +ENV POLICY_LOGS ${POLICY_LOGS} + +# Create DMaaP simulator user and group +# Add simulator-specific directories and set ownership as the simulator user +RUN mkdir -p ${POLICY_HOME}/simulators \ + && mkdir -p ${POLICY_HOME}/simulators/bin \ + && mkdir -p ${POLICY_LOGS} \ + && mkdir /packages + +# Unpack the tarball +COPY /maven/* /packages +RUN tar xvfz /packages/models-simulator.tar.gz --directory ${POLICY_HOME}/simulators \ + && rm /packages/models-simulator.tar.gz + +# Ensure everything has the correct permissions +# Copy scripts simulator user area +COPY simulators.sh ${POLICY_HOME}/simulators/bin +RUN find /opt/app -type d -perm 755 \ + && find /opt/app -type f -perm 644 \ + && chmod 755 ${POLICY_HOME}/simulators/bin/* \ + && chown -R policy:policy $POLICY_HOME $POLICY_LOGS + +USER policy:policy + +ENV PATH ${POLICY_HOME}/simulators/bin:$PATH +ENTRYPOINT [ "bash", "simulators.sh" ] diff --git a/models-sim/packages/models-simulator-docker/src/main/docker/simulators.sh b/models-sim/packages/models-simulator-docker/src/main/docker/simulators.sh new file mode 100644 index 000000000..1cec646de --- /dev/null +++ b/models-sim/packages/models-simulator-docker/src/main/docker/simulators.sh @@ -0,0 +1,43 @@ +#!/bin/bash +# +# ============LICENSE_START======================================================= +# ONAP +# ================================================================================ +# Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. +# Modifications copyright (C) 2020 Bell Canada. 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. +# +# SPDX-License-Identifier: Apache-2.0 +# ============LICENSE_END========================================================= +# + +if [ -z "$SIMULATOR_HOME" ] +then + SIMULATOR_HOME=${POLICY_HOME}/simulators +fi + +JAVA_HOME=/usr/lib/jvm/java-11-openjdk +KEYSTORE="${SIMULATOR_HOME}/etc/ssl/policy-keystore" +KEYSTORE_PASSWD="Pol1cy_0nap" +TRUSTSTORE="${SIMULATOR_HOME}/etc/ssl/policy-truststore" +TRUSTSTORE_PASSWD="Pol1cy_0nap" + +${JAVA_HOME}/bin/java \ + -cp "${SIMULATOR_HOME}/etc:${SIMULATOR_HOME}/lib/*" \ + -Djavax.net.ssl.keyStore="${KEYSTORE}" \ + -Djavax.net.ssl.keyStorePassword="${KEYSTORE_PASSWD}" \ + -Djavax.net.ssl.trustStore="${TRUSTSTORE}" \ + -Djavax.net.ssl.trustStorePassword="${TRUSTSTORE_PASSWD}" \ + org.onap.policy.models.simulators.Main \ + ${SIMULATOR_HOME}/etc/mounted/simParameters.json diff --git a/models-sim/packages/models-simulator-tarball/pom.xml b/models-sim/packages/models-simulator-tarball/pom.xml new file mode 100644 index 000000000..9fb1fb5fd --- /dev/null +++ b/models-sim/packages/models-simulator-tarball/pom.xml @@ -0,0 +1,88 @@ + + + + 4.0.0 + + org.onap.policy.models.sim + policy-models-sim + 2.3.2-SNAPSHOT + + + models-simulator-tarball + + ${project.artifactId} + A module that runs all "simulators", including DMaaP. + + + + org.onap.policy.models.sim + policy-models-simulators + 2.3.2-SNAPSHOT + + + + + + + + src/main/resources + true + + **/version.txt + + + + src/main/resources + false + + **/version.txt + + + + + + + org.apache.maven.plugins + maven-assembly-plugin + + + generate-complete-tar + package + + single + + + + src/main/assembly.xml + + ${project.artifactId}-${project.version} + + + + + + + + diff --git a/models-sim/packages/models-simulator-tarball/src/main/assembly.xml b/models-sim/packages/models-simulator-tarball/src/main/assembly.xml new file mode 100644 index 000000000..9a2fd65df --- /dev/null +++ b/models-sim/packages/models-simulator-tarball/src/main/assembly.xml @@ -0,0 +1,50 @@ + + + + tarball + + tar.gz + + false + + + true + /lib + false + runtime + + *:jar + + + + + + ${project.basedir}/src/main/resources/ssl + + + policy* + + etc${file.separator}ssl + keep + + + diff --git a/models-sim/packages/models-simulator-tarball/src/main/resources/ssl/policy-keystore b/models-sim/packages/models-simulator-tarball/src/main/resources/ssl/policy-keystore new file mode 100644 index 000000000..389df5fe5 Binary files /dev/null and b/models-sim/packages/models-simulator-tarball/src/main/resources/ssl/policy-keystore differ diff --git a/models-sim/packages/models-simulator-tarball/src/main/resources/ssl/policy-truststore b/models-sim/packages/models-simulator-tarball/src/main/resources/ssl/policy-truststore new file mode 100644 index 000000000..8834ac257 Binary files /dev/null and b/models-sim/packages/models-simulator-tarball/src/main/resources/ssl/policy-truststore differ diff --git a/models-sim/packages/pom.xml b/models-sim/packages/pom.xml new file mode 100644 index 000000000..14922559d --- /dev/null +++ b/models-sim/packages/pom.xml @@ -0,0 +1,62 @@ + + + + 4.0.0 + + org.onap.policy.models.sim + policy-models-sim + 2.3.2-SNAPSHOT + + + models-sim-packages + pom + + ${project.artifactId} + The module for packaging the policy-models-simulator component. + + + + true + + + + + default + + true + + + models-simulator-tarball + + + + docker + + models-simulator-tarball + models-simulator-docker + + + false + + + + diff --git a/models-sim/policy-models-simulators/src/main/java/org/onap/policy/models/simulators/CdsServerParameters.java b/models-sim/policy-models-simulators/src/main/java/org/onap/policy/models/simulators/CdsServerParameters.java new file mode 100644 index 000000000..97dc35449 --- /dev/null +++ b/models-sim/policy-models-simulators/src/main/java/org/onap/policy/models/simulators/CdsServerParameters.java @@ -0,0 +1,97 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2020 Bell Canada. + * ================================================================================ + * 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 java.nio.charset.StandardCharsets; +import java.util.Base64; +import lombok.Getter; +import lombok.Setter; +import lombok.ToString; +import org.onap.policy.common.parameters.GroupValidationResult; +import org.onap.policy.common.parameters.ParameterGroup; +import org.onap.policy.common.parameters.ParameterRuntimeException; +import org.onap.policy.common.parameters.annotations.Max; +import org.onap.policy.common.parameters.annotations.Min; +import org.onap.policy.common.parameters.annotations.NotNull; + +@Getter +@Setter +@ToString +public class CdsServerParameters implements ParameterGroup { + + // Port range constants + private static final int MIN_USER_PORT = 1024; + private static final int MAX_USER_PORT = 65535; + + private static final String SERVER_PROPERTIES_TYPE = "CDS gRPC Server Properties"; + + // CDS carrier properties + + // Request timeout in seconds + @Min(value = 1) + private int timeout; + + @Min(value = MIN_USER_PORT) + @Max(value = MAX_USER_PORT) + private int port; + + @Min(value = 0) + private int successRepeatCount; + + private int requestedResponseDelayMs; + + @NotNull + private String host; + + @NotNull + private String username; + + @NotNull + private String password; + + @NotNull + private String resourceLocation; + + @Override + public String getName() { + return SERVER_PROPERTIES_TYPE; + } + + @Override + public void setName(final String name) { + throw new ParameterRuntimeException("The name of this ParameterGroup implementation is always " + getName()); + } + + @Override + public GroupValidationResult validate() { + return new GroupValidationResult(this); + } + + /** + * Generate base64-encoded Authorization header from username and password. + * + * @return Base64 encoded string + */ + public String getBasicAuth() { + String encodedAuth = Base64.getEncoder().encodeToString( + String.format("%s:%s", getUsername(), getPassword()).getBytes(StandardCharsets.UTF_8)); + // Return encoded basic auth header + return "Basic " + encodedAuth; + } +} 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 ed9ade562..a0fafbc9b 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,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2020 Bell Canada. 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. @@ -21,6 +22,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; @@ -48,6 +50,7 @@ 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; @@ -87,6 +90,12 @@ public class Main extends ServiceManagerContainer { AtomicReference provRef = new AtomicReference<>(); addAction(provName, () -> provRef.set(buildDmaapProvider(dmaapProv)), () -> provRef.get().shutdown()); + CdsServerParameters cdsServer = params.getGrpcServer(); + + // Cds Simulator + AtomicReference cdsSim = new AtomicReference<>(); + addAction(cdsServer.getName(), () -> cdsSim.set(buildCdsSimulator(cdsServer)), () -> cdsSim.get().stop()); + // REST server simulators // @formatter:off for (ClassRestServerParameters restsim : params.getRestServers()) { @@ -173,10 +182,17 @@ public class Main extends ServiceManagerContainer { DmaapSimProvider prov = new DmaapSimProvider(params); DmaapSimProvider.setInstance(prov); prov.start(); - return prov; } + private CdsSimulator buildCdsSimulator(CdsServerParameters params) throws IOException { + CdsSimulator 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(); 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 c47ff8876..788829f36 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 @@ -3,6 +3,7 @@ * ONAP * ================================================================================ * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2020 Bell Canada. 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. @@ -44,6 +45,8 @@ public class SimulatorParameters { */ private DmaapSimParameterGroup dmaapProvider; + private CdsServerParameters grpcServer; + /** * Parameters for the REST server simulators that are to be started. */ diff --git a/models-sim/policy-models-simulators/src/test/java/org/onap/policy/models/simulators/CdsServerParametersTest.java b/models-sim/policy-models-simulators/src/test/java/org/onap/policy/models/simulators/CdsServerParametersTest.java new file mode 100644 index 000000000..03aae8e61 --- /dev/null +++ b/models-sim/policy-models-simulators/src/test/java/org/onap/policy/models/simulators/CdsServerParametersTest.java @@ -0,0 +1,46 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2020 Bell Canada. + * ================================================================================ + * 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 static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import org.junit.Test; +import org.onap.policy.common.parameters.ValidationResult; +import org.onap.policy.common.utils.coder.CoderException; +import org.onap.policy.common.utils.coder.StandardCoder; + +public class CdsServerParametersTest { + @Test + public void testValidateString() throws CoderException { + // some fields missing + ValidationResult result = new CdsServerParameters().validate(); + assertFalse(result.isValid()); + assertNotNull(result.getResult()); + + // everything populated + SimulatorParameters simParams = new StandardCoder() + .decode(new File("src/test/resources/simParameters.json"), SimulatorParameters.class); + CdsServerParameters params = simParams.getGrpcServer(); + assertTrue(params.validate().isValid()); + } +} diff --git a/models-sim/policy-models-simulators/src/test/resources/simParameters.json b/models-sim/policy-models-simulators/src/test/resources/simParameters.json index 33821a538..1d1c0aad7 100644 --- a/models-sim/policy-models-simulators/src/test/resources/simParameters.json +++ b/models-sim/policy-models-simulators/src/test/resources/simParameters.json @@ -109,5 +109,17 @@ "sink": "SDNR-CL", "source": "SDNR-CL-RSP" } - ] + ], + "grpcServer": { + "name": "CDS simulator", + "providerClass": "org.onap.policy.simulators.CdsSimulator", + "host": "0.0.0.0", + "port": 6680, + "timeout": 30, + "username": "ccsdkapps", + "password": "ccsdkapps", + "resourceLocation": "org/onap/policy/simulators/cds/", + "successRepeatCount": 0, + "requestedResponseDelayMs": 0 + } } diff --git a/models-sim/pom.xml b/models-sim/pom.xml index 654928e09..ce9cfae06 100644 --- a/models-sim/pom.xml +++ b/models-sim/pom.xml @@ -2,6 +2,7 @@ ============LICENSE_START======================================================= Copyright (C) 2019 Nordix Foundation. Copyright (C) 2020 AT&T Intellectual Property. All rights reserved. + Modifications Copyright (C) 2020 Bell Canada. 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. @@ -40,5 +41,6 @@ models-sim-dmaap policy-models-sim-pdp policy-models-simulators + packages -- cgit 1.2.3-korg