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