da40a17bb1e30c93a49f6958a90b38000c89921b
[moon.git] /
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.h2.persistence;
10
11 import java.sql.Connection;
12 import java.sql.DriverManager;
13
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;
28
29 public class H2Store implements IIDMStore {
30
31     private static final Logger LOG = LoggerFactory.getLogger(H2Store.class);
32
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();
38
39     public H2Store() {
40     }
41
42     public static Connection getConnection(Connection existingConnection) throws StoreException {
43         Connection connection = existingConnection;
44         try {
45             if (existingConnection == null || existingConnection.isClosed()) {
46                 new org.h2.Driver();
47                 connection = DriverManager.getConnection(config.getDbPath(), config.getDbUser(),
48                         config.getDbPwd());
49             }
50         } catch (Exception e) {
51             throw new StoreException("Cannot connect to database server" + e);
52         }
53
54         return connection;
55     }
56
57     public static IdmLightConfig getConfig() {
58         return config;
59     }
60
61     @Override
62     public Domain writeDomain(Domain domain) throws IDMStoreException {
63         try {
64             return domainStore.createDomain(domain);
65         } catch (StoreException e) {
66             LOG.error("StoreException encountered while writing domain", e);
67             throw new IDMStoreException(e);
68         }
69     }
70
71     @Override
72     public Domain readDomain(String domainid) throws IDMStoreException {
73         try {
74             return domainStore.getDomain(domainid);
75         } catch (StoreException e) {
76             LOG.error("StoreException encountered while reading domain", e);
77             throw new IDMStoreException(e);
78         }
79     }
80
81     @Override
82     public Domain deleteDomain(String domainid) throws IDMStoreException {
83         try {
84             return domainStore.deleteDomain(domainid);
85         } catch (StoreException e) {
86             LOG.error("StoreException encountered while deleting domain", e);
87             throw new IDMStoreException(e);
88         }
89     }
90
91     @Override
92     public Domain updateDomain(Domain domain) throws IDMStoreException {
93         try {
94             return domainStore.putDomain(domain);
95         } catch (StoreException e) {
96             LOG.error("StoreException encountered while updating domain", e);
97             throw new IDMStoreException(e);
98         }
99     }
100
101     @Override
102     public Domains getDomains() throws IDMStoreException {
103         try {
104             return domainStore.getDomains();
105         } catch (StoreException e) {
106             LOG.error("StoreException encountered while reading domains", e);
107             throw new IDMStoreException(e);
108         }
109     }
110
111     @Override
112     public Role writeRole(Role role) throws IDMStoreException {
113         try {
114             return roleStore.createRole(role);
115         } catch (StoreException e) {
116             LOG.error("StoreException encountered while writing role", e);
117             throw new IDMStoreException(e);
118         }
119     }
120
121     @Override
122     public Role readRole(String roleid) throws IDMStoreException {
123         try {
124             return roleStore.getRole(roleid);
125         } catch (StoreException e) {
126             LOG.error("StoreException encountered while reading role", e);
127             throw new IDMStoreException(e);
128         }
129     }
130
131     @Override
132     public Role deleteRole(String roleid) throws IDMStoreException {
133         try {
134             return roleStore.deleteRole(roleid);
135         } catch (StoreException e) {
136             LOG.error("StoreException encountered while deleting role", e);
137             throw new IDMStoreException(e);
138         }
139     }
140
141     @Override
142     public Role updateRole(Role role) throws IDMStoreException {
143         try {
144             return roleStore.putRole(role);
145         } catch (StoreException e) {
146             LOG.error("StoreException encountered while updating role", e);
147             throw new IDMStoreException(e);
148         }
149     }
150
151     @Override
152     public Roles getRoles() throws IDMStoreException {
153         try {
154             return roleStore.getRoles();
155         } catch (StoreException e) {
156             LOG.error("StoreException encountered while getting roles", e);
157             throw new IDMStoreException(e);
158         }
159     }
160
161     @Override
162     public User writeUser(User user) throws IDMStoreException {
163         try {
164             return userStore.createUser(user);
165         } catch (StoreException e) {
166             LOG.error("StoreException encountered while writing user", e);
167             throw new IDMStoreException(e);
168         }
169     }
170
171     @Override
172     public User readUser(String userid) throws IDMStoreException {
173         try {
174             return userStore.getUser(userid);
175         } catch (StoreException e) {
176             LOG.error("StoreException encountered while reading user", e);
177             throw new IDMStoreException(e);
178         }
179     }
180
181     @Override
182     public User deleteUser(String userid) throws IDMStoreException {
183         try {
184             return userStore.deleteUser(userid);
185         } catch (StoreException e) {
186             LOG.error("StoreException encountered while deleting user", e);
187             throw new IDMStoreException(e);
188         }
189     }
190
191     @Override
192     public User updateUser(User user) throws IDMStoreException {
193         try {
194             return userStore.putUser(user);
195         } catch (StoreException e) {
196             LOG.error("StoreException encountered while updating user", e);
197             throw new IDMStoreException(e);
198         }
199     }
200
201     @Override
202     public Users getUsers(String username, String domain) throws IDMStoreException {
203         try {
204             return userStore.getUsers(username, domain);
205         } catch (StoreException e) {
206             LOG.error("StoreException encountered while reading users", e);
207             throw new IDMStoreException(e);
208         }
209     }
210
211     @Override
212     public Users getUsers() throws IDMStoreException {
213         try {
214             return userStore.getUsers();
215         } catch (StoreException e) {
216             LOG.error("StoreException encountered while reading users", e);
217             throw new IDMStoreException(e);
218         }
219     }
220
221     @Override
222     public Grant writeGrant(Grant grant) throws IDMStoreException {
223         try {
224             return grantStore.createGrant(grant);
225         } catch (StoreException e) {
226             LOG.error("StoreException encountered while writing grant", e);
227             throw new IDMStoreException(e);
228         }
229     }
230
231     @Override
232     public Grant readGrant(String grantid) throws IDMStoreException {
233         try {
234             return grantStore.getGrant(grantid);
235         } catch (StoreException e) {
236             LOG.error("StoreException encountered while reading grant", e);
237             throw new IDMStoreException(e);
238         }
239     }
240
241     @Override
242     public Grant deleteGrant(String grantid) throws IDMStoreException {
243         try {
244             return grantStore.deleteGrant(grantid);
245         } catch (StoreException e) {
246             LOG.error("StoreException encountered while deleting grant", e);
247             throw new IDMStoreException(e);
248         }
249     }
250
251     @Override
252     public Grants getGrants(String domainid, String userid) throws IDMStoreException {
253         try {
254             return grantStore.getGrants(domainid, userid);
255         } catch (StoreException e) {
256             LOG.error("StoreException encountered while getting grants", e);
257             throw new IDMStoreException(e);
258         }
259     }
260
261     @Override
262     public Grants getGrants(String userid) throws IDMStoreException {
263         try {
264             return grantStore.getGrants(userid);
265         } catch (StoreException e) {
266             LOG.error("StoreException encountered while getting grants", e);
267             throw new IDMStoreException(e);
268         }
269     }
270
271     @Override
272     public Grant readGrant(String domainid, String userid, String roleid) throws IDMStoreException {
273         return readGrant(IDMStoreUtil.createGrantid(userid, domainid, roleid));
274     }
275
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);
282     }
283
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();
287         User u = new User();
288         u.setName(name);
289         u.setDomainid(domain);
290         u.setDescription(description);
291         u.setEmail(email);
292         u.setEnabled(enabled);
293         u.setPassword(password);
294         u.setSalt(SALT);
295         return us.createUser(u);
296     }
297
298     public static Role createRole(String name, String domain, String description)
299             throws StoreException {
300         RoleStore rs = new RoleStore();
301         Role r = new Role();
302         r.setDescription(description);
303         r.setName(name);
304         r.setDomainid(domain);
305         return rs.createRole(r);
306     }
307
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);
312         g.setRoleid(role);
313         g.setUserid(user);
314         return gs.createGrant(g);
315     }
316 }