Remove OSGiH2Store
[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.osgi.service.component.annotations.Activate;
24 import org.osgi.service.component.annotations.Component;
25 import org.osgi.service.component.annotations.Deactivate;
26 import org.osgi.service.component.annotations.Reference;
27 import org.osgi.service.metatype.annotations.AttributeDefinition;
28 import org.osgi.service.metatype.annotations.Designate;
29 import org.osgi.service.metatype.annotations.ObjectClassDefinition;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 @Component(immediate = true, configurationPid = "org.opendaylight.aaa.h2", property = "type=default")
34 @Designate(ocd = H2Store.Configuration.class)
35 public class H2Store implements IIDMStore {
36     @ObjectClassDefinition
37     public @interface Configuration {
38         @AttributeDefinition(name = "dbUserName")
39         String username() default "foo";
40         @AttributeDefinition(name = "dbPassword")
41         String password() default "bar";
42     }
43
44     private static final Logger LOG = LoggerFactory.getLogger(H2Store.class);
45
46     private final DomainStore domainStore;
47     private final UserStore userStore;
48     private final RoleStore roleStore;
49     private final GrantStore grantStore;
50
51     @Activate
52     public H2Store(@Reference final PasswordHashService passwordService, final Configuration config) {
53         this(config.username(), config.password(), passwordService);
54         LOG.info("H2 IDMStore activated");
55     }
56
57     public H2Store(final String dbUsername, final String dbPassword, final PasswordHashService passwordService) {
58         this(new IdmLightSimpleConnectionProvider(
59                 new IdmLightConfigBuilder().dbUser(dbUsername).dbPwd(dbPassword).build()), passwordService);
60     }
61
62     public H2Store(final ConnectionProvider connectionFactory, final PasswordHashService passwordService) {
63         domainStore = new DomainStore(connectionFactory);
64         userStore = new UserStore(connectionFactory, passwordService);
65         roleStore = new RoleStore(connectionFactory);
66         grantStore = new GrantStore(connectionFactory);
67     }
68
69     @Deactivate
70     void deactivate() {
71         LOG.info("H2 IDMStore deactivated");
72     }
73
74     @Override
75     public Domain writeDomain(final Domain domain) throws IDMStoreException {
76         try {
77             return domainStore.createDomain(domain);
78         } catch (StoreException e) {
79             LOG.error("StoreException encountered while writing domain", e);
80             throw new IDMStoreException(e);
81         }
82     }
83
84     @Override
85     public Domain readDomain(final String domainid) throws IDMStoreException {
86         try {
87             return domainStore.getDomain(domainid);
88         } catch (StoreException e) {
89             LOG.error("StoreException encountered while reading domain", e);
90             throw new IDMStoreException(e);
91         }
92     }
93
94     @Override
95     public Domain deleteDomain(final String domainid) throws IDMStoreException {
96         try {
97             return domainStore.deleteDomain(domainid);
98         } catch (StoreException e) {
99             LOG.error("StoreException encountered while deleting domain", e);
100             throw new IDMStoreException(e);
101         }
102     }
103
104     @Override
105     public Domain updateDomain(final Domain domain) throws IDMStoreException {
106         try {
107             return domainStore.putDomain(domain);
108         } catch (StoreException e) {
109             LOG.error("StoreException encountered while updating domain", e);
110             throw new IDMStoreException(e);
111         }
112     }
113
114     @Override
115     public Domains getDomains() throws IDMStoreException {
116         try {
117             return domainStore.getDomains();
118         } catch (StoreException e) {
119             LOG.error("StoreException encountered while reading domains", e);
120             throw new IDMStoreException(e);
121         }
122     }
123
124     @Override
125     public Role writeRole(final Role role) throws IDMStoreException {
126         try {
127             return roleStore.createRole(role);
128         } catch (StoreException e) {
129             LOG.error("StoreException encountered while writing role", e);
130             throw new IDMStoreException(e);
131         }
132     }
133
134     @Override
135     public Role readRole(final String roleid) throws IDMStoreException {
136         try {
137             return roleStore.getRole(roleid);
138         } catch (StoreException e) {
139             LOG.error("StoreException encountered while reading role", e);
140             throw new IDMStoreException(e);
141         }
142     }
143
144     @Override
145     public Role deleteRole(final String roleid) throws IDMStoreException {
146         try {
147             return roleStore.deleteRole(roleid);
148         } catch (StoreException e) {
149             LOG.error("StoreException encountered while deleting role", e);
150             throw new IDMStoreException(e);
151         }
152     }
153
154     @Override
155     public Role updateRole(final Role role) throws IDMStoreException {
156         try {
157             return roleStore.putRole(role);
158         } catch (StoreException e) {
159             LOG.error("StoreException encountered while updating role", e);
160             throw new IDMStoreException(e);
161         }
162     }
163
164     @Override
165     public Roles getRoles() throws IDMStoreException {
166         try {
167             return roleStore.getRoles();
168         } catch (StoreException e) {
169             LOG.error("StoreException encountered while getting roles", e);
170             throw new IDMStoreException(e);
171         }
172     }
173
174     @Override
175     public User writeUser(final User user) throws IDMStoreException {
176         try {
177             return userStore.createUser(user);
178         } catch (StoreException e) {
179             LOG.error("StoreException encountered while writing user", e);
180             throw new IDMStoreException(e);
181         }
182     }
183
184     @Override
185     public User readUser(final String userid) throws IDMStoreException {
186         try {
187             return userStore.getUser(userid);
188         } catch (StoreException e) {
189             LOG.error("StoreException encountered while reading user", e);
190             throw new IDMStoreException(e);
191         }
192     }
193
194     @Override
195     public User deleteUser(final String userid) throws IDMStoreException {
196         try {
197             return userStore.deleteUser(userid);
198         } catch (StoreException e) {
199             LOG.error("StoreException encountered while deleting user", e);
200             throw new IDMStoreException(e);
201         }
202     }
203
204     @Override
205     public User updateUser(final User user) throws IDMStoreException {
206         try {
207             return userStore.putUser(user);
208         } catch (StoreException e) {
209             LOG.error("StoreException encountered while updating user", e);
210             throw new IDMStoreException(e);
211         }
212     }
213
214     @Override
215     public Users getUsers(final String username, final String domain) throws IDMStoreException {
216         try {
217             return userStore.getUsers(username, domain);
218         } catch (StoreException e) {
219             LOG.error("StoreException encountered while reading users", e);
220             throw new IDMStoreException(e);
221         }
222     }
223
224     @Override
225     public Users getUsers() throws IDMStoreException {
226         try {
227             return userStore.getUsers();
228         } catch (StoreException e) {
229             LOG.error("StoreException encountered while reading users", e);
230             throw new IDMStoreException(e);
231         }
232     }
233
234     @Override
235     public Grant writeGrant(final Grant grant) throws IDMStoreException {
236         try {
237             return grantStore.createGrant(grant);
238         } catch (StoreException e) {
239             LOG.error("StoreException encountered while writing grant", e);
240             throw new IDMStoreException(e);
241         }
242     }
243
244     @Override
245     public Grant readGrant(final String grantid) throws IDMStoreException {
246         try {
247             return grantStore.getGrant(grantid);
248         } catch (StoreException e) {
249             LOG.error("StoreException encountered while reading grant", e);
250             throw new IDMStoreException(e);
251         }
252     }
253
254     @Override
255     public Grant readGrant(final String domainid, final String userid, final String roleid) throws IDMStoreException {
256         return readGrant(IDMStoreUtil.createGrantid(userid, domainid, roleid));
257     }
258
259     @Override
260     public Grant deleteGrant(final String grantid) throws IDMStoreException {
261         try {
262             return grantStore.deleteGrant(grantid);
263         } catch (StoreException e) {
264             LOG.error("StoreException encountered while deleting grant", e);
265             throw new IDMStoreException(e);
266         }
267     }
268
269     @Override
270     public Grants getGrants(final String domainid, final String userid) throws IDMStoreException {
271         try {
272             return grantStore.getGrants(domainid, userid);
273         } catch (StoreException e) {
274             LOG.error("StoreException encountered while getting grants", e);
275             throw new IDMStoreException(e);
276         }
277     }
278
279     @Override
280     public Grants getGrants(final String userid) throws IDMStoreException {
281         try {
282             return grantStore.getGrants(userid);
283         } catch (StoreException e) {
284             LOG.error("StoreException encountered while getting grants", e);
285             throw new IDMStoreException(e);
286         }
287     }
288
289     public Domain createDomain(final String domainName, final boolean enable) throws StoreException {
290         Domain domain = new Domain();
291         domain.setName(domainName);
292         domain.setEnabled(enable);
293         return domainStore.createDomain(domain);
294     }
295
296     public User createUser(final String name, final String password, final String domain, final String description,
297             final String email, final boolean enabled, final String salt) throws StoreException {
298         User user = new User();
299         user.setName(name);
300         user.setDomainid(domain);
301         user.setDescription(description);
302         user.setEmail(email);
303         user.setEnabled(enabled);
304         user.setPassword(password);
305         user.setSalt(salt);
306         return userStore.createUser(user);
307     }
308
309     public Role createRole(final String name, final String domain, final String description)
310             throws StoreException {
311         Role role = new Role();
312         role.setDescription(description);
313         role.setName(name);
314         role.setDomainid(domain);
315         return roleStore.createRole(role);
316     }
317
318     public Grant createGrant(final String domain, final String user, final String role) throws StoreException {
319         Grant grant = new Grant();
320         grant.setDomainid(domain);
321         grant.setRoleid(role);
322         grant.setUserid(user);
323         return grantStore.createGrant(grant);
324     }
325
326 }