2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.ccsdk.sli.adaptors.ra;
24 import java.security.SecureRandom;
26 import org.onap.ccsdk.sli.adaptors.lock.comp.LockHelper;
27 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
28 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
29 import org.onap.ccsdk.sli.core.sli.SvcLogicJavaPlugin;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 public class ResourceLockNode implements SvcLogicJavaPlugin {
35 private static final Logger log = LoggerFactory.getLogger(ResourceLockNode.class);
37 private LockHelper lockHelper;
39 public void setLockHelper(LockHelper lockHelper) {
40 this.lockHelper = lockHelper;
43 public void lockResource(Map<String, String> paramMap, SvcLogicContext ctx) throws SvcLogicException {
44 String resourceName = getParam(paramMap, "resource-name", true, null);
45 String lockRequester = getParam(paramMap, "lock-requester", false, generateLockRequester());
46 String lockTimeoutStr = getParam(paramMap, "lock-timeout", false, "600"); // Default lock timeout: 10 min
47 int lockTimeout = Integer.parseInt(lockTimeoutStr);
49 lockHelper.lock(resourceName, lockRequester, lockTimeout);
52 public void unlockResource(Map<String, String> paramMap, SvcLogicContext ctx) throws SvcLogicException {
53 String resourceName = getParam(paramMap, "resource-name", true, null);
55 lockHelper.unlock(resourceName, false);
58 public void lockResource(String resourceName, String lockRequester, int lockTimeout /* sec */) {
59 if (lockRequester == null) {
60 lockRequester = generateLockRequester();
62 if (lockTimeout <= 0) {
66 lockHelper.lock(resourceName, lockRequester, lockTimeout);
69 public void unlockResource(String resourceName) {
70 lockHelper.unlock(resourceName, false);
73 private String getParam(Map<String, String> paramMap, String name, boolean required, String def)
74 throws SvcLogicException {
75 String v = paramMap.get(name);
76 if (v != null && v.trim().length() > 0) {
77 log.info("Param: " + name + ": " + v.trim());
81 throw new SvcLogicException("The following node parameter is required: " + name);
84 log.info("Param: " + name + " not supplied. Using default: " + def);
88 private static String generateLockRequester() {
89 SecureRandom rand = new SecureRandom();
90 return "SynchronizedFunction-" + (int) (rand.nextDouble() * 1000000);