AT&T 2.0.19 Code drop, stage 3
[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         private static final SimpleDateFormat FMT = new SimpleDateFormat("yyyyMM");
184         
185         private static int[] getYears(HttpServletRequest req) throws NumberFormatException {
186                 String yyyymm = req.getParameter("yyyymm");
187                 ArrayList<Integer> ai= new ArrayList<Integer>();
188                 if(yyyymm==null) {
189                         GregorianCalendar gc = new GregorianCalendar();
190                         // three months is the default
191                         for(int i=0;i<3;++i) {
192                                 ai.add(Integer.parseInt(FMT.format(gc.getTime())));
193                                 gc.add(GregorianCalendar.MONTH, -1);
194                         }
195                 } else {
196                         for(String ym : yyyymm.split(",")) {
197                                 String range[] = ym.split("\\s*-\\s*");
198                                 switch(range.length) {
199                                         case 0:
200                                                 break;
201                                         case 1:
202                                                 if(!ym.endsWith("-")) {
203                                                         ai.add(getNum(ym));
204                                                         break;
205                                                 } else {
206                                                         range=new String[] {ym.substring(0, 6),FMT.format(new Date())};
207                                                 }
208                                         default:
209                                                 GregorianCalendar gc = new GregorianCalendar();
210                                                 gc.set(GregorianCalendar.MONTH, Integer.parseInt(range[1].substring(4,6))-1);
211                                                 gc.set(GregorianCalendar.YEAR, Integer.parseInt(range[1].substring(0,4)));
212                                                 int end = getNum(FMT.format(gc.getTime())); 
213                                                 
214                                                 gc.set(GregorianCalendar.MONTH, Integer.parseInt(range[0].substring(4,6))-1);
215                                                 gc.set(GregorianCalendar.YEAR, Integer.parseInt(range[0].substring(0,4)));
216                                                 for(int i=getNum(FMT.format(gc.getTime()));i<=end;gc.add(GregorianCalendar.MONTH, 1),i=getNum(FMT.format(gc.getTime()))) {
217                                                         ai.add(i);
218                                                 }
219
220                                 }
221                         }
222                 }
223                 if(ai.size()==0) {
224                         throw new NumberFormatException(yyyymm + " is an invalid number or range");
225                 }
226                 Collections.sort(ai);
227                 int ym[] = new int[ai.size()];
228                 for(int i=0;i<ym.length;++i) {
229                         ym[i]=ai.get(i);
230                 }
231                 return ym;
232         }
233         
234         private static int getNum(String n) {
235                 if(n==null || n.length()!=6) throw new NumberFormatException(n + " is not in YYYYMM format");
236                 return Integer.parseInt(n);
237         }
238 }