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