diff options
Diffstat (limited to 'models-interactions/model-simulators/src/main')
4 files changed, 202 insertions, 18 deletions
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 new file mode 100644 index 000000000..dbbaa1681 --- /dev/null +++ b/models-interactions/model-simulators/src/main/java/org/onap/policy/simulators/CdsSimulator.java @@ -0,0 +1,104 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2020 Nordix Foundation. + * Modifications 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.simulators; + +import com.google.protobuf.InvalidProtocolBufferException; +import com.google.protobuf.util.JsonFormat; +import io.grpc.Server; +import io.grpc.netty.NettyServerBuilder; +import io.grpc.stub.StreamObserver; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.nio.charset.StandardCharsets; +import lombok.Getter; +import org.apache.commons.io.IOUtils; +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; + +public class CdsSimulator { + @Getter + private final int port; + + private final Server server; + + /** + * Constructs the object, but does not start it. + * + * @param host host name of the server + * @param port port of the server + */ + public CdsSimulator(String host, int port) { + this.port = port; + + BluePrintProcessingServiceImplBase testCdsBlueprintServerImpl = new BluePrintProcessingServiceImplBase() { + + @Override + public StreamObserver<ExecutionServiceInput> process( + final StreamObserver<ExecutionServiceOutput> responseObserver) { + + return new StreamObserver<ExecutionServiceInput>() { + + @Override + public void onNext(final ExecutionServiceInput executionServiceInput) { + try { + String responseString = IOUtils.toString( + getClass().getResource("cds/CreateSubscriptionResponseEvent.json"), + StandardCharsets.UTF_8); + Builder builder = ExecutionServiceOutput.newBuilder(); + JsonFormat.parser().ignoringUnknownFields().merge(responseString, builder); + 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); + } + } + + @Override + public void onError(final Throwable throwable) { + responseObserver.onError(throwable); + } + + @Override + public void onCompleted() { + responseObserver.onCompleted(); + } + }; + } + }; + + server = NettyServerBuilder.forAddress(new InetSocketAddress(host, port)).addService(testCdsBlueprintServerImpl) + .build(); + } + + public void start() throws IOException { + server.start(); + } + + public void stop() { + server.shutdown(); + } +} diff --git a/models-interactions/model-simulators/src/main/java/org/onap/policy/simulators/SimulatorRuntimeException.java b/models-interactions/model-simulators/src/main/java/org/onap/policy/simulators/SimulatorRuntimeException.java new file mode 100644 index 000000000..b97e4d9f7 --- /dev/null +++ b/models-interactions/model-simulators/src/main/java/org/onap/policy/simulators/SimulatorRuntimeException.java @@ -0,0 +1,41 @@ +/*- + * ============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.simulators; + +public class SimulatorRuntimeException extends RuntimeException { + private static final long serialVersionUID = 1L; + + public SimulatorRuntimeException() { + super(); + } + + public SimulatorRuntimeException(String message) { + super(message); + } + + public SimulatorRuntimeException(Throwable cause) { + super(cause); + } + + public SimulatorRuntimeException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/models-interactions/model-simulators/src/main/java/org/onap/policy/simulators/Util.java b/models-interactions/model-simulators/src/main/java/org/onap/policy/simulators/Util.java index 5cb518f4e..efc40367b 100644 --- a/models-interactions/model-simulators/src/main/java/org/onap/policy/simulators/Util.java +++ b/models-interactions/model-simulators/src/main/java/org/onap/policy/simulators/Util.java @@ -21,6 +21,7 @@ package org.onap.policy.simulators; +import java.io.IOException; import java.util.Properties; import org.onap.policy.common.endpoints.http.server.HttpServletServer; import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance; @@ -46,6 +47,7 @@ public class Util { public static final int VFCSIM_SERVER_PORT = 6668; public static final int GUARDSIM_SERVER_PORT = 6669; public static final int SDNCSIM_SERVER_PORT = 6670; + public static final int CDSSIM_SERVER_PORT = 6671; public static final int DMAAPSIM_SERVER_PORT = 3904; private static final String CANNOT_PROCESS_PARAMETERS = "cannot parse parameters "; @@ -67,9 +69,21 @@ public class Util { .build(AAISIM_SERVER_NAME, LOCALHOST, AAISIM_SERVER_PORT, "/", false, true); testServer.addServletClass("/*", AaiSimulatorJaxRs.class.getName()); testServer.waitedStart(5000); - if (!NetworkUtil.isTcpPortOpen(LOCALHOST, testServer.getPort(), 5, 10000L)) { - throw new IllegalStateException(CANNOT_CONNECT + testServer.getPort()); - } + waitForServerToListen(testServer.getPort()); + return testServer; + } + + /** + * Build a CDS simulator. + * + * @return the simulator + * @throws InterruptedException if a thread is interrupted + * @throws IOException if an I/O error occurs + */ + public static CdsSimulator buildCdsSim() throws InterruptedException, IOException { + final CdsSimulator testServer = new CdsSimulator(LOCALHOST, CDSSIM_SERVER_PORT); + testServer.start(); + waitForServerToListen(testServer.getPort()); return testServer; } @@ -84,9 +98,7 @@ public class Util { .build(SDNCSIM_SERVER_NAME, LOCALHOST, SDNCSIM_SERVER_PORT, "/", false, true); testServer.addServletClass("/*", SdncSimulatorJaxRs.class.getName()); testServer.waitedStart(5000); - if (!NetworkUtil.isTcpPortOpen(LOCALHOST, testServer.getPort(), 5, 10000L)) { - throw new IllegalStateException(CANNOT_CONNECT + testServer.getPort()); - } + waitForServerToListen(testServer.getPort()); return testServer; } @@ -102,9 +114,7 @@ public class Util { .build(SOSIM_SERVER_NAME, LOCALHOST, SOSIM_SERVER_PORT, "/", false, true); testServer.addServletClass("/*", SoSimulatorJaxRs.class.getName()); testServer.waitedStart(5000); - if (!NetworkUtil.isTcpPortOpen(LOCALHOST, testServer.getPort(), 5, 10000L)) { - throw new IllegalStateException(CANNOT_CONNECT + testServer.getPort()); - } + waitForServerToListen(testServer.getPort()); return testServer; } @@ -119,9 +129,7 @@ public class Util { .build(VFCSIM_SERVER_NAME, LOCALHOST, VFCSIM_SERVER_PORT, "/", false, true); testServer.addServletClass("/*", VfcSimulatorJaxRs.class.getName()); testServer.waitedStart(5000); - if (!NetworkUtil.isTcpPortOpen(LOCALHOST, testServer.getPort(), 5, 10000L)) { - throw new IllegalStateException(CANNOT_CONNECT + testServer.getPort()); - } + waitForServerToListen(testServer.getPort()); return testServer; } @@ -136,9 +144,7 @@ public class Util { LOCALHOST, GUARDSIM_SERVER_PORT, "/", false, true); testServer.addServletClass("/*", GuardSimulatorJaxRs.class.getName()); testServer.waitedStart(5000); - if (!NetworkUtil.isTcpPortOpen(LOCALHOST, testServer.getPort(), 5, 10000L)) { - throw new IllegalStateException(CANNOT_CONNECT + testServer.getPort()); - } + waitForServerToListen(testServer.getPort()); return testServer; } @@ -170,9 +176,13 @@ public class Util { HttpServletServer testServer = HttpServletServerFactoryInstance.getServerFactory().build(props).get(0); testServer.waitedStart(5000); - if (!NetworkUtil.isTcpPortOpen(LOCALHOST, testServer.getPort(), 50, 1000L)) { - throw new IllegalStateException(CANNOT_CONNECT + testServer.getPort()); - } + waitForServerToListen(testServer.getPort()); return testServer; } + + private static void waitForServerToListen(int port) throws InterruptedException { + if (!NetworkUtil.isTcpPortOpen(LOCALHOST, port, 200, 250L)) { + throw new IllegalStateException(CANNOT_CONNECT + port); + } + } } diff --git a/models-interactions/model-simulators/src/main/resources/org/onap/policy/simulators/cds/CreateSubscriptionResponseEvent.json b/models-interactions/model-simulators/src/main/resources/org/onap/policy/simulators/cds/CreateSubscriptionResponseEvent.json new file mode 100644 index 000000000..adb51adcb --- /dev/null +++ b/models-interactions/model-simulators/src/main/resources/org/onap/policy/simulators/cds/CreateSubscriptionResponseEvent.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 |