ce730cec8e600908c5b7613995ef2e09f5c431d3
[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          * Get History by Subject 
176          */
177         authzAPI.route(GET,"/authz/hist/subject/:type/:subject",API.HISTORY,new Code(facade,"Get History by Perm Type", true) {
178             @Override
179             public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
180                 int[] years;
181                 int descend;
182                 try {
183                     years = getYears(req);
184                     descend = decending(req);
185                 } catch (Exception e) {
186                     context.error(trans, resp, Result.err(Status.ERR_BadData, e.getMessage()));
187                     return;
188                 }
189                 
190                 Result<Void> r = context.getHistoryBySubject(trans, resp, pathParam(req,":type"), pathParam(req,":subject"),years,descend);
191                 switch(r.status) {
192                     case OK:
193                         resp.setStatus(HttpStatus.OK_200); 
194                         break;
195                     default:
196                         context.error(trans,resp,r);
197                 }
198             }
199         });
200     }
201
202     // Check if Ascending
203     private static int decending(HttpServletRequest req) {
204         if ("true".equalsIgnoreCase(req.getParameter("desc")))return -1;
205         if ("true".equalsIgnoreCase(req.getParameter("asc")))return 1;
206         return 0;
207     }
208     
209     // Get Common "yyyymm" parameter, or none
210     
211     private static int[] getYears(HttpServletRequest req) throws NumberFormatException {
212         // Sonar says threading issues.
213         SimpleDateFormat FMT = new SimpleDateFormat("yyyyMM");
214         String yyyymm = req.getParameter("yyyymm");
215         ArrayList<Integer> ai= new ArrayList<>();
216         if (yyyymm==null) {
217             GregorianCalendar gc = new GregorianCalendar();
218             // three months is the default
219             for (int i=0;i<3;++i) {
220                 ai.add(Integer.parseInt(FMT.format(gc.getTime())));
221                 gc.add(GregorianCalendar.MONTH, -1);
222             }
223         } else {
224             for (String ym : yyyymm.split(",")) {
225                 String range[] = ym.split("\\s*-\\s*");
226                 switch(range.length) {
227                     case 0:
228                         break;
229                     case 1:
230                         if (!ym.endsWith("-")) {
231                             ai.add(getNum(ym));
232                             break;
233                         } else {
234                             range=new String[] {ym.substring(0, 6),FMT.format(new Date())};
235                         }
236                     default:
237                         GregorianCalendar gc = new GregorianCalendar();
238                         gc.set(GregorianCalendar.MONTH, Integer.parseInt(range[1].substring(4,6))-1);
239                         gc.set(GregorianCalendar.YEAR, Integer.parseInt(range[1].substring(0,4)));
240                         int end = getNum(FMT.format(gc.getTime())); 
241                         
242                         gc.set(GregorianCalendar.MONTH, Integer.parseInt(range[0].substring(4,6))-1);
243                         gc.set(GregorianCalendar.YEAR, Integer.parseInt(range[0].substring(0,4)));
244                         for (int i=getNum(FMT.format(gc.getTime()));i<=end;gc.add(GregorianCalendar.MONTH, 1),i=getNum(FMT.format(gc.getTime()))) {
245                             ai.add(i);
246                         }
247
248                 }
249             }
250         }
251         if (ai.size()==0) {
252             throw new NumberFormatException(yyyymm + " is an invalid number or range");
253         }
254         Collections.sort(ai);
255         int ym[] = new int[ai.size()];
256         for (int i=0;i<ym.length;++i) {
257             ym[i]=ai.get(i);
258         }
259         return ym;
260     }
261     
262     private static int getNum(String n) {
263         if (n==null || n.length()!=6) throw new NumberFormatException(n + " is not in YYYYMM format");
264         return Integer.parseInt(n);
265     }
266 }