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;
22 import org.immutables.gson.Gson;
23 import org.immutables.value.Value;
24 import org.onap.dcaegen2.collectors.datafile.exceptions.DatafileTaskException;
25 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.DmaapConsumerConfiguration;
26 import org.onap.dcaegen2.services.sdk.rest.services.dmaap.client.config.ImmutableDmaapConsumerConfiguration;
29 @Value.Style(redactedMask = "####")
31 public abstract class ConsumerConfiguration {
33 public abstract String topicUrl();
35 public abstract String trustStorePath();
37 public abstract String trustStorePasswordPath();
39 public abstract String keyStorePath();
41 public abstract String keyStorePasswordPath();
43 public abstract Boolean enableDmaapCertAuth();
46 * Gets the configuration in the SDK version.
48 * @return a <code>DmaapConsumerConfiguration</code> representing the configuration.
50 * @throws DatafileTaskException if something is wrong with the topic URL.
52 public DmaapConsumerConfiguration toDmaap() throws DatafileTaskException {
54 URL url = new URL(topicUrl());
57 if (url.getUserInfo() != null) {
58 String[] userInfo = url.getUserInfo().split(":");
59 userName = userInfo[0];
62 String urlPath = url.getPath();
63 DmaapConsumerUrlPath path = parseDmaapUrlPath(urlPath);
65 return new ImmutableDmaapConsumerConfiguration.Builder() //
66 .endpointUrl(topicUrl()) //
67 .dmaapContentType("application/json") //
68 .dmaapPortNumber(url.getPort()) //
69 .dmaapHostName(url.getHost()) //
70 .dmaapTopicName(path.dmaapTopicName) //
71 .dmaapProtocol(url.getProtocol()) //
72 .dmaapUserName(userName) //
73 .dmaapUserPassword(passwd) //
74 .trustStorePath(this.trustStorePath()) //
75 .trustStorePasswordPath(this.trustStorePasswordPath()) //
76 .keyStorePath(this.keyStorePath()) //
77 .keyStorePasswordPath(this.keyStorePasswordPath()) //
78 .enableDmaapCertAuth(this.enableDmaapCertAuth()) //
79 .consumerId(path.consumerId) //
80 .consumerGroup(path.consumerGroup) //
84 } catch (MalformedURLException e) {
85 throw new DatafileTaskException("Could not parse the URL", e);
89 private class DmaapConsumerUrlPath {
90 final String dmaapTopicName;
91 final String consumerGroup;
92 final String consumerId;
94 DmaapConsumerUrlPath(String dmaapTopicName, String consumerGroup, String consumerId) {
95 this.dmaapTopicName = dmaapTopicName;
96 this.consumerGroup = consumerGroup;
97 this.consumerId = consumerId;
101 private DmaapConsumerUrlPath parseDmaapUrlPath(String urlPath) throws DatafileTaskException {
102 String[] tokens = urlPath.split("/"); // /events/unauthenticated.VES_NOTIFICATION_OUTPUT/OpenDcae-c12/C12
103 if (tokens.length != 5) {
104 throw new DatafileTaskException("The path has incorrect syntax: " + urlPath);
107 final String dmaapTopicName = tokens[1] + "/" + tokens[2]; // /events/unauthenticated.VES_NOTIFICATION_OUTPUT
108 final String consumerGroup = tokens[3]; // OpenDcae-c12
109 final String consumerId = tokens[4]; // C12
110 return new DmaapConsumerUrlPath(dmaapTopicName, consumerGroup, consumerId);