aboutsummaryrefslogtreecommitdiffstats
path: root/sdnr/wireless-transport/code-Carbon-SR1/apps/devicemanager/impl/src/main/java/org/opendaylight/mwtn/base/database/HtDatabaseNode.java
blob: 1bffed3677402a9358aa48b8ac1945e3095bc886 (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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package org.opendaylight.mwtn.base.database;

import static org.elasticsearch.node.NodeBuilder.nodeBuilder;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.DatagramSocket;
import java.net.ServerSocket;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.node.Node;
import org.opendaylight.mwtn.base.internalTypes.Resources;
import org.opendaylight.mwtn.config.impl.AkkaConfig;
import org.opendaylight.mwtn.config.impl.AkkaConfig.ClusterNodeInfo;
import org.opendaylight.mwtn.config.impl.EsConfig;
import org.opendaylight.mwtn.config.impl.GeoConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HtDatabaseNode implements AutoCloseable {



	private static final Logger LOGGER = LoggerFactory.getLogger(HtDatabaseNode.class);
	private static final String DBCONFIGFILENAME = "etc/elasticsearch.yml";
	private static int MIN_PORT_NUMBER = 1024;
	private static int MAX_PORT_NUMBER = 65535;
	private static int ES_PORT = 9200;
	private static int DELAYSECONDS = 30;

	private static String pluginFolder="etc/elasticsearch-plugins";

	private static HtDatabaseNode oneNode = null;
	/**
	 *
	 */
	private static Integer initializedTarget = 0;
	private static Integer initializedReached = 0;

	private final Node node;

	private HtDatabaseNode() {
		LOGGER.debug("Start elasticsearch service");
		node = nodeBuilder().settings(Settings.builder().put("path.home", "etc").put("path.conf", "etc")).node();
		LOGGER.info("Starting Database service. Wait {} s", DELAYSECONDS);
		// Wait for green status but only wait for 2 seconds
		ClusterHealthResponse nodeStatus = node.client().admin().cluster().prepareHealth().setWaitForYellowStatus()
				.setTimeout(TimeValue.timeValueSeconds(DELAYSECONDS)).get();

		LOGGER.debug("Elasticsearch service started with status {}", nodeStatus.toString());
	}

	/**
	 * Close node
	 */
	@Override
	public void close() {
		node.close();
		oneNode = null; //Release the one instance that was started !
	}

	public Boolean getInitialized() {
		synchronized (initializedReached) {
			return initializedTarget != 0 && initializedReached == initializedTarget;
		}
	}

	public void setInitializedReached() {
		synchronized (initializedReached) {
			HtDatabaseNode.initializedReached++;
		}
	}

	public void setInitializedTarget() {
		synchronized (initializedTarget) {
			HtDatabaseNode.initializedTarget++;
		}
	}

	public Client getClient() {
		return node.client();
	}


	/* ---------------------------------------
	 * Static functions below
	 */


	private static void extractZip(String zipFile, String outputFolder) {

		byte[] buffer = new byte[1024];

		try {

			// create output directory is not exists
			File folder = new File(outputFolder);
			if (!folder.exists()) {
				folder.mkdir();
			}

			// get the zip file content
			ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
			// get the zipped file list entry
			ZipEntry ze = zis.getNextEntry();

			while (ze != null) {

				String fileName = ze.getName();

				File newFile = new File(outputFolder + File.separator + fileName);
				System.out.println("file unzip : " + newFile.getAbsoluteFile());
				if(ze.isDirectory())
				{
					newFile.mkdir();
				}
				else
				{

					// create all non exists folders
					// else you will hit FileNotFoundException for compressed folder
					new File(newFile.getParent()).mkdirs();

					FileOutputStream fos = new FileOutputStream(newFile);

					int len;
					while ((len = zis.read(buffer)) > 0) {
						fos.write(buffer, 0, len);
					}

					fos.close();
				}
				ze = zis.getNextEntry();
			}

			zis.closeEntry();
			zis.close();
		} catch (IOException ex) {
			LOGGER.warn("problem extracting " + zipFile + " to " + outputFolder);
		}
	}

	// Visibility package for test purpose
	static void checkorcreateplugins(String pluginFolder) {
		File f = new File(pluginFolder);
		String tmpFilename = pluginFolder + "/tmp.zip";
		if (!f.exists())
			f.mkdir();

		f = new File(pluginFolder + "/head");
		File tmpFile = new File(tmpFilename);
		if (!f.exists()) {
			LOGGER.debug("extracting head plugin");
			if (Resources.extractFileTo("/elasticsearch/plugins/head.zip", tmpFile)) {
				extractZip(tmpFile.getAbsolutePath(), pluginFolder);
			} else
				LOGGER.debug("problem extracting plugin res");
		}
		f = new File(pluginFolder + "/delete-by-query");
		if (!f.exists()) {
			LOGGER.debug("extracting head delete-by-query plugin");
			if (Resources.extractFileTo("/elasticsearch/plugins/delete-by-query.zip", tmpFile)) {
				extractZip(tmpFile.getAbsolutePath(), pluginFolder);
			} else
				LOGGER.debug("problem extracting plugin res");
		}
		if(tmpFile.exists())
			tmpFile.delete();

	}

	/**
	 * Checks to see if a specific port is available.
	 *
	 * @param port
	 *            the port to check for availability
	 */
	private static boolean isPortAvailable(int port) {
		if (port < MIN_PORT_NUMBER || port > MAX_PORT_NUMBER) {
			throw new IllegalArgumentException("Invalid start port: " + port);
		}

		ServerSocket ss = null;
		DatagramSocket ds = null;
		try {
			ss = new ServerSocket(port);
			ss.setReuseAddress(true);
			ds = new DatagramSocket(port);
			ds.setReuseAddress(true);
			return true;
		} catch (IOException e) {
		} finally {
			if (ds != null) {
				ds.close();
			}

			if (ss != null) {
				try {
					ss.close();
				} catch (IOException e) {
					/* should not be thrown */
				}
			}
		}

		return false;
	}

	private static void checkorcreateConfigFile(EsConfig config, AkkaConfig akkaConfig,GeoConfig geoConfig) {
		File f = new File(DBCONFIGFILENAME);
		if (!f.exists()) {
			LOGGER.debug("no " + DBCONFIGFILENAME + " found - extracting from resources");
			if (Resources.extractFileTo("/elasticsearch/elasticsearch.yml", f)) {
				// replace template values
				LOGGER.debug("replace template values with config:" + config);
				Charset charset = StandardCharsets.UTF_8;
				try {
					Path p = f.toPath();
					String hostName = "0.0.0.0"; //Default as initialisation value
					if(akkaConfig!=null && akkaConfig.isCluster())
					{
						LOGGER.debug("cluster configuration found");
						hostName=akkaConfig.getClusterConfig().getHostName(hostName);
						String clusterDBName=akkaConfig.getClusterConfig().getDBClusterName(null);
						String nodeName=String.format("node%d.%s",akkaConfig.getClusterConfig().getRoleMemberIndex(),clusterDBName);
						if(clusterDBName!=null)
						{
							config.setCluster(clusterDBName);
							config.setNode(nodeName);
							LOGGER.info("set db name to "+clusterDBName+" nodename="+nodeName );
						}
						else
							LOGGER.warn("unable to set correct db clustername");
					}
					String content = new String(Files.readAllBytes(p), charset);
					content = content.replaceAll("\\$clustername", config.getCluster()).replaceAll("\\$nodename",
							config.getNode()).replaceAll("\\$hostname", hostName);

					//add cluster configuration
					if(akkaConfig!=null && akkaConfig.isCluster())
					{
						List<ClusterNodeInfo> seedNodes=akkaConfig.getClusterConfig().getSeedNodes();
						String nodesJSONString="[\""+seedNodes.get(0).getRemoteAddress()+"\"";
						for(int i=1;i<seedNodes.size();i++)
							nodesJSONString+=",\""+seedNodes.get(i).getRemoteAddress()+"\"";
						nodesJSONString+="]";
						content+=System.lineSeparator()+String.format("discovery.zen.ping.unicast.hosts: %s",nodesJSONString);

						if(geoConfig!=null)
						{
							LOGGER.debug("adding zone configuration");
							content+=System.lineSeparator()+String.format("cluster.routing.allocation.awareness.force.zone.values: zone1,zone2");
							content+=System.lineSeparator()+String.format("cluster.routing.allocation.awareness.attributes: zone");
							if(geoConfig.isPrimary(akkaConfig.getClusterConfig().getRoleMember()))
							{
								content+=System.lineSeparator()+String.format("node.zone: zone1");
								LOGGER.debug("setting zone to zone1");
							}
							else
							{
								content+=System.lineSeparator()+String.format("node.zone: zone2");
								LOGGER.debug("setting zone to zone2");
							}
						}
					}
					Files.write(p, content.getBytes(charset));
				} catch (IOException e) {
					LOGGER.warn("problem replacing values in file: " + e.getMessage());

				}
			} else {
				LOGGER.warn("problem writing database.yml to etc folder from res");
			}
		}
	}

	/**
	 * Start as singleton
	 * @return the node
	 */
	public static HtDatabaseNode start(EsConfig config) throws IllegalStateException {
		return start(config,null,null);
	}

	public static HtDatabaseNode start(EsConfig config, AkkaConfig akkaConfig,GeoConfig geoConfig) {
		if (isPortAvailable(ES_PORT)) {
			LOGGER.info("ES Port not in use. Start internal ES.");
			if (oneNode == null) {
				checkorcreateplugins(pluginFolder);
				checkorcreateConfigFile(config,akkaConfig,geoConfig);
				oneNode = new HtDatabaseNode();
			} else
				throw new IllegalStateException("Database is already started, but can only be started once. Stop here.");
		} else {
			LOGGER.info("ES Port in use. External ES used.");
		}

		return oneNode;
	}

}