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