Split out datastore implementation from aaa-shiro
[aaa.git] / aaa-idm-store-h2 / src / main / java / org / opendaylight / aaa / datastore / h2 / RoleStore.java
1 /*
2  * Copyright (c) 2014, 2017 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 package org.opendaylight.aaa.datastore.h2;
9
10 import static java.util.Objects.requireNonNull;
11
12 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
13 import java.sql.Connection;
14 import java.sql.PreparedStatement;
15 import java.sql.ResultSet;
16 import java.sql.SQLException;
17 import java.sql.Statement;
18 import org.apache.commons.text.StringEscapeUtils;
19 import org.opendaylight.aaa.api.IDMStoreUtil;
20 import org.opendaylight.aaa.api.model.Role;
21 import org.opendaylight.aaa.api.model.Roles;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * Store for roles.
27  *
28  * @author peter.mellquist@hp.com
29  *
30  */
31 public class RoleStore extends AbstractStore<Role> {
32     private static final Logger LOG = LoggerFactory.getLogger(RoleStore.class);
33
34     public static final String SQL_ID = "roleid";
35     protected static final String SQL_DOMAIN_ID = "domainid";
36     public static final String SQL_NAME = "name";
37     public static final String SQL_DESCR = "description";
38     private static final String TABLE_NAME = "ROLES";
39
40     public RoleStore(final ConnectionProvider dbConnectionFactory) {
41         super(dbConnectionFactory, TABLE_NAME);
42     }
43
44     @Override
45     protected String getTableCreationStatement() {
46         return "CREATE TABLE ROLES " + "(roleid     VARCHAR(128)   PRIMARY KEY,"
47                 + "name        VARCHAR(128)   NOT NULL, " + "domainid    VARCHAR(128)   NOT NULL, "
48                 + "description VARCHAR(128)      NOT NULL)";
49     }
50
51     @Override
52     protected Role fromResultSet(final ResultSet rs) throws SQLException {
53         Role role = new Role();
54         try {
55             role.setRoleid(rs.getString(SQL_ID));
56             role.setDomainid(rs.getString(SQL_DOMAIN_ID));
57             role.setName(rs.getString(SQL_NAME));
58             role.setDescription(rs.getString(SQL_DESCR));
59         } catch (SQLException sqle) {
60             LOG.error("SQL Exception: ", sqle);
61             throw sqle;
62         }
63         return role;
64     }
65
66     public Roles getRoles() throws StoreException {
67         Roles roles = new Roles();
68         roles.setRoles(listAll());
69         return roles;
70     }
71
72     protected Role getRole(final String id) throws StoreException {
73         try (Connection conn = dbConnect();
74                 PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM ROLES WHERE roleid = ? ")) {
75             pstmt.setString(1, id);
76             LOG.debug("query string: {}", pstmt);
77             return firstFromStatement(pstmt);
78         } catch (SQLException s) {
79             throw new StoreException("SQL Exception: " + s);
80         }
81     }
82
83     protected Role createRole(final Role role) throws StoreException {
84         requireNonNull(role);
85         requireNonNull(role.getName());
86         requireNonNull(role.getDomainid());
87         String query = "insert into roles (roleid,domainid,name,description) values(?,?,?,?)";
88         try (Connection conn = dbConnect(); PreparedStatement statement = conn.prepareStatement(query)) {
89             role.setRoleid(IDMStoreUtil.createRoleid(role.getName(), role.getDomainid()));
90             statement.setString(1, role.getRoleid());
91             statement.setString(2, role.getDomainid());
92             statement.setString(3, role.getName());
93             statement.setString(4, role.getDescription());
94             int affectedRows = statement.executeUpdate();
95             if (affectedRows == 0) {
96                 throw new StoreException("Creating role failed, no rows affected.");
97             }
98             return role;
99         } catch (SQLException s) {
100             throw new StoreException("SQL Exception : " + s);
101         }
102     }
103
104     protected Role putRole(final Role role) throws StoreException {
105
106         Role savedRole = this.getRole(role.getRoleid());
107         if (savedRole == null) {
108             return null;
109         }
110
111         if (role.getDescription() != null) {
112             savedRole.setDescription(role.getDescription());
113         }
114         if (role.getName() != null) {
115             savedRole.setName(role.getName());
116         }
117
118         String query = "UPDATE roles SET description = ? WHERE roleid = ?";
119         try (Connection conn = dbConnect(); PreparedStatement statement = conn.prepareStatement(query)) {
120             statement.setString(1, savedRole.getDescription());
121             statement.setString(2, savedRole.getRoleid());
122             statement.executeUpdate();
123         } catch (SQLException s) {
124             throw new StoreException("SQL Exception : " + s);
125         }
126
127         return savedRole;
128     }
129
130     @SuppressFBWarnings("SQL_NONCONSTANT_STRING_PASSED_TO_EXECUTE")
131     protected Role deleteRole(String roleid) throws StoreException {
132         roleid = StringEscapeUtils.escapeHtml4(roleid);
133         Role savedRole = this.getRole(roleid);
134         if (savedRole == null) {
135             return null;
136         }
137
138         String query = String.format("DELETE FROM ROLES WHERE roleid = '%s'", roleid);
139         try (Connection conn = dbConnect(); Statement statement = conn.createStatement()) {
140             int deleteCount = statement.executeUpdate(query);
141             LOG.debug("deleted {} records", deleteCount);
142             return savedRole;
143         } catch (SQLException s) {
144             throw new StoreException("SQL Exception : " + s);
145         }
146     }
147 }