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
8 package org.opendaylight.aaa.datastore.h2;
10 import static java.util.Objects.requireNonNull;
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;
28 * @author peter.mellquist@hp.com
31 public class RoleStore extends AbstractStore<Role> {
32 private static final Logger LOG = LoggerFactory.getLogger(RoleStore.class);
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";
40 public RoleStore(final ConnectionProvider dbConnectionFactory) {
41 super(dbConnectionFactory, TABLE_NAME);
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)";
52 protected Role fromResultSet(final ResultSet rs) throws SQLException {
53 Role role = new Role();
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);
66 public Roles getRoles() throws StoreException {
67 Roles roles = new Roles();
68 roles.setRoles(listAll());
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);
83 protected Role createRole(final Role role) throws StoreException {
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.");
99 } catch (SQLException s) {
100 throw new StoreException("SQL Exception : " + s);
104 protected Role putRole(final Role role) throws StoreException {
106 Role savedRole = this.getRole(role.getRoleid());
107 if (savedRole == null) {
111 if (role.getDescription() != null) {
112 savedRole.setDescription(role.getDescription());
114 if (role.getName() != null) {
115 savedRole.setName(role.getName());
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);
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) {
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);
143 } catch (SQLException s) {
144 throw new StoreException("SQL Exception : " + s);