e7defa4a8c8c49040fb15191d4848470281b1d5a
[moon.git] /
1 /*
2  * Copyright (c) 2014, 2016 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.h2.persistence;
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  *
28  * @author peter.mellquist@hp.com
29  *
30  */
31 public class RoleStore extends AbstractStore<Role> {
32     private static final Logger LOG = LoggerFactory.getLogger(RoleStore.class);
33
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";
39
40     protected RoleStore() {
41         super(TABLE_NAME);
42     }
43
44     @Override
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)";
51     }
52
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     protected 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
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);
82         }
83     }
84
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.");
100             }
101             return role;
102         } catch (SQLException s) {
103             throw new StoreException("SQL Exception : " + s);
104         }
105     }
106
107     protected Role putRole(Role role) throws StoreException {
108
109         Role savedRole = this.getRole(role.getRoleid());
110         if (savedRole == null) {
111             return null;
112         }
113
114         if (role.getDescription() != null) {
115             savedRole.setDescription(role.getDescription());
116         }
117         if (role.getName() != null) {
118             savedRole.setName(role.getName());
119         }
120
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);
129         }
130
131         return savedRole;
132     }
133
134     protected Role deleteRole(String roleid) throws StoreException {
135         roleid = StringEscapeUtils.escapeHtml4(roleid);
136         Role savedRole = this.getRole(roleid);
137         if (savedRole == null) {
138             return null;
139         }
140
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);
146             return savedRole;
147         } catch (SQLException s) {
148             throw new StoreException("SQL Exception : " + s);
149         }
150     }
151 }