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 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;
24 * @author peter.mellquist@hp.com
26 final class GrantStore extends AbstractStore<Grant> {
27 private static final Logger LOG = LoggerFactory.getLogger(GrantStore.class);
29 static final String TABLE = "AAA_GRANTS";
32 SQLTable.GRANT.verifyTable(TABLE);
36 static final String COL_ID = "grantid";
39 // FIXME: 'tenant' vs 'domain' ?
41 static final String COL_TENANTID = "domainid";
45 static final String COL_USERID = "userid";
48 static final String COL_ROLEID = "roleid";
50 GrantStore(final ConnectionProvider dbConnectionFactory) {
51 super(dbConnectionFactory, TABLE);
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)");
67 void cleanTable(final Statement stmt) throws SQLException {
68 stmt.execute("DELETE FROM " + TABLE);
72 protected Grant fromResultSet(final ResultSet rs) throws SQLException {
73 Grant grant = new Grant();
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);
86 Grants getGrants(final String domainId, final String userId) throws StoreException {
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);
95 grants = new Grants();
96 grants.setGrants(listFromStatement(stmt));
97 } catch (SQLException e) {
98 throw new StoreException("SQL Exception", e);
103 Grants getGrants(final String userid) throws StoreException {
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);
110 grants = new Grants();
111 grants.setGrants(listFromStatement(stmt));
112 } catch (SQLException e) {
113 throw new StoreException("SQL Exception", e);
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);
124 return firstFromStatement(stmt);
125 } catch (SQLException e) {
126 throw new StoreException("SQL Exception", e);
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);
140 return firstFromStatement(stmt);
141 } catch (SQLException e) {
142 throw new StoreException("SQL Exception", e);
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);
156 if (stmt.executeUpdate() == 0) {
157 throw new StoreException("Creating grant failed, no rows affected.");
159 grant.setGrantid(IDMStoreUtil.createGrantid(grant.getUserid(), grant.getDomainid(), grant.getRoleid()));
161 } catch (SQLException e) {
162 throw new StoreException("SQL Exception", e);
166 Grant deleteGrant(final String grantid) throws StoreException {
167 final var savedGrant = getGrant(grantid);
168 if (savedGrant == null) {
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);
177 int deleteCount = stmt.executeUpdate();
178 LOG.debug("deleted {} records", deleteCount);
180 } catch (SQLException e) {
181 throw new StoreException("SQL Exception", e);