2 * Copyright (c) 2015 Cisco Systems 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 java.sql.Connection;
12 import java.sql.DriverManager;
14 import org.opendaylight.aaa.api.IDMStoreException;
15 import org.opendaylight.aaa.api.IDMStoreUtil;
16 import org.opendaylight.aaa.api.IIDMStore;
17 import org.opendaylight.aaa.api.model.Domain;
18 import org.opendaylight.aaa.api.model.Domains;
19 import org.opendaylight.aaa.api.model.Grant;
20 import org.opendaylight.aaa.api.model.Grants;
21 import org.opendaylight.aaa.api.model.Role;
22 import org.opendaylight.aaa.api.model.Roles;
23 import org.opendaylight.aaa.api.model.User;
24 import org.opendaylight.aaa.api.model.Users;
25 import org.opendaylight.aaa.h2.config.IdmLightConfig;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 public class H2Store implements IIDMStore {
31 private static final Logger LOG = LoggerFactory.getLogger(H2Store.class);
33 private static IdmLightConfig config = new IdmLightConfig();
34 private DomainStore domainStore = new DomainStore();
35 private UserStore userStore = new UserStore();
36 private RoleStore roleStore = new RoleStore();
37 private GrantStore grantStore = new GrantStore();
42 public static Connection getConnection(Connection existingConnection) throws StoreException {
43 Connection connection = existingConnection;
45 if (existingConnection == null || existingConnection.isClosed()) {
47 connection = DriverManager.getConnection(config.getDbPath(), config.getDbUser(),
50 } catch (Exception e) {
51 throw new StoreException("Cannot connect to database server" + e);
57 public static IdmLightConfig getConfig() {
62 public Domain writeDomain(Domain domain) throws IDMStoreException {
64 return domainStore.createDomain(domain);
65 } catch (StoreException e) {
66 LOG.error("StoreException encountered while writing domain", e);
67 throw new IDMStoreException(e);
72 public Domain readDomain(String domainid) throws IDMStoreException {
74 return domainStore.getDomain(domainid);
75 } catch (StoreException e) {
76 LOG.error("StoreException encountered while reading domain", e);
77 throw new IDMStoreException(e);
82 public Domain deleteDomain(String domainid) throws IDMStoreException {
84 return domainStore.deleteDomain(domainid);
85 } catch (StoreException e) {
86 LOG.error("StoreException encountered while deleting domain", e);
87 throw new IDMStoreException(e);
92 public Domain updateDomain(Domain domain) throws IDMStoreException {
94 return domainStore.putDomain(domain);
95 } catch (StoreException e) {
96 LOG.error("StoreException encountered while updating domain", e);
97 throw new IDMStoreException(e);
102 public Domains getDomains() throws IDMStoreException {
104 return domainStore.getDomains();
105 } catch (StoreException e) {
106 LOG.error("StoreException encountered while reading domains", e);
107 throw new IDMStoreException(e);
112 public Role writeRole(Role role) throws IDMStoreException {
114 return roleStore.createRole(role);
115 } catch (StoreException e) {
116 LOG.error("StoreException encountered while writing role", e);
117 throw new IDMStoreException(e);
122 public Role readRole(String roleid) throws IDMStoreException {
124 return roleStore.getRole(roleid);
125 } catch (StoreException e) {
126 LOG.error("StoreException encountered while reading role", e);
127 throw new IDMStoreException(e);
132 public Role deleteRole(String roleid) throws IDMStoreException {
134 return roleStore.deleteRole(roleid);
135 } catch (StoreException e) {
136 LOG.error("StoreException encountered while deleting role", e);
137 throw new IDMStoreException(e);
142 public Role updateRole(Role role) throws IDMStoreException {
144 return roleStore.putRole(role);
145 } catch (StoreException e) {
146 LOG.error("StoreException encountered while updating role", e);
147 throw new IDMStoreException(e);
152 public Roles getRoles() throws IDMStoreException {
154 return roleStore.getRoles();
155 } catch (StoreException e) {
156 LOG.error("StoreException encountered while getting roles", e);
157 throw new IDMStoreException(e);
162 public User writeUser(User user) throws IDMStoreException {
164 return userStore.createUser(user);
165 } catch (StoreException e) {
166 LOG.error("StoreException encountered while writing user", e);
167 throw new IDMStoreException(e);
172 public User readUser(String userid) throws IDMStoreException {
174 return userStore.getUser(userid);
175 } catch (StoreException e) {
176 LOG.error("StoreException encountered while reading user", e);
177 throw new IDMStoreException(e);
182 public User deleteUser(String userid) throws IDMStoreException {
184 return userStore.deleteUser(userid);
185 } catch (StoreException e) {
186 LOG.error("StoreException encountered while deleting user", e);
187 throw new IDMStoreException(e);
192 public User updateUser(User user) throws IDMStoreException {
194 return userStore.putUser(user);
195 } catch (StoreException e) {
196 LOG.error("StoreException encountered while updating user", e);
197 throw new IDMStoreException(e);
202 public Users getUsers(String username, String domain) throws IDMStoreException {
204 return userStore.getUsers(username, domain);
205 } catch (StoreException e) {
206 LOG.error("StoreException encountered while reading users", e);
207 throw new IDMStoreException(e);
212 public Users getUsers() throws IDMStoreException {
214 return userStore.getUsers();
215 } catch (StoreException e) {
216 LOG.error("StoreException encountered while reading users", e);
217 throw new IDMStoreException(e);
222 public Grant writeGrant(Grant grant) throws IDMStoreException {
224 return grantStore.createGrant(grant);
225 } catch (StoreException e) {
226 LOG.error("StoreException encountered while writing grant", e);
227 throw new IDMStoreException(e);
232 public Grant readGrant(String grantid) throws IDMStoreException {
234 return grantStore.getGrant(grantid);
235 } catch (StoreException e) {
236 LOG.error("StoreException encountered while reading grant", e);
237 throw new IDMStoreException(e);
242 public Grant deleteGrant(String grantid) throws IDMStoreException {
244 return grantStore.deleteGrant(grantid);
245 } catch (StoreException e) {
246 LOG.error("StoreException encountered while deleting grant", e);
247 throw new IDMStoreException(e);
252 public Grants getGrants(String domainid, String userid) throws IDMStoreException {
254 return grantStore.getGrants(domainid, userid);
255 } catch (StoreException e) {
256 LOG.error("StoreException encountered while getting grants", e);
257 throw new IDMStoreException(e);
262 public Grants getGrants(String userid) throws IDMStoreException {
264 return grantStore.getGrants(userid);
265 } catch (StoreException e) {
266 LOG.error("StoreException encountered while getting grants", e);
267 throw new IDMStoreException(e);
272 public Grant readGrant(String domainid, String userid, String roleid) throws IDMStoreException {
273 return readGrant(IDMStoreUtil.createGrantid(userid, domainid, roleid));
276 public static Domain createDomain(String domainName, boolean enable) throws StoreException {
277 DomainStore ds = new DomainStore();
278 Domain d = new Domain();
279 d.setName(domainName);
280 d.setEnabled(enable);
281 return ds.createDomain(d);
284 public static User createUser(String name, String password, String domain, String description,
285 String email, boolean enabled, String SALT) throws StoreException {
286 UserStore us = new UserStore();
289 u.setDomainid(domain);
290 u.setDescription(description);
292 u.setEnabled(enabled);
293 u.setPassword(password);
295 return us.createUser(u);
298 public static Role createRole(String name, String domain, String description)
299 throws StoreException {
300 RoleStore rs = new RoleStore();
302 r.setDescription(description);
304 r.setDomainid(domain);
305 return rs.createRole(r);
308 public static Grant createGrant(String domain, String user, String role) throws StoreException {
309 GrantStore gs = new GrantStore();
310 Grant g = new Grant();
311 g.setDomainid(domain);
314 return gs.createGrant(g);