[AAF-21] Updated Copyright Headers for AAF
[aaf/authz.git] / authz-cmd / src / main / java / com / att / cmd / ns / Create.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aaf\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * ===========================================================================\r
7  * * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * * you may not use this file except in compliance with the License.\r
9  * * You may obtain a copy of the License at\r
10  * * \r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * * \r
13  *  * Unless required by applicable law or agreed to in writing, software\r
14  * * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * * See the License for the specific language governing permissions and\r
17  * * limitations under the License.\r
18  * * ============LICENSE_END====================================================\r
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 package com.att.cmd.ns;\r
24 \r
25 import com.att.cadi.CadiException;\r
26 import com.att.cadi.LocatorException;\r
27 import com.att.cadi.client.Future;\r
28 import com.att.cadi.client.Rcli;\r
29 import com.att.cadi.client.Retryable;\r
30 import com.att.cmd.AAFcli;\r
31 import com.att.cmd.Cmd;\r
32 import com.att.cmd.Param;\r
33 import com.att.cssa.rserv.HttpMethods;\r
34 import com.att.inno.env.APIException;\r
35 \r
36 import aaf.v2_0.NsRequest;\r
37 \r
38 /**\r
39  * p\r
40  *\r
41  */\r
42 public class Create extends Cmd {\r
43         private static final String COMMA = ",";\r
44 \r
45         public Create(NS parent) {\r
46                 super(parent,"create", \r
47                                 new Param("name",true),\r
48                                 new Param("responsible (id[,id]*)",true), \r
49                                 new Param("admin (id[,id]*)",false));\r
50         }\r
51 \r
52         @Override\r
53         public int _exec(int _idx, final String ... args) throws CadiException, APIException, LocatorException {\r
54                 int idx = _idx;\r
55 \r
56                 final NsRequest nr = new NsRequest();\r
57                 \r
58                 String realm = getOrgRealm();\r
59                 \r
60                 nr.setName(args[idx++]);\r
61                 String[] responsible = args[idx++].split(COMMA);\r
62                 for(String s : responsible) {\r
63                         if (s.indexOf('@') < 0 && realm != null) s += '@' + realm;\r
64                         nr.getResponsible().add(s);\r
65                 }\r
66                 String[] admin;\r
67                 if(args.length>idx) {\r
68                         admin = args[idx++].split(COMMA);\r
69                 } else {\r
70                         admin = responsible;\r
71                 }\r
72                 for(String s : admin) {\r
73                         if (s.indexOf('@') < 0 && realm != null) s += '@' + realm;\r
74                         nr.getAdmin().add(s);\r
75                 }\r
76                 \r
77                 // Set Start/End commands\r
78                 setStartEnd(nr);\r
79                 \r
80                 return same(new Retryable<Integer>() {\r
81                         @Override\r
82                         public Integer code(Rcli<?> client) throws CadiException, APIException {\r
83                                 // Requestable\r
84                                 setQueryParamsOn(client);\r
85                                 Future<NsRequest> fp = client.create(\r
86                                                 "/authz/ns", \r
87                                                 getDF(NsRequest.class),\r
88                                                 nr\r
89                                                 );\r
90                                 if(fp.get(AAFcli.timeout())) {\r
91                                         pw().println("Created Namespace");\r
92                                 } else {\r
93                                         if(fp.code()==202) {\r
94                                                 pw().println("Namespace Creation Accepted, but requires Approvals before actualizing");\r
95                                         } else {\r
96                                                 error(fp);\r
97                                         }\r
98                                 }\r
99                                 return fp.code();\r
100                         }\r
101                 });\r
102         }\r
103 \r
104         @Override\r
105         public void detailedHelp(int _indent, StringBuilder sb) {\r
106                 int indent = _indent;\r
107                 detailLine(sb,indent,"Create a Namespace");\r
108                 indent+=2;\r
109                 detailLine(sb,indent,"name        - Namespaces are dot-delimited, ex com.att.myapp");\r
110                 detailLine(sb,indent+14,"and must be created with parent credentials.");\r
111                 detailLine(sb,indent+14,"Ex: to create com.att.myapp, you must be admin for com.att");\r
112                 detailLine(sb,indent+14,"or com");\r
113                 detailLine(sb,indent,"responsible - This is the person(s) who receives Notifications and");\r
114                 detailLine(sb,indent+14,"approves Requests regarding this Namespace. Companies have");\r
115                 detailLine(sb,indent+14,"Policies as to who may take on this responsibility");\r
116                 detailLine(sb,indent,"admin       - These are the people who are allowed to make changes on");\r
117                 detailLine(sb,indent+14,"the Namespace, including creating Roles, Permissions");\r
118                 detailLine(sb,indent+14,"and Credentials");\r
119                 sb.append('\n');\r
120                 detailLine(sb,indent,"Namespaces can be created even though there are Roles/Permissions which");\r
121                 detailLine(sb,indent,"start with the requested sub-namespace.  They are reassigned to the");\r
122                 detailLine(sb,indent,"Child Namespace");\r
123                 indent-=2;\r
124                 api(sb,indent,HttpMethods.POST,"authz/ns",NsRequest.class,true);\r
125         }\r
126 \r
127 }\r