Sync Integ to Master
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / distribution / engine / DmaapClientFactory.java
1 package org.openecomp.sdc.be.components.distribution.engine;
2
3 import com.att.nsa.mr.client.MRClientFactory;
4 import com.att.nsa.mr.client.MRConsumer;
5 import fj.data.Either;
6 import org.openecomp.sdc.be.config.DmaapConsumerConfiguration;
7 import org.openecomp.sdc.security.SecurityUtil;
8 import org.slf4j.Logger;
9 import org.slf4j.LoggerFactory;
10 import org.springframework.stereotype.Component;
11
12 import java.io.File;
13 import java.io.IOException;
14 import java.security.GeneralSecurityException;
15 import java.util.Properties;
16
17 /**
18  * Allows to create DMAAP client of type MRConsumer according received configuration parameters
19  */
20 @Component("dmaapClientFactory")
21 public class DmaapClientFactory {
22     private static final Logger logger = LoggerFactory.getLogger(DmaapClientFactory.class);
23
24     /**
25      * Creates DMAAP consumer according to received parameters
26      * @param parameters
27      * @return an instance object of type MRConsumer
28      * @throws IOException
29      */
30     public MRConsumer create(DmaapConsumerConfiguration parameters) throws Exception {
31         MRConsumer consumer = MRClientFactory.createConsumer(buildProperties(parameters));
32         logger.info("MRConsumer created for topic {}", parameters.getTopic());
33         return consumer;
34     }
35
36     private Properties buildProperties(DmaapConsumerConfiguration parameters) throws Exception{
37         Properties props = new Properties();
38         Either<String,String> passkey = SecurityUtil.INSTANCE.decrypt(parameters.getCredential().getPassword() );
39         if (passkey.isRight()){
40             throw new GeneralSecurityException("invalid password, cannot build properties");
41         }
42         props.setProperty("Latitude", Double.toString(parameters.getLatitude()));
43         props.setProperty("Longitude", Double.toString(parameters.getLongitude()));
44         props.setProperty("Version", parameters.getVersion());
45         props.setProperty("ServiceName", parameters.getServiceName());
46         props.setProperty("Environment", parameters.getEnvironment());
47         props.setProperty("Partner", parameters.getPartner());
48         props.setProperty("routeOffer", parameters.getRouteOffer());        
49         props.setProperty("Protocol", parameters.getProtocol());        
50         props.setProperty("username", parameters.getCredential().getUsername());
51         props.setProperty("password", passkey.left().value() );
52         props.setProperty("contenttype", parameters.getContenttype());        
53         props.setProperty("host", parameters.getHosts());
54         props.setProperty("topic", parameters.getTopic());
55         props.setProperty("group", parameters.getConsumerGroup());
56         props.setProperty("id", parameters.getConsumerId());
57         props.setProperty("timeout", Integer.toString(parameters.getTimeoutMs()));
58         props.setProperty("limit", Integer.toString(parameters.getLimit()));        
59         props.setProperty("AFT_DME2_REQ_TRACE_ON", Boolean.toString(parameters.isDme2TraceOn()));
60         props.setProperty("AFT_ENVIRONMENT", parameters.getAftEnvironment());
61         props.setProperty("AFT_DME2_EP_CONN_TIMEOUT", Integer.toString(parameters.getAftDme2ConnectionTimeoutMs()));
62         props.setProperty("AFT_DME2_ROUNDTRIP_TIMEOUT_MS", Integer.toString(parameters.getAftDme2RoundtripTimeoutMs()));
63         props.setProperty("AFT_DME2_EP_READ_TIMEOUT_MS", Integer.toString(parameters.getAftDme2ReadTimeoutMs()));
64         
65         String dme2PreferredRouterFilePath = parameters.getDme2preferredRouterFilePath();
66         ensureFileExists(dme2PreferredRouterFilePath);
67         props.setProperty("DME2preferredRouterFilePath", dme2PreferredRouterFilePath);
68         
69         props.setProperty("TransportType", "DME2");
70         props.setProperty("SubContextPath", "/");
71         props.setProperty("MethodType", "GET");
72         props.setProperty("authKey", "");
73         props.setProperty("authDate", "");
74         props.setProperty("filter", "");
75         props.setProperty("AFT_DME2_EXCHANGE_REQUEST_HANDLERS", "");
76         props.setProperty("AFT_DME2_EXCHANGE_REPLY_HANDLERS", "");
77         props.setProperty("sessionstickinessrequired", "no");
78
79         return props;
80     }
81
82     private void ensureFileExists(String filePath) throws IOException {
83         File file = new File(filePath);
84         if(file.createNewFile()) {
85             logger.info("The file {} has been created on the disk", file.getAbsolutePath());
86         }
87         else{
88             logger.info("The file {} already exists", file.getAbsolutePath());
89         }
90     }
91 }