add config modify LCM sample request
[appc.git] / appc-adapters / appc-dmaap-adapter / appc-dmaap-adapter-bundle / src / main / java / org / onap / appc / adapter / messaging / dmaap / utils / DmaapUtil.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.adapter.messaging.dmaap.utils;
25
26 import java.io.File;
27 import java.io.FileInputStream;
28 import java.io.FileOutputStream;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.util.Properties;
32
33 import org.onap.appc.configuration.Configuration;
34 import org.onap.appc.configuration.ConfigurationFactory;
35 import com.att.eelf.configuration.EELFLogger;
36 import com.att.eelf.configuration.EELFManager;
37
38 public class DmaapUtil {
39
40     private static final char   DELIMITER             = '_';
41
42     static final String         DMAAP_PROPERTIES_PATH = "org.onap.appc.dmaap.profile.path";
43
44     private static final EELFLogger log                = EELFManager.getInstance().getLogger(DmaapUtil.class);
45
46     private DmaapUtil() {
47     }
48
49     private static String createPreferredRouteFileIfNotExist(String topic) throws IOException {
50         String topicPreferredRouteFileName;
51         topicPreferredRouteFileName = topic + "preferredRoute.properties";
52         File fo = new File(topicPreferredRouteFileName);
53         if (!fo.exists()) {
54             ClassLoader classLoader = DmaapUtil.class.getClassLoader();
55             InputStream inputStream = classLoader.getResourceAsStream("preferredRoute.txt");
56             Properties props = new Properties();
57             props.load(inputStream);
58             String fileName = topic != null ? topic + DELIMITER + "MR1" : DELIMITER + "MR1";
59             props.setProperty("preferredRouteKey", fileName);
60             topicPreferredRouteFileName = topic + "preferredRoute.properties";
61             props.store(new FileOutputStream(topicPreferredRouteFileName),
62                     "preferredRoute.properties file created on the fly for topic:" + topic + " on:"
63                             + System.currentTimeMillis());
64         }
65         return topicPreferredRouteFileName;
66     }
67
68     public static String createConsumerPropFile(String topic, Properties props) throws IOException {
69         String defaultProfFileName = "consumer.properties";
70
71         log.debug("Creating DMaaP Consumer Property File for topic " + topic);
72         return createConsumerProducerPropFile(topic, defaultProfFileName, props);
73     }
74
75     public static String createProducerPropFile(String topic, Properties props) throws IOException {
76         String defaultProfFileName = "producer.properties";
77
78         log.debug("Creating DMaaP Producer Property File for topic " + topic);
79         return createConsumerProducerPropFile(topic, defaultProfFileName, props);
80     }
81
82     private static String createConsumerProducerPropFile(String topic, String defaultProfFileName, Properties props)
83             throws IOException {
84         Properties defaultProps = getDefaultProperties(defaultProfFileName);
85
86         defaultProps.setProperty("topic", topic);
87
88         String preferredRouteFileName = DmaapUtil.createPreferredRouteFileIfNotExist(topic);
89         if (props != null && !props.isEmpty()) {
90             defaultProps.putAll(props);
91         }
92         defaultProps.setProperty("topic", topic);
93         defaultProps.setProperty("DME2preferredRouterFilePath", preferredRouteFileName);
94         String id = defaultProps.getProperty("id");
95         String topicConsumerPropFileName = defaultProfFileName;
96         topicConsumerPropFileName = id != null ? id + DELIMITER + topicConsumerPropFileName
97                 : DELIMITER + topicConsumerPropFileName;
98         topicConsumerPropFileName = topic != null ? topic + DELIMITER + topicConsumerPropFileName
99                 : DELIMITER + topicConsumerPropFileName;
100
101         defaultProps.store(new FileOutputStream(topicConsumerPropFileName), defaultProfFileName
102                 + " file created on the fly for topic:" + topic + " on:" + System.currentTimeMillis());
103         return topicConsumerPropFileName;
104     }
105
106     private static Properties getDefaultProperties(String profileName) {
107         Properties props = new Properties();
108
109         // use appc configuration to get all properties which includes
110         // appc.properties and system properties
111         // allowing variable to be set in any location
112         Configuration config = ConfigurationFactory.getConfiguration();
113         String dmaapPropPath = config.getProperty(DMAAP_PROPERTIES_PATH);
114
115         if (dmaapPropPath != null) {
116             // load from file system
117
118             File profileFile = new File(dmaapPropPath, profileName);
119             FileInputStream inputStream = null;
120
121             log.info("Loading DMaaP Profile from " + profileFile.getAbsolutePath());
122
123             if (profileFile.exists()) {
124                 try {
125                     inputStream = new FileInputStream(profileFile);
126                     props.load(inputStream);
127                 } catch (IOException e) {
128                     log.error("Exception loading DMaaP Profile from " + profileFile.getAbsolutePath(), e);
129                 } finally {
130                     try {
131                         if (inputStream != null) {
132                             inputStream.close();
133                         }
134                     } catch (IOException ex) {
135                         log.warn("Exception closing DMaaP Profile file " + profileFile.getAbsolutePath(), ex);
136                     }
137                 }
138             }
139         }
140         if (props.isEmpty()) {
141             // load default Profile from class
142             log.info("Loading Default DMaaP Profile");
143
144             ClassLoader classLoader = DmaapUtil.class.getClassLoader();
145             InputStream inputStream = classLoader.getResourceAsStream(profileName);
146             try {
147                 props.load(inputStream);
148             } catch (IOException e) {
149                 log.error("Exception loading Default DMaaP Profile", e);
150             }
151         }
152
153         return props;
154     }
155 }