ffcbb351c8f5e6bfa56d16dac4f07ce64e62e7c4
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.context.test.utils;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.net.InetSocketAddress;
26
27 import org.apache.zookeeper.server.NIOServerCnxnFactory;
28 import org.apache.zookeeper.server.ZooKeeperServer;
29 import org.slf4j.ext.XLogger;
30 import org.slf4j.ext.XLoggerFactory;
31
32 public class ZooKeeperServerServiceProvider {
33     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ZooKeeperServerServiceProvider.class);
34
35     private NIOServerCnxnFactory zookeeperFactory;
36     private File zookeeperDirectory;
37     private InetSocketAddress addr;
38
39     public ZooKeeperServerServiceProvider(final File zookeeperDirectory, final InetSocketAddress addr) {
40         this.zookeeperDirectory = zookeeperDirectory;
41         this.addr = addr;
42     }
43
44     public ZooKeeperServerServiceProvider(final File zookeeperDirectory, final String addr, final int port) {
45         this.zookeeperDirectory = zookeeperDirectory;
46         this.addr = new InetSocketAddress(addr, port);
47     }
48
49     public void startZookeeperServer() throws IOException, InterruptedException {
50         LOGGER.info("Starting up ZooKeeperServer using address: {} and port: {}", addr.getAddress(), addr.getPort());
51         final ZooKeeperServer server = new ZooKeeperServer(zookeeperDirectory, zookeeperDirectory, 5000);
52         zookeeperFactory = new NIOServerCnxnFactory();
53         zookeeperFactory.configure(addr, 100);
54         zookeeperFactory.startup(server);
55     }
56
57     public void stopZookeeperServer() {
58         LOGGER.info("Stopping ZooKeeperServer for address: {} and port: {}", addr.getAddress(), addr.getPort());
59         if (zookeeperFactory != null) {
60             zookeeperFactory.shutdown();
61         }
62     }
63
64 }