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