Bump versions to 0.14.2-SNAPSHOT
[aaa.git] / aaa-idm-store-h2 / src / main / java / org / opendaylight / aaa / datastore / h2 / H2Store.java
1 /*
2  * Copyright (c) 2015 Cisco Systems 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.datastore.h2;
10
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;
25
26 public class H2Store implements IIDMStore {
27
28     private static final Logger LOG = LoggerFactory.getLogger(H2Store.class);
29
30     private final DomainStore domainStore;
31     private final UserStore userStore;
32     private final RoleStore roleStore;
33     private final GrantStore grantStore;
34
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);
38     }
39
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);
45     }
46
47     @Override
48     public Domain writeDomain(Domain domain) throws IDMStoreException {
49         try {
50             return domainStore.createDomain(domain);
51         } catch (StoreException e) {
52             LOG.error("StoreException encountered while writing domain", e);
53             throw new IDMStoreException(e);
54         }
55     }
56
57     @Override
58     public Domain readDomain(String domainid) throws IDMStoreException {
59         try {
60             return domainStore.getDomain(domainid);
61         } catch (StoreException e) {
62             LOG.error("StoreException encountered while reading domain", e);
63             throw new IDMStoreException(e);
64         }
65     }
66
67     @Override
68     public Domain deleteDomain(String domainid) throws IDMStoreException {
69         try {
70             return domainStore.deleteDomain(domainid);
71         } catch (StoreException e) {
72             LOG.error("StoreException encountered while deleting domain", e);
73             throw new IDMStoreException(e);
74         }
75     }
76
77     @Override
78     public Domain updateDomain(Domain domain) throws IDMStoreException {
79         try {
80             return domainStore.putDomain(domain);
81         } catch (StoreException e) {
82             LOG.error("StoreException encountered while updating domain", e);
83             throw new IDMStoreException(e);
84         }
85     }
86
87     @Override
88     public Domains getDomains() throws IDMStoreException {
89         try {
90             return domainStore.getDomains();
91         } catch (StoreException e) {
92             LOG.error("StoreException encountered while reading domains", e);
93             throw new IDMStoreException(e);
94         }
95     }
96
97     @Override
98     public Role writeRole(Role role) throws IDMStoreException {
99         try {
100             return roleStore.createRole(role);
101         } catch (StoreException e) {
102             LOG.error("StoreException encountered while writing role", e);
103             throw new IDMStoreException(e);
104         }
105     }
106
107     @Override
108     public Role readRole(String roleid) throws IDMStoreException {
109         try {
110             return roleStore.getRole(roleid);
111         } catch (StoreException e) {
112             LOG.error("StoreException encountered while reading role", e);
113             throw new IDMStoreException(e);
114         }
115     }
116
117     @Override
118     public Role deleteRole(String roleid) throws IDMStoreException {
119         try {
120             return roleStore.deleteRole(roleid);
121         } catch (StoreException e) {
122             LOG.error("StoreException encountered while deleting role", e);
123             throw new IDMStoreException(e);
124         }
125     }
126
127     @Override
128     public Role updateRole(Role role) throws IDMStoreException {
129         try {
130             return roleStore.putRole(role);
131         } catch (StoreException e) {
132             LOG.error("StoreException encountered while updating role", e);
133             throw new IDMStoreException(e);
134         }
135     }
136
137     @Override
138     public Roles getRoles() throws IDMStoreException {
139         try {
140             return roleStore.getRoles();
141         } catch (StoreException e) {
142             LOG.error("StoreException encountered while getting roles", e);
143             throw new IDMStoreException(e);
144         }
145     }
146
147     @Override
148     public User writeUser(User user) throws IDMStoreException {
149         try {
150             return userStore.createUser(user);
151         } catch (StoreException e) {
152             LOG.error("StoreException encountered while writing user", e);
153             throw new IDMStoreException(e);
154         }
155     }
156
157     @Override
158     public User readUser(String userid) throws IDMStoreException {
159         try {
160             return userStore.getUser(userid);
161         } catch (StoreException e) {
162             LOG.error("StoreException encountered while reading user", e);
163             throw new IDMStoreException(e);
164         }
165     }
166
167     @Override
168     public User deleteUser(String userid) throws IDMStoreException {
169         try {
170             return userStore.deleteUser(userid);
171         } catch (StoreException e) {
172             LOG.error("StoreException encountered while deleting user", e);
173             throw new IDMStoreException(e);
174         }
175     }
176
177     @Override
178     public User updateUser(User user) throws IDMStoreException {
179         try {
180             return userStore.putUser(user);
181         } catch (StoreException e) {
182             LOG.error("StoreException encountered while updating user", e);
183             throw new IDMStoreException(e);
184         }
185     }
186
187     @Override
188     public Users getUsers(String username, String domain) throws IDMStoreException {
189         try {
190             return userStore.getUsers(username, domain);
191         } catch (StoreException e) {
192             LOG.error("StoreException encountered while reading users", e);
193             throw new IDMStoreException(e);
194         }
195     }
196
197     @Override
198     public Users getUsers() throws IDMStoreException {
199         try {
200             return userStore.getUsers();
201         } catch (StoreException e) {
202             LOG.error("StoreException encountered while reading users", e);
203             throw new IDMStoreException(e);
204         }
205     }
206
207     @Override
208     public Grant writeGrant(Grant grant) throws IDMStoreException {
209         try {
210             return grantStore.createGrant(grant);
211         } catch (StoreException e) {
212             LOG.error("StoreException encountered while writing grant", e);
213             throw new IDMStoreException(e);
214         }
215     }
216
217     @Override
218     public Grant readGrant(String grantid) throws IDMStoreException {
219         try {
220             return grantStore.getGrant(grantid);
221         } catch (StoreException e) {
222             LOG.error("StoreException encountered while reading grant", e);
223             throw new IDMStoreException(e);
224         }
225     }
226
227     @Override
228     public Grant readGrant(String domainid, String userid, String roleid) throws IDMStoreException {
229         return readGrant(IDMStoreUtil.createGrantid(userid, domainid, roleid));
230     }
231
232     @Override
233     public Grant deleteGrant(String grantid) throws IDMStoreException {
234         try {
235             return grantStore.deleteGrant(grantid);
236         } catch (StoreException e) {
237             LOG.error("StoreException encountered while deleting grant", e);
238             throw new IDMStoreException(e);
239         }
240     }
241
242     @Override
243     public Grants getGrants(String domainid, String userid) throws IDMStoreException {
244         try {
245             return grantStore.getGrants(domainid, userid);
246         } catch (StoreException e) {
247             LOG.error("StoreException encountered while getting grants", e);
248             throw new IDMStoreException(e);
249         }
250     }
251
252     @Override
253     public Grants getGrants(String userid) throws IDMStoreException {
254         try {
255             return grantStore.getGrants(userid);
256         } catch (StoreException e) {
257             LOG.error("StoreException encountered while getting grants", e);
258             throw new IDMStoreException(e);
259         }
260     }
261
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);
267     }
268
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();
272         user.setName(name);
273         user.setDomainid(domain);
274         user.setDescription(description);
275         user.setEmail(email);
276         user.setEnabled(enabled);
277         user.setPassword(password);
278         user.setSalt(salt);
279         return userStore.createUser(user);
280     }
281
282     public Role createRole(String name, String domain, String description)
283             throws StoreException {
284         Role role = new Role();
285         role.setDescription(description);
286         role.setName(name);
287         role.setDomainid(domain);
288         return roleStore.createRole(role);
289     }
290
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);
297     }
298
299 }