[POLICY-22] Reorganizing drools-apps
[policy/drools-applications.git] / controlloop / common / guard / src / main / java / org / onap / policy / guard / PolicyGuard.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * guard
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.policy.guard;
21
22 import java.util.HashMap;
23 import java.util.Map;
24 import java.util.UUID;
25
26 import org.onap.policy.controlloop.policy.TargetType;
27 import org.onap.policy.guard.impl.PNFTargetLock;
28 import org.onap.policy.guard.impl.VMTargetLock;
29
30 public class PolicyGuard {
31
32         private static Map<String, TargetLock> activeLocks = new HashMap<String, TargetLock>();
33                 
34         public static class LockResult<A, B> {
35                 private A a;
36                 private B b;
37                 
38                 public static <A, B> LockResult<A, B> createLockResult(A a, B b) {
39                         return new LockResult<A, B>(a, b);
40                 }
41                 
42                 public LockResult(A a, B b) {
43                         this.a = a;
44                         this.b = b;
45                 }
46                 
47                 public A getA() {
48                         return a;
49                 }
50                 
51                 public B getB() {
52                         return b;
53                 }
54         }
55         
56         public static LockResult<GuardResult, TargetLock> lockTarget(TargetType targetType, String targetInstance, UUID requestID, LockCallback callback) {
57                 
58                 synchronized(activeLocks) {
59                         //
60                         // Is there a lock on this instance already?
61                         //
62                         if (activeLocks.containsKey(targetInstance)) {
63                                 return LockResult.createLockResult(GuardResult.LOCK_DENIED, null);
64                         }
65                         TargetLock lock = null;
66                         switch (targetType) {
67                         case PNF:
68                                 //
69                                 // Create the Lock object
70                                 //
71                                 lock = new PNFTargetLock(targetType, targetInstance, requestID, callback);
72                                 break;
73                         case VM:
74                                 //
75                                 // Create the Lock object
76                                 //
77                                 lock = new VMTargetLock(targetType, targetInstance, requestID, callback);
78                                 break;
79                         default:
80                                 return LockResult.createLockResult(GuardResult.LOCK_EXCEPTION, null);
81                         }
82                         //
83                         // Keep track of it
84                         //
85                         activeLocks.put(targetInstance, lock);
86                         //
87                         // Return result
88                         //
89                         System.out.println("Locking " + lock);
90                         return LockResult.createLockResult(GuardResult.LOCK_ACQUIRED, lock);
91                 }
92         }
93         
94         public static boolean   unlockTarget(TargetLock lock) {
95                 synchronized(activeLocks) {
96                         if (activeLocks.containsKey(lock.getTargetInstance())) {
97                                 System.out.println("Unlocking " + lock);
98                                 return (activeLocks.remove(lock.getTargetInstance()) != null);
99                         }
100                         return false;
101                 }
102         }
103         
104         public static boolean   isLocked(TargetType targetType, String targetInstance, UUID requestID) {
105                 synchronized(activeLocks) {
106                         if (activeLocks.containsKey(targetInstance)) {
107                                 TargetLock lock = activeLocks.get(targetInstance);
108                                 return (lock.getTargetType().equals(targetType) && lock.getRequestID().equals(requestID));
109                         }
110                         return false;
111                 }               
112         }
113         
114 }