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 java.sql.Connection;
13 import java.sql.PreparedStatement;
14 import java.sql.ResultSet;
15 import java.sql.SQLException;
16 import org.opendaylight.aaa.api.IDMStoreUtil;
17 import org.opendaylight.aaa.api.model.Role;
18 import org.opendaylight.aaa.api.model.Roles;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
25 * @author peter.mellquist@hp.com
28 public class RoleStore extends AbstractStore<Role> {
29 private static final Logger LOG = LoggerFactory.getLogger(RoleStore.class);
31 public static final String SQL_ID = "roleid";
32 protected static final String SQL_DOMAIN_ID = "domainid";
33 public static final String SQL_NAME = "name";
34 public static final String SQL_DESCR = "description";
35 private static final String TABLE_NAME = "ROLES";
37 public RoleStore(final ConnectionProvider dbConnectionFactory) {
38 super(dbConnectionFactory, TABLE_NAME);
42 protected String getTableCreationStatement() {
43 return "CREATE TABLE ROLES " + "(roleid VARCHAR(128) PRIMARY KEY,"
44 + "name VARCHAR(128) NOT NULL, " + "domainid VARCHAR(128) NOT NULL, "
45 + "description VARCHAR(128) NOT NULL)";
49 protected Role fromResultSet(final ResultSet rs) throws SQLException {
50 Role role = new Role();
52 role.setRoleid(rs.getString(SQL_ID));
53 role.setDomainid(rs.getString(SQL_DOMAIN_ID));
54 role.setName(rs.getString(SQL_NAME));
55 role.setDescription(rs.getString(SQL_DESCR));
56 } catch (SQLException sqle) {
57 LOG.error("SQL Exception: ", sqle);
63 public Roles getRoles() throws StoreException {
64 Roles roles = new Roles();
65 roles.setRoles(listAll());
69 protected Role getRole(final String id) throws StoreException {
70 try (Connection conn = dbConnect();
71 PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM ROLES WHERE roleid = ? ")) {
72 pstmt.setString(1, id);
73 LOG.debug("query string: {}", pstmt);
74 return firstFromStatement(pstmt);
75 } catch (SQLException s) {
76 throw new StoreException("SQL Exception: " + s);
80 protected Role createRole(final Role role) throws StoreException {
82 requireNonNull(role.getName());
83 requireNonNull(role.getDomainid());
84 String query = "insert into roles (roleid,domainid,name,description) values(?,?,?,?)";
85 try (Connection conn = dbConnect(); PreparedStatement statement = conn.prepareStatement(query)) {
86 role.setRoleid(IDMStoreUtil.createRoleid(role.getName(), role.getDomainid()));
87 statement.setString(1, role.getRoleid());
88 statement.setString(2, role.getDomainid());
89 statement.setString(3, role.getName());
90 statement.setString(4, role.getDescription());
91 int affectedRows = statement.executeUpdate();
92 if (affectedRows == 0) {
93 throw new StoreException("Creating role failed, no rows affected.");
96 } catch (SQLException s) {
97 throw new StoreException("SQL Exception : " + s);
101 protected Role putRole(final Role role) throws StoreException {
103 Role savedRole = getRole(role.getRoleid());
104 if (savedRole == null) {
108 if (role.getDescription() != null) {
109 savedRole.setDescription(role.getDescription());
111 if (role.getName() != null) {
112 savedRole.setName(role.getName());
115 String query = "UPDATE roles SET description = ? WHERE roleid = ?";
116 try (Connection conn = dbConnect(); PreparedStatement statement = conn.prepareStatement(query)) {
117 statement.setString(1, savedRole.getDescription());
118 statement.setString(2, savedRole.getRoleid());
119 statement.executeUpdate();
120 } catch (SQLException s) {
121 throw new StoreException("SQL Exception : " + s);
127 protected Role deleteRole(final String roleid) throws StoreException {
128 Role savedRole = getRole(roleid);
129 if (savedRole == null) {
133 String query = "DELETE FROM ROLES WHERE roleid = ?";
134 try (Connection conn = dbConnect(); PreparedStatement statement = conn.prepareStatement(query)) {
135 statement.setString(1, roleid);
136 int deleteCount = statement.executeUpdate();
137 LOG.debug("deleted {} records", deleteCount);
139 } catch (SQLException s) {
140 throw new StoreException("SQL Exception : " + s);