bd8f0c3c57f8eb9bab688248586e42c15fffb432
[dcaegen2/collectors/datafile.git] /
1 /*-
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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
13  * the License.
14  * ============LICENSE_END========================================================================
15  */
16
17 package org.onap.dcaegen2.collectors.datafile.configuration;
18
19 import java.net.MalformedURLException;
20 import java.net.URL;
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;
26
27 @Value.Immutable
28 @Value.Style(redactedMask = "####")
29 @Gson.TypeAdapters
30 public abstract class ConsumerConfiguration {
31     @Value.Redacted
32     public abstract String topicUrl();
33
34     public abstract String trustStorePath();
35
36     public abstract String trustStorePasswordPath();
37
38     public abstract String keyStorePath();
39
40     public abstract String keyStorePasswordPath();
41
42     public abstract Boolean enableDmaapCertAuth();
43
44     public DmaapConsumerConfiguration toDmaap() throws DatafileTaskException {
45         try {
46             URL url = new URL(topicUrl());
47             String passwd = "";
48             String userName = "";
49             if (url.getUserInfo() != null) {
50                 String[] userInfo = url.getUserInfo().split(":");
51                 userName = userInfo[0];
52                 passwd = userInfo[1];
53             }
54             String urlPath = url.getPath();
55             DmaapConsumerUrlPath path = parseDmaapUrlPath(urlPath);
56
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) //
72                     .timeoutMs(-1) //
73                     .messageLimit(-1) //
74                     .build();
75         } catch (MalformedURLException e) {
76             throw new DatafileTaskException("Could not parse the URL", e);
77         }
78     }
79
80     private class DmaapConsumerUrlPath {
81         final String dmaapTopicName;
82         final String consumerGroup;
83         final String consumerId;
84
85         DmaapConsumerUrlPath(String dmaapTopicName, String consumerGroup, String consumerId) {
86             this.dmaapTopicName = dmaapTopicName;
87             this.consumerGroup = consumerGroup;
88             this.consumerId = consumerId;
89         }
90     }
91
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);
96         }
97
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);
102     }
103
104 }