aboutsummaryrefslogtreecommitdiffstats
path: root/mdbc-server/src/main/java/org/onap/music/mdbc/MdbcServer.java
blob: 500ed81668a5c10465c5eee495d398185d70a8ff (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
/*
 * ============LICENSE_START====================================================
 * org.onap.music.mdbc
 * =============================================================================
 * Copyright (C) 2018 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.music.mdbc;

import org.onap.music.mdbc.configurations.NodeConfiguration;
import org.onap.music.mdbc.tables.MusicTxDigestDaemon;
import org.apache.calcite.avatica.remote.Driver.Serialization;
import org.apache.calcite.avatica.remote.LocalService;
import org.apache.calcite.avatica.server.HttpServer;
import org.apache.calcite.avatica.util.Unsafe;

import org.onap.music.logging.EELFLoggerDelegate;
import com.beust.jcommander.IStringConverter;
import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;

import java.util.Locale;
import java.util.Properties;

public class MdbcServer {
  private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MdbcStatement.class);

  @Parameter(names = { "-c", "--configuration" }, required = true,
      description = "This is the file that contains the ranges that are assigned to this MDBC server")
  private String configurationFile;

  @Parameter(names = { "-u", "--url" }, required = true,
      description = "JDBC driver url for the server")
  private String url;

  @Parameter(names = { "-p", "--port" }, required = true,
      description = "Port the server should bind")
  private int port;

  @Parameter(names = { "-s", "--user" }, required = true,
      description = "Mysql usr")
  private String user;

  @Parameter(names = { "-a", "--pass" }, required = true,
      description = "Mysql password")
  private String password;

  final private Serialization serialization = Serialization.PROTOBUF;

  @Parameter(names = { "-h", "-help", "--help" }, help = true,
      description = "Print the help message")
  private boolean help = false;

  private NodeConfiguration config;
  private HttpServer server;
  private MdbcServerLogic meta;

  public void start() {
    if (null != server) {
      logger.error("The server was already started");
      Unsafe.systemExit(ExitCodes.ALREADY_STARTED.ordinal());
      return;
    }

    try {
    	config = NodeConfiguration.readJsonFromFile(configurationFile);
    	//\TODO Add configuration file with Server Info
    	Properties connectionProps = new Properties();
    	connectionProps.put("user", user);
    	connectionProps.put("password", password);
    	String defaultMusicMixin = Utils.getDefaultMusicMixin();
    	if(defaultMusicMixin!=null){
    		connectionProps.put(Configuration.KEY_MUSIC_MIXIN_NAME,defaultMusicMixin);
		}
    	String defaultDBMixin = Utils.getDefaultDBMixin();
		if(defaultMusicMixin!=null){
    		connectionProps.put(Configuration.KEY_DB_MIXIN_NAME,defaultDBMixin);
		}
		Utils.registerDefaultDrivers();
    	meta = new MdbcServerLogic(url,connectionProps,config);
    	LocalService service = new LocalService(meta);

    	// Construct the server
    	this.server = new HttpServer.Builder<>()
    			.withHandler(service, serialization)
    			.withPort(port)
    			.build();

    	// Then start it
    	server.start();
  	  
    	logger.info("Started Avatica server on port {} with serialization {}", server.getPort(),
    			serialization);
    } catch (Exception e) {
    	logger.error("Failed to start Avatica server", e);
    	Unsafe.systemExit(ExitCodes.START_FAILED.ordinal());
    }  
    
  }

  public void stop() {
	  if (null != server) {
	      meta.getStateManager().releaseAllPartitions();
		  server.stop();
		  server = null;
	  }
  }

  public void join() throws InterruptedException {
	  server.join();
  }

  public static void main(String[] args) {
	  final MdbcServer server = new MdbcServer();
	  @SuppressWarnings("deprecation")
	  JCommander jc = new JCommander(server, args);
	  if (server.help) {
		  jc.usage();
		  Unsafe.systemExit(ExitCodes.USAGE.ordinal());
		  return;
	  }

	  server.start();

	  // Try to clean up when the server is stopped.
	  Runtime.getRuntime().addShutdownHook(
			  new Thread(new Runnable() {
				  @Override public void run() {
					  logger.info("Stopping server");
					  server.stop();
					  logger.info("Server stopped");
				  }
			  }));

	  try {
		  server.join();
	  } catch (InterruptedException e) {
		  // Reset interruption
		  Thread.currentThread().interrupt();
		  // And exit now.
		  return;
	  }
  }

  /**
   * Converter from String to Serialization. Must be public for JCommander.
   */
  public static class SerializationConverter implements IStringConverter<Serialization> {
	  @Override public Serialization convert(String value) {
		  return Serialization.valueOf(value.toUpperCase(Locale.ROOT));
	  }
  }

  /**
   * Codes for exit conditions
   */
  private enum ExitCodes {
	  NORMAL,
	  ALREADY_STARTED, // 1
	  START_FAILED,    // 2
	  USAGE;           // 3
  }
}

// End StandaloneServer.java