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.datastore.h2;
11 import org.opendaylight.aaa.api.IDMStoreException;
12 import org.opendaylight.aaa.api.IDMStoreUtil;
13 import org.opendaylight.aaa.api.IIDMStore;
14 import org.opendaylight.aaa.api.model.Domain;
15 import org.opendaylight.aaa.api.model.Domains;
16 import org.opendaylight.aaa.api.model.Grant;
17 import org.opendaylight.aaa.api.model.Grants;
18 import org.opendaylight.aaa.api.model.Role;
19 import org.opendaylight.aaa.api.model.Roles;
20 import org.opendaylight.aaa.api.model.User;
21 import org.opendaylight.aaa.api.model.Users;
22 import org.opendaylight.aaa.api.password.service.PasswordHashService;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 public class H2Store implements IIDMStore {
28 private static final Logger LOG = LoggerFactory.getLogger(H2Store.class);
30 private final DomainStore domainStore;
31 private final UserStore userStore;
32 private final RoleStore roleStore;
33 private final GrantStore grantStore;
35 public H2Store(final String dbUsername, final String dbPassword, final PasswordHashService passwordService) {
36 this(new IdmLightSimpleConnectionProvider(
37 new IdmLightConfigBuilder().dbUser(dbUsername).dbPwd(dbPassword).build()), passwordService);
40 public H2Store(ConnectionProvider connectionFactory, final PasswordHashService passwordService) {
41 this.domainStore = new DomainStore(connectionFactory);
42 this.userStore = new UserStore(connectionFactory, passwordService);
43 this.roleStore = new RoleStore(connectionFactory);
44 this.grantStore = new GrantStore(connectionFactory);
48 public Domain writeDomain(Domain domain) throws IDMStoreException {
50 return domainStore.createDomain(domain);
51 } catch (StoreException e) {
52 LOG.error("StoreException encountered while writing domain", e);
53 throw new IDMStoreException(e);
58 public Domain readDomain(String domainid) throws IDMStoreException {
60 return domainStore.getDomain(domainid);
61 } catch (StoreException e) {
62 LOG.error("StoreException encountered while reading domain", e);
63 throw new IDMStoreException(e);
68 public Domain deleteDomain(String domainid) throws IDMStoreException {
70 return domainStore.deleteDomain(domainid);
71 } catch (StoreException e) {
72 LOG.error("StoreException encountered while deleting domain", e);
73 throw new IDMStoreException(e);
78 public Domain updateDomain(Domain domain) throws IDMStoreException {
80 return domainStore.putDomain(domain);
81 } catch (StoreException e) {
82 LOG.error("StoreException encountered while updating domain", e);
83 throw new IDMStoreException(e);
88 public Domains getDomains() throws IDMStoreException {
90 return domainStore.getDomains();
91 } catch (StoreException e) {
92 LOG.error("StoreException encountered while reading domains", e);
93 throw new IDMStoreException(e);
98 public Role writeRole(Role role) throws IDMStoreException {
100 return roleStore.createRole(role);
101 } catch (StoreException e) {
102 LOG.error("StoreException encountered while writing role", e);
103 throw new IDMStoreException(e);
108 public Role readRole(String roleid) throws IDMStoreException {
110 return roleStore.getRole(roleid);
111 } catch (StoreException e) {
112 LOG.error("StoreException encountered while reading role", e);
113 throw new IDMStoreException(e);
118 public Role deleteRole(String roleid) throws IDMStoreException {
120 return roleStore.deleteRole(roleid);
121 } catch (StoreException e) {
122 LOG.error("StoreException encountered while deleting role", e);
123 throw new IDMStoreException(e);
128 public Role updateRole(Role role) throws IDMStoreException {
130 return roleStore.putRole(role);
131 } catch (StoreException e) {
132 LOG.error("StoreException encountered while updating role", e);
133 throw new IDMStoreException(e);
138 public Roles getRoles() throws IDMStoreException {
140 return roleStore.getRoles();
141 } catch (StoreException e) {
142 LOG.error("StoreException encountered while getting roles", e);
143 throw new IDMStoreException(e);
148 public User writeUser(User user) throws IDMStoreException {
150 return userStore.createUser(user);
151 } catch (StoreException e) {
152 LOG.error("StoreException encountered while writing user", e);
153 throw new IDMStoreException(e);
158 public User readUser(String userid) throws IDMStoreException {
160 return userStore.getUser(userid);
161 } catch (StoreException e) {
162 LOG.error("StoreException encountered while reading user", e);
163 throw new IDMStoreException(e);
168 public User deleteUser(String userid) throws IDMStoreException {
170 return userStore.deleteUser(userid);
171 } catch (StoreException e) {
172 LOG.error("StoreException encountered while deleting user", e);
173 throw new IDMStoreException(e);
178 public User updateUser(User user) throws IDMStoreException {
180 return userStore.putUser(user);
181 } catch (StoreException e) {
182 LOG.error("StoreException encountered while updating user", e);
183 throw new IDMStoreException(e);
188 public Users getUsers(String username, String domain) throws IDMStoreException {
190 return userStore.getUsers(username, domain);
191 } catch (StoreException e) {
192 LOG.error("StoreException encountered while reading users", e);
193 throw new IDMStoreException(e);
198 public Users getUsers() throws IDMStoreException {
200 return userStore.getUsers();
201 } catch (StoreException e) {
202 LOG.error("StoreException encountered while reading users", e);
203 throw new IDMStoreException(e);
208 public Grant writeGrant(Grant grant) throws IDMStoreException {
210 return grantStore.createGrant(grant);
211 } catch (StoreException e) {
212 LOG.error("StoreException encountered while writing grant", e);
213 throw new IDMStoreException(e);
218 public Grant readGrant(String grantid) throws IDMStoreException {
220 return grantStore.getGrant(grantid);
221 } catch (StoreException e) {
222 LOG.error("StoreException encountered while reading grant", e);
223 throw new IDMStoreException(e);
228 public Grant readGrant(String domainid, String userid, String roleid) throws IDMStoreException {
229 return readGrant(IDMStoreUtil.createGrantid(userid, domainid, roleid));
233 public Grant deleteGrant(String grantid) throws IDMStoreException {
235 return grantStore.deleteGrant(grantid);
236 } catch (StoreException e) {
237 LOG.error("StoreException encountered while deleting grant", e);
238 throw new IDMStoreException(e);
243 public Grants getGrants(String domainid, String userid) throws IDMStoreException {
245 return grantStore.getGrants(domainid, userid);
246 } catch (StoreException e) {
247 LOG.error("StoreException encountered while getting grants", e);
248 throw new IDMStoreException(e);
253 public Grants getGrants(String userid) throws IDMStoreException {
255 return grantStore.getGrants(userid);
256 } catch (StoreException e) {
257 LOG.error("StoreException encountered while getting grants", e);
258 throw new IDMStoreException(e);
262 public Domain createDomain(String domainName, boolean enable) throws StoreException {
263 Domain domain = new Domain();
264 domain.setName(domainName);
265 domain.setEnabled(enable);
266 return domainStore.createDomain(domain);
269 public User createUser(String name, String password, String domain, String description,
270 String email, boolean enabled, String salt) throws StoreException {
271 User user = new User();
273 user.setDomainid(domain);
274 user.setDescription(description);
275 user.setEmail(email);
276 user.setEnabled(enabled);
277 user.setPassword(password);
279 return userStore.createUser(user);
282 public Role createRole(String name, String domain, String description)
283 throws StoreException {
284 Role role = new Role();
285 role.setDescription(description);
287 role.setDomainid(domain);
288 return roleStore.createRole(role);
291 public Grant createGrant(String domain, String user, String role) throws StoreException {
292 Grant grant = new Grant();
293 grant.setDomainid(domain);
294 grant.setRoleid(role);
295 grant.setUserid(user);
296 return grantStore.createGrant(grant);