2 * ============LICENSE_START======================================================================
3 * Copyright (C) 2018,2019 Nordix Foundation. All rights reserved.
4 * ===============================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
6 * in compliance with the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software distributed under the License
11 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12 * or implied. See the License for the specific language governing permissions and limitations under
14 * ============LICENSE_END========================================================================
17 package org.onap.dcaegen2.collectors.datafile.configuration;
19 import java.net.MalformedURLException;
21 import org.immutables.gson.Gson;
22 import org.immutables.value.Value;
23 import org.onap.dcaegen2.collectors.datafile.exceptions.DatafileTaskException;
24 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapConsumerConfiguration;
25 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.ImmutableDmaapConsumerConfiguration;
28 @Value.Style(redactedMask = "####")
30 public abstract class ConsumerConfiguration {
32 public abstract String topicUrl();
34 public abstract String trustStorePath();
36 public abstract String trustStorePasswordPath();
38 public abstract String keyStorePath();
40 public abstract String keyStorePasswordPath();
42 public abstract Boolean enableDmaapCertAuth();
45 * Gets the configuration in the SDK version.
47 * @return a <code>DmaapConsumerConfiguration</code> representing the configuration.
49 * @throws DatafileTaskException if something is wrong with the topic URL.
51 public DmaapConsumerConfiguration toDmaap() throws DatafileTaskException {
53 URL url = new URL(topicUrl());
56 if (url.getUserInfo() != null) {
57 String[] userInfo = url.getUserInfo().split(":");
58 userName = userInfo[0];
61 String urlPath = url.getPath();
62 DmaapConsumerUrlPath path = parseDmaapUrlPath(urlPath);
64 return new ImmutableDmaapConsumerConfiguration.Builder() //
65 .dmaapContentType("application/json") //
66 .dmaapPortNumber(url.getPort()) //
67 .dmaapHostName(url.getHost()) //
68 .dmaapTopicName(path.dmaapTopicName) //
69 .dmaapProtocol(url.getProtocol()) //
70 .dmaapUserName(userName) //
71 .dmaapUserPassword(passwd) //
72 .trustStorePath(this.trustStorePath()) //
73 .trustStorePasswordPath(this.trustStorePasswordPath()) //
74 .keyStorePath(this.keyStorePath()) //
75 .keyStorePasswordPath(this.keyStorePasswordPath()) //
76 .enableDmaapCertAuth(this.enableDmaapCertAuth()) //
77 .consumerId(path.consumerId) //
78 .consumerGroup(path.consumerGroup) //
82 } catch (MalformedURLException e) {
83 throw new DatafileTaskException("Could not parse the URL", e);
87 private class DmaapConsumerUrlPath {
88 final String dmaapTopicName;
89 final String consumerGroup;
90 final String consumerId;
92 DmaapConsumerUrlPath(String dmaapTopicName, String consumerGroup, String consumerId) {
93 this.dmaapTopicName = dmaapTopicName;
94 this.consumerGroup = consumerGroup;
95 this.consumerId = consumerId;
99 private DmaapConsumerUrlPath parseDmaapUrlPath(String urlPath) throws DatafileTaskException {
100 String[] tokens = urlPath.split("/"); // /events/unauthenticated.VES_NOTIFICATION_OUTPUT/OpenDcae-c12/C12
101 if (tokens.length != 5) {
102 throw new DatafileTaskException("The path has incorrect syntax: " + urlPath);
105 final String dmaapTopicName = tokens[1] + "/" + tokens[2]; // /events/unauthenticated.VES_NOTIFICATION_OUTPUT
106 final String consumerGroup = tokens[3]; // OpenDcae-c12
107 final String consumerId = tokens[4]; // C12
108 return new DmaapConsumerUrlPath(dmaapTopicName, consumerGroup, consumerId);