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.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;
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";
44 private static final Logger LOG = LoggerFactory.getLogger(H2Store.class);
46 private final DomainStore domainStore;
47 private final UserStore userStore;
48 private final RoleStore roleStore;
49 private final GrantStore grantStore;
52 public H2Store(@Reference final PasswordHashService passwordService, final Configuration config) {
53 this(config.username(), config.password(), passwordService);
54 LOG.info("H2 IDMStore activated");
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);
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);
71 LOG.info("H2 IDMStore deactivated");
75 public Domain writeDomain(final Domain domain) throws IDMStoreException {
77 return domainStore.createDomain(domain);
78 } catch (StoreException e) {
79 LOG.error("StoreException encountered while writing domain", e);
80 throw new IDMStoreException(e);
85 public Domain readDomain(final String domainid) throws IDMStoreException {
87 return domainStore.getDomain(domainid);
88 } catch (StoreException e) {
89 LOG.error("StoreException encountered while reading domain", e);
90 throw new IDMStoreException(e);
95 public Domain deleteDomain(final String domainid) throws IDMStoreException {
97 return domainStore.deleteDomain(domainid);
98 } catch (StoreException e) {
99 LOG.error("StoreException encountered while deleting domain", e);
100 throw new IDMStoreException(e);
105 public Domain updateDomain(final Domain domain) throws IDMStoreException {
107 return domainStore.putDomain(domain);
108 } catch (StoreException e) {
109 LOG.error("StoreException encountered while updating domain", e);
110 throw new IDMStoreException(e);
115 public Domains getDomains() throws IDMStoreException {
117 return domainStore.getDomains();
118 } catch (StoreException e) {
119 LOG.error("StoreException encountered while reading domains", e);
120 throw new IDMStoreException(e);
125 public Role writeRole(final Role role) throws IDMStoreException {
127 return roleStore.createRole(role);
128 } catch (StoreException e) {
129 LOG.error("StoreException encountered while writing role", e);
130 throw new IDMStoreException(e);
135 public Role readRole(final String roleid) throws IDMStoreException {
137 return roleStore.getRole(roleid);
138 } catch (StoreException e) {
139 LOG.error("StoreException encountered while reading role", e);
140 throw new IDMStoreException(e);
145 public Role deleteRole(final String roleid) throws IDMStoreException {
147 return roleStore.deleteRole(roleid);
148 } catch (StoreException e) {
149 LOG.error("StoreException encountered while deleting role", e);
150 throw new IDMStoreException(e);
155 public Role updateRole(final Role role) throws IDMStoreException {
157 return roleStore.putRole(role);
158 } catch (StoreException e) {
159 LOG.error("StoreException encountered while updating role", e);
160 throw new IDMStoreException(e);
165 public Roles getRoles() throws IDMStoreException {
167 return roleStore.getRoles();
168 } catch (StoreException e) {
169 LOG.error("StoreException encountered while getting roles", e);
170 throw new IDMStoreException(e);
175 public User writeUser(final User user) throws IDMStoreException {
177 return userStore.createUser(user);
178 } catch (StoreException e) {
179 LOG.error("StoreException encountered while writing user", e);
180 throw new IDMStoreException(e);
185 public User readUser(final String userid) throws IDMStoreException {
187 return userStore.getUser(userid);
188 } catch (StoreException e) {
189 LOG.error("StoreException encountered while reading user", e);
190 throw new IDMStoreException(e);
195 public User deleteUser(final String userid) throws IDMStoreException {
197 return userStore.deleteUser(userid);
198 } catch (StoreException e) {
199 LOG.error("StoreException encountered while deleting user", e);
200 throw new IDMStoreException(e);
205 public User updateUser(final User user) throws IDMStoreException {
207 return userStore.putUser(user);
208 } catch (StoreException e) {
209 LOG.error("StoreException encountered while updating user", e);
210 throw new IDMStoreException(e);
215 public Users getUsers(final String username, final String domain) throws IDMStoreException {
217 return userStore.getUsers(username, domain);
218 } catch (StoreException e) {
219 LOG.error("StoreException encountered while reading users", e);
220 throw new IDMStoreException(e);
225 public Users getUsers() throws IDMStoreException {
227 return userStore.getUsers();
228 } catch (StoreException e) {
229 LOG.error("StoreException encountered while reading users", e);
230 throw new IDMStoreException(e);
235 public Grant writeGrant(final Grant grant) throws IDMStoreException {
237 return grantStore.createGrant(grant);
238 } catch (StoreException e) {
239 LOG.error("StoreException encountered while writing grant", e);
240 throw new IDMStoreException(e);
245 public Grant readGrant(final String grantid) throws IDMStoreException {
247 return grantStore.getGrant(grantid);
248 } catch (StoreException e) {
249 LOG.error("StoreException encountered while reading grant", e);
250 throw new IDMStoreException(e);
255 public Grant readGrant(final String domainid, final String userid, final String roleid) throws IDMStoreException {
256 return readGrant(IDMStoreUtil.createGrantid(userid, domainid, roleid));
260 public Grant deleteGrant(final String grantid) throws IDMStoreException {
262 return grantStore.deleteGrant(grantid);
263 } catch (StoreException e) {
264 LOG.error("StoreException encountered while deleting grant", e);
265 throw new IDMStoreException(e);
270 public Grants getGrants(final String domainid, final String userid) throws IDMStoreException {
272 return grantStore.getGrants(domainid, userid);
273 } catch (StoreException e) {
274 LOG.error("StoreException encountered while getting grants", e);
275 throw new IDMStoreException(e);
280 public Grants getGrants(final String userid) throws IDMStoreException {
282 return grantStore.getGrants(userid);
283 } catch (StoreException e) {
284 LOG.error("StoreException encountered while getting grants", e);
285 throw new IDMStoreException(e);
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);
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();
300 user.setDomainid(domain);
301 user.setDescription(description);
302 user.setEmail(email);
303 user.setEnabled(enabled);
304 user.setPassword(password);
306 return userStore.createUser(user);
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);
314 role.setDomainid(domain);
315 return roleStore.createRole(role);
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);