Added oparent to sdc main
[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.MRClientFactory;
24 import com.att.nsa.mr.client.MRConsumer;
25 import fj.data.Either;
26 import org.openecomp.sdc.be.config.DmaapConsumerConfiguration;
27 import org.openecomp.sdc.common.log.wrappers.Logger;
28 import org.openecomp.sdc.security.SecurityUtil;
29 import org.springframework.stereotype.Component;
30
31 import java.io.File;
32 import java.io.IOException;
33 import java.security.GeneralSecurityException;
34 import java.util.Properties;
35
36 /**
37  * Allows to create DMAAP client of type MRConsumer according received configuration parameters
38  */
39 @Component("dmaapClientFactory")
40 public class DmaapClientFactory {
41     private static final Logger logger = Logger.getLogger(DmaapClientFactory.class.getName());
42
43     /**
44      * Creates DMAAP consumer according to received parameters
45      * @param parameters
46      * @return an instance object of type MRConsumer
47      * @throws IOException
48      */
49     public MRConsumer create(DmaapConsumerConfiguration parameters) throws Exception {
50         MRConsumer consumer = MRClientFactory.createConsumer(buildProperties(parameters));
51         logger.info("MRConsumer created for topic {}", parameters.getTopic());
52         return consumer;
53     }
54
55     private Properties buildProperties(DmaapConsumerConfiguration parameters) throws GeneralSecurityException, IOException {
56         Properties props = new Properties();
57         Either<String,String> passkey = SecurityUtil.INSTANCE.decrypt(parameters.getCredential().getPassword() );
58         if (passkey.isRight()){
59             throw new GeneralSecurityException("invalid password, cannot build properties");
60         }
61         props.setProperty("Latitude", Double.toString(parameters.getLatitude()));
62         props.setProperty("Longitude", Double.toString(parameters.getLongitude()));
63         props.setProperty("Version", parameters.getVersion());
64         props.setProperty("ServiceName", parameters.getServiceName());
65         props.setProperty("Environment", parameters.getEnvironment());
66         props.setProperty("Partner", parameters.getPartner());
67         props.setProperty("routeOffer", parameters.getRouteOffer());        
68         props.setProperty("Protocol", parameters.getProtocol());        
69         props.setProperty("username", parameters.getCredential().getUsername());
70         props.setProperty("password", passkey.left().value() );
71         props.setProperty("contenttype", parameters.getContenttype());        
72         props.setProperty("host", parameters.getHosts());
73         props.setProperty("topic", parameters.getTopic());
74         props.setProperty("group", parameters.getConsumerGroup());
75         props.setProperty("id", parameters.getConsumerId());
76         props.setProperty("timeout", Integer.toString(parameters.getTimeoutMs()));
77         props.setProperty("limit", Integer.toString(parameters.getLimit()));        
78         props.setProperty("AFT_DME2_REQ_TRACE_ON", Boolean.toString(parameters.isDme2TraceOn()));
79         props.setProperty("AFT_ENVIRONMENT", parameters.getAftEnvironment());
80         props.setProperty("AFT_DME2_EP_CONN_TIMEOUT", Integer.toString(parameters.getAftDme2ConnectionTimeoutMs()));
81         props.setProperty("AFT_DME2_ROUNDTRIP_TIMEOUT_MS", Integer.toString(parameters.getAftDme2RoundtripTimeoutMs()));
82         props.setProperty("AFT_DME2_EP_READ_TIMEOUT_MS", Integer.toString(parameters.getAftDme2ReadTimeoutMs()));
83         
84         String dme2PreferredRouterFilePath = parameters.getDme2preferredRouterFilePath();
85         ensureFileExists(dme2PreferredRouterFilePath);
86         props.setProperty("DME2preferredRouterFilePath", dme2PreferredRouterFilePath);
87         
88         props.setProperty("TransportType", "DME2");
89         props.setProperty("SubContextPath", "/");
90         props.setProperty("MethodType", "GET");
91         props.setProperty("authKey", "");
92         props.setProperty("authDate", "");
93         props.setProperty("filter", "");
94         props.setProperty("AFT_DME2_EXCHANGE_REQUEST_HANDLERS", "");
95         props.setProperty("AFT_DME2_EXCHANGE_REPLY_HANDLERS", "");
96         props.setProperty("sessionstickinessrequired", "no");
97
98         return props;
99     }
100
101     private void ensureFileExists(String filePath) throws IOException {
102         File file = new File(filePath);
103         if(file.createNewFile()) {
104             logger.info("The file {} has been created on the disk", file.getAbsolutePath());
105         }
106         else{
107             logger.info("The file {} already exists", file.getAbsolutePath());
108         }
109     }
110 }