diff options
Diffstat (limited to 'src/main')
11 files changed, 499 insertions, 186 deletions
diff --git a/src/main/java/org/onap/dcaegen2/services/pmmapper/App.java b/src/main/java/org/onap/dcaegen2/services/pmmapper/App.java index b52a5f1..25e3918 100644 --- a/src/main/java/org/onap/dcaegen2/services/pmmapper/App.java +++ b/src/main/java/org/onap/dcaegen2/services/pmmapper/App.java @@ -42,6 +42,7 @@ import org.onap.dcaegen2.services.pmmapper.messagerouter.VESPublisher; import org.onap.dcaegen2.services.pmmapper.model.Event; import org.onap.dcaegen2.services.pmmapper.model.MapperConfig; import org.onap.dcaegen2.services.pmmapper.healthcheck.HealthCheckHandler; +import org.onap.dcaegen2.services.pmmapper.ssl.SSLContextFactory; import org.onap.dcaegen2.services.pmmapper.utils.DataRouterUtils; import org.onap.dcaegen2.services.pmmapper.utils.MeasConverter; import org.onap.dcaegen2.services.pmmapper.utils.MeasSplitter; @@ -53,6 +54,8 @@ import reactor.core.publisher.Flux; import reactor.core.publisher.FluxSink; import reactor.core.scheduler.Schedulers; +import javax.net.ssl.SSLContext; +import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; @@ -64,7 +67,7 @@ public class App { private static Path xmlSchema = Paths.get("/opt/app/pm-mapper/etc/measCollec_plusString.xsd"); private static FluxSink<Event> fluxSink; - public static void main(String[] args) throws InterruptedException, TooManyTriesException, CBSConfigException, EnvironmentConfigException, CBSServerError, MapperConfigException { + public static void main(String[] args) throws InterruptedException, TooManyTriesException, CBSConfigException, EnvironmentConfigException, CBSServerError, MapperConfigException, IOException { Flux<Event> flux = Flux.create(eventFluxSink -> fluxSink = eventFluxSink); HealthCheckHandler healthCheckHandler = new HealthCheckHandler(); MapperConfig mapperConfig = new ConfigHandler().getMapperConfig(); @@ -89,7 +92,7 @@ public class App { .filter(events -> App.filter(filterHandler, events, mapperConfig)) .concatMap(events -> App.map(mapper, events, mapperConfig)) .concatMap(vesPublisher::publish) - .subscribe(events -> logger.unwrap().info("Event Processed")); + .subscribe(event -> App.sendEventProcessed(mapperConfig, event)); DataRouterSubscriber dataRouterSubscriber = new DataRouterSubscriber(fluxSink::next, mapperConfig); dataRouterSubscriber.start(); @@ -98,8 +101,17 @@ public class App { configurables.add(mapperConfig); DynamicConfiguration dynamicConfiguration = new DynamicConfiguration(configurables, mapperConfig); - Undertow.builder() - .addHttpListener(8081, "0.0.0.0") + Undertow.Builder builder = Undertow.builder(); + + SSLContextFactory sslContextFactory = new SSLContextFactory(mapperConfig); + SSLContext sslContext = sslContextFactory.createSSLContext(mapperConfig); + SSLContext.setDefault(sslContext); + + if(mapperConfig.getEnableHttp()) { + builder.addHttpListener(8081, "0.0.0.0"); + } + + builder.addHttpsListener(8443, "0.0.0.0", sslContext) .setHandler(Handlers.routing() .add("put", "/delivery/{filename}", dataRouterSubscriber) .add("get", "/healthcheck", healthCheckHandler) diff --git a/src/main/java/org/onap/dcaegen2/services/pmmapper/datarouter/DataRouterSubscriber.java b/src/main/java/org/onap/dcaegen2/services/pmmapper/datarouter/DataRouterSubscriber.java index 19a4750..a0a8eaf 100644 --- a/src/main/java/org/onap/dcaegen2/services/pmmapper/datarouter/DataRouterSubscriber.java +++ b/src/main/java/org/onap/dcaegen2/services/pmmapper/datarouter/DataRouterSubscriber.java @@ -85,7 +85,7 @@ public class DataRouterSubscriber implements HttpHandler, Configurable { private Random jitterGenerator; private Gson metadataBuilder; private MapperConfig config; - private String subscriberId; + public static String subscriberId; @NonNull private EventReceiver eventReceiver; diff --git a/src/main/java/org/onap/dcaegen2/services/pmmapper/exceptions/CreateContextException.java b/src/main/java/org/onap/dcaegen2/services/pmmapper/exceptions/CreateContextException.java new file mode 100644 index 0000000..a5a230c --- /dev/null +++ b/src/main/java/org/onap/dcaegen2/services/pmmapper/exceptions/CreateContextException.java @@ -0,0 +1,27 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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.dcaegen2.services.pmmapper.exceptions; + +public class CreateContextException extends RuntimeException { + public CreateContextException(String message, Throwable cause) { + super(message, cause); + } +}
\ No newline at end of file diff --git a/src/main/java/org/onap/dcaegen2/services/pmmapper/exceptions/KeyManagerException.java b/src/main/java/org/onap/dcaegen2/services/pmmapper/exceptions/KeyManagerException.java new file mode 100644 index 0000000..d123991 --- /dev/null +++ b/src/main/java/org/onap/dcaegen2/services/pmmapper/exceptions/KeyManagerException.java @@ -0,0 +1,27 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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.dcaegen2.services.pmmapper.exceptions; + +public class KeyManagerException extends RuntimeException { + public KeyManagerException(String message, Throwable cause) { + super(message, cause); + } +}
\ No newline at end of file diff --git a/src/main/java/org/onap/dcaegen2/services/pmmapper/exceptions/LoadKeyStoreException.java b/src/main/java/org/onap/dcaegen2/services/pmmapper/exceptions/LoadKeyStoreException.java new file mode 100644 index 0000000..96bfad5 --- /dev/null +++ b/src/main/java/org/onap/dcaegen2/services/pmmapper/exceptions/LoadKeyStoreException.java @@ -0,0 +1,27 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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.dcaegen2.services.pmmapper.exceptions; + +public class LoadKeyStoreException extends RuntimeException { + public LoadKeyStoreException(String message, Throwable cause) { + super(message, cause); + } +}
\ No newline at end of file diff --git a/src/main/java/org/onap/dcaegen2/services/pmmapper/exceptions/ServerResponseException.java b/src/main/java/org/onap/dcaegen2/services/pmmapper/exceptions/ServerResponseException.java new file mode 100644 index 0000000..b52e2d4 --- /dev/null +++ b/src/main/java/org/onap/dcaegen2/services/pmmapper/exceptions/ServerResponseException.java @@ -0,0 +1,27 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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.dcaegen2.services.pmmapper.exceptions; + +public class ServerResponseException extends Exception { + public ServerResponseException(String message, Throwable cause) { + super(message, cause); + } +}
\ No newline at end of file diff --git a/src/main/java/org/onap/dcaegen2/services/pmmapper/exceptions/TrustManagerException.java b/src/main/java/org/onap/dcaegen2/services/pmmapper/exceptions/TrustManagerException.java new file mode 100644 index 0000000..75ce61d --- /dev/null +++ b/src/main/java/org/onap/dcaegen2/services/pmmapper/exceptions/TrustManagerException.java @@ -0,0 +1,27 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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.dcaegen2.services.pmmapper.exceptions; + +public class TrustManagerException extends RuntimeException { + public TrustManagerException(String message, Throwable cause) { + super(message, cause); + } +}
\ No newline at end of file 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 bd4eafb..b9d58ee 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 @@ -1,176 +1,197 @@ -/*-
- * ============LICENSE_START=======================================================
- * Copyright (C) 2019 Nordix Foundation.
- * ================================================================================
- * 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.dcaegen2.services.pmmapper.model;
-
-import java.net.MalformedURLException;
-import java.net.URL;
-
-import org.onap.dcaegen2.services.pmmapper.config.Configurable;
-import org.onap.dcaegen2.services.pmmapper.utils.GSONRequired;
-import com.google.gson.annotations.SerializedName;
-import lombok.Getter;
-import lombok.AccessLevel;
-import lombok.EqualsAndHashCode;
-import lombok.NoArgsConstructor;
-
-@Getter
-@EqualsAndHashCode
-@NoArgsConstructor
-public class MapperConfig implements Configurable{
-
- public static final String CLIENT_NAME = "pm-mapper";
-
- @GSONRequired
- @Getter(AccessLevel.PRIVATE)
- @SerializedName("streams_subscribes")
- private StreamsSubscribes streamsSubscribes;
-
- @GSONRequired
- @Getter(AccessLevel.PRIVATE)
- @SerializedName("streams_publishes")
- private StreamsPublishes streamsPublishes;
-
- @GSONRequired
- @SerializedName("buscontroller_feed_subscription_endpoint")
- private String busControllerSubscriptionEndpoint;
-
- @GSONRequired
- @SerializedName("dmaap_dr_feed_id")
- private String dmaapDRFeedId;
-
- @GSONRequired
- @SerializedName("dmaap_dr_delete_endpoint")
- private String dmaapDRDeleteEndpoint;
-
- public String getBusControllerDeliveryUrl() {
- return this.getStreamsSubscribes().getDmaapSubscriber().getDmaapInfo().getDeliveryUrl();
- }
-
- public String getDcaeLocation() {
- return this.getStreamsSubscribes().getDmaapSubscriber().getDmaapInfo().getLocation();
- }
-
- public String getBusControllerUserName() {
- return this.getStreamsSubscribes().getDmaapSubscriber().getDmaapInfo().getUsername();
- }
-
- public String getBusControllerPassword() {
- return this.getStreamsSubscribes().getDmaapSubscriber().getDmaapInfo().getPassword();
- }
-
- public URL getBusControllerSubscriptionUrl() throws MalformedURLException {
- return new URL(this.getBusControllerSubscriptionEndpoint());
- }
-
- public String getSubscriberIdentity(){
- return this.getStreamsSubscribes().getDmaapSubscriber().getDmaapInfo().getSubscriberId();
- }
-
- public String getSubscriberDcaeLocation() {
- return this.getStreamsSubscribes().getDmaapSubscriber().getDmaapInfo().getLocation();
- }
-
- public String getPublisherTopicUrl() {
- return this.getStreamsPublishes().getDmaapPublisher().getDmaapInfo().getTopicUrl();
- }
-
- public boolean dmaapInfoEquals(MapperConfig mapperConfig){
- return this
- .getStreamsSubscribes()
- .getDmaapSubscriber()
- .getDmaapInfo()
- .equals(mapperConfig.getStreamsSubscribes().getDmaapSubscriber().getDmaapInfo());
- }
-
- @Getter
- @EqualsAndHashCode
- private class StreamsSubscribes {
- @GSONRequired
- @SerializedName("dmaap_subscriber")
- DmaapSubscriber dmaapSubscriber;
- }
-
- @Getter
- @EqualsAndHashCode
- class DmaapSubscriber {
- @GSONRequired
- @SerializedName("dmaap_info")
- DmaapInfo dmaapInfo;
- }
-
- @Getter
- @EqualsAndHashCode
- private class StreamsPublishes {
- @GSONRequired
- @SerializedName("dmaap_publisher")
- DmaapPublisher dmaapPublisher;
- }
-
- @Getter
- @EqualsAndHashCode
- class DmaapPublisher {
- @GSONRequired
- @SerializedName("dmaap_info")
- DmaapInfo dmaapInfo;
- }
-
- @Getter
- @EqualsAndHashCode
- class DmaapInfo {
- private String location;
- private String username;
- private String password;
-
- @SerializedName("delivery_url")
- private String deliveryUrl;
-
- @SerializedName("subscriber_id")
- private String subscriberId;
-
- @SerializedName("aaf_username")
- private String aafUsername;
-
- @SerializedName("aaf_password")
- private String aafPassword;
-
- @SerializedName("client_role")
- private String clientRole;
-
- @SerializedName("client_id")
- private String clientId;
-
- @SerializedName("topic_url")
- private String topicUrl;
- }
-
- @SerializedName("pm-mapper-filter")
- MeasFilterConfig filterConfig;
-
- @Override
- public void reconfigure(MapperConfig mapperConfig) {
- if(!this.equals(mapperConfig)) {
- this.streamsSubscribes = mapperConfig.getStreamsSubscribes();
- this.streamsPublishes = mapperConfig.getStreamsPublishes();
- this.busControllerSubscriptionEndpoint = mapperConfig.getBusControllerSubscriptionEndpoint();
- this.dmaapDRFeedId = mapperConfig.getDmaapDRFeedId();
- this.dmaapDRDeleteEndpoint = mapperConfig.getDmaapDRDeleteEndpoint();
- }
- }
+/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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.dcaegen2.services.pmmapper.model; + +import java.net.MalformedURLException; +import java.net.URL; + +import org.onap.dcaegen2.services.pmmapper.config.Configurable; +import org.onap.dcaegen2.services.pmmapper.utils.GSONRequired; +import com.google.gson.annotations.SerializedName; +import lombok.Getter; +import lombok.AccessLevel; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; + +@Getter +@EqualsAndHashCode +@NoArgsConstructor +public class MapperConfig implements Configurable{ + + public static final String CLIENT_NAME = "pm-mapper"; + + @GSONRequired + @SerializedName("enable_http") + private Boolean enableHttp; + + @GSONRequired + @SerializedName("key_store_path") + private String keyStorePath; + + @GSONRequired + @SerializedName("key_store_pass_path") + private String keyStorePassPath; + + @GSONRequired + @SerializedName("trust_store_path") + private String trustStorePath; + + @GSONRequired + @SerializedName("trust_store_pass_path") + private String trustStorePassPath; + + @GSONRequired + @Getter(AccessLevel.PRIVATE) + @SerializedName("streams_subscribes") + private StreamsSubscribes streamsSubscribes; + + @GSONRequired + @Getter(AccessLevel.PRIVATE) + @SerializedName("streams_publishes") + private StreamsPublishes streamsPublishes; + + @GSONRequired + @SerializedName("buscontroller_feed_subscription_endpoint") + private String busControllerSubscriptionEndpoint; + + @GSONRequired + @SerializedName("dmaap_dr_feed_id") + private String dmaapDRFeedId; + + @GSONRequired + @SerializedName("dmaap_dr_delete_endpoint") + private String dmaapDRDeleteEndpoint; + + @GSONRequired + @SerializedName("pm-mapper-filter") + private MeasFilterConfig filterConfig; + + public String getBusControllerDeliveryUrl() { + return this.getStreamsSubscribes().getDmaapSubscriber().getDmaapInfo().getDeliveryUrl(); + } + + public String getDcaeLocation() { + return this.getStreamsSubscribes().getDmaapSubscriber().getDmaapInfo().getLocation(); + } + + public String getBusControllerUserName() { + return this.getStreamsSubscribes().getDmaapSubscriber().getDmaapInfo().getUsername(); + } + + public String getBusControllerPassword() { + return this.getStreamsSubscribes().getDmaapSubscriber().getDmaapInfo().getPassword(); + } + + public URL getBusControllerSubscriptionUrl() throws MalformedURLException { + return new URL(this.getBusControllerSubscriptionEndpoint()); + } + + public String getSubscriberIdentity(){ + return this.getStreamsSubscribes().getDmaapSubscriber().getDmaapInfo().getSubscriberId(); + } + + public String getSubscriberDcaeLocation() { + return this.getStreamsSubscribes().getDmaapSubscriber().getDmaapInfo().getLocation(); + } + + public String getPublisherTopicUrl() { + return this.getStreamsPublishes().getDmaapPublisher().getDmaapInfo().getTopicUrl(); + } + + public boolean dmaapInfoEquals(MapperConfig mapperConfig){ + return this + .getStreamsSubscribes() + .getDmaapSubscriber() + .getDmaapInfo() + .equals(mapperConfig.getStreamsSubscribes().getDmaapSubscriber().getDmaapInfo()); + } + + @Getter + @EqualsAndHashCode + private class StreamsSubscribes { + @GSONRequired + @SerializedName("dmaap_subscriber") + DmaapSubscriber dmaapSubscriber; + } + + @Getter + @EqualsAndHashCode + class DmaapSubscriber { + @GSONRequired + @SerializedName("dmaap_info") + DmaapInfo dmaapInfo; + } + + @Getter + @EqualsAndHashCode + private class StreamsPublishes { + @GSONRequired + @SerializedName("dmaap_publisher") + DmaapPublisher dmaapPublisher; + } + + @Getter + @EqualsAndHashCode + class DmaapPublisher { + @GSONRequired + @SerializedName("dmaap_info") + DmaapInfo dmaapInfo; + } + + @Getter + @EqualsAndHashCode + class DmaapInfo { + private String location; + private String username; + private String password; + + @SerializedName("delivery_url") + private String deliveryUrl; + + @SerializedName("subscriber_id") + private String subscriberId; + + @SerializedName("aaf_username") + private String aafUsername; + + @SerializedName("aaf_password") + private String aafPassword; + + @SerializedName("client_role") + private String clientRole; + + @SerializedName("client_id") + private String clientId; + + @SerializedName("topic_url") + private String topicUrl; + } + + @Override + public void reconfigure(MapperConfig mapperConfig) { + if(!this.equals(mapperConfig)) { + this.streamsSubscribes = mapperConfig.getStreamsSubscribes(); + this.streamsPublishes = mapperConfig.getStreamsPublishes(); + this.busControllerSubscriptionEndpoint = mapperConfig.getBusControllerSubscriptionEndpoint(); + this.dmaapDRFeedId = mapperConfig.getDmaapDRFeedId(); + this.dmaapDRDeleteEndpoint = mapperConfig.getDmaapDRDeleteEndpoint(); + } + } }
\ No newline at end of file diff --git a/src/main/java/org/onap/dcaegen2/services/pmmapper/ssl/SSLContextFactory.java b/src/main/java/org/onap/dcaegen2/services/pmmapper/ssl/SSLContextFactory.java new file mode 100644 index 0000000..68e63f5 --- /dev/null +++ b/src/main/java/org/onap/dcaegen2/services/pmmapper/ssl/SSLContextFactory.java @@ -0,0 +1,133 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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.dcaegen2.services.pmmapper.ssl; + +import org.onap.dcaegen2.services.pmmapper.exceptions.CreateContextException; +import org.onap.dcaegen2.services.pmmapper.exceptions.KeyManagerException; +import org.onap.dcaegen2.services.pmmapper.exceptions.LoadKeyStoreException; +import org.onap.dcaegen2.services.pmmapper.exceptions.TrustManagerException; +import org.onap.dcaegen2.services.pmmapper.model.MapperConfig; +import org.onap.logging.ref.slf4j.ONAPLogAdapter; +import org.slf4j.LoggerFactory; + +import javax.net.ssl.KeyManager; +import javax.net.ssl.KeyManagerFactory; +import javax.net.ssl.SSLContext; +import javax.net.ssl.TrustManager; +import javax.net.ssl.TrustManagerFactory; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Paths; +import java.security.KeyManagementException; +import java.security.KeyStore; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; +import java.security.UnrecoverableKeyException; +import java.security.cert.CertificateException; +import java.util.Base64; + +import static java.nio.file.Files.readAllBytes; + +public class SSLContextFactory { + private static final ONAPLogAdapter logger = new ONAPLogAdapter(LoggerFactory.getLogger(SSLContextFactory.class)); + private static MapperConfig mapperConfig; + + public SSLContextFactory(MapperConfig config) { + mapperConfig = config; + } + + public SSLContext createSSLContext(MapperConfig mapperConfig) throws IOException { + SSLContext sslContext = null; + + try { + KeyStore keyStore = loadKeyStore(mapperConfig.getKeyStorePath(), mapperConfig.getKeyStorePassPath()); + KeyManager[] keyManagers = createKeyManager(keyStore); + + KeyStore trustStore = loadKeyStore(mapperConfig.getTrustStorePath(), mapperConfig.getTrustStorePassPath()); + TrustManager[] trustManagers = createTrustManager(trustStore); + + sslContext = SSLContext.getInstance("TLSv1.2"); + sslContext.init(keyManagers, trustManagers, null); + } catch(KeyManagementException | NoSuchAlgorithmException e) { + logger.unwrap().error("Failed to create SSL Context.", e); + throw new CreateContextException("Failed to create SSL Context", e); + } + return sslContext; + } + + private KeyManager[] createKeyManager(KeyStore keyStore) throws NoSuchAlgorithmException, IOException { + KeyManager[] keyManager; + KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); + + try { + keyManagerFactory.init(keyStore, getPassword(mapperConfig.getKeyStorePassPath()).toCharArray()); + } catch (KeyStoreException | UnrecoverableKeyException e) { + logger.unwrap().error("Failed to initialize keystore.", e); + throw new KeyManagerException("Failed to create KeyManager from Keystore", e); + } + keyManager = keyManagerFactory.getKeyManagers(); + + return keyManager; + } + + private TrustManager[] createTrustManager(KeyStore trustStore) throws NoSuchAlgorithmException { + TrustManager[] trustManagers; + TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); + try { + trustManagerFactory.init(trustStore); + } catch (KeyStoreException e) { + throw new TrustManagerException("Failed to create TrustManager from Truststore", e); + } + trustManagers = trustManagerFactory.getTrustManagers(); + + return trustManagers; + } + + private KeyStore loadKeyStore(String path, String passwordPath) throws IOException, NoSuchAlgorithmException { + String type = "JKS"; + String encodedKeystore = new String(readAllBytes(Paths.get(path))); + String password = getPassword(passwordPath); + + KeyStore keyStore = null; + + try { + keyStore = KeyStore.getInstance(type); + byte[] decodedKeystore = Base64.getMimeDecoder().decode(encodedKeystore); + InputStream stream = new ByteArrayInputStream(decodedKeystore); + keyStore.load(stream, password.toCharArray()); + } catch(KeyStoreException | CertificateException e) { + logger.unwrap().error("Failed to load Keystore from given configuration.", e); + throw new LoadKeyStoreException("Failed to load Keystore from given configuration", e); + } + return keyStore; + } + + private String getPassword(String passwordPath) throws IOException { + try { + String password = new String(readAllBytes(Paths.get(passwordPath))); + password = password.replace("\n", "").replace("\r", ""); + return password; + } catch (IOException e) { + logger.unwrap().error("Could not read password from: {}.", passwordPath, e); + throw new IOException("Password not found"); + } + } +} diff --git a/src/main/java/org/onap/dcaegen2/services/pmmapper/utils/DataRouterUtils.java b/src/main/java/org/onap/dcaegen2/services/pmmapper/utils/DataRouterUtils.java index f30fb96..5147863 100644 --- a/src/main/java/org/onap/dcaegen2/services/pmmapper/utils/DataRouterUtils.java +++ b/src/main/java/org/onap/dcaegen2/services/pmmapper/utils/DataRouterUtils.java @@ -20,6 +20,7 @@ package org.onap.dcaegen2.services.pmmapper.utils; +import org.onap.dcaegen2.services.pmmapper.datarouter.DataRouterSubscriber; import org.onap.dcaegen2.services.pmmapper.exceptions.ProcessEventException; import org.onap.dcaegen2.services.pmmapper.model.Event; import org.onap.dcaegen2.services.pmmapper.model.MapperConfig; @@ -41,7 +42,7 @@ public class DataRouterUtils { public static String processEvent(MapperConfig config, Event event){ logger.unwrap().info("Sending processed to DataRouter"); String baseDelete = config.getDmaapDRDeleteEndpoint(); - String subscriberIdentity = config.getSubscriberIdentity(); + String subscriberIdentity = DataRouterSubscriber.subscriberId; String delete = String.format("%s/%s/%s", baseDelete, subscriberIdentity, event.getPublishIdentity()); try { return new RequestSender().send("DELETE", delete); @@ -50,4 +51,4 @@ public class DataRouterUtils { throw new ProcessEventException("Process event failure", exception); } } -} +}
\ No newline at end of file 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 658f820..fdbae59 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 @@ -30,11 +30,15 @@ import java.nio.charset.StandardCharsets; import java.util.UUID;
import java.util.stream.Collectors;
+import org.onap.dcaegen2.services.pmmapper.exceptions.ServerResponseException;
import org.onap.dcaegen2.services.pmmapper.model.MapperConfig;
import org.onap.logging.ref.slf4j.ONAPLogAdapter;
import org.onap.logging.ref.slf4j.ONAPLogConstants;
import org.slf4j.LoggerFactory;
+import javax.net.ssl.HttpsURLConnection;
+import javax.net.ssl.SSLContext;
+
public class RequestSender {
private static final int MAX_RETRIES = 5;
private static final int RETRY_INTERVAL = 1000;
@@ -79,9 +83,16 @@ public class RequestSender { for (int i = 1; i <= MAX_RETRIES; i++) {
final URL url = new URL(urlString);
final HttpURLConnection connection = getHttpURLConnection(method, url, invocationID, requestID);
+
+
+ if("https".equalsIgnoreCase(url.getProtocol())) {
+ HttpsURLConnection.setDefaultSSLSocketFactory(SSLContext.getDefault().getSocketFactory());
+ }
+
if(!body.isEmpty()) {
setMessageBody(connection, body);
}
+
logger.unwrap().info("Sending {} request to {}.", method, urlString);
try (InputStream is = connection.getInputStream();
@@ -90,13 +101,13 @@ public class RequestSender { .collect(Collectors.joining("\n"));
int responseCode = connection.getResponseCode();
if (!(isWithinErrorRange(responseCode))) {
- logger.unwrap().info("Server Response Received:\n{}", result);
+ logger.unwrap().info("Response code: {}, Server Response Received:\n{}",responseCode, result);
break;
}
} catch (Exception e) {
if (retryLimitReached(i)) {
- logger.unwrap().error("Execution error: "+connection.getResponseMessage(), e);
- throw new Exception(SERVER_ERROR_MESSAGE + ": " + connection.getResponseMessage(), e);
+ logger.unwrap().error("Execution error: {}", connection.getResponseMessage(), e);
+ throw new ServerResponseException(SERVER_ERROR_MESSAGE + ": " + connection.getResponseMessage(), e);
}
}
@@ -105,7 +116,7 @@ public class RequestSender { return result;
}
- private HttpURLConnection getHttpURLConnection(String method, URL url, UUID invocationID, UUID requestID) throws Exception {
+ private HttpURLConnection getHttpURLConnection(String method, URL url, UUID invocationID, UUID requestID) throws IOException {
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setReadTimeout(DEFAULT_READ_TIMEOUT);
connection.setRequestProperty(ONAPLogConstants.Headers.REQUEST_ID, requestID.toString());
|