Bump versions to 0.14.2-SNAPSHOT
[aaa.git] / aaa-shiro / impl / 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 edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
12 import java.sql.Connection;
13 import java.sql.PreparedStatement;
14 import java.sql.ResultSet;
15 import java.sql.SQLException;
16 import java.sql.Statement;
17 import org.apache.commons.text.StringEscapeUtils;
18 import org.opendaylight.aaa.api.IDMStoreUtil;
19 import org.opendaylight.aaa.api.model.Grant;
20 import org.opendaylight.aaa.api.model.Grants;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * Grant store.
26  *
27  * @author peter.mellquist@hp.com
28  *
29  */
30 public class GrantStore extends AbstractStore<Grant> {
31     private static final Logger LOG = LoggerFactory.getLogger(GrantStore.class);
32
33     public static final String SQL_ID = "grantid";
34     public static final String SQL_TENANTID = "domainid";
35     public static final String SQL_USERID = "userid";
36     public static final String SQL_ROLEID = "roleid";
37     private static final String TABLE_NAME = "GRANTS";
38
39     public GrantStore(ConnectionProvider dbConnectionFactory) {
40         super(dbConnectionFactory, TABLE_NAME);
41     }
42
43     @Override
44     protected String getTableCreationStatement() {
45         return "CREATE TABLE GRANTS "
46                 + "(grantid    VARCHAR(128) PRIMARY KEY,"
47                 + "domainid    VARCHAR(128)         NOT NULL, "
48                 + "userid      VARCHAR(128)         NOT NULL, "
49                 + "roleid      VARCHAR(128)         NOT NULL)";
50     }
51
52     @Override
53     protected Grant fromResultSet(ResultSet rs) throws SQLException {
54         Grant grant = new Grant();
55         try {
56             grant.setGrantid(rs.getString(SQL_ID));
57             grant.setDomainid(rs.getString(SQL_TENANTID));
58             grant.setUserid(rs.getString(SQL_USERID));
59             grant.setRoleid(rs.getString(SQL_ROLEID));
60         } catch (SQLException sqle) {
61             LOG.error("SQL Exception: ", sqle);
62             throw sqle;
63         }
64         return grant;
65     }
66
67     public Grants getGrants(String did, String uid) throws StoreException {
68         Grants grants = new Grants();
69         try (Connection conn = dbConnect();
70              PreparedStatement pstmt = conn
71                      .prepareStatement("SELECT * FROM grants WHERE domainid = ? AND userid = ?")) {
72             pstmt.setString(1, did);
73             pstmt.setString(2, uid);
74             LOG.debug("query string: {}", pstmt);
75             grants.setGrants(listFromStatement(pstmt));
76         } catch (SQLException e) {
77             throw new StoreException("SQL Exception", e);
78         }
79         return grants;
80     }
81
82     protected Grants getGrants(String userid) throws StoreException {
83         Grants grants = new Grants();
84         try (Connection conn = dbConnect();
85              PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM GRANTS WHERE userid = ? ")) {
86             pstmt.setString(1, userid);
87             LOG.debug("query string: {}", pstmt);
88             grants.setGrants(listFromStatement(pstmt));
89         } catch (SQLException e) {
90             throw new StoreException("SQL Exception", e);
91         }
92         return grants;
93     }
94
95     protected Grant getGrant(String id) throws StoreException {
96         try (Connection conn = dbConnect();
97              PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM GRANTS WHERE grantid = ? ")) {
98             pstmt.setString(1, id);
99             LOG.debug("query string: {}", pstmt);
100             return firstFromStatement(pstmt);
101         } catch (SQLException e) {
102             throw new StoreException("SQL Exception", e);
103         }
104     }
105
106     protected Grant getGrant(String did, String uid, String rid) throws StoreException {
107         try (Connection conn = dbConnect();
108              PreparedStatement pstmt = conn
109                      .prepareStatement("SELECT * FROM GRANTS WHERE domainid = ? AND userid = ? AND roleid = ? ")) {
110             pstmt.setString(1, did);
111             pstmt.setString(2, uid);
112             pstmt.setString(3, rid);
113             LOG.debug("query string: {}", pstmt);
114             return firstFromStatement(pstmt);
115         } catch (SQLException e) {
116             throw new StoreException("SQL Exception", e);
117         }
118     }
119
120     protected Grant createGrant(Grant grant) throws StoreException {
121         String query = "insert into grants  (grantid,domainid,userid,roleid) values(?,?,?,?)";
122         try (Connection conn = dbConnect();
123              PreparedStatement statement = conn.prepareStatement(query)) {
124             statement.setString(
125                     1,
126                     IDMStoreUtil.createGrantid(grant.getUserid(), grant.getDomainid(),
127                             grant.getRoleid()));
128             statement.setString(2, grant.getDomainid());
129             statement.setString(3, grant.getUserid());
130             statement.setString(4, grant.getRoleid());
131             int affectedRows = statement.executeUpdate();
132             if (affectedRows == 0) {
133                 throw new StoreException("Creating grant failed, no rows affected.");
134             }
135             grant.setGrantid(IDMStoreUtil.createGrantid(grant.getUserid(), grant.getDomainid(),
136                     grant.getRoleid()));
137             return grant;
138         } catch (SQLException e) {
139             throw new StoreException("SQL Exception", e);
140         }
141     }
142
143     @SuppressFBWarnings("SQL_NONCONSTANT_STRING_PASSED_TO_EXECUTE")
144     protected Grant deleteGrant(String grantid) throws StoreException {
145         grantid = StringEscapeUtils.escapeHtml4(grantid);
146         Grant savedGrant = this.getGrant(grantid);
147         if (savedGrant == null) {
148             return null;
149         }
150
151         String query = String.format("DELETE FROM GRANTS WHERE grantid = '%s'", grantid);
152         try (Connection conn = dbConnect();
153              Statement statement = conn.createStatement()) {
154             int deleteCount = statement.executeUpdate(query);
155             LOG.debug("deleted {} records", deleteCount);
156             return savedGrant;
157         } catch (SQLException e) {
158             throw new StoreException("SQL Exception", e);
159         }
160     }
161 }