ee86e0ba19cfc9bacd8b09cd8d3e2cc17570d61b
[moon.git] /
1 /*
2  * Copyright (c) 2014, 2016 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.h2.persistence;
10
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;
16
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;
23
24 /**
25  *
26  * @author peter.mellquist@hp.com
27  *
28  */
29 public class GrantStore extends AbstractStore<Grant> {
30     private static final Logger LOG = LoggerFactory.getLogger(GrantStore.class);
31
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";
37
38     protected GrantStore() {
39         super(TABLE_NAME);
40     }
41
42     @Override
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)";
49     }
50
51     protected Grant fromResultSet(ResultSet rs) throws SQLException {
52         Grant grant = new Grant();
53         try {
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);
60             throw sqle;
61         }
62         return grant;
63     }
64
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);
76         }
77         return grants;
78     }
79
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);
89         }
90         return grants;
91     }
92
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);
101         }
102     }
103
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);
115         }
116     }
117
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)) {
122             statement.setString(
123                     1,
124                     IDMStoreUtil.createGrantid(grant.getUserid(), grant.getDomainid(),
125                             grant.getRoleid()));
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.");
132             }
133             grant.setGrantid(IDMStoreUtil.createGrantid(grant.getUserid(), grant.getDomainid(),
134                     grant.getRoleid()));
135             return grant;
136         } catch (SQLException s) {
137             throw new StoreException("SQL Exception : " + s);
138         }
139     }
140
141     protected Grant deleteGrant(String grantid) throws StoreException {
142         grantid = StringEscapeUtils.escapeHtml4(grantid);
143         Grant savedGrant = this.getGrant(grantid);
144         if (savedGrant == null) {
145             return null;
146         }
147
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);
153             return savedGrant;
154         } catch (SQLException s) {
155             throw new StoreException("SQL Exception : " + s);
156         }
157     }
158 }