0fed2789d987ab3acfc0c20123c3ad732e11c2b4
[moon.git] /
1 /*
2  * Copyright (c) 2016 Inocybe Technologies 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.idm.rest.test;
10
11 import java.util.ArrayList;
12 import java.util.List;
13
14 import org.opendaylight.aaa.api.IDMStoreException;
15 import org.opendaylight.aaa.api.IIDMStore;
16 import org.opendaylight.aaa.api.model.Domain;
17 import org.opendaylight.aaa.api.model.Domains;
18 import org.opendaylight.aaa.api.model.Grant;
19 import org.opendaylight.aaa.api.model.Grants;
20 import org.opendaylight.aaa.api.model.Role;
21 import org.opendaylight.aaa.api.model.Roles;
22 import org.opendaylight.aaa.api.model.User;
23 import org.opendaylight.aaa.api.model.Users;
24
25 public class IDMTestStore implements IIDMStore {
26
27     private List<Domain> domains = new ArrayList<Domain>();
28     private List<Grant> grants = new ArrayList<Grant>();
29     private List<Role> roles = new ArrayList<Role>();
30     private List<User> users = new ArrayList<User>();
31
32     public IDMTestStore() {
33         // TODO Auto-generated constructor stub
34     }
35
36     @Override
37     public Domain writeDomain(Domain domain) throws IDMStoreException {
38         domain.setDomainid(String.valueOf(domains.size()));
39         domains.add(domain);
40         return domain;
41     }
42
43     @Override
44     public Domain readDomain(String domainid) throws IDMStoreException {
45         for(Domain dom : domains)  {
46             if (dom.getDomainid().equals(domainid)) {
47                 return dom;
48             }
49         }
50         return null;
51     }
52
53     @Override
54     public Domain deleteDomain(String domainid) throws IDMStoreException {
55         for(Domain dom : domains)  {
56             if (dom.getDomainid().equals(domainid)) {
57                 domains.remove(dom);
58                 return dom;
59             }
60         }
61         return null;
62     }
63
64     @Override
65     public Domain updateDomain(Domain domain) throws IDMStoreException {
66         for(Domain dom : domains)  {
67             if (dom.getDomainid().equals(domain.getDomainid())) {
68                 domains.remove(dom);
69                 domains.add(domain);
70                 return domain;
71             }
72         }
73         return null;
74     }
75
76     @Override
77     public Domains getDomains() throws IDMStoreException {
78         Domains doms =  new Domains();
79         doms.setDomains(domains);
80         return doms;
81     }
82
83     @Override
84     public Role writeRole(Role role) throws IDMStoreException {
85         role.setRoleid(String.valueOf(roles.size()));
86         roles.add(role);
87         return role;
88     }
89
90     @Override
91     public Role readRole(String roleid) throws IDMStoreException {
92         for (Role role : roles) {
93             if (role.getRoleid().equals(roleid)) {
94                 return role;
95             }
96         }
97         return null;
98     }
99
100     @Override
101     public Role deleteRole(String roleid) throws IDMStoreException {
102         for (Role role : roles) {
103             if (role.getRoleid().equals(roleid)) {
104                 roles.remove(role);
105                 return role;
106             }
107         }
108         return null;
109     }
110
111     @Override
112     public Role updateRole(Role role) throws IDMStoreException {
113         for (Role inRole : roles) {
114             if (inRole.getRoleid().equals(role.getRoleid())) {
115                 roles.remove(inRole);
116                 roles.add(role);
117                 return role;
118             }
119         }
120         return null;
121     }
122
123     @Override
124     public Roles getRoles() throws IDMStoreException {
125         Roles rols = new Roles();
126         rols.setRoles(roles);
127         return rols;
128     }
129
130     @Override
131     public User writeUser(User user) throws IDMStoreException {
132         user.setUserid(String.valueOf(users.size()));
133         users.add(user);
134         return user;
135     }
136
137     @Override
138     public User readUser(String userid) throws IDMStoreException {
139         for(User usr : users) {
140             if (usr.getUserid().equals(userid)) {
141                 return usr;
142             }
143         }
144         return null;
145     }
146
147     @Override
148     public User deleteUser(String userid) throws IDMStoreException {
149         for(User usr : users) {
150             if (usr.getUserid().equals(userid)) {
151                 users.remove(usr);
152                 return usr;
153             }
154         }
155         return null;
156     }
157
158     @Override
159     public User updateUser(User user) throws IDMStoreException {
160         for(User usr : users) {
161             if (usr.getUserid().equals(user.getUserid())) {
162                 users.remove(usr);
163                 users.add(user);
164                 return usr;
165             }
166         }
167         return null;
168     }
169
170     @Override
171     public Users getUsers() throws IDMStoreException {
172         Users usrs = new Users();
173         usrs.setUsers(users);
174         return usrs;
175     }
176
177     @Override
178     public Users getUsers(String username, String domainId) throws IDMStoreException {
179         Users usrs = new Users();
180         User user = null;
181         Domain domain = null;
182         for(User usr : users) {
183             if (usr.getName().equals(username)) {
184                 user = usr;
185                 break;
186             }
187         }
188         for(Domain dom : domains) {
189             if (dom.getDomainid().equals(domainId)) {
190                 domain = dom;
191                 break;
192             }
193         }
194         if (user == null || domain == null)
195             return usrs;
196         for (Grant grant : grants) {
197             if (grant.getUserid().equals(user.getUserid()) && grant.getDomainid().equals(domain.getDomainid())) {
198                 List<User> usrList = new ArrayList<User>();
199                 usrList.add(user);
200                 usrs.setUsers(usrList);
201                 break;
202             }
203         }
204         return usrs;
205     }
206
207     @Override
208     public Grant writeGrant(Grant grant) throws IDMStoreException {
209         grant.setGrantid(String.valueOf(grants.size()));
210         grants.add(grant);
211         return grant;
212     }
213
214     @Override
215     public Grant readGrant(String grantid) throws IDMStoreException {
216         for (Grant grant : grants) {
217             if (grant.getGrantid().equals(grantid)) {
218                 return grant;
219             }
220         }
221         return null;
222     }
223
224     @Override
225     public Grant deleteGrant(String grantid) throws IDMStoreException {
226         for (Grant grant : grants) {
227             if (grant.getGrantid().equals(grantid)) {
228                 grants.remove(grant);
229                 return grant;
230             }
231         }
232         return null;
233     }
234
235     @Override
236     public Grants getGrants(String domainid, String userid) throws IDMStoreException {
237         Grants usrGrants = new Grants();
238         List<Grant> usrGrant = new ArrayList<Grant>();
239         for (Grant grant : grants) {
240             if (grant.getUserid().equals(userid) && grant.getDomainid().equals(domainid)) {
241                 usrGrant.add(grant);
242             }
243         }
244         usrGrants.setGrants(usrGrant);
245         return usrGrants;
246     }
247
248     @Override
249     public Grants getGrants(String userid) throws IDMStoreException {
250         Grants usrGrants = new Grants();
251         List<Grant> usrGrant = new ArrayList<Grant>();
252         for (Grant grant : grants) {
253             if (grant.getUserid().equals(userid)) {
254                 usrGrant.add(grant);
255             }
256         }
257         usrGrants.setGrants(usrGrant);
258         return usrGrants;
259     }
260
261     @Override
262     public Grant readGrant(String domainid, String userid, String roleid) throws IDMStoreException {
263         for (Grant grant : grants) {
264             if (grant.getDomainid().equals(domainid) && grant.getUserid().equals(userid) && grant.getRoleid().equals(roleid)) {
265                 return grant;
266             }
267         }
268         return null;
269     }
270
271 }