AAA-160: Fix aaa-cli commands
[aaa.git] / aaa-cli / src / main / java / org / opendaylight / aaa / cli / dmstore / AddRole.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.commands.Command;
12 import org.apache.karaf.shell.commands.Option;
13 import org.opendaylight.aaa.api.model.Role;
14 import org.opendaylight.aaa.cli.AaaCliAbstractCommand;
15 import org.opendaylight.aaa.cli.utils.CliUtils;
16 import org.opendaylight.aaa.cli.utils.DataStoreUtils;
17
18 @Command(name = "add-role", scope = "aaa", description = "Add role.")
19
20 /**
21  * @author mserngawy
22  *
23  */
24 public class AddRole extends AaaCliAbstractCommand {
25
26     @Option(name = "-name", aliases = {
27             "--roleName" }, description = "The role name", required = true, multiValued = false)
28     private String roleName = "";
29
30     @Option(name = "-dname", aliases = {
31             "--domainName" }, description = "The domain name", required = true, multiValued = false)
32     private String domainName = "";
33
34     @Option(name = "-desc", aliases = {
35             "--roleDescription" }, description = "The role Description", required = true, multiValued = false)
36     private String roleDesc = "";
37
38     public AddRole() {
39         super();
40     }
41
42     @Override
43     protected Object doExecute() throws Exception {
44         if (super.doExecute() == null) {
45             return CliUtils.LOGIN_FAILED_MESS;
46         }
47         Role role = new Role();
48         role.setDescription(roleDesc);
49         role.setName(roleName);
50         final String domainId = DataStoreUtils.getDomainId(identityStore, domainName);
51         if (domainId == null) {
52             return "Domain does not exist";
53         }
54         role.setDomainid(domainId);
55         role = identityStore.writeRole(role);
56         if (role != null) {
57             return "Role " + roleName + " has been created, Role Id is " + role.getRoleid();
58         }
59         return null;
60     }
61 }