2 * Copyright (c) 2014, 2016 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.h2.persistence;
11 import java.sql.Connection;
12 import java.sql.PreparedStatement;
13 import java.sql.ResultSet;
14 import java.sql.SQLException;
15 import java.sql.Statement;
17 import org.apache.commons.lang3.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;
26 * @author peter.mellquist@hp.com
29 public class GrantStore extends AbstractStore<Grant> {
30 private static final Logger LOG = LoggerFactory.getLogger(GrantStore.class);
32 protected final static String SQL_ID = "grantid";
33 protected final static String SQL_TENANTID = "domainid";
34 protected final static String SQL_USERID = "userid";
35 protected final static String SQL_ROLEID = "roleid";
36 private static final String TABLE_NAME = "GRANTS";
38 protected GrantStore() {
43 protected String getTableCreationStatement() {
44 return "CREATE TABLE GRANTS "
45 + "(grantid VARCHAR(128) PRIMARY KEY,"
46 + "domainid VARCHAR(128) NOT NULL, "
47 + "userid VARCHAR(128) NOT NULL, "
48 + "roleid VARCHAR(128) NOT NULL)";
51 protected Grant fromResultSet(ResultSet rs) throws SQLException {
52 Grant grant = new Grant();
54 grant.setGrantid(rs.getString(SQL_ID));
55 grant.setDomainid(rs.getString(SQL_TENANTID));
56 grant.setUserid(rs.getString(SQL_USERID));
57 grant.setRoleid(rs.getString(SQL_ROLEID));
58 } catch (SQLException sqle) {
59 LOG.error("SQL Exception: ", sqle);
65 protected Grants getGrants(String did, String uid) throws StoreException {
66 Grants grants = new Grants();
67 try (Connection conn = dbConnect();
68 PreparedStatement pstmt = conn
69 .prepareStatement("SELECT * FROM grants WHERE domainid = ? AND userid = ?")) {
70 pstmt.setString(1, did);
71 pstmt.setString(2, uid);
72 LOG.debug("query string: {}", pstmt.toString());
73 grants.setGrants(listFromStatement(pstmt));
74 } catch (SQLException s) {
75 throw new StoreException("SQL Exception : " + s);
80 protected Grants getGrants(String userid) throws StoreException {
81 Grants grants = new Grants();
82 try (Connection conn = dbConnect();
83 PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM GRANTS WHERE userid = ? ")) {
84 pstmt.setString(1, userid);
85 LOG.debug("query string: {}", pstmt.toString());
86 grants.setGrants(listFromStatement(pstmt));
87 } catch (SQLException s) {
88 throw new StoreException("SQL Exception : " + s);
93 protected Grant getGrant(String id) throws StoreException {
94 try (Connection conn = dbConnect();
95 PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM GRANTS WHERE grantid = ? ")) {
96 pstmt.setString(1, id);
97 LOG.debug("query string: ", pstmt.toString());
98 return firstFromStatement(pstmt);
99 } catch (SQLException s) {
100 throw new StoreException("SQL Exception : " + s);
104 protected Grant getGrant(String did, String uid, String rid) throws StoreException {
105 try (Connection conn = dbConnect();
106 PreparedStatement pstmt = conn
107 .prepareStatement("SELECT * FROM GRANTS WHERE domainid = ? AND userid = ? AND roleid = ? ")) {
108 pstmt.setString(1, did);
109 pstmt.setString(2, uid);
110 pstmt.setString(3, rid);
111 LOG.debug("query string: {}", pstmt.toString());
112 return firstFromStatement(pstmt);
113 } catch (SQLException s) {
114 throw new StoreException("SQL Exception : " + s);
118 protected Grant createGrant(Grant grant) throws StoreException {
119 String query = "insert into grants (grantid,domainid,userid,roleid) values(?,?,?,?)";
120 try (Connection conn = dbConnect();
121 PreparedStatement statement = conn.prepareStatement(query)) {
124 IDMStoreUtil.createGrantid(grant.getUserid(), grant.getDomainid(),
126 statement.setString(2, grant.getDomainid());
127 statement.setString(3, grant.getUserid());
128 statement.setString(4, grant.getRoleid());
129 int affectedRows = statement.executeUpdate();
130 if (affectedRows == 0) {
131 throw new StoreException("Creating grant failed, no rows affected.");
133 grant.setGrantid(IDMStoreUtil.createGrantid(grant.getUserid(), grant.getDomainid(),
136 } catch (SQLException s) {
137 throw new StoreException("SQL Exception : " + s);
141 protected Grant deleteGrant(String grantid) throws StoreException {
142 grantid = StringEscapeUtils.escapeHtml4(grantid);
143 Grant savedGrant = this.getGrant(grantid);
144 if (savedGrant == null) {
148 String query = String.format("DELETE FROM GRANTS WHERE grantid = '%s'", grantid);
149 try (Connection conn = dbConnect();
150 Statement statement = conn.createStatement()) {
151 int deleteCount = statement.executeUpdate(query);
152 LOG.debug("deleted {} records", deleteCount);
154 } catch (SQLException s) {
155 throw new StoreException("SQL Exception : " + s);