Initial OpenECOMP policy/engine commit
[policy/engine.git] / ecomp-sdk-app / src / main / java / org / openecomp / policy / controller / PolicyRolesController.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.File;
25 import java.io.PrintWriter;
26 import java.nio.file.Path;
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
34
35 import org.json.JSONObject;
36 import org.openecomp.policy.dao.PolicyRolesDao;
37 import org.openecomp.policy.rest.jpa.PolicyRoles;
38 import org.openecomp.portalsdk.core.controller.RestrictedBaseController;
39 import org.openecomp.portalsdk.core.web.support.JsonMessage;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.http.MediaType;
42 import org.springframework.stereotype.Controller;
43 import org.springframework.web.bind.annotation.RequestMapping;
44 import org.springframework.web.servlet.ModelAndView;
45
46 import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
47 import com.fasterxml.jackson.annotation.PropertyAccessor;
48 import com.fasterxml.jackson.databind.DeserializationFeature;
49 import com.fasterxml.jackson.databind.JsonNode;
50 import com.fasterxml.jackson.databind.ObjectMapper;
51
52 @Controller
53 @RequestMapping("/")
54 public class PolicyRolesController extends RestrictedBaseController{
55         
56         @Autowired
57         PolicyRolesDao policyRolesDao;
58         List<String> scopelist;
59         
60         @RequestMapping(value={"/get_RolesData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
61         public void getPolicyRolesEntityData(HttpServletRequest request, HttpServletResponse response){
62                 try{
63                         Map<String, Object> model = new HashMap<String, Object>();
64                         ObjectMapper mapper = new ObjectMapper();
65                         model.put("rolesDatas", mapper.writeValueAsString(policyRolesDao.getUserRoles()));
66                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
67                         JSONObject j = new JSONObject(msg);
68                         response.getWriter().write(j.toString());
69                 }
70                 catch (Exception e){
71                         e.printStackTrace();
72                 }
73         }
74         
75         @RequestMapping(value={"/save_NonSuperRolesData"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
76         public ModelAndView SaveRolesEntityData(HttpServletRequest request, HttpServletResponse response){
77                 try{
78                         ObjectMapper mapper = new ObjectMapper();
79                         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
80                     JsonNode root = mapper.readTree(request.getReader());
81                     PolicyRoles adapter = mapper.readValue(root.get("editRoleData").toString(), PolicyRoles.class);
82                     policyRolesDao.update(adapter);
83                     response.setCharacterEncoding("UTF-8");
84                         response.setContentType("application / json");
85                         request.setCharacterEncoding("UTF-8");
86                 
87                         PrintWriter out = response.getWriter();
88                         String responseString = mapper.writeValueAsString(policyRolesDao.getUserRoles());
89                         JSONObject j = new JSONObject("{rolesDatas: " + responseString + "}");
90
91                         out.write(j.toString());
92                 }
93                 catch (Exception e){
94                         e.printStackTrace();
95                 }
96                 return null;
97         }
98         
99         @RequestMapping(value={"/get_PolicyRolesScopeData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
100         public void getPolicyScopesEntityData(HttpServletRequest request, HttpServletResponse response){
101                 try{
102                         scopelist = new ArrayList<String>();
103                         Map<String, Object> model = new HashMap<String, Object>();
104                         ObjectMapper mapper = new ObjectMapper();
105                         mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
106                         Path gitPath = PolicyController.getGitPath().toAbsolutePath();
107                         model.put("scopeDatas", mapper.writeValueAsString(readFileRepository(gitPath)));
108                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
109                         JSONObject j = new JSONObject(msg);
110                         response.getWriter().write(j.toString());
111                 }
112                 catch (Exception e){
113                         e.printStackTrace();
114                 }
115         }
116         
117         public List<String> readFileRepository(Path path){
118                 File root = new File(path.toString());
119                 for ( File file : root.listFiles()){
120                         if(file.isDirectory()){
121                                 String fileName = getDomain(file);
122                                 if (!(fileName.contains(".git") || fileName.equals(".DS_Store"))) {
123                                         scopelist.add(fileName);
124                                 }
125                                 readFileRepository(file.toPath());
126                         }
127                 }
128                 return scopelist;
129         }
130         
131         public String getDomain(File file) {
132                 String filePath = file.getAbsolutePath();
133                 int startIndex = filePath.indexOf("repository")  + 11;
134                 return filePath.substring(startIndex, filePath.length());
135         }
136
137 }