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.context.impl.locking;
23 import java.util.Collections;
24 import java.util.HashMap;
26 import java.util.concurrent.locks.ReadWriteLock;
28 import org.onap.policy.apex.context.ContextException;
29 import org.onap.policy.apex.context.LockManager;
30 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
31 import org.slf4j.ext.XLogger;
32 import org.slf4j.ext.XLoggerFactory;
35 * This class implements the {@link LockManager} functionality that is common across all implementations. Lock managers
36 * for specific lock mechanisms specialize this class.
38 * @author Liam Fallon (liam.fallon@ericsson.com)
40 public abstract class AbstractLockManager implements LockManager {
41 // Logger for this class
42 private static final XLogger LOGGER = XLoggerFactory.getXLogger(AbstractLockManager.class);
44 // Recurring string constants
45 private static final String CONTEXT_ITEM = " context item ";
47 // The key of this lock manager
48 private AxArtifactKey key = null;
50 // Map of locks in use on this distributor for each context map
51 private final Map<String, Map<String, ReadWriteLock>> lockMaps = Collections
52 .synchronizedMap(new HashMap<String, Map<String, ReadWriteLock>>());
57 * @see org.onap.policy.apex.context.LockManager#init(org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey)
60 public void init(final AxArtifactKey lockManagerKey) throws ContextException {
61 this.key = lockManagerKey;
67 * @see org.onap.policy.apex.context.LockManager#getKey()
70 public AxArtifactKey getKey() {
77 * @see org.onap.policy.apex.core.context.LockManager#lockForReading(org.onap.policy.apex.core.model.concepts.
78 * AxArtifactKey, java.lang.String)
81 public synchronized void lockForReading(final String lockTypeKey, final String lockKey) throws ContextException {
82 LOGGER.entry("lockForReading(" + lockTypeKey + "_" + lockKey + ")");
84 // Find the lock or create a new one
85 final ReadWriteLock lock = getLock(lockTypeKey, lockKey, true);
88 lock.readLock().lock();
89 LOGGER.exit("lockForReading(" + lockTypeKey + "_" + lockKey + ")");
90 } catch (final Exception e) {
91 LOGGER.warn("error acquiring read lock on context map " + lockTypeKey + CONTEXT_ITEM + lockKey, e);
92 throw new ContextException(
93 "error acquiring read lock on context map " + lockTypeKey + CONTEXT_ITEM + lockKey, e);
100 * @see org.onap.policy.apex.core.context.LockManager#lockForWriting(java.lang.String, java.lang.String)
103 public synchronized void lockForWriting(final String lockTypeKey, final String lockKey) throws ContextException {
104 LOGGER.entry("lockForWriting(" + lockTypeKey + "_" + lockKey + ")");
106 // Find the lock or create a new one
107 final ReadWriteLock lock = getLock(lockTypeKey, lockKey, true);
110 lock.writeLock().lock();
111 LOGGER.exit("lockForWriting(" + lockTypeKey + "_" + lockKey + ")");
112 } catch (final Exception e) {
113 LOGGER.warn("error acquiring write lock on context map " + lockTypeKey + CONTEXT_ITEM + lockKey, e);
114 throw new ContextException(
115 "error acquiring write lock on context map " + lockTypeKey + CONTEXT_ITEM + lockKey, e);
122 * @see org.onap.policy.apex.core.context.LockManager#unlockForReading(java.lang.String, java.lang.String)
125 public void unlockForReading(final String lockTypeKey, final String lockKey) throws ContextException {
126 LOGGER.entry("unlockForReading(" + lockTypeKey + "_" + lockKey + ")");
129 final ReadWriteLock lock = getLock(lockTypeKey, lockKey, false);
132 lock.readLock().unlock();
133 LOGGER.exit("unlockForReading(" + lockTypeKey + "_" + lockKey + ")");
134 } catch (final Exception e) {
135 LOGGER.warn("error releasing read lock on context map " + lockTypeKey + CONTEXT_ITEM + lockKey, e);
136 throw new ContextException(
137 "error releasing read lock on context map " + lockTypeKey + CONTEXT_ITEM + lockKey, e);
144 * @see org.onap.policy.apex.core.context.LockManager#unlockForWriting(java.lang.String, java.lang.String)
147 public void unlockForWriting(final String lockTypeKey, final String lockKey) throws ContextException {
148 LOGGER.entry("unlockForWriting(" + lockTypeKey + "_" + lockKey + ")");
151 final ReadWriteLock lock = getLock(lockTypeKey, lockKey, false);
154 lock.writeLock().unlock();
155 LOGGER.exit("unlockForWriting(" + lockTypeKey + "_" + lockKey + ")");
156 } catch (final Exception e) {
157 LOGGER.warn("error releasing write lock on context map " + lockTypeKey + CONTEXT_ITEM + lockKey, e);
158 throw new ContextException(
159 "error releasing write lock on context map " + lockTypeKey + CONTEXT_ITEM + lockKey, e);
164 * Get a reentrant read write lock from whatever locking mechanism is in use.
166 * @param lockId The unique ID of the lock.
168 * @throws ContextException On errors getting a lock
170 protected abstract ReadWriteLock getReentrantReadWriteLock(String lockId) throws ContextException;
173 * Get a lock for a context item in a context map.
175 * @param lockTypeKey The key of the map where the context item to lock is
176 * @param lockKey The key on the map to lock
177 * @param createMode if true, create a lock if it does not exist
179 * @throws ContextException On errors getting the lock
181 private ReadWriteLock getLock(final String lockTypeKey, final String lockKey, final boolean createMode)
182 throws ContextException {
183 // Check if we have a lock type map for this lock type yet
184 if (!lockMaps.containsKey(lockTypeKey)) {
185 // Create a lock type map for the lock type
186 lockMaps.put(lockTypeKey, Collections.synchronizedMap(new HashMap<String, ReadWriteLock>()));
189 // Find or create a lock in the lock map
190 ReadWriteLock lock = lockMaps.get(lockTypeKey).get(lockKey);
195 // Should we create a lock?
196 String errorMessage = "error getting lock on context map " + lockTypeKey + CONTEXT_ITEM + lockKey;
198 String message = errorMessage + ", lock does not exist";
199 LOGGER.warn(message);
200 throw new ContextException(message);
204 // Create the lock using the specialization of this abstract class
205 lock = getReentrantReadWriteLock(lockTypeKey + "_" + lockKey);
207 // Add the lock to the lock map
208 lockMaps.get(lockTypeKey).put(lockKey, lock);
210 if (LOGGER.isTraceEnabled()) {
211 LOGGER.trace("created lock {}_{}", lockTypeKey, lockKey);
214 } catch (final Exception e) {
215 LOGGER.warn(errorMessage, e);
216 throw new ContextException(errorMessage, e);