2 * Copyright (c) 2014, 2016 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.h2.persistence;
11 import com.google.common.base.Preconditions;
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;
19 import org.apache.commons.lang3.StringEscapeUtils;
20 import org.opendaylight.aaa.api.IDMStoreUtil;
21 import org.opendaylight.aaa.api.model.Role;
22 import org.opendaylight.aaa.api.model.Roles;
23 import org.slf4j.Logger;
24 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 protected final static String SQL_ID = "roleid";
35 protected final static String SQL_DOMAIN_ID = "domainid";
36 protected final static String SQL_NAME = "name";
37 protected final static String SQL_DESCR = "description";
38 private static final String TABLE_NAME = "ROLES";
40 protected RoleStore() {
45 protected String getTableCreationStatement() {
46 return "CREATE TABLE ROLES "
47 + "(roleid VARCHAR(128) PRIMARY KEY,"
48 + "name VARCHAR(128) NOT NULL, "
49 + "domainid VARCHAR(128) NOT NULL, "
50 + "description VARCHAR(128) NOT NULL)";
53 protected Role fromResultSet(ResultSet rs) throws SQLException {
54 Role role = new Role();
56 role.setRoleid(rs.getString(SQL_ID));
57 role.setDomainid(rs.getString(SQL_DOMAIN_ID));
58 role.setName(rs.getString(SQL_NAME));
59 role.setDescription(rs.getString(SQL_DESCR));
60 } catch (SQLException sqle) {
61 LOG.error("SQL Exception: ", sqle);
67 protected Roles getRoles() throws StoreException {
68 Roles roles = new Roles();
69 roles.setRoles(listAll());
73 protected Role getRole(String id) throws StoreException {
74 try (Connection conn = dbConnect();
75 PreparedStatement pstmt = conn
76 .prepareStatement("SELECT * FROM ROLES WHERE roleid = ? ")) {
77 pstmt.setString(1, id);
78 LOG.debug("query string: {}", pstmt.toString());
79 return firstFromStatement(pstmt);
80 } catch (SQLException s) {
81 throw new StoreException("SQL Exception: " + s);
85 protected Role createRole(Role role) throws StoreException {
86 Preconditions.checkNotNull(role);
87 Preconditions.checkNotNull(role.getName());
88 Preconditions.checkNotNull(role.getDomainid());
89 String query = "insert into roles (roleid,domainid,name,description) values(?,?,?,?)";
90 try (Connection conn = dbConnect();
91 PreparedStatement statement = conn.prepareStatement(query)) {
92 role.setRoleid(IDMStoreUtil.createRoleid(role.getName(), role.getDomainid()));
93 statement.setString(1, role.getRoleid());
94 statement.setString(2, role.getDomainid());
95 statement.setString(3, role.getName());
96 statement.setString(4, role.getDescription());
97 int affectedRows = statement.executeUpdate();
98 if (affectedRows == 0) {
99 throw new StoreException("Creating role failed, no rows affected.");
102 } catch (SQLException s) {
103 throw new StoreException("SQL Exception : " + s);
107 protected Role putRole(Role role) throws StoreException {
109 Role savedRole = this.getRole(role.getRoleid());
110 if (savedRole == null) {
114 if (role.getDescription() != null) {
115 savedRole.setDescription(role.getDescription());
117 if (role.getName() != null) {
118 savedRole.setName(role.getName());
121 String query = "UPDATE roles SET description = ? WHERE roleid = ?";
122 try (Connection conn = dbConnect();
123 PreparedStatement statement = conn.prepareStatement(query)) {
124 statement.setString(1, savedRole.getDescription());
125 statement.setString(2, savedRole.getRoleid());
126 statement.executeUpdate();
127 } catch (SQLException s) {
128 throw new StoreException("SQL Exception : " + s);
134 protected Role deleteRole(String roleid) throws StoreException {
135 roleid = StringEscapeUtils.escapeHtml4(roleid);
136 Role savedRole = this.getRole(roleid);
137 if (savedRole == null) {
141 String query = String.format("DELETE FROM ROLES WHERE roleid = '%s'", roleid);
142 try (Connection conn = dbConnect();
143 Statement statement = conn.createStatement()) {
144 int deleteCount = statement.executeUpdate(query);
145 LOG.debug("deleted {} records", deleteCount);
147 } catch (SQLException s) {
148 throw new StoreException("SQL Exception : " + s);