f1c8b6870b989d2866a27bb717150bbeeeed1689
[policy/drools-pdp.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * feature-distributed-locking
4  * ================================================================================
5  * Copyright (C) 2018 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.distributed.locking;
21
22 import java.sql.Connection;
23 import java.sql.PreparedStatement;
24 import java.sql.SQLException;
25 import java.util.Properties;
26 import java.util.UUID;
27 import org.apache.commons.dbcp2.BasicDataSource;
28 import org.apache.commons.dbcp2.BasicDataSourceFactory;
29 import org.onap.policy.common.utils.properties.exception.PropertyException;
30 import org.onap.policy.drools.core.lock.PolicyResourceLockFeatureAPI;
31 import org.onap.policy.drools.features.PolicyEngineFeatureAPI;
32 import org.onap.policy.drools.persistence.SystemPersistence;
33 import org.onap.policy.drools.system.PolicyEngine;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 public class DistributedLockingFeature implements PolicyEngineFeatureAPI, PolicyResourceLockFeatureAPI {
38         
39         /**
40          * Logger instance
41          */
42         private static final Logger logger = LoggerFactory.getLogger(DistributedLockingFeature.class);
43         
44         /**
45          * Properties Configuration Name
46          */
47         public static final String CONFIGURATION_PROPERTIES_NAME = "feature-distributed-locking";
48         
49         /**
50          * Properties for locking feature
51          */
52         private DistributedLockingProperties lockProps;
53         
54         /**
55          * Data source used to connect to the DB containing locks.
56          */
57         private BasicDataSource dataSource;
58         
59         /**
60          * UUID 
61          */
62         private static final UUID uuid = UUID.randomUUID();
63         
64         @Override
65         public int getSequenceNumber() {
66         return 1000;
67         }
68         
69         @Override
70     public OperResult beforeLock(String resourceId, String owner, int holdSec) {
71                 
72                 TargetLock tLock = new TargetLock(resourceId, uuid, owner, dataSource);
73
74         return(tLock.lock(holdSec) ? OperResult.OPER_ACCEPTED : OperResult.OPER_DENIED);                                
75         }
76     
77     @Override
78     public OperResult beforeRefresh(String resourceId, String owner, int holdSec) {
79         
80         TargetLock tLock = new TargetLock(resourceId, uuid, owner, dataSource);
81
82         return(tLock.refresh(holdSec) ? OperResult.OPER_ACCEPTED : OperResult.OPER_DENIED);                
83     }
84
85         @Override
86         public OperResult beforeUnlock(String resourceId, String owner) {
87                 TargetLock tLock = new TargetLock(resourceId, uuid, owner, dataSource);
88                 
89                 return(tLock.unlock() ? OperResult.OPER_ACCEPTED : OperResult.OPER_DENIED);
90         }
91         
92         @Override
93         public OperResult beforeIsLockedBy(String resourceId, String owner) {
94                 TargetLock tLock = new TargetLock(resourceId, uuid, owner, dataSource);
95
96         return(tLock.isActive() ? OperResult.OPER_ACCEPTED : OperResult.OPER_DENIED);
97         }
98         
99         @Override
100         public OperResult beforeIsLocked(String resourceId) {
101                 TargetLock tLock = new TargetLock(resourceId, uuid, "dummyOwner", dataSource);
102
103         return(tLock.isLocked() ? OperResult.OPER_ACCEPTED : OperResult.OPER_DENIED);
104         }
105         
106         @Override
107         public boolean afterStart(PolicyEngine engine) {
108
109                 try {
110                         this.lockProps = new DistributedLockingProperties(SystemPersistence.manager.getProperties(DistributedLockingFeature.CONFIGURATION_PROPERTIES_NAME));
111                         this.dataSource = makeDataSource();
112                 } catch (PropertyException e) {
113                         logger.error("DistributedLockingFeature feature properies have not been loaded", e);
114                         throw new DistributedLockingFeatureException(e);
115                 } catch(InterruptedException e) {
116             logger.error("DistributedLockingFeature failed to create data source", e);
117                     Thread.currentThread().interrupt();
118             throw new DistributedLockingFeatureException(e);
119         } catch(Exception e) {
120             logger.error("DistributedLockingFeature failed to create data source", e);
121             throw new DistributedLockingFeatureException(e);
122                 }
123                 
124                 cleanLockTable();
125                 
126                 return false;
127         }
128         
129         /**
130          * @return a new, pooled data source
131          * @throws Exception
132          */
133         private BasicDataSource makeDataSource() throws Exception {
134         Properties props = new Properties();
135         props.put("driverClassName", lockProps.getDbDriver());
136         props.put("url", lockProps.getDbUrl());
137         props.put("username", lockProps.getDbUser());
138         props.put("password", lockProps.getDbPwd());
139         props.put("testOnBorrow", "true");
140         props.put("poolPreparedStatements", "true");
141
142         // additional properties are listed in the GenericObjectPool API
143         
144         return BasicDataSourceFactory.createDataSource(props);
145     }
146
147     /**
148          * This method kills the heartbeat thread and calls refreshLockTable which removes
149          * any records from the db where the current host is the owner.
150          */
151         @Override
152         public boolean beforeShutdown(PolicyEngine engine) {
153                 cleanLockTable();
154                 return false;
155         }
156
157         /**
158          * This method removes all records owned by the current host from the db.
159          */
160         private void cleanLockTable() {
161                 
162             try (Connection conn = dataSource.getConnection();
163                         PreparedStatement statement = conn.prepareStatement(
164                                         "DELETE FROM pooling.locks WHERE host = ? OR expirationTime < now()");
165                 ){
166                         
167                                 statement.setString(1, uuid.toString());
168                                 statement.executeUpdate();
169                         
170                 } catch (SQLException e) {
171                         logger.error("error in refreshLockTable()", e);
172                 }
173                 
174         }       
175 }