aboutsummaryrefslogtreecommitdiffstats
path: root/odl-aaa-moon/aaa/aaa-h2-store/src/main/java/org/opendaylight/aaa/h2/persistence/GrantStore.java
blob: ee86e0ba19cfc9bacd8b09cd8d3e2cc17570d61b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
 * Copyright (c) 2014, 2016 Hewlett-Packard Development Company, L.P. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */

package org.opendaylight.aaa.h2.persistence;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.commons.lang3.StringEscapeUtils;
import org.opendaylight.aaa.api.IDMStoreUtil;
import org.opendaylight.aaa.api.model.Grant;
import org.opendaylight.aaa.api.model.Grants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author peter.mellquist@hp.com
 *
 */
public class GrantStore extends AbstractStore<Grant> {
    private static final Logger LOG = LoggerFactory.getLogger(GrantStore.class);

    protected final static String SQL_ID = "grantid";
    protected final static String SQL_TENANTID = "domainid";
    protected final static String SQL_USERID = "userid";
    protected final static String SQL_ROLEID = "roleid";
    private static final String TABLE_NAME = "GRANTS";

    protected GrantStore() {
        super(TABLE_NAME);
    }

    @Override
    protected String getTableCreationStatement() {
        return "CREATE TABLE GRANTS "
                + "(grantid    VARCHAR(128) PRIMARY KEY,"
                + "domainid    VARCHAR(128)         NOT NULL, "
                + "userid      VARCHAR(128)         NOT NULL, "
                + "roleid      VARCHAR(128)         NOT NULL)";
    }

    protected Grant fromResultSet(ResultSet rs) throws SQLException {
        Grant grant = new Grant();
        try {
            grant.setGrantid(rs.getString(SQL_ID));
            grant.setDomainid(rs.getString(SQL_TENANTID));
            grant.setUserid(rs.getString(SQL_USERID));
            grant.setRoleid(rs.getString(SQL_ROLEID));
        } catch (SQLException sqle) {
            LOG.error("SQL Exception: ", sqle);
            throw sqle;
        }
        return grant;
    }

    protected Grants getGrants(String did, String uid) throws StoreException {
        Grants grants = new Grants();
        try (Connection conn = dbConnect();
             PreparedStatement pstmt = conn
                     .prepareStatement("SELECT * FROM grants WHERE domainid = ? AND userid = ?")) {
            pstmt.setString(1, did);
            pstmt.setString(2, uid);
            LOG.debug("query string: {}", pstmt.toString());
            grants.setGrants(listFromStatement(pstmt));
        } catch (SQLException s) {
            throw new StoreException("SQL Exception : " + s);
        }
        return grants;
    }

    protected Grants getGrants(String userid) throws StoreException {
        Grants grants = new Grants();
        try (Connection conn = dbConnect();
             PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM GRANTS WHERE userid = ? ")) {
            pstmt.setString(1, userid);
            LOG.debug("query string: {}", pstmt.toString());
            grants.setGrants(listFromStatement(pstmt));
        } catch (SQLException s) {
            throw new StoreException("SQL Exception : " + s);
        }
        return grants;
    }

    protected Grant getGrant(String id) throws StoreException {
        try (Connection conn = dbConnect();
             PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM GRANTS WHERE grantid = ? ")) {
            pstmt.setString(1, id);
            LOG.debug("query string: ", pstmt.toString());
            return firstFromStatement(pstmt);
        } catch (SQLException s) {
            throw new StoreException("SQL Exception : " + s);
        }
    }

    protected Grant getGrant(String did, String uid, String rid) throws StoreException {
        try (Connection conn = dbConnect();
             PreparedStatement pstmt = conn
                     .prepareStatement("SELECT * FROM GRANTS WHERE domainid = ? AND userid = ? AND roleid = ? ")) {
            pstmt.setString(1, did);
            pstmt.setString(2, uid);
            pstmt.setString(3, rid);
            LOG.debug("query string: {}", pstmt.toString());
            return firstFromStatement(pstmt);
        } catch (SQLException s) {
            throw new StoreException("SQL Exception : " + s);
        }
    }

    protected Grant createGrant(Grant grant) throws StoreException {
        String query = "insert into grants  (grantid,domainid,userid,roleid) values(?,?,?,?)";
        try (Connection conn = dbConnect();
             PreparedStatement statement = conn.prepareStatement(query)) {
            statement.setString(
                    1,
                    IDMStoreUtil.createGrantid(grant.getUserid(), grant.getDomainid(),
                            grant.getRoleid()));
            statement.setString(2, grant.getDomainid());
            statement.setString(3, grant.getUserid());
            statement.setString(4, grant.getRoleid());
            int affectedRows = statement.executeUpdate();
            if (affectedRows == 0) {
                throw new StoreException("Creating grant failed, no rows affected.");
            }
            grant.setGrantid(IDMStoreUtil.createGrantid(grant.getUserid(), grant.getDomainid(),
                    grant.getRoleid()));
            return grant;
        } catch (SQLException s) {
            throw new StoreException("SQL Exception : " + s);
        }
    }

    protected Grant deleteGrant(String grantid) throws StoreException {
        grantid = StringEscapeUtils.escapeHtml4(grantid);
        Grant savedGrant = this.getGrant(grantid);
        if (savedGrant == null) {
            return null;
        }

        String query = String.format("DELETE FROM GRANTS WHERE grantid = '%s'", grantid);
        try (Connection conn = dbConnect();
             Statement statement = conn.createStatement()) {
            int deleteCount = statement.executeUpdate(query);
            LOG.debug("deleted {} records", deleteCount);
            return savedGrant;
        } catch (SQLException s) {
            throw new StoreException("SQL Exception : " + s);
        }
    }
}