Drop dependency on commons-text
[aaa.git] / aaa-idm-store-h2 / src / main / java / org / opendaylight / aaa / datastore / h2 / GrantStore.java
1 /*
2  * Copyright (c) 2014, 2017 Hewlett-Packard Development Company, L.P. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.aaa.datastore.h2;
10
11 import com.google.common.annotations.VisibleForTesting;
12 import java.sql.ResultSet;
13 import java.sql.SQLException;
14 import java.sql.Statement;
15 import org.opendaylight.aaa.api.IDMStoreUtil;
16 import org.opendaylight.aaa.api.model.Grant;
17 import org.opendaylight.aaa.api.model.Grants;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 /**
22  * Grant store.
23  *
24  * @author peter.mellquist@hp.com
25  */
26 final class GrantStore extends AbstractStore<Grant> {
27     private static final Logger LOG = LoggerFactory.getLogger(GrantStore.class);
28
29     static final String TABLE = "AAA_GRANTS";
30
31     static {
32         SQLTable.GRANT.verifyTable(TABLE);
33     }
34
35     // FIXME: javadoc
36     static final String COL_ID = "grantid";
37
38     // FIXME: javadoc
39     // FIXME: 'tenant' vs 'domain' ?
40     @VisibleForTesting
41     static final String COL_TENANTID = "domainid";
42
43     // FIXME: javadoc
44     @VisibleForTesting
45     static final String COL_USERID = "userid";
46     // FIXME: javadoc
47     @VisibleForTesting
48     static final String COL_ROLEID = "roleid";
49
50     GrantStore(final ConnectionProvider dbConnectionFactory) {
51         super(dbConnectionFactory, TABLE);
52     }
53
54     @Override
55     void createTable(final Statement stmt) throws SQLException {
56         stmt.executeUpdate("CREATE TABLE " + TABLE + " ("
57             + COL_ID       + " VARCHAR(128) PRIMARY KEY, "
58             // FIXME: foreign key to DomainStore.COL_ID?
59             + COL_TENANTID + " VARCHAR(128) NOT NULL, "
60             // FIXME: foreign key to UserStore.COL_ID?
61             + COL_USERID   + " VARCHAR(128) NOT NULL, "
62             // FIXME: foreign key to RoleStore.COL_ID?
63             + COL_ROLEID   + " VARCHAR(128) NOT NULL)");
64     }
65
66     @Override
67     void cleanTable(final Statement stmt) throws SQLException {
68         stmt.execute("DELETE FROM " + TABLE);
69     }
70
71     @Override
72     protected Grant fromResultSet(final ResultSet rs) throws SQLException {
73         Grant grant = new Grant();
74         try {
75             grant.setGrantid(rs.getString(COL_ID));
76             grant.setDomainid(rs.getString(COL_TENANTID));
77             grant.setUserid(rs.getString(COL_USERID));
78             grant.setRoleid(rs.getString(COL_ROLEID));
79         } catch (SQLException sqle) {
80             LOG.error("SQL Exception: ", sqle);
81             throw sqle;
82         }
83         return grant;
84     }
85
86     Grants getGrants(final String domainId, final String userId) throws StoreException {
87         final Grants grants;
88         try (var conn = dbConnect();
89              var stmt = conn.prepareStatement("SELECT * FROM " + TABLE
90                  + " WHERE " + COL_TENANTID + " = ? AND " + COL_USERID + " = ?")) {
91             stmt.setString(1, domainId);
92             stmt.setString(2, userId);
93             LOG.debug("getGrants() request: {}", stmt);
94
95             grants = new Grants();
96             grants.setGrants(listFromStatement(stmt));
97         } catch (SQLException e) {
98             throw new StoreException("SQL Exception", e);
99         }
100         return grants;
101     }
102
103     Grants getGrants(final String userid) throws StoreException {
104         final Grants grants;
105         try (var conn = dbConnect();
106              var stmt = conn.prepareStatement("SELECT * FROM " + TABLE + " WHERE " + COL_USERID + " = ?")) {
107             stmt.setString(1, userid);
108             LOG.debug("getGrants() request: {}", stmt);
109
110             grants = new Grants();
111             grants.setGrants(listFromStatement(stmt));
112         } catch (SQLException e) {
113             throw new StoreException("SQL Exception", e);
114         }
115         return grants;
116     }
117
118     Grant getGrant(final String id) throws StoreException {
119         try (var conn = dbConnect();
120              var stmt = conn.prepareStatement("SELECT * FROM " + TABLE + " WHERE " + COL_ID + " = ?")) {
121             stmt.setString(1, id);
122             LOG.debug("getGrant() request: {}", stmt);
123
124             return firstFromStatement(stmt);
125         } catch (SQLException e) {
126             throw new StoreException("SQL Exception", e);
127         }
128     }
129
130     // FIXME: seems to be unused
131     Grant getGrant(final String did, final String uid, final String rid) throws StoreException {
132         try (var conn = dbConnect();
133              var stmt = conn.prepareStatement("SELECT * FROM " + TABLE
134                  + " WHERE " + COL_TENANTID + " = ? AND " + COL_USERID + " = ? AND " + COL_ROLEID + " = ?")) {
135             stmt.setString(1, did);
136             stmt.setString(2, uid);
137             stmt.setString(3, rid);
138             LOG.debug("getGrant() request: {}", stmt);
139
140             return firstFromStatement(stmt);
141         } catch (SQLException e) {
142             throw new StoreException("SQL Exception", e);
143         }
144     }
145
146     Grant createGrant(final Grant grant) throws StoreException {
147         try (var conn = dbConnect();
148              var stmt = conn.prepareStatement("INSERT INTO " + TABLE + " ("
149                  + COL_ID + ", " + COL_TENANTID + ", " + COL_USERID + ", " + COL_ROLEID + ") VALUES (?, ?, ?, ?)")) {
150             stmt.setString(1, IDMStoreUtil.createGrantid(grant.getUserid(), grant.getDomainid(), grant.getRoleid()));
151             stmt.setString(2, grant.getDomainid());
152             stmt.setString(3, grant.getUserid());
153             stmt.setString(4, grant.getRoleid());
154             LOG.debug("createGrant() request: {}", stmt);
155
156             if (stmt.executeUpdate() == 0) {
157                 throw new StoreException("Creating grant failed, no rows affected.");
158             }
159             grant.setGrantid(IDMStoreUtil.createGrantid(grant.getUserid(), grant.getDomainid(), grant.getRoleid()));
160             return grant;
161         } catch (SQLException e) {
162             throw new StoreException("SQL Exception", e);
163         }
164     }
165
166     Grant deleteGrant(final String grantid) throws StoreException {
167         final var savedGrant = getGrant(grantid);
168         if (savedGrant == null) {
169             return null;
170         }
171
172         try (var conn = dbConnect();
173              var stmt = conn.prepareStatement("DELETE FROM " + TABLE +  " WHERE " + COL_ID + " = ?")) {
174             stmt.setString(1, grantid);
175             LOG.debug("deleteGrant() request: {}", stmt);
176
177             int deleteCount = stmt.executeUpdate();
178             LOG.debug("deleted {} records", deleteCount);
179             return savedGrant;
180         } catch (SQLException e) {
181             throw new StoreException("SQL Exception", e);
182         }
183     }
184 }