2 * Copyright (c) 2014, 2017 Hewlett-Packard Development Company, L.P. and others. All rights reserved.
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
9 package org.opendaylight.aaa.datastore.h2;
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;
27 * @author peter.mellquist@hp.com
30 public class GrantStore extends AbstractStore<Grant> {
31 private static final Logger LOG = LoggerFactory.getLogger(GrantStore.class);
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";
39 public GrantStore(ConnectionProvider dbConnectionFactory) {
40 super(dbConnectionFactory, TABLE_NAME);
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)";
53 protected Grant fromResultSet(ResultSet rs) throws SQLException {
54 Grant grant = new Grant();
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);
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);
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);
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);
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);
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)) {
126 IDMStoreUtil.createGrantid(grant.getUserid(), grant.getDomainid(),
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.");
135 grant.setGrantid(IDMStoreUtil.createGrantid(grant.getUserid(), grant.getDomainid(),
138 } catch (SQLException e) {
139 throw new StoreException("SQL Exception", e);
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) {
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);
157 } catch (SQLException e) {
158 throw new StoreException("SQL Exception", e);