aboutsummaryrefslogtreecommitdiffstats
path: root/upstream/odl-aaa-moon/aaa/aaa-authn-mdsal-store/aaa-authn-mdsal-store-impl/src/main/java/org/opendaylight/aaa/authn/mdsal/store/IDMMDSALStore.java
blob: 88fba0ba2a03513cbc75c9c402dc7a6f15e0c62f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/*
 * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */
package org.opendaylight.aaa.authn.mdsal.store;

import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import com.google.common.util.concurrent.CheckedFuture;
import java.util.List;
import java.util.concurrent.ExecutionException;
import org.opendaylight.aaa.api.IDMStoreException;
import org.opendaylight.aaa.api.IDMStoreUtil;
import org.opendaylight.aaa.api.SHA256Calculator;
import org.opendaylight.controller.md.sal.binding.api.DataBroker;
import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
import org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.Authentication;
import org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.Domain;
import org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.DomainBuilder;
import org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.DomainKey;
import org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.Grant;
import org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.GrantBuilder;
import org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.GrantKey;
import org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.Role;
import org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.RoleBuilder;
import org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.RoleKey;
import org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.User;
import org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.UserBuilder;
import org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.authentication.UserKey;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Sharon Aicler - saichler@cisco.com
 *
 */
public class IDMMDSALStore {

    private static final Logger LOG = LoggerFactory.getLogger(IDMMDSALStore.class);
    private final DataBroker dataBroker;

    public IDMMDSALStore(DataBroker dataBroker) {
        this.dataBroker = dataBroker;
    }

    public static final String getString(String aValue, String bValue) {
        if (aValue != null)
            return aValue;
        return bValue;
    }

    public static final Boolean getBoolean(Boolean aValue, Boolean bValue) {
        if (aValue != null)
            return aValue;
        return bValue;
    }

    public static boolean waitForSubmit(CheckedFuture<Void, TransactionCommitFailedException> submit) {
        // This can happen only when testing
        if (submit == null)
            return false;
        while (!submit.isDone() && !submit.isCancelled()) {
            try {
                Thread.sleep(1000);
            } catch (Exception err) {
                LOG.error("Interrupted", err);
            }
        }
        return submit.isCancelled();
    }

    // Domain methods
    public Domain writeDomain(Domain domain) {
        Preconditions.checkNotNull(domain);
        Preconditions.checkNotNull(domain.getName());
        Preconditions.checkNotNull(domain.isEnabled());
        DomainBuilder b = new DomainBuilder();
        b.setDescription(domain.getDescription());
        b.setDomainid(domain.getName());
        b.setEnabled(domain.isEnabled());
        b.setName(domain.getName());
        b.setKey(new DomainKey(b.getName()));
        domain = b.build();
        InstanceIdentifier<Domain> ID = InstanceIdentifier.create(Authentication.class).child(
                Domain.class, new DomainKey(domain.getDomainid()));
        WriteTransaction wrt = dataBroker.newWriteOnlyTransaction();
        wrt.put(LogicalDatastoreType.CONFIGURATION, ID, domain, true);
        CheckedFuture<Void, TransactionCommitFailedException> submit = wrt.submit();
        if (!waitForSubmit(submit)) {
            return domain;
        } else {
            return null;
        }
    }

    public Domain readDomain(String domainid) {
        Preconditions.checkNotNull(domainid);
        InstanceIdentifier<Domain> ID = InstanceIdentifier.create(Authentication.class).child(
                Domain.class, new DomainKey(domainid));
        ReadOnlyTransaction rot = dataBroker.newReadOnlyTransaction();
        CheckedFuture<Optional<Domain>, ReadFailedException> read = rot.read(
                LogicalDatastoreType.CONFIGURATION, ID);
        if (read == null) {
            LOG.error("Failed to read domain from data store");
            return null;
        }
        Optional<Domain> optional = null;
        try {
            optional = read.get();
        } catch (InterruptedException | ExecutionException e1) {
            LOG.error("Failed to read domain from data store", e1);
            return null;
        }

        if (optional == null)
            return null;

        if (!optional.isPresent())
            return null;

        return optional.get();
    }

    public Domain deleteDomain(String domainid) {
        Preconditions.checkNotNull(domainid);
        Domain domain = readDomain(domainid);
        if (domain == null) {
            LOG.error("Failed to delete domain from data store, unknown domain");
            return null;
        }
        InstanceIdentifier<Domain> ID = InstanceIdentifier.create(Authentication.class).child(
                Domain.class, new DomainKey(domainid));
        WriteTransaction wrt = dataBroker.newWriteOnlyTransaction();
        wrt.delete(LogicalDatastoreType.CONFIGURATION, ID);
        wrt.submit();
        return domain;
    }

    public Domain updateDomain(Domain domain) throws IDMStoreException {
        Preconditions.checkNotNull(domain);
        Preconditions.checkNotNull(domain.getDomainid());
        Domain existing = readDomain(domain.getDomainid());
        DomainBuilder b = new DomainBuilder();
        b.setDescription(getString(domain.getDescription(), existing.getDescription()));
        b.setName(existing.getName());
        b.setEnabled(getBoolean(domain.isEnabled(), existing.isEnabled()));
        return writeDomain(b.build());
    }

    public List<Domain> getAllDomains() {
        InstanceIdentifier<Authentication> id = InstanceIdentifier.create(Authentication.class);
        ReadOnlyTransaction rot = dataBroker.newReadOnlyTransaction();
        CheckedFuture<Optional<Authentication>, ReadFailedException> read = rot.read(
                LogicalDatastoreType.CONFIGURATION, id);
        if (read == null)
            return null;

        try {
            if (read.get() == null)
                return null;
            if (read.get().isPresent()) {
                Authentication auth = read.get().get();
                return auth.getDomain();
            }
        } catch (Exception err) {
            LOG.error("Failed to read domains", err);
        }
        return null;
    }

    public List<Role> getAllRoles() {
        InstanceIdentifier<Authentication> id = InstanceIdentifier.create(Authentication.class);
        ReadOnlyTransaction rot = dataBroker.newReadOnlyTransaction();
        CheckedFuture<Optional<Authentication>, ReadFailedException> read = rot.read(
                LogicalDatastoreType.CONFIGURATION, id);
        if (read == null)
            return null;

        try {
            if (read.get() == null)
                return null;
            if (read.get().isPresent()) {
                Authentication auth = read.get().get();
                return auth.getRole();
            }
        } catch (Exception err) {
            LOG.error("Failed to read domains", err);
        }
        return null;
    }

    public List<User> getAllUsers() {
        InstanceIdentifier<Authentication> id = InstanceIdentifier.create(Authentication.class);
        ReadOnlyTransaction rot = dataBroker.newReadOnlyTransaction();
        CheckedFuture<Optional<Authentication>, ReadFailedException> read = rot.read(
                LogicalDatastoreType.CONFIGURATION, id);
        if (read == null)
            return null;

        try {
            if (read.get() == null)
                return null;
            if (read.get().isPresent()) {
                Authentication auth = read.get().get();
                return auth.getUser();
            }
        } catch (Exception err) {
            LOG.error("Failed to read domains", err);
        }
        return null;
    }

    public List<Grant> getAllGrants() {
        InstanceIdentifier<Authentication> id = InstanceIdentifier.create(Authentication.class);
        ReadOnlyTransaction rot = dataBroker.newReadOnlyTransaction();
        CheckedFuture<Optional<Authentication>, ReadFailedException> read = rot.read(
                LogicalDatastoreType.CONFIGURATION, id);
        if (read == null)
            return null;

        try {
            if (read.get() == null)
                return null;
            if (read.get().isPresent()) {
                Authentication auth = read.get().get();
                return auth.getGrant();
            }
        } catch (Exception err) {
            LOG.error("Failed to read domains", err);
        }
        return null;
    }

    // Role methods
    public Role writeRole(Role role) {
        Preconditions.checkNotNull(role);
        Preconditions.checkNotNull(role.getName());
        Preconditions.checkNotNull(role.getDomainid());
        Preconditions.checkNotNull(readDomain(role.getDomainid()));
        RoleBuilder b = new RoleBuilder();
        b.setDescription(role.getDescription());
        b.setRoleid(IDMStoreUtil.createRoleid(role.getName(), role.getDomainid()));
        b.setKey(new RoleKey(b.getRoleid()));
        b.setName(role.getName());
        b.setDomainid(role.getDomainid());
        role = b.build();
        InstanceIdentifier<Role> ID = InstanceIdentifier.create(Authentication.class).child(
                Role.class, new RoleKey(role.getRoleid()));
        WriteTransaction wrt = dataBroker.newWriteOnlyTransaction();
        wrt.put(LogicalDatastoreType.CONFIGURATION, ID, role, true);
        CheckedFuture<Void, TransactionCommitFailedException> submit = wrt.submit();
        if (!waitForSubmit(submit)) {
            return role;
        } else {
            return null;
        }
    }

    public Role readRole(String roleid) {
        Preconditions.checkNotNull(roleid);
        InstanceIdentifier<Role> ID = InstanceIdentifier.create(Authentication.class).child(
                Role.class, new RoleKey(roleid));
        ReadOnlyTransaction rot = dataBroker.newReadOnlyTransaction();
        CheckedFuture<Optional<Role>, ReadFailedException> read = rot.read(
                LogicalDatastoreType.CONFIGURATION, ID);
        if (read == null) {
            LOG.error("Failed to read role from data store");
            return null;
        }
        Optional<Role> optional = null;
        try {
            optional = read.get();
        } catch (InterruptedException | ExecutionException e1) {
            LOG.error("Failed to read role from data store", e1);
            return null;
        }

        if (optional == null)
            return null;

        if (!optional.isPresent())
            return null;

        return optional.get();
    }

    public Role deleteRole(String roleid) {
        Preconditions.checkNotNull(roleid);
        Role role = readRole(roleid);
        if (role == null) {
            LOG.error("Failed to delete role from data store, unknown role");
            return null;
        }
        InstanceIdentifier<Role> ID = InstanceIdentifier.create(Authentication.class).child(
                Role.class, new RoleKey(roleid));
        WriteTransaction wrt = dataBroker.newWriteOnlyTransaction();
        wrt.delete(LogicalDatastoreType.CONFIGURATION, ID);
        wrt.submit();
        return role;
    }

    public Role updateRole(Role role) {
        Preconditions.checkNotNull(role);
        Preconditions.checkNotNull(role.getRoleid());
        Role existing = readRole(role.getRoleid());
        RoleBuilder b = new RoleBuilder();
        b.setDescription(getString(role.getDescription(), existing.getDescription()));
        b.setName(existing.getName());
        b.setDomainid(existing.getDomainid());
        return writeRole(b.build());
    }

    // User methods
    public User writeUser(User user) throws IDMStoreException {
        Preconditions.checkNotNull(user);
        Preconditions.checkNotNull(user.getName());
        Preconditions.checkNotNull(user.getDomainid());
        Preconditions.checkNotNull(readDomain(user.getDomainid()));
        UserBuilder b = new UserBuilder();
        if (user.getSalt() == null) {
            b.setSalt(SHA256Calculator.generateSALT());
        } else {
            b.setSalt(user.getSalt());
        }
        b.setUserid(IDMStoreUtil.createUserid(user.getName(), user.getDomainid()));
        b.setDescription(user.getDescription());
        b.setDomainid(user.getDomainid());
        b.setEmail(user.getEmail());
        b.setEnabled(user.isEnabled());
        b.setKey(new UserKey(b.getUserid()));
        b.setName(user.getName());
        b.setPassword(SHA256Calculator.getSHA256(user.getPassword(), b.getSalt()));
        user = b.build();
        InstanceIdentifier<User> ID = InstanceIdentifier.create(Authentication.class).child(
                User.class, new UserKey(user.getUserid()));
        WriteTransaction wrt = dataBroker.newWriteOnlyTransaction();
        wrt.put(LogicalDatastoreType.CONFIGURATION, ID, user, true);
        CheckedFuture<Void, TransactionCommitFailedException> submit = wrt.submit();
        if (!waitForSubmit(submit)) {
            return user;
        } else {
            return null;
        }
    }

    public User readUser(String userid) {
        Preconditions.checkNotNull(userid);
        InstanceIdentifier<User> ID = InstanceIdentifier.create(Authentication.class).child(
                User.class, new UserKey(userid));
        ReadOnlyTransaction rot = dataBroker.newReadOnlyTransaction();
        CheckedFuture<Optional<User>, ReadFailedException> read = rot.read(
                LogicalDatastoreType.CONFIGURATION, ID);
        if (read == null) {
            LOG.error("Failed to read user from data store");
            return null;
        }
        Optional<User> optional = null;
        try {
            optional = read.get();
        } catch (InterruptedException | ExecutionException e1) {
            LOG.error("Failed to read domain from data store", e1);
            return null;
        }

        if (optional == null)
            return null;

        if (!optional.isPresent())
            return null;

        return optional.get();
    }

    public User deleteUser(String userid) {
        Preconditions.checkNotNull(userid);
        User user = readUser(userid);
        if (user == null) {
            LOG.error("Failed to delete user from data store, unknown user");
            return null;
        }
        InstanceIdentifier<User> ID = InstanceIdentifier.create(Authentication.class).child(
                User.class, new UserKey(userid));
        WriteTransaction wrt = dataBroker.newWriteOnlyTransaction();
        wrt.delete(LogicalDatastoreType.CONFIGURATION, ID);
        wrt.submit();
        return user;
    }

    public User updateUser(User user) throws IDMStoreException {
        Preconditions.checkNotNull(user);
        Preconditions.checkNotNull(user.getUserid());
        User existing = readUser(user.getUserid());
        UserBuilder b = new UserBuilder();
        b.setName(existing.getName());
        b.setDomainid(existing.getDomainid());
        b.setDescription(getString(user.getDescription(), existing.getDescription()));
        b.setEmail(getString(user.getEmail(), existing.getEmail()));
        b.setEnabled(getBoolean(user.isEnabled(), existing.isEnabled()));
        b.setPassword(getString(user.getPassword(), existing.getPassword()));
        b.setSalt(getString(user.getSalt(), existing.getSalt()));
        return writeUser(b.build());
    }

    // Grant methods
    public Grant writeGrant(Grant grant) throws IDMStoreException {
        Preconditions.checkNotNull(grant);
        Preconditions.checkNotNull(grant.getDomainid());
        Preconditions.checkNotNull(grant.getUserid());
        Preconditions.checkNotNull(grant.getRoleid());
        Preconditions.checkNotNull(readDomain(grant.getDomainid()));
        Preconditions.checkNotNull(readUser(grant.getUserid()));
        Preconditions.checkNotNull(readRole(grant.getRoleid()));
        GrantBuilder b = new GrantBuilder();
        b.setDomainid(grant.getDomainid());
        b.setRoleid(grant.getRoleid());
        b.setUserid(grant.getUserid());
        b.setGrantid(IDMStoreUtil.createGrantid(grant.getUserid(), grant.getDomainid(),
                grant.getRoleid()));
        b.setKey(new GrantKey(b.getGrantid()));
        grant = b.build();
        InstanceIdentifier<Grant> ID = InstanceIdentifier.create(Authentication.class).child(
                Grant.class, new GrantKey(grant.getGrantid()));
        WriteTransaction wrt = dataBroker.newWriteOnlyTransaction();
        wrt.put(LogicalDatastoreType.CONFIGURATION, ID, grant, true);
        CheckedFuture<Void, TransactionCommitFailedException> submit = wrt.submit();
        if (!waitForSubmit(submit)) {
            return grant;
        } else {
            return null;
        }
    }

    public Grant readGrant(String grantid) {
        Preconditions.checkNotNull(grantid);
        InstanceIdentifier<Grant> ID = InstanceIdentifier.create(Authentication.class).child(
                Grant.class, new GrantKey(grantid));
        ReadOnlyTransaction rot = dataBroker.newReadOnlyTransaction();
        CheckedFuture<Optional<Grant>, ReadFailedException> read = rot.read(
                LogicalDatastoreType.CONFIGURATION, ID);
        if (read == null) {
            LOG.error("Failed to read grant from data store");
            return null;
        }
        Optional<Grant> optional = null;
        try {
            optional = read.get();
        } catch (InterruptedException | ExecutionException e1) {
            LOG.error("Failed to read domain from data store", e1);
            return null;
        }

        if (optional == null)
            return null;

        if (!optional.isPresent())
            return null;

        return optional.get();
    }

    public Grant deleteGrant(String grantid) {
        Preconditions.checkNotNull(grantid);
        Grant grant = readGrant(grantid);
        if (grant == null) {
            LOG.error("Failed to delete grant from data store, unknown grant");
            return null;
        }
        InstanceIdentifier<Grant> ID = InstanceIdentifier.create(Authentication.class).child(
                Grant.class, new GrantKey(grantid));
        WriteTransaction wrt = dataBroker.newWriteOnlyTransaction();
        wrt.delete(LogicalDatastoreType.CONFIGURATION, ID);
        wrt.submit();
        return grant;
    }
}