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.model.Domain;
21 import org.opendaylight.aaa.api.model.Domains;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
27 * @author peter.mellquist@hp.com
30 public class DomainStore extends AbstractStore<Domain> {
31 private static final Logger LOG = LoggerFactory.getLogger(DomainStore.class);
33 protected final static String SQL_ID = "domainid";
34 protected final static String SQL_NAME = "name";
35 protected final static String SQL_DESCR = "description";
36 protected final static String SQL_ENABLED = "enabled";
37 private static final String TABLE_NAME = "DOMAINS";
39 protected DomainStore() {
44 protected String getTableCreationStatement() {
45 return "CREATE TABLE DOMAINS "
46 + "(domainid VARCHAR(128) PRIMARY KEY,"
47 + "name VARCHAR(128) UNIQUE NOT NULL, "
48 + "description VARCHAR(128) , "
49 + "enabled INTEGER NOT NULL)";
53 protected Domain fromResultSet(ResultSet rs) throws SQLException {
54 Domain domain = new Domain();
55 domain.setDomainid(rs.getString(SQL_ID));
56 domain.setName(rs.getString(SQL_NAME));
57 domain.setDescription(rs.getString(SQL_DESCR));
58 domain.setEnabled(rs.getInt(SQL_ENABLED) == 1);
62 protected Domains getDomains() throws StoreException {
63 Domains domains = new Domains();
64 domains.setDomains(listAll());
68 protected Domains getDomains(String domainName) throws StoreException {
69 LOG.debug("getDomains for: {}", domainName);
70 Domains domains = new Domains();
71 try (Connection conn = dbConnect();
72 PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM DOMAINS WHERE name = ?")) {
73 pstmt.setString(1, domainName);
74 LOG.debug("query string: {}", pstmt.toString());
75 domains.setDomains(listFromStatement(pstmt));
76 } catch (SQLException e) {
77 LOG.error("Error listing domains matching {}", domainName, e);
78 throw new StoreException("Error listing domains", e);
83 protected Domain getDomain(String id) throws StoreException {
84 try (Connection conn = dbConnect();
85 PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM DOMAINS WHERE domainid = ? ")) {
86 pstmt.setString(1, id);
87 LOG.debug("query string: {}", pstmt.toString());
88 return firstFromStatement(pstmt);
89 } catch (SQLException e) {
90 LOG.error("Error retrieving domain {}", id, e);
91 throw new StoreException("Error loading domain", e);
95 protected Domain createDomain(Domain domain) throws StoreException {
96 Preconditions.checkNotNull(domain);
97 Preconditions.checkNotNull(domain.getName());
98 Preconditions.checkNotNull(domain.isEnabled());
99 String query = "insert into DOMAINS (domainid,name,description,enabled) values(?, ?, ?, ?)";
100 try (Connection conn = dbConnect();
101 PreparedStatement statement = conn.prepareStatement(query)) {
102 statement.setString(1, domain.getName());
103 statement.setString(2, domain.getName());
104 statement.setString(3, domain.getDescription());
105 statement.setInt(4, domain.isEnabled() ? 1 : 0);
106 int affectedRows = statement.executeUpdate();
107 if (affectedRows == 0) {
108 throw new StoreException("Creating domain failed, no rows affected.");
110 domain.setDomainid(domain.getName());
112 } catch (SQLException e) {
113 LOG.error("Error creating domain {}", domain.getName(), e);
114 throw new StoreException("Error creating domain", e);
118 protected Domain putDomain(Domain domain) throws StoreException {
119 Domain savedDomain = this.getDomain(domain.getDomainid());
120 if (savedDomain == null) {
124 if (domain.getDescription() != null) {
125 savedDomain.setDescription(domain.getDescription());
127 if (domain.getName() != null) {
128 savedDomain.setName(domain.getName());
130 if (domain.isEnabled() != null) {
131 savedDomain.setEnabled(domain.isEnabled());
134 String query = "UPDATE DOMAINS SET description = ?, enabled = ? WHERE domainid = ?";
135 try (Connection conn = dbConnect();
136 PreparedStatement statement = conn.prepareStatement(query)) {
137 statement.setString(1, savedDomain.getDescription());
138 statement.setInt(2, savedDomain.isEnabled() ? 1 : 0);
139 statement.setString(3, savedDomain.getDomainid());
140 statement.executeUpdate();
141 } catch (SQLException e) {
142 LOG.error("Error updating domain {}", domain.getDomainid(), e);
143 throw new StoreException("Error updating domain", e);
149 protected Domain deleteDomain(String domainid) throws StoreException {
150 domainid = StringEscapeUtils.escapeHtml4(domainid);
151 Domain deletedDomain = this.getDomain(domainid);
152 if (deletedDomain == null) {
155 String query = String.format("DELETE FROM DOMAINS WHERE domainid = '%s'", domainid);
156 try (Connection conn = dbConnect();
157 Statement statement = conn.createStatement()) {
158 int deleteCount = statement.executeUpdate(query);
159 LOG.debug("deleted {} records", deleteCount);
160 return deletedDomain;
161 } catch (SQLException e) {
162 LOG.error("Error deleting domain {}", domainid, e);
163 throw new StoreException("Error deleting domain", e);