Migrate callers of Optional.get()
[aaa.git] / aaa-cli-jar / src / main / java / org / opendaylight / aaa / cli / jar / StandaloneCommandLineInterface.java
1 /*
2  * Copyright (c) 2016 - 2017 Red Hat, Inc. 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.cli.jar;
9
10 import static java.util.Objects.requireNonNull;
11
12 import java.io.File;
13 import java.io.IOException;
14 import java.util.List;
15 import java.util.Optional;
16 import java.util.stream.Collectors;
17 import org.opendaylight.aaa.api.IDMStoreException;
18 import org.opendaylight.aaa.api.IIDMStore;
19 import org.opendaylight.aaa.api.StoreBuilder;
20 import org.opendaylight.aaa.api.model.User;
21 import org.opendaylight.aaa.api.password.service.PasswordHashService;
22 import org.opendaylight.aaa.datastore.h2.H2Store;
23 import org.opendaylight.aaa.datastore.h2.IdmLightConfig;
24 import org.opendaylight.aaa.datastore.h2.IdmLightConfigBuilder;
25 import org.opendaylight.aaa.datastore.h2.IdmLightSimpleConnectionProvider;
26 import org.opendaylight.aaa.impl.password.service.DefaultPasswordHashService;
27
28 /**
29  * AAA CLI interface.
30  * This is for a "standalone Java" environment (i.e. plain JSE; non-OSGi, no Karaf).
31  *
32  * @author Michael Vorburger.ch
33  */
34 public class StandaloneCommandLineInterface {
35
36     private final IIDMStore identityStore;
37     private final StoreBuilder storeBuilder;
38     private static final String DOMAIN = IIDMStore.DEFAULT_DOMAIN;
39     private final PasswordHashService passwordService;
40
41     public StandaloneCommandLineInterface(File directoryWithDatabaseFile) throws IOException, IDMStoreException {
42         IdmLightConfigBuilder configBuider = new IdmLightConfigBuilder();
43         configBuider.dbDirectory(directoryWithDatabaseFile.getCanonicalPath()).dbUser("foo").dbPwd("bar");
44         IdmLightConfig config = configBuider.build();
45
46         passwordService = new DefaultPasswordHashService();
47
48         H2Store h2Store = new H2Store(new IdmLightSimpleConnectionProvider(config), passwordService);
49         this.identityStore = h2Store;
50
51         this.storeBuilder = new StoreBuilder(h2Store);
52         storeBuilder.initDomainAndRolesWithoutUsers(DOMAIN);
53     }
54
55     public List<String> getAllUserNames() throws IDMStoreException {
56         List<User> users = identityStore.getUsers().getUsers();
57         return users.stream().map(User::getName).collect(Collectors.toList());
58     }
59
60     public boolean resetPassword(String userIdWithoutDomain, String newPassword) throws IDMStoreException {
61         Optional<User> optUser = getSingleUser(userIdWithoutDomain);
62         if (!optUser.isPresent()) {
63             return false;
64         } else {
65             User user = optUser.orElseThrow();
66             user.setPassword(newPassword);
67             identityStore.updateUser(user);
68             return true;
69         }
70     }
71
72     /**
73      * Check a user's password.
74      * See <a href="https://bugs.opendaylight.org/show_bug.cgi?id=8721">Bug 8721 requirement</a>.
75      */
76     public boolean checkUserPassword(String userIdWithoutDomain, String password) throws IDMStoreException {
77         Optional<User> optUser = getSingleUser(userIdWithoutDomain);
78         if (!optUser.isPresent()) {
79             return false;
80         } else {
81             User user = optUser.orElseThrow();
82             return passwordService.passwordsMatch(password, user.getPassword(), user.getSalt());
83         }
84     }
85
86     private Optional<User> getSingleUser(String userIdWithoutDomain) throws IDMStoreException {
87         requireNonNull(userIdWithoutDomain, "userIdWithoutDomain == null");
88         List<User> users = identityStore.getUsers(userIdWithoutDomain, DOMAIN).getUsers();
89         if (users.isEmpty()) {
90             return Optional.empty();
91         }
92         if (users.size() > 1) {
93             throw new IDMStoreException("More than 1 user found: " + userIdWithoutDomain);
94         }
95         return Optional.of(users.get(0));
96     }
97
98     public void createNewUser(String userName, String password, boolean isAdmin) throws IDMStoreException {
99         requireNonNull(userName, "userName == null");
100         storeBuilder.createUser(DOMAIN, userName, password, isAdmin);
101     }
102
103     public boolean deleteUser(String userIdWithoutDomain) throws IDMStoreException {
104         requireNonNull(userIdWithoutDomain, "userIdWithoutDomain == null");
105         return storeBuilder.deleteUser(DOMAIN, userIdWithoutDomain);
106     }
107 }