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