diff options
author | emartin <ephraim.martin@est.tech> | 2019-04-23 14:49:01 +0000 |
---|---|---|
committer | emartin <ephraim.martin@est.tech> | 2019-04-23 14:49:01 +0000 |
commit | d5f3810292e6c85d123de7aa28fa0c860a21c183 (patch) | |
tree | 917dee3f70477c64e545ff84482d4a42236e6758 | |
parent | ea5f0af6c28863b5f8ee220e105b6c3221c68b03 (diff) |
Fix minor bugs in ves publisher1.0.0
* Publisher now uses existing RequestID
* Introduce basicAuth for publisher
* Clean up ves string by removing new lines
* Update pm-mapper topic name in blueprint
* Update reconfigure script to use https
Change-Id: Ib743127b04077b063c73415b1475be83e200ec3b
Issue-ID: DCAEGEN2-1444
Signed-off-by: emartin <ephraim.martin@est.tech>
6 files changed, 50 insertions, 15 deletions
diff --git a/dpo/blueprints/k8s-pm-mapper.yaml b/dpo/blueprints/k8s-pm-mapper.yaml index ba8182a..eabe51e 100644 --- a/dpo/blueprints/k8s-pm-mapper.yaml +++ b/dpo/blueprints/k8s-pm-mapper.yaml @@ -155,7 +155,7 @@ node_templates: get_input: client_id topic_url: { concat: [{ get_input: dmaap_mr_service_protocol },"://",{ get_input: dmaap_mr_service_host }, - ":",{ get_input: dmaap_mr_service_port },"/events/PM_MAPPER"]} + ":",{ get_input: dmaap_mr_service_port },"/events/org.onap.dmaap.mr.PM_MAPPER"]} location: get_input: dcae_location docker_config: diff --git a/src/main/java/org/onap/dcaegen2/services/pmmapper/messagerouter/VESPublisher.java b/src/main/java/org/onap/dcaegen2/services/pmmapper/messagerouter/VESPublisher.java index 77b0545..46d40e4 100644 --- a/src/main/java/org/onap/dcaegen2/services/pmmapper/messagerouter/VESPublisher.java +++ b/src/main/java/org/onap/dcaegen2/services/pmmapper/messagerouter/VESPublisher.java @@ -20,6 +20,8 @@ package org.onap.dcaegen2.services.pmmapper.messagerouter;
+import java.nio.charset.StandardCharsets;
+import java.util.Base64;
import java.util.List;
import org.onap.dcaegen2.services.pmmapper.exceptions.MRPublisherException;
@@ -61,7 +63,13 @@ public class VESPublisher { private void publish(String ves) {
try {
String topicUrl = config.getPublisherTopicUrl();
- sender.send("POST", topicUrl, ves);
+ ves = ves.replaceAll("\n", "");
+ String userCredentials = topicUrl.startsWith("https") ? Base64.getEncoder()
+ .encodeToString((this.config.getPublisherUserName() + ":" +
+ this.config.getPublisherPassword())
+ .getBytes(StandardCharsets.UTF_8))
+ : "";
+ sender.send("POST", topicUrl, ves, userCredentials);
} catch (Exception e) {
throw new MRPublisherException(e.getMessage(), e);
}
diff --git a/src/main/java/org/onap/dcaegen2/services/pmmapper/model/MapperConfig.java b/src/main/java/org/onap/dcaegen2/services/pmmapper/model/MapperConfig.java index 390fa0d..0630d53 100644 --- a/src/main/java/org/onap/dcaegen2/services/pmmapper/model/MapperConfig.java +++ b/src/main/java/org/onap/dcaegen2/services/pmmapper/model/MapperConfig.java @@ -118,6 +118,12 @@ public class MapperConfig implements Configurable{ @GSONRequired @SerializedName("dmaap_info") DmaapInfo dmaapInfo; + + @SerializedName("aaf_username") + private String aafUsername; + + @SerializedName("aaf_password") + private String aafPassword; } @Getter @@ -133,12 +139,6 @@ public class MapperConfig implements Configurable{ @SerializedName("subscriber_id") private String subscriberId; - @SerializedName("aaf_username") - private String aafUsername; - - @SerializedName("aaf_password") - private String aafPassword; - @SerializedName("client_role") private String clientRole; @@ -149,6 +149,14 @@ public class MapperConfig implements Configurable{ private String topicUrl; } + public String getPublisherUserName() { + return this.getStreamsPublishes().getDmaapPublisher().getAafUsername(); + } + + public String getPublisherPassword() { + return this.getStreamsPublishes().getDmaapPublisher().getAafPassword(); + } + @Override public void reconfigure(MapperConfig mapperConfig) { if(!this.equals(mapperConfig)) { diff --git a/src/main/java/org/onap/dcaegen2/services/pmmapper/utils/RequestSender.java b/src/main/java/org/onap/dcaegen2/services/pmmapper/utils/RequestSender.java index fdbae59..c8e29f3 100644 --- a/src/main/java/org/onap/dcaegen2/services/pmmapper/utils/RequestSender.java +++ b/src/main/java/org/onap/dcaegen2/services/pmmapper/utils/RequestSender.java @@ -27,6 +27,8 @@ import java.io.OutputStream; import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
+import java.util.Base64;
+import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;
@@ -38,6 +40,7 @@ import org.slf4j.LoggerFactory; import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
+import org.jboss.logging.MDC;
public class RequestSender {
private static final int MAX_RETRIES = 5;
@@ -68,16 +71,27 @@ public class RequestSender { }
/**
+ * Works just like {@link RequestSender#send(method,urlString,body, basicAuth)}, except {@code basicAuth }
+ * is set to empty String by default.
+ * @see RequestSender#send(String,String,String,String)
+ */
+ public String send(String method, final String urlString, final String body) throws Exception {
+ return send(method,urlString,body,"");
+ }
+
+ /**
* Sends an http request to a given endpoint.
* @param method of the outbound request
* @param urlString representing given endpoint
* @param body of the request as json
+ * @param encodedCredentials base64-encoded username password credentials
* @return http response body
* @throws Exception
*/
- public String send(String method, final String urlString, final String body) throws Exception {
+ public String send(String method, final String urlString, final String body, final String encodedCredentials) throws Exception {
final UUID invocationID = logger.invoke(ONAPLogConstants.InvocationMode.SYNCHRONOUS);
- final UUID requestID = UUID.randomUUID();
+ String requestID = Optional.ofNullable((String)MDC.get(ONAPLogConstants.MDCs.REQUEST_ID))
+ .orElse( UUID.randomUUID().toString());
String result = "";
for (int i = 1; i <= MAX_RETRIES; i++) {
@@ -89,6 +103,10 @@ public class RequestSender { HttpsURLConnection.setDefaultSSLSocketFactory(SSLContext.getDefault().getSocketFactory());
}
+ if(!encodedCredentials.isEmpty()) {
+ connection.setRequestProperty("Authorization", "Basic " + encodedCredentials);
+ }
+
if(!body.isEmpty()) {
setMessageBody(connection, body);
}
@@ -116,10 +134,10 @@ public class RequestSender { return result;
}
- private HttpURLConnection getHttpURLConnection(String method, URL url, UUID invocationID, UUID requestID) throws IOException {
+ private HttpURLConnection getHttpURLConnection(String method, URL url, UUID invocationID, String requestID) throws IOException {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setReadTimeout(DEFAULT_READ_TIMEOUT);
- connection.setRequestProperty(ONAPLogConstants.Headers.REQUEST_ID, requestID.toString());
+ connection.setRequestProperty(ONAPLogConstants.Headers.REQUEST_ID, requestID);
connection.setRequestProperty(ONAPLogConstants.Headers.INVOCATION_ID, invocationID.toString());
connection.setRequestProperty(ONAPLogConstants.Headers.PARTNER_NAME, MapperConfig.CLIENT_NAME);
connection.setRequestMethod(method);
diff --git a/src/main/resources/reconfigure.sh b/src/main/resources/reconfigure.sh index ac6f940..79aea06 100644 --- a/src/main/resources/reconfigure.sh +++ b/src/main/resources/reconfigure.sh @@ -2,5 +2,5 @@ while true do sleep 60 - echo $(wget -S --spider localhost:8081/reconfigure 2>&1) >> /var/log/ONAP/dcaegen2/services/pm-mapper/reconfigure.log + echo $(wget -S --spider --no-check-certificate https://localhost:8443/reconfigure 2>&1) >> /var/log/ONAP/dcaegen2/services/pm-mapper/reconfigure.log done diff --git a/src/test/java/org/onap/dcaegen2/pmmapper/messagerouter/VESPublisherTest.java b/src/test/java/org/onap/dcaegen2/pmmapper/messagerouter/VESPublisherTest.java index 69d34f8..2244d2d 100644 --- a/src/test/java/org/onap/dcaegen2/pmmapper/messagerouter/VESPublisherTest.java +++ b/src/test/java/org/onap/dcaegen2/pmmapper/messagerouter/VESPublisherTest.java @@ -67,7 +67,7 @@ public class VESPublisherTest { Flux<Event> flux = sut.publish(events);
- verify(sender, times(3)).send(Mockito.anyString(),Mockito.anyString(), Mockito.anyString());
+ verify(sender, times(3)).send(Mockito.anyString(),Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
StepVerifier.create(flux)
.expectNextMatches(event::equals)
.expectComplete()
@@ -79,11 +79,12 @@ public class VESPublisherTest { Event event = mock(Event.class);
List<Event> events = Arrays.asList(event,event,event);
when(event.getVes()).thenReturn(ves);
- when(sender.send("POST",topicURL,ves)).thenThrow(Exception.class);
+ when(sender.send("POST",topicURL,ves,"base64encoded")).thenThrow(Exception.class);
Flux<Event> flux = sut.publish(events);
StepVerifier.create(flux)
+ .expectNext(events.get(0))
.verifyComplete();
}
}
|