RA: Expose resource locking as DG node
[ccsdk/sli/adaptors.git] / resource-assignment / provider / src / main / java / org / onap / ccsdk / sli / adaptors / ra / ResourceLockNode.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : SDN-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                      reserved.
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.ccsdk.sli.adaptors.ra;
23
24 import java.security.SecureRandom;
25 import java.util.Map;
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;
32
33 public class ResourceLockNode implements SvcLogicJavaPlugin {
34
35     private static final Logger log = LoggerFactory.getLogger(ResourceLockNode.class);
36
37     private LockHelper lockHelper;
38
39     public void setLockHelper(LockHelper lockHelper) {
40         this.lockHelper = lockHelper;
41     }
42
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);
48
49         lockHelper.lock(resourceName, lockRequester, lockTimeout);
50     }
51
52     public void unlockResource(Map<String, String> paramMap, SvcLogicContext ctx) throws SvcLogicException {
53         String resourceName = getParam(paramMap, "resource-name", true, null);
54
55         lockHelper.unlock(resourceName, false);
56     }
57
58     public void lockResource(String resourceName, String lockRequester, int lockTimeout /* sec */) {
59         if (lockRequester == null) {
60             lockRequester = generateLockRequester();
61         }
62         if (lockTimeout <= 0) {
63             lockTimeout = 600;
64         }
65
66         lockHelper.lock(resourceName, lockRequester, lockTimeout);
67     }
68
69     public void unlockResource(String resourceName) {
70         lockHelper.unlock(resourceName, false);
71     }
72
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());
78             return v.trim();
79         }
80         if (required) {
81             throw new SvcLogicException("The following node parameter is required: " + name);
82         }
83
84         log.info("Param: " + name + " not supplied. Using default: " + def);
85         return def;
86     }
87
88     private static String generateLockRequester() {
89         SecureRandom rand = new SecureRandom();
90         return "SynchronizedFunction-" + (int) (rand.nextDouble() * 1000000);
91     }
92 }
93