Consolidate PolicyRestAdapter setup
[policy/engine.git] / ONAP-PAP-REST / src / main / java / org / onap / policy / pap / xacml / rest / handler / APIRequestHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PAP-REST
4  * ================================================================================
5  * Copyright (C) 2017-2019 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 package org.onap.policy.pap.xacml.rest.handler;
22
23 import java.io.IOException;
24
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse;
27
28 import org.onap.policy.common.logging.OnapLoggingContext;
29 import org.onap.policy.pap.xacml.rest.service.ImportService;
30 import org.onap.policy.pap.xacml.rest.service.MetricService;
31 import org.onap.policy.xacml.api.pap.OnapPDPGroup;
32
33 public class APIRequestHandler {
34
35     private OnapPDPGroup newGroup;
36
37     /**
38      * Do get.
39      *
40      * @param request the request
41      * @param response the response
42      * @param apiflag the apiflag
43      * @throws IOException Signals that an I/O exception has occurred.
44      */
45     public void doGet(HttpServletRequest request, HttpServletResponse response, String apiflag) throws IOException {
46         // Request from the API to get Dictionary Items
47         if ("api".equalsIgnoreCase(apiflag)) {
48             DictionaryHandler dictionaryHandler = DictionaryHandler.getInstance();
49             dictionaryHandler.doDictionaryAPIGet(request, response);
50             return;
51         }
52         // Request from the API to get the ActiveVersion from the PolicyVersion table
53         if ("version".equalsIgnoreCase(apiflag)) {
54             PushPolicyHandler pushHandler = new PushPolicyHandler();
55             pushHandler.getActiveVersion(request, response);
56             return;
57         }
58         // Request from the API to get the URI from the gitpath
59         if ("uri".equalsIgnoreCase(apiflag)) {
60             PushPolicyHandler pushHandler = new PushPolicyHandler();
61             pushHandler.getSelectedURI(request, response);
62             return;
63         }
64         if ("getMetrics".equalsIgnoreCase(apiflag)) {
65             MetricService.doGetPolicyMetrics(response);
66         }
67     }
68
69     /**
70      * Do put.
71      *
72      * @param request the request
73      * @param response the response
74      * @param service the service
75      * @throws IOException Signals that an I/O exception has occurred.
76      */
77     public void doPut(HttpServletRequest request, HttpServletResponse response, String service) throws IOException {
78         if ("MICROSERVICE".equalsIgnoreCase(service) || "BRMSPARAM".equalsIgnoreCase(service)
79                 || "OPTIMIZATION".equalsIgnoreCase(service)) {
80             ImportService importService = new ImportService();
81             importService.doImportMicroServicePut(request, response);
82             return;
83         }
84         if ("dictionaryItem".equalsIgnoreCase(service)) {
85             DictionaryHandler dictionaryHandler = DictionaryHandler.getInstance();
86             dictionaryHandler.doDictionaryAPIPut(request, response);
87         } else {
88             SavePolicyHandler savePolicy = SavePolicyHandler.getInstance();
89             savePolicy.doPolicyAPIPut(request, response);
90         }
91     }
92
93     /**
94      * Do delete.
95      *
96      * @param request the request
97      * @param response the response
98      * @param loggingContext the logging context
99      * @param apiflag the apiflag
100      * @throws IOException Signals that an I/O exception has occurred.
101      */
102     public void doDelete(HttpServletRequest request, HttpServletResponse response, OnapLoggingContext loggingContext,
103             String apiflag) throws IOException {
104         DeleteHandler deleteHandler = DeleteHandler.getInstance();
105         if ("deletePapApi".equalsIgnoreCase(apiflag)) {
106             deleteHandler.doApiDeleteFromPap(request, response);
107         } else if ("deletePdpApi".equalsIgnoreCase(apiflag)) {
108             deleteHandler.doApiDeleteFromPdp(request, response, loggingContext);
109             setNewGroup(deleteHandler.getDeletedGroup());
110         }
111     }
112
113     private void setNewGroup(OnapPDPGroup newGroup) {
114         this.newGroup = newGroup;
115     }
116
117     public OnapPDPGroup getNewGroup() {
118         return newGroup;
119     }
120 }