d2976d799566ee8612a97c5a97e7ea0fe88a9844
[policy/apex-pdp.git] /
1 /*-
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
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.plugins.context.locking.curator;
22
23 import java.util.concurrent.TimeUnit;
24 import java.util.concurrent.locks.ReadWriteLock;
25
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;
39
40 /**
41  * The Class CuratorLockManager manages the Curator interface towards Zookeeper for administering the Apex Context Album
42  * instance locks..
43  */
44 public class CuratorLockManager extends AbstractLockManager {
45     // Logger for this class
46     private static final XLogger LOGGER = XLoggerFactory.getXLogger(CuratorLockManager.class);
47
48     // The Curator framework used for locking
49     private CuratorFramework curatorFramework;
50
51     // The address of the Zookeeper server
52     private String curatorZookeeperAddress;
53
54     /**
55      * Constructor, set up a lock manager that uses Curator locking.
56      *
57      * @throws ContextException On errors connecting to Curator
58      */
59     public CuratorLockManager() throws ContextException {
60         LOGGER.entry("CuratorLockManager(): setting up the Curator lock manager . . .");
61
62         LOGGER.exit("CuratorLockManager(): Curator lock manager set up");
63     }
64
65     /*
66      * (non-Javadoc)
67      *
68      * @see org.onap.policy.apex.context.impl.locking.AbstractLockManager#init(org.onap.policy.apex. model.
69      * basicmodel.concepts.AxArtifactKey)
70      */
71     @Override
72     public void init(final AxArtifactKey key) throws ContextException {
73         LOGGER.entry("init(" + key + ")");
74
75         super.init(key);
76
77         // Get the lock manager parameters
78         final CuratorLockManagerParameters lockParameters = ParameterService
79                         .get(CuratorLockManagerParameters.class.getSimpleName());
80
81         // Check if the curator address has been set
82         curatorZookeeperAddress = lockParameters.getZookeeperAddress();
83         if (curatorZookeeperAddress == null || curatorZookeeperAddress.trim().length() == 0) {
84             LOGGER.warn("could not set up Curator locking, check if the curator Zookeeper address parameter is set correctly");
85             throw new ContextException(
86                             "could not set up Curator locking, check if the curator Zookeeper address parameter is set correctly");
87         }
88
89         // Set up the curator framework we'll use
90         curatorFramework = CuratorFrameworkFactory.builder().connectString(curatorZookeeperAddress)
91                         .retryPolicy(new ExponentialBackoffRetry(lockParameters.getZookeeperConnectSleepTime(),
92                                         lockParameters.getZookeeperContextRetries()))
93                         .build();
94
95         // Listen for changes on the Curator connection
96         curatorFramework.getConnectionStateListenable().addListener(new CuratorManagerConnectionStateListener());
97
98         // Start the framework and specify Ephemeral nodes
99         curatorFramework.start();
100
101         // Wait for the connection to be made
102         try {
103             curatorFramework.blockUntilConnected(
104                             lockParameters.getZookeeperConnectSleepTime() * lockParameters.getZookeeperContextRetries(),
105                             TimeUnit.MILLISECONDS);
106         } catch (final InterruptedException e) {
107             // restore the interrupt status
108             Thread.currentThread().interrupt();
109             String message = "error connecting to Zookeeper server at \"" + curatorZookeeperAddress
110                             + "\", wait for connection timed out";
111             LOGGER.warn(message);
112             throw new ContextException(message);
113         }
114
115         if (!curatorFramework.getZookeeperClient().isConnected()) {
116             String message = "could not connect to Zookeeper server at \"" + curatorZookeeperAddress
117                             + "\", see error log for details";
118             LOGGER.warn(message);
119             throw new ContextException(message);
120         }
121
122         // We'll use Ephemeral nodes for locks on the Zookeeper server
123         curatorFramework.create().withMode(CreateMode.EPHEMERAL_SEQUENTIAL);
124
125         LOGGER.exit("init(" + key + "," + lockParameters + ")");
126     }
127
128     /*
129      * (non-Javadoc)
130      *
131      * @see org.onap.policy.apex.core.context.impl.locking.AbstractLockManager#getReentrantReadWriteLock(
132      * java.lang.String)
133      */
134     @Override
135     public ReadWriteLock getReentrantReadWriteLock(final String lockId) throws ContextException {
136         // Check if the framework is active
137         if (curatorFramework != null && curatorFramework.getZookeeperClient().isConnected()) {
138             return new CuratorReentrantReadWriteLock(curatorFramework, "/" + lockId);
139         } else {
140             throw new ContextException("creation of lock using Zookeeper server at \"" + curatorZookeeperAddress
141                             + "\", failed, see error log for details");
142         }
143     }
144
145     /*
146      * (non-Javadoc)
147      *
148      * @see org.onap.policy.apex.core.context.LockManager#shutdown()
149      */
150     @Override
151     public void shutdown() {
152         if (curatorFramework == null) {
153             return;
154         }
155         CloseableUtils.closeQuietly(curatorFramework);
156         curatorFramework = null;
157     }
158
159     /**
160      * This class is a callback class for state changes on the curator to Zookeeper connection.
161      */
162     private class CuratorManagerConnectionStateListener implements ConnectionStateListener {
163
164         /*
165          * (non-Javadoc)
166          *
167          * @see org.apache.curator.framework.state.ConnectionStateListener#stateChanged(org.apache.
168          * curator.framework.CuratorFramework, org.apache.curator.framework.state.ConnectionState)
169          */
170         @Override
171         public void stateChanged(final CuratorFramework incomngCuratorFramework, final ConnectionState newState) {
172             // Is the state changed for this curator framework?
173             if (!incomngCuratorFramework.equals(curatorFramework)) {
174                 return;
175             }
176
177             LOGGER.info("curator state of client \"{}\" connected to \"{}\" changed to {}", curatorFramework,
178                             curatorZookeeperAddress, newState);
179
180             if (newState != ConnectionState.CONNECTED) {
181                 shutdown();
182             }
183         }
184     }
185 }