Collection syntax change because of Sonar
[aaf/authz.git] / auth / auth-service / src / main / java / org / onap / aaf / auth / service / api / API_History.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.service.api;
23
24 import static org.onap.aaf.auth.layer.Result.OK;
25 import static org.onap.aaf.auth.rserv.HttpMethods.GET;
26
27 import java.text.SimpleDateFormat;
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.Date;
31 import java.util.GregorianCalendar;
32
33 import javax.servlet.http.HttpServletRequest;
34 import javax.servlet.http.HttpServletResponse;
35
36 import org.eclipse.jetty.http.HttpStatus;
37 import org.onap.aaf.auth.dao.cass.Status;
38 import org.onap.aaf.auth.env.AuthzTrans;
39 import org.onap.aaf.auth.layer.Result;
40 import org.onap.aaf.auth.service.AAF_Service;
41 import org.onap.aaf.auth.service.Code;
42 import org.onap.aaf.auth.service.facade.AuthzFacade;
43 import org.onap.aaf.auth.service.mapper.Mapper.API;
44
45 /**
46  * Pull certain types of History Info
47  * 
48  * Specify yyyymm as 
49  *      single - 201504
50  *  commas 201503,201504
51  *  ranges 201501-201504
52  *  combinations 201301,201401,201501-201504
53  *  
54  * @author Jonathan
55  *
56  */
57 public class API_History {
58         /**
59          * Normal Init level APIs
60          * 
61          * @param authzAPI
62          * @param facade
63          * @throws Exception
64          */
65         public static void init(AAF_Service authzAPI, AuthzFacade facade) throws Exception {
66                 /**
67                  * Get History
68                  */
69                 authzAPI.route(GET,"/authz/hist/user/:user",API.HISTORY,new Code(facade,"Get History by User", true) {
70                         @Override
71                         public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
72                                 int[] years;
73                                 int descend;
74                                 try {
75                                         years = getYears(req);
76                                         descend = decending(req);
77                                 } catch(Exception e) {
78                                         context.error(trans, resp, Result.err(Status.ERR_BadData, e.getMessage()));
79                                         return;
80                                 }
81
82                                 Result<Void> r = context.getHistoryByUser(trans, resp, pathParam(req,":user"),years,descend);
83                                 switch(r.status) {
84                                         case OK:
85                                                 resp.setStatus(HttpStatus.OK_200); 
86                                                 break;
87                                         default:
88                                                 context.error(trans,resp,r);
89                                 }
90                         }
91                 });
92
93                 /**
94                  * Get History by NS
95                  */
96                 authzAPI.route(GET,"/authz/hist/ns/:ns",API.HISTORY,new Code(facade,"Get History by Namespace", true) {
97                         @Override
98                         public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
99                                 int[] years;
100                                 int descend;
101                                 try {
102                                         years = getYears(req);
103                                         descend = decending(req);
104                                 } catch(Exception e) {
105                                         context.error(trans, resp, Result.err(Status.ERR_BadData, e.getMessage()));
106                                         return;
107                                 }
108                                 
109                                 Result<Void> r = context.getHistoryByNS(trans, resp, pathParam(req,":ns"),years,descend);
110                                 switch(r.status) {
111                                         case OK:
112                                                 resp.setStatus(HttpStatus.OK_200); 
113                                                 break;
114                                         default:
115                                                 context.error(trans,resp,r);
116                                 }
117                         }
118                 });
119
120                 /**
121                  * Get History by Role
122                  */
123                 authzAPI.route(GET,"/authz/hist/role/:role",API.HISTORY,new Code(facade,"Get History by Role", true) {
124                         @Override
125                         public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
126                                 int[] years;
127                                 int descend;
128                                 try {
129                                         years = getYears(req);
130                                         descend = decending(req);
131                                 } catch(Exception e) {
132                                         context.error(trans, resp, Result.err(Status.ERR_BadData, e.getMessage()));
133                                         return;
134                                 }
135
136                                 Result<Void> r = context.getHistoryByRole(trans, resp, pathParam(req,":role"),years,descend);
137                                 switch(r.status) {
138                                         case OK:
139                                                 resp.setStatus(HttpStatus.OK_200); 
140                                                 break;
141                                         default:
142                                                 context.error(trans,resp,r);
143                                 }
144                         }
145                 });
146
147                 /**
148                  * Get History by Perm Type
149                  */
150                 authzAPI.route(GET,"/authz/hist/perm/:type",API.HISTORY,new Code(facade,"Get History by Perm Type", true) {
151                         @Override
152                         public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
153                                 int[] years;
154                                 int descend;
155                                 try {
156                                         years = getYears(req);
157                                         descend = decending(req);
158                                 } catch(Exception e) {
159                                         context.error(trans, resp, Result.err(Status.ERR_BadData, e.getMessage()));
160                                         return;
161                                 }
162                                 
163                                 Result<Void> r = context.getHistoryByPerm(trans, resp, pathParam(req,":type"),years,descend);
164                                 switch(r.status) {
165                                         case OK:
166                                                 resp.setStatus(HttpStatus.OK_200); 
167                                                 break;
168                                         default:
169                                                 context.error(trans,resp,r);
170                                 }
171                         }
172                 });
173         }
174
175         // Check if Ascending
176         private static int decending(HttpServletRequest req) {
177                 if("true".equalsIgnoreCase(req.getParameter("desc")))return -1;
178                 if("true".equalsIgnoreCase(req.getParameter("asc")))return 1;
179                 return 0;
180         }
181         
182         // Get Common "yyyymm" parameter, or none
183         
184         private static int[] getYears(HttpServletRequest req) throws NumberFormatException {
185                 // Sonar says threading issues.
186                 SimpleDateFormat FMT = new SimpleDateFormat("yyyyMM");
187                 String yyyymm = req.getParameter("yyyymm");
188                 ArrayList<Integer> ai= new ArrayList<>();
189                 if(yyyymm==null) {
190                         GregorianCalendar gc = new GregorianCalendar();
191                         // three months is the default
192                         for(int i=0;i<3;++i) {
193                                 ai.add(Integer.parseInt(FMT.format(gc.getTime())));
194                                 gc.add(GregorianCalendar.MONTH, -1);
195                         }
196                 } else {
197                         for(String ym : yyyymm.split(",")) {
198                                 String range[] = ym.split("\\s*-\\s*");
199                                 switch(range.length) {
200                                         case 0:
201                                                 break;
202                                         case 1:
203                                                 if(!ym.endsWith("-")) {
204                                                         ai.add(getNum(ym));
205                                                         break;
206                                                 } else {
207                                                         range=new String[] {ym.substring(0, 6),FMT.format(new Date())};
208                                                 }
209                                         default:
210                                                 GregorianCalendar gc = new GregorianCalendar();
211                                                 gc.set(GregorianCalendar.MONTH, Integer.parseInt(range[1].substring(4,6))-1);
212                                                 gc.set(GregorianCalendar.YEAR, Integer.parseInt(range[1].substring(0,4)));
213                                                 int end = getNum(FMT.format(gc.getTime())); 
214                                                 
215                                                 gc.set(GregorianCalendar.MONTH, Integer.parseInt(range[0].substring(4,6))-1);
216                                                 gc.set(GregorianCalendar.YEAR, Integer.parseInt(range[0].substring(0,4)));
217                                                 for(int i=getNum(FMT.format(gc.getTime()));i<=end;gc.add(GregorianCalendar.MONTH, 1),i=getNum(FMT.format(gc.getTime()))) {
218                                                         ai.add(i);
219                                                 }
220
221                                 }
222                         }
223                 }
224                 if(ai.size()==0) {
225                         throw new NumberFormatException(yyyymm + " is an invalid number or range");
226                 }
227                 Collections.sort(ai);
228                 int ym[] = new int[ai.size()];
229                 for(int i=0;i<ym.length;++i) {
230                         ym[i]=ai.get(i);
231                 }
232                 return ym;
233         }
234         
235         private static int getNum(String n) {
236                 if(n==null || n.length()!=6) throw new NumberFormatException(n + " is not in YYYYMM format");
237                 return Integer.parseInt(n);
238         }
239 }