2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
22 package org.onap.aaf.auth.service.api;
24 import static org.onap.aaf.auth.layer.Result.OK;
25 import static org.onap.aaf.auth.rserv.HttpMethods.GET;
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;
33 import javax.servlet.http.HttpServletRequest;
34 import javax.servlet.http.HttpServletResponse;
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;
46 * Pull certain types of History Info
50 * commas 201503,201504
51 * ranges 201501-201504
52 * combinations 201301,201401,201501-201504
57 public class API_History {
59 * Normal Init level APIs
65 public static void init(AAF_Service authzAPI, AuthzFacade facade) throws Exception {
69 authzAPI.route(GET,"/authz/hist/user/:user",API.HISTORY,new Code(facade,"Get History by User", true) {
71 public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
75 years = getYears(req);
76 descend = decending(req);
77 } catch(Exception e) {
78 context.error(trans, resp, Result.err(Status.ERR_BadData, e.getMessage()));
82 Result<Void> r = context.getHistoryByUser(trans, resp, pathParam(req,":user"),years,descend);
85 resp.setStatus(HttpStatus.OK_200);
88 context.error(trans,resp,r);
96 authzAPI.route(GET,"/authz/hist/ns/:ns",API.HISTORY,new Code(facade,"Get History by Namespace", true) {
98 public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
102 years = getYears(req);
103 descend = decending(req);
104 } catch(Exception e) {
105 context.error(trans, resp, Result.err(Status.ERR_BadData, e.getMessage()));
109 Result<Void> r = context.getHistoryByNS(trans, resp, pathParam(req,":ns"),years,descend);
112 resp.setStatus(HttpStatus.OK_200);
115 context.error(trans,resp,r);
121 * Get History by Role
123 authzAPI.route(GET,"/authz/hist/role/:role",API.HISTORY,new Code(facade,"Get History by Role", true) {
125 public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
129 years = getYears(req);
130 descend = decending(req);
131 } catch(Exception e) {
132 context.error(trans, resp, Result.err(Status.ERR_BadData, e.getMessage()));
136 Result<Void> r = context.getHistoryByRole(trans, resp, pathParam(req,":role"),years,descend);
139 resp.setStatus(HttpStatus.OK_200);
142 context.error(trans,resp,r);
148 * Get History by Perm Type
150 authzAPI.route(GET,"/authz/hist/perm/:type",API.HISTORY,new Code(facade,"Get History by Perm Type", true) {
152 public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
156 years = getYears(req);
157 descend = decending(req);
158 } catch(Exception e) {
159 context.error(trans, resp, Result.err(Status.ERR_BadData, e.getMessage()));
163 Result<Void> r = context.getHistoryByPerm(trans, resp, pathParam(req,":type"),years,descend);
166 resp.setStatus(HttpStatus.OK_200);
169 context.error(trans,resp,r);
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;
182 // Get Common "yyyymm" parameter, or none
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<Integer>();
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);
197 for(String ym : yyyymm.split(",")) {
198 String range[] = ym.split("\\s*-\\s*");
199 switch(range.length) {
203 if(!ym.endsWith("-")) {
207 range=new String[] {ym.substring(0, 6),FMT.format(new Date())};
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()));
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()))) {
225 throw new NumberFormatException(yyyymm + " is an invalid number or range");
227 Collections.sort(ai);
228 int ym[] = new int[ai.size()];
229 for(int i=0;i<ym.length;++i) {
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);