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();
44 public DmaapConsumerConfiguration toDmaap() throws DatafileTaskException {
46 URL url = new URL(topicUrl());
49 if (url.getUserInfo() != null) {
50 String[] userInfo = url.getUserInfo().split(":");
51 userName = userInfo[0];
54 String urlPath = url.getPath();
55 DmaapConsumerUrlPath path = parseDmaapUrlPath(urlPath);
57 return new ImmutableDmaapConsumerConfiguration.Builder() //
58 .dmaapContentType("application/json") //
59 .dmaapPortNumber(url.getPort()) //
60 .dmaapHostName(url.getHost()) //
61 .dmaapTopicName(path.dmaapTopicName) //
62 .dmaapProtocol(url.getProtocol()) //
63 .dmaapUserName(userName) //
64 .dmaapUserPassword(passwd) //
65 .trustStorePath(this.trustStorePath()) //
66 .trustStorePasswordPath(this.trustStorePasswordPath()) //
67 .keyStorePath(this.keyStorePath()) //
68 .keyStorePasswordPath(this.keyStorePasswordPath()) //
69 .enableDmaapCertAuth(this.enableDmaapCertAuth()) //
70 .consumerId(path.consumerId) //
71 .consumerGroup(path.consumerGroup) //
75 } catch (MalformedURLException e) {
76 throw new DatafileTaskException("Could not parse the URL", e);
80 private class DmaapConsumerUrlPath {
81 final String dmaapTopicName;
82 final String consumerGroup;
83 final String consumerId;
85 DmaapConsumerUrlPath(String dmaapTopicName, String consumerGroup, String consumerId) {
86 this.dmaapTopicName = dmaapTopicName;
87 this.consumerGroup = consumerGroup;
88 this.consumerId = consumerId;
92 private DmaapConsumerUrlPath parseDmaapUrlPath(String urlPath) throws DatafileTaskException {
93 String[] tokens = urlPath.split("/"); // UrlPath: /events/unauthenticated.VES_NOTIFICATION_OUTPUT/OpenDcae-c12/C12
94 if (tokens.length != 5) {
95 throw new DatafileTaskException("The path has incorrect syntax: " + urlPath);
98 final String dmaapTopicName = tokens[1] + "/" + tokens[2]; // e.g. /events/unauthenticated.VES_NOTIFICATION_OUTPUT
99 final String consumerGroup = tokens[3]; // ex. OpenDcae-c12
100 final String consumerId = tokens[4]; // ex. C12
101 return new DmaapConsumerUrlPath(dmaapTopicName, consumerGroup, consumerId);