Policy TestSuite Enabled
[policy/engine.git] / ECOMP-PAP-REST / src / main / java / org / openecomp / policy / pap / xacml / rest / controller / EnforcerDictionaryController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP-PAP-REST
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.pap.xacml.rest.controller;
22
23 import java.io.PrintWriter;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletResponse;
31
32 import org.json.JSONObject;
33 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
34 import org.openecomp.policy.common.logging.flexlogger.Logger;
35 import org.openecomp.policy.pap.xacml.rest.util.JsonMessage;
36 import org.openecomp.policy.rest.dao.CommonClassDao;
37 import org.openecomp.policy.rest.jpa.EnforcingType;
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 com.fasterxml.jackson.databind.DeserializationFeature;
45 import com.fasterxml.jackson.databind.JsonNode;
46 import com.fasterxml.jackson.databind.ObjectMapper;
47
48 @Controller
49 public class EnforcerDictionaryController {
50
51         private static final Logger LOGGER      = FlexLogger.getLogger(EnforcerDictionaryController.class);
52         
53         @Autowired
54         CommonClassDao commonClassDao;
55
56         @RequestMapping(value={"/get_EnforcerTypeData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
57         public void getEnforcerDictionaryEntityData(HttpServletRequest request, HttpServletResponse response){
58                 try{
59                         Map<String, Object> model = new HashMap<String, Object>();
60                         ObjectMapper mapper = new ObjectMapper();
61                         List<Object> list = commonClassDao.getData(EnforcingType.class);
62                         List<String> dictList = new ArrayList<String>();
63                         for(int i = 0; i < list.size(); i++){
64                                 EnforcingType dict = (EnforcingType) list.get(i);
65                                 dictList.add(dict.getEnforcingType());
66                         }
67                         model.put("enforcerDictionaryDatas", mapper.writeValueAsString(dictList));
68                         org.openecomp.policy.pap.xacml.rest.util.JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
69                         JSONObject j = new JSONObject(msg);
70                         response.getWriter().write(j.toString());
71                 }
72                 catch (Exception e){
73                         LOGGER.error("Exception Occured"+e);
74                 }
75         }
76         
77         @RequestMapping(value={"/enforcer_dictionary/save_enforcerType"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
78         public ModelAndView saveEnforcerDictionary(HttpServletRequest request, HttpServletResponse response) throws Exception{
79                 try {
80                         ObjectMapper mapper = new ObjectMapper();
81                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
82                         JsonNode root = mapper.readTree(request.getReader());
83                         EnforcingType enforcingType = (EnforcingType)mapper.readValue(root.get("enforcerDictionaryData").toString(), EnforcingType.class);
84                         if(enforcingType.getId() == 0){
85                                 commonClassDao.save(enforcingType);
86                         }else{
87                                 commonClassDao.update(enforcingType); 
88                         } 
89                         response.setCharacterEncoding("UTF-8");
90                         response.setContentType("application / json");
91                         request.setCharacterEncoding("UTF-8");
92
93                         PrintWriter out = response.getWriter();
94                         String responseString = mapper.writeValueAsString(commonClassDao.getData(EnforcingType.class));
95                         JSONObject j = new JSONObject("{enforcerDictionaryDatas: " + responseString + "}");
96
97                         out.write(j.toString());
98
99                         return null;
100                 }
101                 catch (Exception e){
102                         response.setCharacterEncoding("UTF-8");
103                         request.setCharacterEncoding("UTF-8");
104                         PrintWriter out = response.getWriter();
105                         out.write(e.getMessage());
106                 }
107                 return null;
108         }
109
110         @RequestMapping(value={"/enforcer_dictionary/remove_enforcer"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
111         public ModelAndView removeEnforcerDictionary(HttpServletRequest request, HttpServletResponse response) throws Exception {
112                 try{
113                         ObjectMapper mapper = new ObjectMapper();
114                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
115                         JsonNode root = mapper.readTree(request.getReader());
116                         EnforcingType enforcingType = (EnforcingType)mapper.readValue(root.get("data").toString(), EnforcingType.class);
117                         commonClassDao.delete(enforcingType);
118                         response.setCharacterEncoding("UTF-8");
119                         response.setContentType("application / json");
120                         request.setCharacterEncoding("UTF-8");
121
122                         PrintWriter out = response.getWriter();
123
124                         String responseString = mapper.writeValueAsString(commonClassDao.getData(EnforcingType.class));
125                         JSONObject j = new JSONObject("{enforcerDictionaryDatas: " + responseString + "}");
126                         out.write(j.toString());
127
128                         return null;
129                 }
130                 catch (Exception e){
131                         System.out.println(e);
132                         response.setCharacterEncoding("UTF-8");
133                         request.setCharacterEncoding("UTF-8");
134                         PrintWriter out = response.getWriter();
135                         out.write(e.getMessage());
136                 }
137                 return null;
138         }
139 }