46d5d052c14eb2836009f8f7625387f94093ad10
[aaf/authz.git] / auth / auth-cmd / src / main / java / org / onap / aaf / auth / cmd / user / ID.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  * ===========================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END====================================================
19  *
20  */
21
22 package org.onap.aaf.auth.cmd.user;
23
24 import org.onap.aaf.auth.cmd.AAFcli;
25 import org.onap.aaf.auth.cmd.Cmd;
26 import org.onap.aaf.auth.cmd.Param;
27 import org.onap.aaf.auth.rserv.HttpMethods;
28 import org.onap.aaf.cadi.CadiException;
29 import org.onap.aaf.cadi.LocatorException;
30 import org.onap.aaf.cadi.client.Future;
31 import org.onap.aaf.cadi.client.Rcli;
32 import org.onap.aaf.cadi.client.Retryable;
33 import org.onap.aaf.misc.env.APIException;
34
35 import aaf.v2_0.CredRequest;
36
37 public class ID extends Cmd {
38     public static final String ATTEMPT_FAILED_SPECIFICS_WITHELD = "Attempt Failed.  Specifics witheld.";
39     private static final String CRED_PATH = "/authn/cred";
40     private static final String[] options = {"add","del"};
41     public ID(User parent) {
42         super(parent,"fqi",
43                 new Param(optionsToString(options),true),
44                 new Param("id",true)
45         );
46     }
47
48     @Override
49     public int _exec(int _idx, final String ... args) throws CadiException, APIException, LocatorException { 
50         int idx = _idx;
51         String key = args[idx++];
52         final int option = whichOption(options,key);
53
54         final CredRequest cr = new CredRequest();
55         cr.setId(args[idx++]);
56         cr.setType(10);
57         if (args.length>idx)
58             cr.setEntry(args[idx]);
59         
60         // Set Start/End commands
61         setStartEnd(cr);
62         Integer ret = same(new Retryable<Integer>() {
63             @Override
64             public Integer code(Rcli<?> client) throws CadiException, APIException {
65                 Future<CredRequest> fp=null;
66                 String verb =null;
67                 switch(option) {
68                     case 0:
69                         fp = client.create(
70                             CRED_PATH, 
71                             getDF(CredRequest.class), 
72                             cr
73                             );
74                         verb = "Added ID [";
75                         break;
76                     case 1:
77                         setQueryParamsOn(client);
78                         fp = client.delete(CRED_PATH,
79                             getDF(CredRequest.class),
80                             cr
81                             );
82                         verb = "Deleted ID [";
83                         break;
84                     default:
85                         break;
86                 }
87                 if (fp==null) {
88                     return null; // get by Sonar check.
89                 }
90                 if (fp.get(AAFcli.timeout())) {
91                     pw().print(verb);
92                     pw().print(cr.getId());
93                     pw().println(']');
94                 } else if (fp.code()==202) {
95                     pw().println("ID Action Accepted, but requires Approvals before actualizing");
96                 } else if (fp.code()==409 && option==0) {
97                     pw().println("FQI already exists");
98                 } else if (fp.code()==406 && option==1) {
99                     pw().println("FQI does not exist");
100                 } else {
101                     pw().println(ATTEMPT_FAILED_SPECIFICS_WITHELD);
102                 }
103                 return fp.code();
104             }
105         });
106         if (ret==null)ret = -1;
107         return ret;
108     }
109     
110     @Override
111     public void detailedHelp(int _indent, StringBuilder sb) {
112             int indent = _indent;
113         detailLine(sb,indent,"Add or Delete Fully Qualified Identity: An ID attached to the Namespace");
114         indent+=2;
115         detailLine(sb,indent,"fqi      - the ID to create/delete within AAF");
116         sb.append('\n');
117         detailLine(sb,indent,"This usage has NO Credential, and serves only to allow IDs to be attached");
118         detailLine(sb,indent,"to Roles before credentials such as Certificates are established.");
119         detailLine(sb,indent,"The Domain can be related to any Namespace you have access to *");
120         detailLine(sb,indent,"The Domain is in reverse order of Namespace, i.e. ");
121         detailLine(sb,indent+2,"NS of com.att.myapp can create user of XY1234@myapp.att.com");
122         indent-=2;
123         api(sb,indent,HttpMethods.POST,"authn/cred",CredRequest.class,true);
124         api(sb,indent,HttpMethods.DELETE,"authn/cred",CredRequest.class,false);
125         api(sb,indent,HttpMethods.PUT,"authn/cred",CredRequest.class,false);
126     }
127 }