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
9 package org.opendaylight.aaa.impl.datastore.h2;
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;
29 * @author peter.mellquist@hp.com
32 public class RoleStore extends AbstractStore<Role> {
33 private static final Logger LOG = LoggerFactory.getLogger(RoleStore.class);
35 public static final String SQL_ID = "roleid";
36 protected static final String SQL_DOMAIN_ID = "domainid";
37 public static final String SQL_NAME = "name";
38 public static final String SQL_DESCR = "description";
39 private static final String TABLE_NAME = "ROLES";
41 public RoleStore(ConnectionProvider dbConnectionFactory) {
42 super(dbConnectionFactory, TABLE_NAME);
46 protected String getTableCreationStatement() {
47 return "CREATE TABLE ROLES " + "(roleid VARCHAR(128) PRIMARY KEY,"
48 + "name VARCHAR(128) NOT NULL, " + "domainid VARCHAR(128) NOT NULL, "
49 + "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 public 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.prepareStatement("SELECT * FROM ROLES WHERE roleid = ? ")) {
76 pstmt.setString(1, id);
77 LOG.debug("query string: {}", pstmt.toString());
78 return firstFromStatement(pstmt);
79 } catch (SQLException s) {
80 throw new StoreException("SQL Exception: " + s);
84 protected Role createRole(Role role) throws StoreException {
85 Preconditions.checkNotNull(role);
86 Preconditions.checkNotNull(role.getName());
87 Preconditions.checkNotNull(role.getDomainid());
88 String query = "insert into roles (roleid,domainid,name,description) values(?,?,?,?)";
89 try (Connection conn = dbConnect(); PreparedStatement statement = conn.prepareStatement(query)) {
90 role.setRoleid(IDMStoreUtil.createRoleid(role.getName(), role.getDomainid()));
91 statement.setString(1, role.getRoleid());
92 statement.setString(2, role.getDomainid());
93 statement.setString(3, role.getName());
94 statement.setString(4, role.getDescription());
95 int affectedRows = statement.executeUpdate();
96 if (affectedRows == 0) {
97 throw new StoreException("Creating role failed, no rows affected.");
100 } catch (SQLException s) {
101 throw new StoreException("SQL Exception : " + s);
105 protected Role putRole(Role role) throws StoreException {
107 Role savedRole = this.getRole(role.getRoleid());
108 if (savedRole == null) {
112 if (role.getDescription() != null) {
113 savedRole.setDescription(role.getDescription());
115 if (role.getName() != null) {
116 savedRole.setName(role.getName());
119 String query = "UPDATE roles SET description = ? WHERE roleid = ?";
120 try (Connection conn = dbConnect(); PreparedStatement statement = conn.prepareStatement(query)) {
121 statement.setString(1, savedRole.getDescription());
122 statement.setString(2, savedRole.getRoleid());
123 statement.executeUpdate();
124 } catch (SQLException s) {
125 throw new StoreException("SQL Exception : " + s);
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);