aboutsummaryrefslogtreecommitdiffstats
path: root/pnfsimulator/src/main/java
diff options
context:
space:
mode:
Diffstat (limited to 'pnfsimulator/src/main/java')
-rw-r--r--pnfsimulator/src/main/java/org/onap/pnfsimulator/rest/model/SimulatorRequest.java4
-rw-r--r--pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/SimulatorService.java20
-rw-r--r--pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/TemplateVariablesReplacer.java50
-rw-r--r--pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/client/HttpClientAdapterImpl.java29
-rw-r--r--pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/scheduler/EventJob.java2
5 files changed, 87 insertions, 18 deletions
diff --git a/pnfsimulator/src/main/java/org/onap/pnfsimulator/rest/model/SimulatorRequest.java b/pnfsimulator/src/main/java/org/onap/pnfsimulator/rest/model/SimulatorRequest.java
index 2b06658..d00b311 100644
--- a/pnfsimulator/src/main/java/org/onap/pnfsimulator/rest/model/SimulatorRequest.java
+++ b/pnfsimulator/src/main/java/org/onap/pnfsimulator/rest/model/SimulatorRequest.java
@@ -48,4 +48,8 @@ public class SimulatorRequest {
@JsonDeserialize(using = JsonObjectDeserializer.class)
private JsonObject patch;
+ @Nullable
+ @JsonDeserialize(using = JsonObjectDeserializer.class)
+ private JsonObject variables;
+
}
diff --git a/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/SimulatorService.java b/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/SimulatorService.java
index 02f50e4..e5576b8 100644
--- a/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/SimulatorService.java
+++ b/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/SimulatorService.java
@@ -45,6 +45,7 @@ import java.util.Optional;
public class SimulatorService {
private final TemplatePatcher templatePatcher;
+ private final TemplateVariablesReplacer templateVariablesReplacer;
private final TemplateReader templateReader;
private final EventDataService eventDataService;
private final EventScheduler eventScheduler;
@@ -53,14 +54,20 @@ public class SimulatorService {
private static final JsonObject EMPTY_JSON_OBJECT = new JsonObject();
@Autowired
- public SimulatorService(TemplatePatcher templatePatcher, TemplateReader templateReader,
- EventScheduler eventScheduler, EventDataService eventDataService,
- SimulatorConfigService simulatorConfigService, SslAuthenticationHelper sslAuthenticationHelper) {
+ public SimulatorService(
+ TemplatePatcher templatePatcher,
+ TemplateReader templateReader,
+ EventScheduler eventScheduler,
+ EventDataService eventDataService,
+ SimulatorConfigService simulatorConfigService,
+ TemplateVariablesReplacer templateVariablesReplacer,
+ SslAuthenticationHelper sslAuthenticationHelper) {
this.templatePatcher = templatePatcher;
this.templateReader = templateReader;
this.eventDataService = eventDataService;
this.eventScheduler = eventScheduler;
this.simulatorConfigService = simulatorConfigService;
+ this.templateVariablesReplacer = templateVariablesReplacer;
this.sslAuthenticationHelper = sslAuthenticationHelper;
}
@@ -71,16 +78,19 @@ public class SimulatorService {
JsonObject input = Optional.ofNullable(simulatorRequest.getPatch()).orElse(new JsonObject());
JsonObject patchedJson = templatePatcher
.mergeTemplateWithPatch(template, input);
+ JsonObject variables = Optional.ofNullable(simulatorRequest.getVariables()).orElse(new JsonObject());
+ JsonObject patchedJsonWithVariablesSubstituted = templateVariablesReplacer.substituteVariables(patchedJson, variables);
+
JsonObject keywords = new JsonObject();
- EventData eventData = eventDataService.persistEventData(template, patchedJson, input, keywords);
+ EventData eventData = eventDataService.persistEventData(template, patchedJsonWithVariablesSubstituted, input, keywords);
String targetVesUrl = getDefaultUrlIfNotProvided(simulatorParams.getVesServerUrl());
return eventScheduler
.scheduleEvent(targetVesUrl, Optional.ofNullable(simulatorParams.getRepeatInterval()).orElse(1),
Optional.ofNullable(simulatorParams.getRepeatCount()).orElse(1), simulatorRequest.getTemplateName(),
eventData.getId(),
- patchedJson);
+ patchedJsonWithVariablesSubstituted);
}
public void triggerOneTimeEvent(FullEvent event) throws IOException, GeneralSecurityException {
diff --git a/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/TemplateVariablesReplacer.java b/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/TemplateVariablesReplacer.java
new file mode 100644
index 0000000..eb0b141
--- /dev/null
+++ b/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/TemplateVariablesReplacer.java
@@ -0,0 +1,50 @@
+/*
+ * ============LICENSE_START=======================================================
+ * PNF-REGISTRATION-HANDLER
+ * ================================================================================
+ * Copyright (C) 2019 Nokia. 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.pnfsimulator.simulator;
+
+import com.google.gson.Gson;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+
+import java.util.Map.Entry;
+
+import lombok.val;
+import org.springframework.stereotype.Component;
+
+@Component
+public class TemplateVariablesReplacer {
+ private static final Gson GSON = new Gson();
+ private static final String OBJECT_KEYWORD_MARK = "#";
+ private static final String ESCAPED_QUOTE = "\"";
+ private static final String STRING_KEYWORD_MARK = ESCAPED_QUOTE + OBJECT_KEYWORD_MARK + "%s" + ESCAPED_QUOTE;
+
+ JsonObject substituteVariables(JsonObject source, JsonObject variables) {
+ var result = source.toString();
+ for (val variable : variables.entrySet()) {
+ result = substituteVariable(result, variable);
+ }
+ return GSON.fromJson(result, JsonObject.class);
+ }
+
+ private String substituteVariable(String sourceAsString, Entry<String, JsonElement> variable) {
+ return sourceAsString.replaceAll(String.format(STRING_KEYWORD_MARK, variable.getKey()), variable.getValue().toString());
+ }
+
+}
diff --git a/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/client/HttpClientAdapterImpl.java b/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/client/HttpClientAdapterImpl.java
index 7ddef86..a881698 100644
--- a/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/client/HttpClientAdapterImpl.java
+++ b/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/client/HttpClientAdapterImpl.java
@@ -50,20 +50,20 @@ public class HttpClientAdapterImpl implements HttpClientAdapter {
private static final String CONTENT_TYPE = "Content-Type";
private static final String APPLICATION_JSON = "application/json";
private static final RequestConfig CONFIG = RequestConfig.custom()
- .setConnectTimeout(CONNECTION_TIMEOUT)
- .setConnectionRequestTimeout(CONNECTION_TIMEOUT)
- .setSocketTimeout(CONNECTION_TIMEOUT)
- .build();
+ .setConnectTimeout(CONNECTION_TIMEOUT)
+ .setConnectionRequestTimeout(CONNECTION_TIMEOUT)
+ .setSocketTimeout(CONNECTION_TIMEOUT)
+ .build();
private static final Marker INVOKE = MarkerFactory.getMarker("INVOKE");
private SslSupportLevel sslSupportLevel;
private HttpClient client;
private final String targetUrl;
public HttpClientAdapterImpl(String targetUrl, SslAuthenticationHelper sslAuthenticationHelper)
- throws IOException, GeneralSecurityException {
+ throws IOException, GeneralSecurityException {
this.sslSupportLevel = sslAuthenticationHelper.isClientCertificateEnabled()
- ? SslSupportLevel.CLIENT_CERT_AUTH
- : SslSupportLevel.getSupportLevelBasedOnProtocol(targetUrl);
+ ? SslSupportLevel.CLIENT_CERT_AUTH
+ : SslSupportLevel.getSupportLevelBasedOnProtocol(targetUrl);
this.client = sslSupportLevel.getClient(CONFIG, sslAuthenticationHelper);
this.targetUrl = targetUrl;
}
@@ -76,11 +76,8 @@ public class HttpClientAdapterImpl implements HttpClientAdapter {
@Override
public void send(String content) {
try {
- HttpPost request = createRequest(content);
- HttpResponse response = client.execute(request);
-
- //response has to be fully consumed otherwise apache won't release connection
- EntityUtils.consumeQuietly(response.getEntity());
+ HttpResponse response = sendAndRetrieve(content);
+ EntityUtils.consumeQuietly(response.getEntity()); //response has to be fully consumed otherwise apache won't release connection
LOGGER.info(INVOKE, "Message sent, ves response code: {}", response.getStatusLine());
} catch (IOException e) {
LOGGER.warn("Error sending message to ves: {}", e.getMessage(), e.getCause());
@@ -91,6 +88,13 @@ public class HttpClientAdapterImpl implements HttpClientAdapter {
return sslSupportLevel;
}
+ private HttpResponse sendAndRetrieve(String content) throws IOException {
+ HttpPost request = createRequest(content);
+ HttpResponse httpResponse = client.execute(request);
+ request.releaseConnection();
+ return httpResponse;
+ }
+
private HttpPost createRequest(String content) throws UnsupportedEncodingException {
HttpPost request = new HttpPost(this.targetUrl);
StringEntity stringEntity = new StringEntity(content);
@@ -101,5 +105,4 @@ public class HttpClientAdapterImpl implements HttpClientAdapter {
return request;
}
-
}
diff --git a/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/scheduler/EventJob.java b/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/scheduler/EventJob.java
index 21e0466..a467370 100644
--- a/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/scheduler/EventJob.java
+++ b/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/scheduler/EventJob.java
@@ -73,6 +73,8 @@ public class EventJob implements Job {
private Optional<HttpClientAdapter> getHttpClientAdapter(JobDataMap jobDataMap, String vesUrl) {
HttpClientAdapter adapter = null;
try {
+ adapter = (HttpClientAdapter) (jobDataMap.containsKey(CLIENT_ADAPTER) ? jobDataMap.get(CLIENT_ADAPTER)
+ : new HttpClientAdapterImpl(vesUrl, new SslAuthenticationHelper()));
adapter = (HttpClientAdapter) (
jobDataMap.containsKey(CLIENT_ADAPTER)
? jobDataMap.get(CLIENT_ADAPTER)