summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/dcae/commonFunction/DmaapPropertyReader.java
blob: dabd2b15d0c64b9c138b85c0ffd08fede84fafa6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*-
 * ============LICENSE_START=======================================================
 * PROJECT
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.dcae.commonFunction;

import java.io.FileNotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonIOException;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;

public class DmaapPropertyReader {

	private static final String HOST_URL = " HOST-URL:";
	private static final String TOPIC = "TOPIC:";
	private static final String CAMBRIA_URL = "cambria.url";
	private static final String CAMBRIA_HOSTS = "cambria.hosts";
	private static final String PWD = " PWD:";
	private static final String USER = " USER:";
	private static final String BASIC_AUTH_PASSWORD = "basicAuthPassword";
	private static final String BASIC_AUTH_USER_NAME = "basicAuthUsername";
	private static DmaapPropertyReader instance = null;

	private static final Logger log = LoggerFactory.getLogger(DmaapPropertyReader.class);

	public HashMap<String, String> dmaap_hash = new HashMap<>();

	public DmaapPropertyReader(String CambriaConfigFile) {

		FileReader fr = null;
		try {
			JsonElement root = null;
			fr = new FileReader(CambriaConfigFile);
			root = new JsonParser().parse(fr);

			// Check if dmaap config is handled by legacy controller/service
			// manager
			if (root.getAsJsonObject().has("channels")) {
				JsonArray jsonObject = (JsonArray) root.getAsJsonObject().get("channels");

				for (int i = 0; i < jsonObject.size(); i++) {
					log.debug(TOPIC + jsonObject.get(i).getAsJsonObject().get("cambria.topic") + HOST_URL
							+ jsonObject.get(i).getAsJsonObject().get(CAMBRIA_URL) + " HOSTS:"
							+ jsonObject.get(i).getAsJsonObject().get(CAMBRIA_HOSTS) + PWD
							+ jsonObject.get(i).getAsJsonObject().get(BASIC_AUTH_PASSWORD) + USER
							+ jsonObject.get(i).getAsJsonObject().get(BASIC_AUTH_USER_NAME) + " NAME:"
							+ jsonObject.get(i).getAsJsonObject().get("name"));

					String convertedname = jsonObject.get(i).getAsJsonObject().get("name").toString().replace("\"", "");
					dmaap_hash.put(convertedname + ".cambria.topic",
							jsonObject.get(i).getAsJsonObject().get("cambria.topic").toString().replace("\"", ""));

					if (jsonObject.get(i).getAsJsonObject().get(CAMBRIA_HOSTS) != null) {
						dmaap_hash.put(convertedname + ".cambria.hosts",
								jsonObject.get(i).getAsJsonObject().get(CAMBRIA_HOSTS).toString().replace("\"", ""));
					}
					if (jsonObject.get(i).getAsJsonObject().get(CAMBRIA_URL) != null) {
						dmaap_hash.put(convertedname + ".cambria.url",
								jsonObject.get(i).getAsJsonObject().get(CAMBRIA_URL).toString().replace("\"", ""));
					}
					if (jsonObject.get(i).getAsJsonObject().get(BASIC_AUTH_PASSWORD) != null) {
						dmaap_hash.put(convertedname + ".basicAuthPassword", jsonObject.get(i).getAsJsonObject()
								.get(BASIC_AUTH_PASSWORD).toString().replace("\"", ""));
					}
					if (jsonObject.get(i).getAsJsonObject().get(BASIC_AUTH_USER_NAME) != null) {
						dmaap_hash.put(convertedname + ".basicAuthUsername", jsonObject.get(i).getAsJsonObject()
								.get(BASIC_AUTH_USER_NAME).toString().replace("\"", ""));
					}

				}
			} else {

				// Handing new format from controllergen2/config_binding_service
				JsonObject jsonObject = root.getAsJsonObject();
				Set<Map.Entry<String, JsonElement>> entries = jsonObject.entrySet();

				for (Map.Entry<String, JsonElement> entry : entries) {

					JsonElement topicurl = entry.getValue().getAsJsonObject().get("dmaap_info").getAsJsonObject()
							.get("topic_url");
					String[] urlParts = dmaapUrlSplit(topicurl.toString().replace("\"", ""));

					String mrTopic = null;
					String mrUrl = null;
					String[] hostport = null;
					String username = null;
					String userpwd = null;

					try {

						if (null != urlParts) {
							mrUrl = urlParts[2];

							// DCAE internal dmaap topic convention
							if ("events".equals(urlParts[3])) {
								mrTopic = urlParts[4];
							} else {
								// ONAP dmaap topic convention
								mrTopic = urlParts[3];
								hostport = mrUrl.split(":");
							}

						}
					} catch (NullPointerException e) {
						System.out.println("NullPointerException");
						e.getMessage();
					}

					if (entry.getValue().getAsJsonObject().has("aaf_username")) {
						username = entry.getValue().getAsJsonObject().get("aaf_username").toString().replace("\"", "");
					}
					if (entry.getValue().getAsJsonObject().has("aaf_password")) {
						userpwd = entry.getValue().getAsJsonObject().get("aaf_password").toString().replace("\"", "");
					}
					if (hostport == null) {
						log.debug(TOPIC + mrTopic + HOST_URL + mrUrl + PWD + userpwd + USER + username);
					} else {
						log.debug(TOPIC + mrTopic + HOST_URL + mrUrl + " HOSTS:" + hostport[0] + PWD
								+ userpwd + USER + username + " NAME:" + entry.getKey());
					}

					dmaap_hash.put(entry.getKey() + ".cambria.topic", mrTopic);

					if (!(hostport == null)) {
						dmaap_hash.put(entry.getKey() + ".cambria.hosts", hostport[0]);
					}

					if (!(mrUrl == null)) {
						dmaap_hash.put(entry.getKey() + ".cambria.url", mrUrl);
					}

					if (!(username == null)) {
						dmaap_hash.put(entry.getKey() + ".basicAuthUsername", username);
					}

					if (!(userpwd == null)) {
						dmaap_hash.put(entry.getKey() + ".basicAuthPassword", userpwd);
					}

				}

			}

		} catch (JsonIOException | JsonSyntaxException |

				FileNotFoundException e1) {
			e1.printStackTrace();
			log.error("Problem loading Dmaap Channel configuration file: " + e1.toString());
		} finally {
			if (fr != null) {
				try {
					fr.close();
				} catch (IOException e) {
					log.error("Error closing file reader stream : " + e.toString());
				}
			}
		}

	}

	/***
	 * Dmaap url structure pub - https://<dmaaphostname>:<port>/events/
	 * <namespace>.<dmaapcluster>.<topic>, sub - https://<dmaaphostname>:
	 * <port>/events/<namespace>.<dmaapcluster>.<topic>/G1/u1";
	 *
	 * Onap url structure pub - http://<dmaaphostname>:<port>/<unauthenticated>.
	 * <topic>,
	 */

	private String[] dmaapUrlSplit(String dmUrl) {
		String[] multUrls = dmUrl.split(",");

		StringBuffer newUrls = new StringBuffer();
		String[] urlParts = null;
		for (int i = 0; i < multUrls.length; i++) {
			urlParts = multUrls[i].split("/");
			if (i == 0) {
				newUrls = newUrls.append(urlParts[2]);
			} else {
				newUrls = newUrls.append(",").append(urlParts[2]);
			}
		}
		return urlParts;
	}

	public static synchronized DmaapPropertyReader getInstance(String channelConfig) {
		if (instance == null) {
			instance = new DmaapPropertyReader(channelConfig);
		}
		return instance;
	}

	public String getKeyValue(String hashKey) {
		return this.dmaap_hash.get(hashKey);
	}
}