Catalog alignment
[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
21 package org.openecomp.sdc.be.components.distribution.engine;
22
23 import com.att.nsa.mr.client.MRBatchingPublisher;
24 import com.att.nsa.mr.client.MRClientFactory;
25 import com.att.nsa.mr.client.MRConsumer;
26 import fj.data.Either;
27 import org.onap.sdc.security.SecurityUtil;
28 import org.openecomp.sdc.be.config.DmaapConsumerConfiguration;
29 import org.openecomp.sdc.be.config.DmaapProducerConfiguration;
30 import org.openecomp.sdc.common.log.wrappers.Logger;
31 import org.springframework.stereotype.Component;
32
33 import java.io.File;
34 import java.io.IOException;
35 import java.security.GeneralSecurityException;
36 import java.util.Properties;
37
38 /**
39  * Allows to create DMAAP client of type MRConsumer according received configuration parameters
40  */
41 @Component("dmaapClientFactory")
42 public class DmaapClientFactory {
43     private static final Logger logger = Logger.getLogger(DmaapClientFactory.class.getName());
44
45     /**
46      * Creates DMAAP consumer according to received parameters
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      * @param parameters
60      * @return an instance object of type MRConsumer
61      * @throws IOException
62      */
63     public MRBatchingPublisher createProducer(DmaapProducerConfiguration parameters) throws Exception {
64         Properties prop = buildProducerProperties(parameters);
65         MRBatchingPublisher producer = MRClientFactory.createBatchingPublisher(prop);
66         logger.info("MRBatchingPublisher created for topic {}", parameters.getTopic());
67         return producer;
68     }
69
70     private Properties buildProperties(DmaapConsumerConfiguration parameters) throws GeneralSecurityException, IOException {
71         Properties props = new Properties();
72         Either<String,String> passkey = SecurityUtil.INSTANCE.decrypt(parameters.getCredential().getPassword() );
73         if (passkey.isRight()){
74             throw new GeneralSecurityException("invalid password, cannot build properties");
75         }
76         props.setProperty("Latitude", Double.toString(parameters.getLatitude()));
77         props.setProperty("Longitude", Double.toString(parameters.getLongitude()));
78         props.setProperty("Version", parameters.getVersion());
79         props.setProperty("ServiceName", parameters.getServiceName());
80         props.setProperty("Environment", parameters.getEnvironment());
81         props.setProperty("Partner", parameters.getPartner());
82         props.setProperty("routeOffer", parameters.getRouteOffer());        
83         props.setProperty("Protocol", parameters.getProtocol());        
84         props.setProperty("username", parameters.getCredential().getUsername());
85         props.setProperty("password", passkey.left().value() );
86         props.setProperty("contenttype", parameters.getContenttype());        
87         props.setProperty("host", parameters.getHosts());
88         props.setProperty("topic", parameters.getTopic());
89         props.setProperty("group", parameters.getConsumerGroup());
90         props.setProperty("id", parameters.getConsumerId());
91         props.setProperty("timeout", Integer.toString(parameters.getTimeoutMs()));
92         props.setProperty("limit", Integer.toString(parameters.getLimit()));        
93         props.setProperty("AFT_DME2_REQ_TRACE_ON", Boolean.toString(parameters.isDme2TraceOn()));
94         props.setProperty("AFT_ENVIRONMENT", parameters.getAftEnvironment());
95         props.setProperty("AFT_DME2_EP_CONN_TIMEOUT", Integer.toString(parameters.getAftDme2ConnectionTimeoutMs()));
96         props.setProperty("AFT_DME2_ROUNDTRIP_TIMEOUT_MS", Integer.toString(parameters.getAftDme2RoundtripTimeoutMs()));
97         props.setProperty("AFT_DME2_EP_READ_TIMEOUT_MS", Integer.toString(parameters.getAftDme2ReadTimeoutMs()));
98
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_KEYSTORE", parameters.getAftDme2ClientKeystore());
102         props.setProperty("AFT_DME2_CLIENT_KEYSTORE_PASSWORD", parameters.getAftDme2ClientKeystorePassword());
103         props.setProperty("AFT_DME2_CLIENT_SSL_CERT_ALIAS", parameters.getAftDme2ClientSslCertAlias());
104
105         
106         String dme2PreferredRouterFilePath = parameters.getDme2preferredRouterFilePath();
107         ensureFileExists(dme2PreferredRouterFilePath);
108         props.setProperty("DME2preferredRouterFilePath", dme2PreferredRouterFilePath);
109         
110         props.setProperty("TransportType", "DME2");
111         props.setProperty("SubContextPath", "/");
112         props.setProperty("MethodType", "GET");
113         props.setProperty("authKey", "");
114         props.setProperty("authDate", "");
115         props.setProperty("filter", "");
116         props.setProperty("AFT_DME2_EXCHANGE_REQUEST_HANDLERS", "");
117         props.setProperty("AFT_DME2_EXCHANGE_REPLY_HANDLERS", "");
118         props.setProperty("sessionstickinessrequired", "no");
119
120         return props;
121     }
122
123     private Properties buildProducerProperties(DmaapProducerConfiguration parameters) throws GeneralSecurityException, IOException { 
124         logger.info("The DmaapProducerConfiguration is {} ", parameters);
125         Properties props = new Properties();
126         Either<String,String> passkey = SecurityUtil.INSTANCE.decrypt(parameters.getCredential().getPassword() );
127         if (passkey.isRight()){
128             throw new GeneralSecurityException("invalid password, cannot build properties");
129         }
130         props.setProperty("Latitude", Double.toString(parameters.getLatitude()));
131         props.setProperty("Longitude", Double.toString(parameters.getLongitude()));
132         props.setProperty("Version", parameters.getVersion());
133         props.setProperty("ServiceName", parameters.getServiceName());
134         props.setProperty("Environment", parameters.getEnvironment());
135         props.setProperty("Partner", parameters.getPartner());
136         props.setProperty("routeOffer", parameters.getRouteOffer());
137         props.setProperty("Protocol", parameters.getProtocol());
138         props.setProperty("username", parameters.getCredential().getUsername());
139         props.setProperty("password", passkey.left().value() );
140         props.setProperty("contenttype", parameters.getContenttype());
141         props.setProperty("host", parameters.getHosts());
142         props.setProperty("topic", parameters.getTopic());
143         props.setProperty("group", parameters.getConsumerGroup());
144         props.setProperty("id", parameters.getConsumerId());
145         props.setProperty("AFT_DME2_REQ_TRACE_ON", Boolean.toString(parameters.isDme2TraceOn()));
146         props.setProperty("AFT_ENVIRONMENT", parameters.getAftEnvironment());
147         props.setProperty("AFT_DME2_EP_CONN_TIMEOUT", Integer.toString(parameters.getAftDme2ConnectionTimeoutMs()));
148         props.setProperty("AFT_DME2_ROUNDTRIP_TIMEOUT_MS", Integer.toString(parameters.getAftDme2RoundtripTimeoutMs()));
149         props.setProperty("AFT_DME2_EP_READ_TIMEOUT_MS", Integer.toString(parameters.getAftDme2ReadTimeoutMs()));
150         
151         props.setProperty("AFT_DME2_SSL_ENABLE", Boolean.toString(parameters.isAftDme2SslEnable()));
152         props.setProperty("AFT_DME2_CLIENT_IGNORE_SSL_CONFIG", Boolean.toString(parameters.isAftDme2ClientIgnoreSslConfig()));
153         props.setProperty("AFT_DME2_CLIENT_KEYSTORE", parameters.getAftDme2ClientKeystore());
154         props.setProperty("AFT_DME2_CLIENT_KEYSTORE_PASSWORD", parameters.getAftDme2ClientKeystorePassword());
155         props.setProperty("AFT_DME2_CLIENT_SSL_CERT_ALIAS", parameters.getAftDme2ClientSslCertAlias());
156
157         String dme2PreferredRouterFilePath = parameters.getDme2preferredRouterFilePath();
158         ensureFileExists(dme2PreferredRouterFilePath);
159         props.setProperty("DME2preferredRouterFilePath", dme2PreferredRouterFilePath);
160         props.setProperty("TransportType", "HTTPAAF");
161         props.setProperty("SubContextPath", "/");
162         props.setProperty("MethodType", "POST");
163         props.setProperty("authKey", "");
164         props.setProperty("authDate", "");
165         props.setProperty("AFT_DME2_EXCHANGE_REQUEST_HANDLERS", "");
166         props.setProperty("AFT_DME2_EXCHANGE_REPLY_HANDLERS", "");
167         props.setProperty("sessionstickinessrequired", "no");
168
169         props.setProperty("maxBatchSize","1");
170         props.setProperty("maxAgeMs","250");
171         props.setProperty("partition","1");
172         props.setProperty("MessageSentThreadOccurance","10");
173         props.setProperty("Authorization","Basic bTEzMzMxQGNjZC5hdHQuY29tOkFhMTIzNDU2");
174
175         return props;
176     }
177
178     private void ensureFileExists(String filePath) throws IOException {
179         File file = new File(filePath);
180         if(file.createNewFile()) {
181             logger.info("The file {} has been created on the disk", file.getAbsolutePath());
182         }
183         else{
184             logger.info("The file {} already exists", file.getAbsolutePath());
185         }
186     }
187 }