4db7963dfb1297420873bc157f305311ee5bfc4c
[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
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;
27
28 @Value.Immutable
29 @Value.Style(redactedMask = "####")
30 @Gson.TypeAdapters
31 public abstract class ConsumerConfiguration {
32     @Value.Redacted
33     public abstract String topicUrl();
34
35     public abstract String trustStorePath();
36
37     public abstract String trustStorePasswordPath();
38
39     public abstract String keyStorePath();
40
41     public abstract String keyStorePasswordPath();
42
43     public abstract Boolean enableDmaapCertAuth();
44
45     /**
46      * Gets the configuration in the SDK version.
47      *
48      * @return a <code>DmaapConsumerConfiguration</code> representing the configuration.
49      *
50      * @throws DatafileTaskException if something is wrong with the topic URL.
51      */
52     public DmaapConsumerConfiguration toDmaap() throws DatafileTaskException {
53         try {
54             URL url = new URL(topicUrl());
55             String passwd = "";
56             String userName = "";
57             if (url.getUserInfo() != null) {
58                 String[] userInfo = url.getUserInfo().split(":");
59                 userName = userInfo[0];
60                 passwd = userInfo[1];
61             }
62             String urlPath = url.getPath();
63             DmaapConsumerUrlPath path = parseDmaapUrlPath(urlPath);
64
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) //
81                 .timeoutMs(-1) //
82                 .messageLimit(-1) //
83                 .build();
84         } catch (MalformedURLException e) {
85             throw new DatafileTaskException("Could not parse the URL", e);
86         }
87     }
88
89     private class DmaapConsumerUrlPath {
90         final String dmaapTopicName;
91         final String consumerGroup;
92         final String consumerId;
93
94         DmaapConsumerUrlPath(String dmaapTopicName, String consumerGroup, String consumerId) {
95             this.dmaapTopicName = dmaapTopicName;
96             this.consumerGroup = consumerGroup;
97             this.consumerId = consumerId;
98         }
99     }
100
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);
105         }
106
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);
111     }
112
113 }