Bump MRI upstreams
[aaa.git] / aaa-cli / src / main / java / org / opendaylight / aaa / cli / dmstore / AddGrant.java
1 /*
2  * Copyright (c) 2016, 2017 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.cli.dmstore;
10
11 import org.apache.karaf.shell.api.action.Command;
12 import org.apache.karaf.shell.api.action.Option;
13 import org.apache.karaf.shell.api.action.lifecycle.Service;
14 import org.opendaylight.aaa.api.model.Grant;
15 import org.opendaylight.aaa.cli.AaaCliAbstractCommand;
16 import org.opendaylight.aaa.cli.utils.DataStoreUtils;
17
18 /**
19  * Adds a grant.
20  *
21  * @author mserngawy
22  */
23 @Service
24 @Command(name = "add-grant", scope = "aaa", description = "Add Grant.")
25 public class AddGrant extends AaaCliAbstractCommand {
26     @Option(name = "-uname",
27             aliases = { "--userName" },
28             description = "The user name",
29             required = true,
30             multiValued = false)
31     private String userName;
32
33     @Option(name = "-dname",
34             aliases = { "--domainName" },
35             description = "The domain name",
36             required = true,
37             multiValued = false)
38     private String domainName;
39
40     @Option(name = "-rname",
41             aliases = { "--roleName" },
42             description = "The role name",
43             required = false,
44             multiValued = false)
45     private String roleName;
46
47     @Override
48     public Object execute() throws Exception {
49         if (super.execute() == null) {
50             return LOGIN_FAILED_MESS;
51         }
52         final String domainId = DataStoreUtils.getDomainId(identityStore, domainName);
53         if (domainId == null) {
54             return "Domain does not exist";
55         }
56         final String roleId = DataStoreUtils.getRoleId(identityStore, roleName);
57         if (roleId == null) {
58             return "Role does not exist";
59         }
60         final String usrId = DataStoreUtils.getUserId(identityStore, userName);
61         if (usrId == null) {
62             return "User does not exist";
63         }
64         Grant grant = new Grant();
65         grant.setDomainid(domainId);
66         grant.setRoleid(roleId);
67         grant.setUserid(usrId);
68         grant = identityStore.writeGrant(grant);
69         if (grant != null) {
70             return "Grant has been created, Grant id is " + grant.getGrantid();
71         }
72         return null;
73     }
74 }