Initial OpenECOMP policy/engine commit
[policy/engine.git] / ecomp-sdk-app / src / main / java / org / openecomp / policy / controller / PDPController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2017 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.openecomp.policy.controller;
22
23
24 import java.io.PrintWriter;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.List;
28
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
31
32 import org.json.JSONObject;
33 import org.openecomp.policy.dao.GroupEntityDao;
34 import org.openecomp.policy.dao.PDPEntityDao;
35 import org.openecomp.policy.model.PDPGroupContainer;
36 import org.openecomp.portalsdk.core.controller.RestrictedBaseController;
37 import org.openecomp.portalsdk.core.web.support.JsonMessage;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.http.MediaType;
40 import org.springframework.stereotype.Controller;
41 import org.springframework.web.bind.annotation.RequestMapping;
42 import org.springframework.web.servlet.ModelAndView;
43
44 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
45 import org.openecomp.policy.common.logging.flexlogger.Logger;
46
47 import org.openecomp.policy.xacml.api.XACMLErrorConstants;
48 import org.openecomp.policy.xacml.api.pap.EcompPDPGroup;
49
50 import com.att.research.xacml.api.pap.PAPException;
51 import com.att.research.xacml.api.pap.PDPGroup;
52 import org.openecomp.policy.xacml.std.pap.StdPDP;
53 import org.openecomp.policy.xacml.std.pap.StdPDPGroup;
54 import com.fasterxml.jackson.databind.DeserializationFeature;
55 import com.fasterxml.jackson.databind.JsonNode;
56 import com.fasterxml.jackson.databind.ObjectMapper;
57
58 @Controller
59 @RequestMapping({"/"})
60 public class PDPController extends RestrictedBaseController {
61         private static final  Logger logger = FlexLogger.getLogger(PDPController.class);
62         
63         @Autowired
64         GroupEntityDao groupDAO;
65         
66         @Autowired
67         PDPEntityDao pdpDAO;
68         
69         protected List<EcompPDPGroup> groups = Collections.synchronizedList(new ArrayList<EcompPDPGroup>());
70         private PDPGroupContainer container;
71         
72         public synchronized void refreshGroups() {
73                 synchronized(this.groups) { 
74                         this.groups.clear();
75                         try {
76                                 this.groups.addAll(PolicyController.getPapEngine().getEcompPDPGroups());
77                         } catch (PAPException e) {
78                                 String message = "Unable to retrieve Groups from server: " + e;
79                                 logger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR+"Pap Engine is Null" + message);
80                         }
81                 
82                 }
83         }
84         
85         @RequestMapping(value={"/get_PDPGroupContainerData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
86         public void getPDPGroupContainerData(HttpServletRequest request, HttpServletResponse response){
87                 try{
88                         ObjectMapper mapper = new ObjectMapper();
89                         refreshGroups();
90                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(groups));
91                         JSONObject j = new JSONObject(msg);
92                         response.getWriter().write(j.toString());
93                 }
94                 catch (Exception e){
95                         logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Error Occured while retrieving the PDP Group Container data" + e);
96                 }
97         }
98         
99         @RequestMapping(value={"/get_PDPGroupData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
100         public void getPDPGroupEntityData(HttpServletRequest request, HttpServletResponse response){
101                 try{
102                         ObjectMapper mapper = new ObjectMapper();
103                         refreshGroups();
104                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(groups));
105                         JSONObject j = new JSONObject(msg);
106                         response.getWriter().write(j.toString());
107                 }
108                 catch (Exception e){
109                         logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Error Occured while retrieving the PDP Group data" + e);
110                 }
111         }
112         
113         @RequestMapping(value={"/pdp_Group/save_pdp_group"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
114           public ModelAndView savePDPGroup(HttpServletRequest request, HttpServletResponse response) throws Exception{
115             try {
116               ObjectMapper mapper = new ObjectMapper();
117               mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
118               JsonNode root = mapper.readTree(request.getReader());
119               this.container = new PDPGroupContainer(PolicyController.getPapEngine());
120               StdPDPGroup pdpGroupData =  mapper.readValue(root.get("pdpGroupData").toString().replace("groupName", "name"), StdPDPGroup.class);
121               try {
122                   if(pdpGroupData.getId() == null){
123                           this.container.addNewGroup(pdpGroupData.getName(), pdpGroupData.getDescription());
124                   }else{
125                           this.container.updateGroup(pdpGroupData);
126                   }
127                                 
128                         } catch (Exception e) {
129                                 String message = "Unable to create Group.  Reason:\n" + e.getMessage();
130                                 logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Error Occured while creating the PDP Group" + message);
131                         }
132                 
133             
134               response.setCharacterEncoding("UTF-8");
135               response.setContentType("application / json");
136               request.setCharacterEncoding("UTF-8");
137               
138               PrintWriter out = response.getWriter();
139               refreshGroups();
140               JsonMessage msg = new JsonMessage(mapper.writeValueAsString(groups));
141                   JSONObject j = new JSONObject(msg);
142               out.write(j.toString());
143               
144               return null;
145             }
146             catch (Exception e){
147              logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Error Occured while Saving the PDP Group" + e);
148               response.setCharacterEncoding("UTF-8");
149               request.setCharacterEncoding("UTF-8");
150               PrintWriter out = response.getWriter();
151               out.write(e.getMessage());
152             }
153             return null;
154           }
155           
156           @RequestMapping(value={"/pdp_Group/remove_pdp_group"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
157           public ModelAndView removePDPGroup(HttpServletRequest request, HttpServletResponse response) throws Exception {
158             try{
159               ObjectMapper mapper = new ObjectMapper();
160               mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
161               JsonNode root = mapper.readTree(request.getReader());
162               this.container = new PDPGroupContainer(PolicyController.getPapEngine()); 
163               StdPDPGroup pdpGroupData =  mapper.readValue(root.get("pdpGroupData").toString(), StdPDPGroup.class);
164                 if(pdpGroupData.getName().equals("Default")) {
165                                 throw new UnsupportedOperationException("You can't remove the Default Group.");
166                         }else{
167                                 this.container.removeGroup(pdpGroupData, null);
168                         }
169           
170               response.setCharacterEncoding("UTF-8");
171               response.setContentType("application / json");
172               request.setCharacterEncoding("UTF-8");
173               
174               PrintWriter out = response.getWriter();
175               
176               refreshGroups();
177               JsonMessage msg = new JsonMessage(mapper.writeValueAsString(groups));
178                   JSONObject j = new JSONObject(msg);
179               out.write(j.toString());
180               
181               return null;
182             }
183             catch (Exception e){
184               logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Error Occured while Removing the PDP Group" + e);
185               response.setCharacterEncoding("UTF-8");
186               request.setCharacterEncoding("UTF-8");
187               PrintWriter out = response.getWriter();
188               out.write(e.getMessage());
189             }
190             return null;
191           }
192           
193           @RequestMapping(value={"/pdp_Group/save_pdpTogroup"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
194           public ModelAndView savePDPToGroup(HttpServletRequest request, HttpServletResponse response) throws Exception{
195             try {
196               ObjectMapper mapper = new ObjectMapper();
197               mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
198               JsonNode root = mapper.readTree(request.getReader());
199               this.container = new PDPGroupContainer(PolicyController.getPapEngine()); 
200               String update = root.get("update").toString();
201               PdpData pdpGroupData = (PdpData)mapper.readValue(root.get("pdpInGroup").toString(), PdpData.class);
202               StdPDPGroup activeGroupData =  mapper.readValue(root.get("activePDP").toString(), StdPDPGroup.class);
203               try {
204                   
205                   if(update.contains("false")){
206                           this.container.addNewPDP(pdpGroupData.getId(), activeGroupData, pdpGroupData.getName(), pdpGroupData.getDescription(), pdpGroupData.getJmxPort());
207                   }else{
208                           this.container.updateGroup(activeGroupData);
209                   }
210                         } catch (Exception e) {
211                                 String message = "Unable to create Group.  Reason:\n" + e.getMessage();
212                                  logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Error Occured while Creating Pdp in PDP Group" + message);
213                         }
214                 
215             
216               response.setCharacterEncoding("UTF-8");
217               response.setContentType("application / json");
218               request.setCharacterEncoding("UTF-8");
219               
220               PrintWriter out = response.getWriter();
221               refreshGroups();
222               JsonMessage msg = new JsonMessage(mapper.writeValueAsString(groups));
223                   JSONObject j = new JSONObject(msg);
224               out.write(j.toString());
225               
226               return null;
227             }
228             catch (Exception e){
229               logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Error Occured while Creating Pdp in PDP Group" + e);
230               response.setCharacterEncoding("UTF-8");
231               request.setCharacterEncoding("UTF-8");
232               PrintWriter out = response.getWriter();
233               out.write(e.getMessage());
234             }
235             return null;
236           }
237           
238           @RequestMapping(value={"/pdp_Group/remove_pdpFromGroup"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
239           public ModelAndView removePDPFromGroup(HttpServletRequest request, HttpServletResponse response) throws Exception {
240             try{
241               ObjectMapper mapper = new ObjectMapper();
242               mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
243               JsonNode root = mapper.readTree(request.getReader());
244               this.container = new PDPGroupContainer(PolicyController.getPapEngine()); 
245               StdPDP deletePdp =  mapper.readValue(root.get("data").toString(), StdPDP.class);
246               StdPDPGroup activeGroupData =  mapper.readValue(root.get("activePDP").toString(), StdPDPGroup.class);
247                 
248               this.container.removePDP(deletePdp, activeGroupData);
249               response.setCharacterEncoding("UTF-8");
250               response.setContentType("application / json");
251               request.setCharacterEncoding("UTF-8");
252               
253               PrintWriter out = response.getWriter();
254               refreshGroups();
255               String responseString = mapper.writeValueAsString(groups);
256               JSONObject j = new JSONObject("{pdpEntityDatas: " + responseString + "}");
257               out.write(j.toString());
258               
259               return null;
260             }
261             catch (Exception e){
262               logger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Error Occured while Removing Pdp from PDP Group" + e);
263               response.setCharacterEncoding("UTF-8");
264               request.setCharacterEncoding("UTF-8");
265               PrintWriter out = response.getWriter();
266               out.write(e.getMessage());
267             }
268             return null;
269           }
270 }
271
272 class PdpData{
273         String id;
274         int jmxPort;
275         String name;
276         String description;
277         public String getId() {
278                 return id;
279         }
280         public void setId(String id) {
281                 this.id = id;
282         }
283         public int getJmxPort() {
284                 return jmxPort;
285         }
286         public void setJmxPort(int jmxPort) {
287                 this.jmxPort = jmxPort;
288         }
289         public String getName() {
290                 return name;
291         }
292         public void setName(String name) {
293                 this.name = name;
294         }
295         public String getDescription() {
296                 return description;
297         }
298         public void setDescription(String description) {
299                 this.description = description;
300         }
301         
302 }