4b718f832035d8a7e92f4e94d05f85a9fa4bdb59
[aaa.git] / aaa-shiro / impl / src / main / java / org / opendaylight / aaa / datastore / h2 / OSGiH2Store.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. 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 package org.opendaylight.aaa.datastore.h2;
9
10 import static com.google.common.base.Verify.verifyNotNull;
11
12 import com.google.common.annotations.Beta;
13 import org.opendaylight.aaa.api.IDMStoreException;
14 import org.opendaylight.aaa.api.IIDMStore;
15 import org.opendaylight.aaa.api.model.Domain;
16 import org.opendaylight.aaa.api.model.Domains;
17 import org.opendaylight.aaa.api.model.Grant;
18 import org.opendaylight.aaa.api.model.Grants;
19 import org.opendaylight.aaa.api.model.Role;
20 import org.opendaylight.aaa.api.model.Roles;
21 import org.opendaylight.aaa.api.model.User;
22 import org.opendaylight.aaa.api.model.Users;
23 import org.opendaylight.aaa.api.password.service.PasswordHashService;
24 import org.osgi.service.component.annotations.Activate;
25 import org.osgi.service.component.annotations.Component;
26 import org.osgi.service.component.annotations.Deactivate;
27 import org.osgi.service.component.annotations.Reference;
28 import org.osgi.service.metatype.annotations.AttributeDefinition;
29 import org.osgi.service.metatype.annotations.Designate;
30 import org.osgi.service.metatype.annotations.ObjectClassDefinition;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 @Beta
35 @Component(immediate = true, configurationPid = "org.opendaylight.aaa.h2", property = "type=default")
36 @Designate(ocd = OSGiH2Store.Configuration.class)
37 // FIXME: merge this with H2Store when we have constructor injection
38 public final class OSGiH2Store implements IIDMStore {
39     @ObjectClassDefinition
40     public @interface Configuration {
41         @AttributeDefinition(name = "dbUserName")
42         String username() default "foo";
43         @AttributeDefinition(name = "dbPassword")
44         String password() default "bar";
45     }
46
47     private static final Logger LOG = LoggerFactory.getLogger(OSGiH2Store.class);
48
49     @Reference
50     PasswordHashService passwordService;
51
52     private H2Store delegate;
53
54     @Override
55     public Domain writeDomain(final Domain domain) throws IDMStoreException {
56         return delegate().writeDomain(domain);
57     }
58
59     @Override
60     public Domain readDomain(final String domainid) throws IDMStoreException {
61         return delegate().readDomain(domainid);
62     }
63
64     @Override
65     public Domain deleteDomain(final String domainid) throws IDMStoreException {
66         return delegate().deleteDomain(domainid);
67     }
68
69     @Override
70     public Domain updateDomain(final Domain domain) throws IDMStoreException {
71         return delegate().updateDomain(domain);
72     }
73
74     @Override
75     public Domains getDomains() throws IDMStoreException {
76         return delegate().getDomains();
77     }
78
79     @Override
80     public Role writeRole(final Role role) throws IDMStoreException {
81         return delegate().writeRole(role);
82     }
83
84     @Override
85     public Role readRole(final String roleid) throws IDMStoreException {
86         return delegate().readRole(roleid);
87     }
88
89     @Override
90     public Role deleteRole(final String roleid) throws IDMStoreException {
91         return delegate().deleteRole(roleid);
92     }
93
94     @Override
95     public Role updateRole(final Role role) throws IDMStoreException {
96         return delegate().updateRole(role);
97     }
98
99     @Override
100     public Roles getRoles() throws IDMStoreException {
101         return delegate().getRoles();
102     }
103
104     @Override
105     public User writeUser(final User user) throws IDMStoreException {
106         return delegate().writeUser(user);
107     }
108
109     @Override
110     public User readUser(final String userid) throws IDMStoreException {
111         return delegate().readUser(userid);
112     }
113
114     @Override
115     public User deleteUser(final String userid) throws IDMStoreException {
116         return delegate().deleteUser(userid);
117     }
118
119     @Override
120     public User updateUser(final User user) throws IDMStoreException {
121         return delegate().updateUser(user);
122     }
123
124     @Override
125     public Users getUsers() throws IDMStoreException {
126         return delegate().getUsers();
127     }
128
129     @Override
130     public Users getUsers(final String username, final String domain) throws IDMStoreException {
131         return delegate().getUsers(username, domain);
132     }
133
134     @Override
135     public Grant writeGrant(final Grant grant) throws IDMStoreException {
136         return delegate().writeGrant(grant);
137     }
138
139     @Override
140     public Grant readGrant(final String grantid) throws IDMStoreException {
141         return delegate().readGrant(grantid);
142     }
143
144     @Override
145     public Grant readGrant(final String domainid, final String userid, final String roleid) throws IDMStoreException {
146         return delegate().readGrant(domainid, userid, roleid);
147     }
148
149     @Override
150     public Grant deleteGrant(final String grantid) throws IDMStoreException {
151         return delegate().deleteGrant(grantid);
152     }
153
154     @Override
155     public Grants getGrants(final String domainid, final String userid) throws IDMStoreException {
156         return delegate().getGrants(domainid, userid);
157     }
158
159     @Override
160     public Grants getGrants(final String userid) throws IDMStoreException {
161         return delegate().getGrants(userid);
162     }
163
164     @Activate
165     void activate(final Configuration config) {
166         delegate = new H2Store(config.username(), config.password(), passwordService);
167         LOG.info("H2 IDMStore activated");
168     }
169
170     @Deactivate
171     void deactivate() {
172         delegate = null;
173         LOG.info("H2 IDMStore deactivated");
174     }
175
176     private H2Store delegate() {
177         return verifyNotNull(delegate);
178     }
179 }