f82a018a2cbd35f7fb7a97657a21767d5b8c007e
[aaa.git] / aaa-authn-api / src / main / java / org / opendaylight / aaa / api / StoreBuilder.java
1 /*
2  * Copyright (c) 2014, 2015 Hewlett-Packard Development Company, L.P. 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.api;
10
11 import org.opendaylight.aaa.api.model.Domain;
12 import org.opendaylight.aaa.api.model.Grant;
13 import org.opendaylight.aaa.api.model.Role;
14 import org.opendaylight.aaa.api.model.User;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 /**
19  * StoreBuilder is triggered during feature installation by
20  * <code>AAAIDMLightModule.createInstance()</code>. StoreBuilder is responsible
21  * for initializing the H2 database with initial default user account
22  * information. By default, the following users are created:
23  * <ol>
24  * <li>admin</li>
25  * <li>user</li>
26  * </ol>
27  *
28  * By default, the following domain is created:
29  * <ol>
30  * <li>sdn</li>
31  * </ol>
32  *
33  * By default, the following grants are created:
34  * <ol>
35  * <li>admin with admin role on sdn</li>
36  * <li>admin with user role on sdn</li>
37  * <li>user with user role on sdn</li>
38  * </ol>
39  *
40  * @author peter.mellquist@hp.com
41  * @author saichler@cisco.com
42  */
43 public class StoreBuilder {
44
45     private static final Logger LOG = LoggerFactory.getLogger(StoreBuilder.class);
46
47     private final IIDMStore store;
48
49     public StoreBuilder(IIDMStore store) {
50         super();
51         this.store = store;
52     }
53
54     public void init() throws IDMStoreException {
55         LOG.info("creating idmlight schema in store");
56
57         // Check whether the default domain exists. If it exists, then do not
58         // create default data in the store.
59         // TODO Address the fact that someone may delete the sdn domain, or make
60         // sdn mandatory.
61         Domain defaultDomain = store.readDomain(IIDMStore.DEFAULT_DOMAIN);
62         if (defaultDomain != null) {
63             LOG.info("Found default domain in Store, skipping insertion of default data");
64             return;
65         }
66
67         // make domain
68         Domain domain = new Domain();
69         User adminUser = new User();
70         User userUser = new User();
71         Role adminRole = new Role();
72         Role userRole = new Role();
73         domain.setEnabled(true);
74         domain.setName(IIDMStore.DEFAULT_DOMAIN);
75         domain.setDescription("default odl sdn domain");
76         domain = store.writeDomain(domain);
77
78         // Create default users
79         // "admin" user
80         adminUser.setEnabled(true);
81         adminUser.setName("admin");
82         adminUser.setDomainid(domain.getDomainid());
83         adminUser.setDescription("admin user");
84         adminUser.setEmail("");
85         adminUser.setPassword("admin");
86         adminUser = store.writeUser(adminUser);
87         // "user" user
88         userUser.setEnabled(true);
89         userUser.setName("user");
90         userUser.setDomainid(domain.getDomainid());
91         userUser.setDescription("user user");
92         userUser.setEmail("");
93         userUser.setPassword("user");
94         userUser = store.writeUser(userUser);
95
96         // Create default Roles ("admin" and "user")
97         adminRole.setName("admin");
98         adminRole.setDomainid(domain.getDomainid());
99         adminRole.setDescription("a role for admins");
100         adminRole = store.writeRole(adminRole);
101         userRole.setName("user");
102         userRole.setDomainid(domain.getDomainid());
103         userRole.setDescription("a role for users");
104         userRole = store.writeRole(userRole);
105
106         // Create default grants
107         Grant grant = new Grant();
108         grant.setDomainid(domain.getDomainid());
109         grant.setUserid(userUser.getUserid());
110         grant.setRoleid(userRole.getRoleid());
111         grant = store.writeGrant(grant);
112
113         grant.setDomainid(domain.getDomainid());
114         grant.setUserid(adminUser.getUserid());
115         grant.setRoleid(userRole.getRoleid());
116         grant = store.writeGrant(grant);
117
118         grant.setDomainid(domain.getDomainid());
119         grant.setUserid(adminUser.getUserid());
120         grant.setRoleid(adminRole.getRoleid());
121         grant = store.writeGrant(grant);
122     }
123 }