2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.plugins.context.locking.curator;
23 import java.util.concurrent.TimeUnit;
24 import java.util.concurrent.locks.ReadWriteLock;
26 import org.apache.curator.framework.CuratorFramework;
27 import org.apache.curator.framework.CuratorFrameworkFactory;
28 import org.apache.curator.framework.state.ConnectionState;
29 import org.apache.curator.framework.state.ConnectionStateListener;
30 import org.apache.curator.retry.ExponentialBackoffRetry;
31 import org.apache.curator.utils.CloseableUtils;
32 import org.apache.zookeeper.CreateMode;
33 import org.onap.policy.apex.context.ContextException;
34 import org.onap.policy.apex.context.impl.locking.AbstractLockManager;
35 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
36 import org.onap.policy.common.parameters.ParameterService;
37 import org.slf4j.ext.XLogger;
38 import org.slf4j.ext.XLoggerFactory;
41 * The Class CuratorLockManager manages the Curator interface towards Zookeeper for administering the Apex Context Album
44 public class CuratorLockManager extends AbstractLockManager {
45 // Logger for this class
46 private static final XLogger LOGGER = XLoggerFactory.getXLogger(CuratorLockManager.class);
48 // The Curator framework used for locking
49 private CuratorFramework curatorFramework;
51 // The address of the Zookeeper server
52 private String curatorZookeeperAddress;
55 * Constructor, set up a lock manager that uses Curator locking.
57 * @throws ContextException On errors connecting to Curator
59 public CuratorLockManager() throws ContextException {
60 LOGGER.entry("CuratorLockManager(): setting up the Curator lock manager . . .");
62 LOGGER.exit("CuratorLockManager(): Curator lock manager set up");
68 * @see org.onap.policy.apex.context.impl.locking.AbstractLockManager#init(org.onap.policy.apex. model.
69 * basicmodel.concepts.AxArtifactKey)
72 public void init(final AxArtifactKey key) throws ContextException {
73 LOGGER.entry("init(" + key + ")");
77 // Get the lock manager parameters
78 final CuratorLockManagerParameters lockParameters = ParameterService
79 .get(CuratorLockManagerParameters.class.getSimpleName());
81 // Check if the curator address has been set
82 curatorZookeeperAddress = lockParameters.getZookeeperAddress();
83 if (curatorZookeeperAddress == null || curatorZookeeperAddress.trim().length() == 0) {
84 String message = "could not set up Curator locking, "
85 + "check if the curator Zookeeper address parameter is set correctly";
87 throw new ContextException(message);
90 // Set up the curator framework we'll use
91 curatorFramework = CuratorFrameworkFactory.builder().connectString(curatorZookeeperAddress)
92 .retryPolicy(new ExponentialBackoffRetry(lockParameters.getZookeeperConnectSleepTime(),
93 lockParameters.getZookeeperContextRetries()))
96 // Listen for changes on the Curator connection
97 curatorFramework.getConnectionStateListenable().addListener(new CuratorManagerConnectionStateListener());
99 // Start the framework and specify Ephemeral nodes
100 curatorFramework.start();
102 // Wait for the connection to be made
104 curatorFramework.blockUntilConnected(
105 lockParameters.getZookeeperConnectSleepTime() * lockParameters.getZookeeperContextRetries(),
106 TimeUnit.MILLISECONDS);
107 } catch (final InterruptedException e) {
108 // restore the interrupt status
109 Thread.currentThread().interrupt();
110 String message = "error connecting to Zookeeper server at \"" + curatorZookeeperAddress
111 + "\", wait for connection timed out";
112 LOGGER.warn(message);
113 throw new ContextException(message);
116 if (!curatorFramework.getZookeeperClient().isConnected()) {
117 String message = "could not connect to Zookeeper server at \"" + curatorZookeeperAddress
118 + "\", see error log for details";
119 LOGGER.warn(message);
120 throw new ContextException(message);
123 // We'll use Ephemeral nodes for locks on the Zookeeper server
124 curatorFramework.create().withMode(CreateMode.EPHEMERAL_SEQUENTIAL);
126 LOGGER.exit("init(" + key + "," + lockParameters + ")");
132 * @see org.onap.policy.apex.core.context.impl.locking.AbstractLockManager#getReentrantReadWriteLock(
136 public ReadWriteLock getReentrantReadWriteLock(final String lockId) throws ContextException {
137 // Check if the framework is active
138 if (curatorFramework != null && curatorFramework.getZookeeperClient().isConnected()) {
139 return new CuratorReentrantReadWriteLock(curatorFramework, "/" + lockId);
141 throw new ContextException("creation of lock using Zookeeper server at \"" + curatorZookeeperAddress
142 + "\", failed, see error log for details");
149 * @see org.onap.policy.apex.core.context.LockManager#shutdown()
152 public void shutdown() {
153 if (curatorFramework == null) {
156 CloseableUtils.closeQuietly(curatorFramework);
157 curatorFramework = null;
161 * This class is a callback class for state changes on the curator to Zookeeper connection.
163 private class CuratorManagerConnectionStateListener implements ConnectionStateListener {
168 * @see org.apache.curator.framework.state.ConnectionStateListener#stateChanged(org.apache.
169 * curator.framework.CuratorFramework, org.apache.curator.framework.state.ConnectionState)
172 public void stateChanged(final CuratorFramework incomngCuratorFramework, final ConnectionState newState) {
173 // Is the state changed for this curator framework?
174 if (!incomngCuratorFramework.equals(curatorFramework)) {
178 LOGGER.info("curator state of client \"{}\" connected to \"{}\" changed to {}", curatorFramework,
179 curatorZookeeperAddress, newState);
181 if (newState != ConnectionState.CONNECTED) {