84a54060b7b66e696c6d15b1c9a26c79f35c7dae
[aaa.git] / aaa-shiro / impl / src / main / java / org / opendaylight / aaa / impl / 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
9 package org.opendaylight.aaa.impl.datastore.h2;
10
11 import com.google.common.base.Preconditions;
12
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
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;
25
26 /**
27  * Store for roles.
28  *
29  * @author peter.mellquist@hp.com
30  *
31  */
32 public class RoleStore extends AbstractStore<Role> {
33     private static final Logger LOG = LoggerFactory.getLogger(RoleStore.class);
34
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";
40
41     public RoleStore(ConnectionProvider dbConnectionFactory) {
42         super(dbConnectionFactory, TABLE_NAME);
43     }
44
45     @Override
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)";
50     }
51
52     @Override
53     protected Role fromResultSet(ResultSet rs) throws SQLException {
54         Role role = new Role();
55         try {
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);
62             throw sqle;
63         }
64         return role;
65     }
66
67     public Roles getRoles() throws StoreException {
68         Roles roles = new Roles();
69         roles.setRoles(listAll());
70         return roles;
71     }
72
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);
81         }
82     }
83
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.");
98             }
99             return role;
100         } catch (SQLException s) {
101             throw new StoreException("SQL Exception : " + s);
102         }
103     }
104
105     protected Role putRole(Role role) throws StoreException {
106
107         Role savedRole = this.getRole(role.getRoleid());
108         if (savedRole == null) {
109             return null;
110         }
111
112         if (role.getDescription() != null) {
113             savedRole.setDescription(role.getDescription());
114         }
115         if (role.getName() != null) {
116             savedRole.setName(role.getName());
117         }
118
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);
126         }
127
128         return savedRole;
129     }
130
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 }