Remove legacy certificate handling
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / distribution / engine / DmaapClientFactory.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.openecomp.sdc.be.components.distribution.engine;
21
22 import com.att.nsa.mr.client.MRBatchingPublisher;
23 import com.att.nsa.mr.client.MRClientFactory;
24 import com.att.nsa.mr.client.MRConsumer;
25 import fj.data.Either;
26 import java.io.File;
27 import java.io.IOException;
28 import java.security.GeneralSecurityException;
29 import java.util.Properties;
30 import org.onap.sdc.security.SecurityUtil;
31 import org.openecomp.sdc.be.config.DmaapConsumerConfiguration;
32 import org.openecomp.sdc.be.config.DmaapProducerConfiguration;
33 import org.openecomp.sdc.common.log.wrappers.Logger;
34 import org.springframework.stereotype.Component;
35
36 /**
37  * Allows to create DMAAP client of type MRConsumer according received configuration parameters
38  */
39 @Component("dmaapClientFactory")
40 public class DmaapClientFactory {
41
42     private static final Logger logger = Logger.getLogger(DmaapClientFactory.class.getName());
43
44     /**
45      * Creates DMAAP consumer according to received parameters
46      *
47      * @param parameters
48      * @return an instance object of type MRConsumer
49      * @throws IOException
50      */
51     public MRConsumer create(DmaapConsumerConfiguration parameters) throws GeneralSecurityException, IOException {
52         MRConsumer consumer = MRClientFactory.createConsumer(buildProperties(parameters));
53         logger.info("MRConsumer created for topic {}", parameters.getTopic());
54         return consumer;
55     }
56
57     /**
58      * Creates DMAAP consumer according to received parameters
59      *
60      * @param parameters
61      * @return an instance object of type MRConsumer
62      * @throws IOException
63      */
64     public MRBatchingPublisher createProducer(DmaapProducerConfiguration parameters) throws Exception {
65         Properties prop = buildProducerProperties(parameters);
66         MRBatchingPublisher producer = MRClientFactory.createBatchingPublisher(prop);
67         logger.info("MRBatchingPublisher created for topic {}", parameters.getTopic());
68         return producer;
69     }
70
71     private Properties buildProperties(DmaapConsumerConfiguration parameters) throws GeneralSecurityException, IOException {
72         Properties props = new Properties();
73         Either<String, String> passkey = SecurityUtil.decrypt(parameters.getCredential().getPassword());
74         if (passkey.isRight()) {
75             throw new GeneralSecurityException("invalid password, cannot build properties");
76         }
77         props.setProperty("Latitude", Double.toString(parameters.getLatitude()));
78         props.setProperty("Longitude", Double.toString(parameters.getLongitude()));
79         props.setProperty("Version", parameters.getVersion());
80         props.setProperty("ServiceName", parameters.getServiceName());
81         props.setProperty("Environment", parameters.getEnvironment());
82         props.setProperty("Partner", parameters.getPartner());
83         props.setProperty("routeOffer", parameters.getRouteOffer());
84         props.setProperty("Protocol", parameters.getProtocol());
85         props.setProperty("username", parameters.getCredential().getUsername());
86         props.setProperty("password", passkey.left().value());
87         props.setProperty("contenttype", parameters.getContenttype());
88         props.setProperty("host", parameters.getHosts());
89         props.setProperty("topic", parameters.getTopic());
90         props.setProperty("group", parameters.getConsumerGroup());
91         props.setProperty("id", parameters.getConsumerId());
92         props.setProperty("timeout", Integer.toString(parameters.getTimeoutMs()));
93         props.setProperty("limit", Integer.toString(parameters.getLimit()));
94         props.setProperty("AFT_DME2_REQ_TRACE_ON", Boolean.toString(parameters.isDme2TraceOn()));
95         props.setProperty("AFT_ENVIRONMENT", parameters.getAftEnvironment());
96         props.setProperty("AFT_DME2_EP_CONN_TIMEOUT", Integer.toString(parameters.getAftDme2ConnectionTimeoutMs()));
97         props.setProperty("AFT_DME2_ROUNDTRIP_TIMEOUT_MS", Integer.toString(parameters.getAftDme2RoundtripTimeoutMs()));
98         props.setProperty("AFT_DME2_EP_READ_TIMEOUT_MS", Integer.toString(parameters.getAftDme2ReadTimeoutMs()));
99         props.setProperty("AFT_DME2_SSL_ENABLE", Boolean.toString(parameters.isAftDme2SslEnable()));
100         props.setProperty("AFT_DME2_CLIENT_IGNORE_SSL_CONFIG", Boolean.toString(parameters.isAftDme2ClientIgnoreSslConfig()));
101         props.setProperty("AFT_DME2_CLIENT_SSL_CERT_ALIAS", parameters.getAftDme2ClientSslCertAlias());
102         String dme2PreferredRouterFilePath = parameters.getDme2preferredRouterFilePath();
103         ensureFileExists(dme2PreferredRouterFilePath);
104         props.setProperty("DME2preferredRouterFilePath", dme2PreferredRouterFilePath);
105         props.setProperty("TransportType", "DME2");
106         props.setProperty("SubContextPath", "/");
107         props.setProperty("MethodType", "GET");
108         props.setProperty("authKey", "");
109         props.setProperty("authDate", "");
110         props.setProperty("filter", "");
111         props.setProperty("AFT_DME2_EXCHANGE_REQUEST_HANDLERS", "");
112         props.setProperty("AFT_DME2_EXCHANGE_REPLY_HANDLERS", "");
113         props.setProperty("sessionstickinessrequired", "no");
114         return props;
115     }
116
117     private Properties buildProducerProperties(DmaapProducerConfiguration parameters) throws GeneralSecurityException, IOException {
118         logger.info("The DmaapProducerConfiguration is {} ", parameters);
119         Properties props = new Properties();
120         Either<String, String> passkey = SecurityUtil.decrypt(parameters.getCredential().getPassword());
121         if (passkey.isRight()) {
122             throw new GeneralSecurityException("invalid password, cannot build properties");
123         }
124         props.setProperty("Latitude", Double.toString(parameters.getLatitude()));
125         props.setProperty("Longitude", Double.toString(parameters.getLongitude()));
126         props.setProperty("Version", parameters.getVersion());
127         props.setProperty("ServiceName", parameters.getServiceName());
128         props.setProperty("Environment", parameters.getEnvironment());
129         props.setProperty("Partner", parameters.getPartner());
130         props.setProperty("routeOffer", parameters.getRouteOffer());
131         props.setProperty("Protocol", parameters.getProtocol());
132         props.setProperty("username", parameters.getCredential().getUsername());
133         props.setProperty("password", passkey.left().value());
134         props.setProperty("contenttype", parameters.getContenttype());
135         props.setProperty("host", parameters.getHosts());
136         props.setProperty("topic", parameters.getTopic());
137         props.setProperty("group", parameters.getConsumerGroup());
138         props.setProperty("id", parameters.getConsumerId());
139         props.setProperty("AFT_DME2_REQ_TRACE_ON", Boolean.toString(parameters.isDme2TraceOn()));
140         props.setProperty("AFT_ENVIRONMENT", parameters.getAftEnvironment());
141         props.setProperty("AFT_DME2_EP_CONN_TIMEOUT", Integer.toString(parameters.getAftDme2ConnectionTimeoutMs()));
142         props.setProperty("AFT_DME2_ROUNDTRIP_TIMEOUT_MS", Integer.toString(parameters.getAftDme2RoundtripTimeoutMs()));
143         props.setProperty("AFT_DME2_EP_READ_TIMEOUT_MS", Integer.toString(parameters.getAftDme2ReadTimeoutMs()));
144         props.setProperty("AFT_DME2_SSL_ENABLE", Boolean.toString(parameters.isAftDme2SslEnable()));
145         props.setProperty("AFT_DME2_CLIENT_IGNORE_SSL_CONFIG", Boolean.toString(parameters.isAftDme2ClientIgnoreSslConfig()));
146         props.setProperty("AFT_DME2_CLIENT_SSL_CERT_ALIAS", parameters.getAftDme2ClientSslCertAlias());
147         String dme2PreferredRouterFilePath = parameters.getDme2preferredRouterFilePath();
148         ensureFileExists(dme2PreferredRouterFilePath);
149         props.setProperty("DME2preferredRouterFilePath", dme2PreferredRouterFilePath);
150         props.setProperty("TransportType", "HTTPAAF");
151         props.setProperty("SubContextPath", "/");
152         props.setProperty("MethodType", "POST");
153         props.setProperty("authKey", "");
154         props.setProperty("authDate", "");
155         props.setProperty("AFT_DME2_EXCHANGE_REQUEST_HANDLERS", "");
156         props.setProperty("AFT_DME2_EXCHANGE_REPLY_HANDLERS", "");
157         props.setProperty("sessionstickinessrequired", "no");
158         props.setProperty("maxBatchSize", "1");
159         props.setProperty("maxAgeMs", "250");
160         props.setProperty("partition", "1");
161         props.setProperty("MessageSentThreadOccurance", "10");
162         props.setProperty("Authorization", "Basic bTEzMzMxQGNjZC5hdHQuY29tOkFhMTIzNDU2");
163         return props;
164     }
165
166     private void ensureFileExists(String filePath) throws IOException {
167         File file = new File(filePath);
168         if (file.createNewFile()) {
169             logger.info("The file {} has been created on the disk", file.getAbsolutePath());
170         } else {
171             logger.info("The file {} already exists", file.getAbsolutePath());
172         }
173     }
174 }