e62a11e032881e59ffb6ec3d0560e195fc637e25
[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     /**
45      * Gets the configuration in the SDK version.
46      *
47      * @return a <code>DmaapConsumerConfiguration</code> representing the configuration.
48      *
49      * @throws DatafileTaskException if something is wrong with the topic URL.
50      */
51     public DmaapConsumerConfiguration toDmaap() throws DatafileTaskException {
52         try {
53             URL url = new URL(topicUrl());
54             String passwd = "";
55             String userName = "";
56             if (url.getUserInfo() != null) {
57                 String[] userInfo = url.getUserInfo().split(":");
58                 userName = userInfo[0];
59                 passwd = userInfo[1];
60             }
61             String urlPath = url.getPath();
62             DmaapConsumerUrlPath path = parseDmaapUrlPath(urlPath);
63
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) //
79                 .timeoutMs(-1) //
80                 .messageLimit(-1) //
81                 .build();
82         } catch (MalformedURLException e) {
83             throw new DatafileTaskException("Could not parse the URL", e);
84         }
85     }
86
87     private class DmaapConsumerUrlPath {
88         final String dmaapTopicName;
89         final String consumerGroup;
90         final String consumerId;
91
92         DmaapConsumerUrlPath(String dmaapTopicName, String consumerGroup, String consumerId) {
93             this.dmaapTopicName = dmaapTopicName;
94             this.consumerGroup = consumerGroup;
95             this.consumerId = consumerId;
96         }
97     }
98
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);
103         }
104
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);
109     }
110
111 }