2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.controller;
25 import java.io.FileInputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.List;
32 import java.util.Properties;
34 import javax.annotation.PostConstruct;
35 import javax.mail.MessagingException;
36 import javax.script.SimpleBindings;
37 import javax.servlet.http.HttpServletRequest;
38 import javax.servlet.http.HttpServletResponse;
40 import org.json.JSONObject;
41 import org.onap.policy.admin.PolicyNotificationMail;
42 import org.onap.policy.admin.RESTfulPAPEngine;
43 import org.onap.policy.common.logging.flexlogger.FlexLogger;
44 import org.onap.policy.common.logging.flexlogger.Logger;
45 import org.onap.policy.model.PDPGroupContainer;
46 import org.onap.policy.rest.XACMLRestProperties;
47 import org.onap.policy.rest.XacmlAdminAuthorization;
48 import org.onap.policy.rest.dao.CommonClassDao;
49 import org.onap.policy.rest.jpa.Datatype;
50 import org.onap.policy.rest.jpa.FunctionDefinition;
51 import org.onap.policy.rest.jpa.PolicyEntity;
52 import org.onap.policy.rest.jpa.PolicyVersion;
53 import org.onap.policy.rest.jpa.UserInfo;
54 import org.onap.policy.xacml.api.XACMLErrorConstants;
55 import org.onap.policy.xacml.api.pap.PAPPolicyEngine;
56 import org.openecomp.policy.model.Roles;
57 import org.openecomp.portalsdk.core.controller.RestrictedBaseController;
58 import org.openecomp.portalsdk.core.web.support.JsonMessage;
59 import org.openecomp.portalsdk.core.web.support.UserUtils;
60 import org.springframework.beans.factory.annotation.Autowired;
61 import org.springframework.http.MediaType;
62 import org.springframework.stereotype.Controller;
63 import org.springframework.web.bind.annotation.RequestMapping;
64 import org.springframework.web.bind.annotation.RequestMethod;
65 import org.springframework.web.servlet.ModelAndView;
67 import com.att.research.xacml.util.XACMLProperties;
68 import com.fasterxml.jackson.databind.ObjectMapper;
73 public class PolicyController extends RestrictedBaseController {
74 private static final Logger policyLogger = FlexLogger.getLogger(PolicyController.class);
76 private static CommonClassDao commonClassDao;
78 // Our authorization object
80 XacmlAdminAuthorization authorizer = new XacmlAdminAuthorization();
84 private static PAPPolicyEngine papEngine;
86 private static String logTableLimit;
87 private static String systemAlertTableLimit;
88 protected static Map<String, String> dropDownMap = new HashMap<>();
89 public static Map<String, String> getDropDownMap() {
93 public static void setDropDownMap(Map<String, String> dropDownMap) {
94 PolicyController.dropDownMap = dropDownMap;
97 public static String getDomain() {
98 return XACMLProperties.getProperty(XACMLRestProperties.PROP_ADMIN_DOMAIN, "urn");
101 private static final Object mapAccess = new Object();
102 private static Map<Datatype, List<FunctionDefinition>> mapDatatype2Function = null;
103 private static Map<String, FunctionDefinition> mapID2Function = null;
105 //Constant variables used across Policy-sdk
106 private static final String policyData = "policyData";
107 private static final String characterEncoding = "UTF-8";
108 private static final String contentType = "application/json";
109 private static final String file = "file";
111 //Smtp Java Mail Properties
112 private static String smtpHost = null;
113 private static String smtpPort = null;
114 private static String smtpUsername = null;
115 private static String smtpPassword = null;
116 private static String smtpApplicationName = null;
117 private static String smtpEmailExtension = null;
119 private static String logdbDriver = null;
120 private static String logdbUrl = null;
121 private static String logdbUserName = null;
122 private static String logdbPassword = null;
123 private static String logdbDialect = null;
124 //Xacml db properties
125 private static String xacmldbUrl = null;
126 private static String xacmldbUserName = null;
127 private static String xacmldbPassword = null;
130 private static String autoPushAvailable;
131 private static String autoPushDSClosedLoop;
132 private static String autoPushDSFirewall;
133 private static String autoPushDSMicroservice;
134 private static String autoPushPDPGroup;
137 private static String papUrl;
139 //MicroService Model Properties
140 private static String msOnapName;
141 private static String msPolicyName;
144 private static String configHome;
145 private static String actionHome;
148 private PolicyController(CommonClassDao commonClassDao){
149 PolicyController.commonClassDao = commonClassDao;
152 public PolicyController() {
157 Properties prop = new Properties();
158 InputStream input = null;
160 input = new FileInputStream("xacml.admin.properties");
161 // load a properties file
164 setPapUrl(prop.getProperty("xacml.rest.pap.url"));
165 // get the property values
166 setSmtpHost(prop.getProperty("onap.smtp.host"));
167 setSmtpPort(prop.getProperty("onap.smtp.port"));
168 setSmtpUsername(prop.getProperty("onap.smtp.userName"));
169 setSmtpPassword(prop.getProperty("onap.smtp.password"));
170 setSmtpApplicationName(prop.getProperty("onap.application.name"));
171 setSmtpEmailExtension(prop.getProperty("onap.smtp.emailExtension"));
172 //Log Database Properties
173 setLogdbDriver(prop.getProperty("xacml.log.db.driver"));
174 setLogdbUrl(prop.getProperty("xacml.log.db.url"));
175 setLogdbUserName(prop.getProperty("xacml.log.db.user"));
176 setLogdbPassword(prop.getProperty("xacml.log.db.password"));
177 setLogdbDialect(prop.getProperty("onap.dialect"));
178 //Xacml Database Properties
179 setXacmldbUrl(prop.getProperty("javax.persistence.jdbc.url"));
180 setXacmldbUserName(prop.getProperty("javax.persistence.jdbc.user"));
181 setXacmldbPassword(prop.getProperty("javax.persistence.jdbc.password"));
183 setAutoPushAvailable(prop.getProperty("xacml.automatic.push"));
184 setAutoPushDSClosedLoop(prop.getProperty("xacml.autopush.closedloop"));
185 setAutoPushDSFirewall(prop.getProperty("xacml.autopush.firewall"));
186 setAutoPushDSMicroservice(prop.getProperty("xacml.autopush.microservice"));
187 setAutoPushPDPGroup(prop.getProperty("xacml.autopush.pdpGroup"));
188 //Micro Service Properties
189 setMsOnapName(prop.getProperty("xacml.policy.msOnapName"));
190 setMsPolicyName(prop.getProperty("xacml.policy.msPolicyName"));
192 setConfigHome(prop.getProperty("xacml.rest.config.webapps") + "Config");
193 setActionHome(prop.getProperty("xacml.rest.config.webapps") + "Action");
194 //Get the Property Values for Dashboard tab Limit
196 setLogTableLimit(prop.getProperty("xacml.onap.dashboard.logTableLimit"));
197 setSystemAlertTableLimit(prop.getProperty("xacml.onap.dashboard.systemAlertTableLimit"));
199 policyLogger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Dashboard tab Property fields are missing" +e);
200 setLogTableLimit("5000");
201 setSystemAlertTableLimit("2000");
203 System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME, "xacml.admin.properties");
204 } catch (IOException ex) {
205 policyLogger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Exception Occured while reading the Smtp properties from xacml.admin.properties file" +ex);
210 } catch (IOException e) {
211 policyLogger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Exception Occured while Closing the xacml.admin.properties file" +e);
216 //Initialize the FunctionDefinition table at Server Start up
217 Map<Datatype, List<FunctionDefinition>> functionMap = getFunctionDatatypeMap();
218 for (Datatype id : functionMap.keySet()) {
219 List<FunctionDefinition> functionDefinations = functionMap.get(id);
220 for (FunctionDefinition functionDef : functionDefinations) {
221 dropDownMap.put(functionDef.getShortname(),functionDef.getXacmlid());
227 public static Map<Datatype, List<FunctionDefinition>> getFunctionDatatypeMap() {
228 synchronized(mapAccess) {
229 if (mapDatatype2Function == null) {
233 return mapDatatype2Function;
236 public static Map<String, FunctionDefinition> getFunctionIDMap() {
237 synchronized(mapAccess) {
238 if (mapID2Function == null) {
242 return mapID2Function;
245 private static void buildFunctionMaps() {
246 mapDatatype2Function = new HashMap<>();
247 mapID2Function = new HashMap<>();
248 List<Object> functiondefinitions = commonClassDao.getData(FunctionDefinition.class);
249 for (int i = 0; i < functiondefinitions.size(); i ++) {
250 FunctionDefinition value = (FunctionDefinition) functiondefinitions.get(i);
251 mapID2Function.put(value.getXacmlid(), value);
252 if (!mapDatatype2Function.containsKey(value.getDatatypeBean())) {
253 mapDatatype2Function.put(value.getDatatypeBean(), new ArrayList<FunctionDefinition>());
255 mapDatatype2Function.get(value.getDatatypeBean()).add(value);
259 @RequestMapping(value={"/get_FunctionDefinitionDataByName"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
260 public void getFunctionDefinitionData(HttpServletRequest request, HttpServletResponse response){
262 Map<String, Object> model = new HashMap<>();
263 ObjectMapper mapper = new ObjectMapper();
264 model.put("functionDefinitionDatas", mapper.writeValueAsString(commonClassDao.getDataByColumn(FunctionDefinition.class, "shortname")));
265 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
266 JSONObject j = new JSONObject(msg);
267 response.getWriter().write(j.toString());
270 policyLogger.error(XACMLErrorConstants.ERROR_DATA_ISSUE +"Error while retriving the Function Definition data"+e);
274 public PolicyEntity getPolicyEntityData(String scope, String policyName){
275 String key = scope + ":" + policyName;
276 List<Object> data = commonClassDao.getDataById(PolicyEntity.class, "scope:policyName", key);
277 return (PolicyEntity) data.get(0);
280 public static Map<String, Roles> getUserRoles(String userId) {
281 Map<String, Roles> scopes = new HashMap<>();
282 List<Object> roles = commonClassDao.getDataById(Roles.class, "loginId", userId);
283 if (roles != null && !roles.isEmpty()) {
284 for (Object role : roles) {
285 scopes.put(((Roles) role).getScope(), (Roles) role);
291 public List<String> getRolesOfUser(String userId) {
292 List<String> rolesList = new ArrayList<>();
293 List<Object> roles = commonClassDao.getDataById(Roles.class, "loginId", userId);
294 for (Object role: roles) {
295 rolesList.add(((Roles) role).getRole());
300 public List<Object> getRoles(String userId) {
301 return commonClassDao.getDataById(Roles.class, "loginId", userId);
304 //Get List of User Roles
305 @RequestMapping(value={"/get_UserRolesData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
306 public void getUserRolesEntityData(HttpServletRequest request, HttpServletResponse response){
308 String userId = UserUtils.getUserSession(request).getOrgUserId();
309 Map<String, Object> model = new HashMap<>();
310 ObjectMapper mapper = new ObjectMapper();
311 model.put("userRolesDatas", mapper.writeValueAsString(getRolesOfUser(userId)));
312 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
313 JSONObject j = new JSONObject(msg);
314 response.getWriter().write(j.toString());
317 policyLogger.error("Exception Occured"+e);
321 //Policy tabs Model and View
322 @RequestMapping(value= {"/policy", "/policy/Editor" } , method = RequestMethod.GET)
323 public ModelAndView view(HttpServletRequest request){
324 String myRequestURL = request.getRequestURL().toString();
327 // Set the URL for the RESTful PAP Engine
329 setPapEngine((PAPPolicyEngine) new RESTfulPAPEngine(myRequestURL));
330 new PDPGroupContainer((PAPPolicyEngine) new RESTfulPAPEngine(myRequestURL));
331 } catch (Exception e) {
332 policyLogger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR+"Exception Occured while loading PAP"+e);
334 Map<String, Object> model = new HashMap<>();
335 return new ModelAndView("policy_Editor","model", model);
338 public PAPPolicyEngine getPapEngine() {
342 public static void setPapEngine(PAPPolicyEngine papEngine) {
343 PolicyController.papEngine = papEngine;
346 public String getUserName(String createdBy) {
347 String loginId = createdBy;
348 List<Object> data = commonClassDao.getDataById(UserInfo.class, "loginId", loginId);
349 return data.get(0).toString();
352 public static boolean getActivePolicy(String query) {
353 if(commonClassDao.getDataByQuery(query, new SimpleBindings()).size() > 0){
360 public void executeQuery(String query) {
361 commonClassDao.updateQuery(query);
364 public void saveData(Object cloneEntity) {
365 commonClassDao.save(cloneEntity);
368 public void updateData(Object entity) {
369 commonClassDao.update(entity);
372 public void deleteData(Object entity) {
373 commonClassDao.delete(entity);
376 public List<Object> getData(@SuppressWarnings("rawtypes") Class className){
377 return commonClassDao.getData(className);
380 public PolicyVersion getPolicyEntityFromPolicyVersion(String query){
381 return (PolicyVersion) commonClassDao.getEntityItem(PolicyVersion.class, "policyName", query);
384 public List<Object> getDataByQuery(String query, SimpleBindings params){
385 return commonClassDao.getDataByQuery(query, params);
389 @SuppressWarnings("rawtypes")
390 public Object getEntityItem(Class className, String columname, String key){
391 return commonClassDao.getEntityItem(className, columname, key);
395 public void watchPolicyFunction(PolicyVersion entity, String policyName, String mode){
396 PolicyNotificationMail email = new PolicyNotificationMail();
398 email.sendMail(entity, policyName, mode, commonClassDao);
399 } catch (MessagingException e) {
400 policyLogger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Excepton Occured while Renaming/Deleting a Policy or Scope" + e);
405 public JSONObject switchVersionPolicyContent(String policyName) {
406 String dbCheckName = policyName.replace("/", ".");
407 if(dbCheckName.contains("Config_")){
408 dbCheckName = dbCheckName.replace(".Config_", ":Config_");
409 }else if(dbCheckName.contains("Action_")){
410 dbCheckName = dbCheckName.replace(".Action_", ":Action_");
411 }else if(dbCheckName.contains("Decision_")){
412 dbCheckName = dbCheckName.replace(".Decision_", ":Decision_");
414 String[] splitDBCheckName = dbCheckName.split(":");
415 String query = "FROM PolicyEntity where policyName like :splitDBCheckName1 and scope = :splitDBCheckName0";
416 SimpleBindings params = new SimpleBindings();
417 params.put("splitDBCheckName1", splitDBCheckName[1] + "%");
418 params.put("splitDBCheckName0", splitDBCheckName[0]);
419 List<Object> policyEntity = commonClassDao.getDataByQuery(query, params);
420 List<String> av = new ArrayList<>();
421 for(Object entity : policyEntity){
422 PolicyEntity pEntity = (PolicyEntity) entity;
423 String removeExtension = pEntity.getPolicyName().replace(".xml", "");
424 String version = removeExtension.substring(removeExtension.lastIndexOf(".")+1);
427 if(policyName.contains("/")){
428 policyName = policyName.replace("/", File.separator);
430 PolicyVersion entity = (PolicyVersion) commonClassDao.getEntityItem(PolicyVersion.class, "policyName", policyName);
431 JSONObject el = new JSONObject();
432 el.put("activeVersion", entity.getActiveVersion());
433 el.put("availableVersions", av);
434 el.put("highestVersion", entity.getHigherVersion());
438 public static String getLogTableLimit() {
439 return logTableLimit;
442 public static void setLogTableLimit(String logTableLimit) {
443 PolicyController.logTableLimit = logTableLimit;
446 public static String getSystemAlertTableLimit() {
447 return systemAlertTableLimit;
450 public static void setSystemAlertTableLimit(String systemAlertTableLimit) {
451 PolicyController.systemAlertTableLimit = systemAlertTableLimit;
454 public static CommonClassDao getCommonClassDao() {
455 return commonClassDao;
458 public static void setCommonClassDao(CommonClassDao commonClassDao) {
459 PolicyController.commonClassDao = commonClassDao;
462 public XacmlAdminAuthorization getAuthorizer() {
466 public void setAuthorizer(XacmlAdminAuthorization authorizer) {
467 this.authorizer = authorizer;
470 public static Map<Datatype, List<FunctionDefinition>> getMapDatatype2Function() {
471 return mapDatatype2Function;
474 public static void setMapDatatype2Function(Map<Datatype, List<FunctionDefinition>> mapDatatype2Function) {
475 PolicyController.mapDatatype2Function = mapDatatype2Function;
478 public static Map<String, FunctionDefinition> getMapID2Function() {
479 return mapID2Function;
482 public static void setMapID2Function(Map<String, FunctionDefinition> mapID2Function) {
483 PolicyController.mapID2Function = mapID2Function;
486 public static String getSmtpHost() {
490 public static void setSmtpHost(String smtpHost) {
491 PolicyController.smtpHost = smtpHost;
494 public static String getSmtpPort() {
498 public static void setSmtpPort(String smtpPort) {
499 PolicyController.smtpPort = smtpPort;
502 public static String getSmtpUsername() {
506 public static void setSmtpUsername(String smtpUsername) {
507 PolicyController.smtpUsername = smtpUsername;
510 public static String getSmtpPassword() {
514 public static void setSmtpPassword(String smtpPassword) {
515 PolicyController.smtpPassword = smtpPassword;
518 public static String getSmtpApplicationName() {
519 return smtpApplicationName;
522 public static void setSmtpApplicationName(String smtpApplicationName) {
523 PolicyController.smtpApplicationName = smtpApplicationName;
526 public static String getSmtpEmailExtension() {
527 return smtpEmailExtension;
530 public static void setSmtpEmailExtension(String smtpEmailExtension) {
531 PolicyController.smtpEmailExtension = smtpEmailExtension;
534 public static String getLogdbDriver() {
538 public static void setLogdbDriver(String logdbDriver) {
539 PolicyController.logdbDriver = logdbDriver;
542 public static String getLogdbUrl() {
546 public static void setLogdbUrl(String logdbUrl) {
547 PolicyController.logdbUrl = logdbUrl;
550 public static String getLogdbUserName() {
551 return logdbUserName;
554 public static void setLogdbUserName(String logdbUserName) {
555 PolicyController.logdbUserName = logdbUserName;
558 public static String getLogdbPassword() {
559 return logdbPassword;
562 public static void setLogdbPassword(String logdbPassword) {
563 PolicyController.logdbPassword = logdbPassword;
566 public static String getLogdbDialect() {
570 public static void setLogdbDialect(String logdbDialect) {
571 PolicyController.logdbDialect = logdbDialect;
574 public static String getXacmldbUrl() {
578 public static void setXacmldbUrl(String xacmldbUrl) {
579 PolicyController.xacmldbUrl = xacmldbUrl;
582 public static String getXacmldbUserName() {
583 return xacmldbUserName;
586 public static void setXacmldbUserName(String xacmldbUserName) {
587 PolicyController.xacmldbUserName = xacmldbUserName;
590 public static String getXacmldbPassword() {
591 return xacmldbPassword;
594 public static void setXacmldbPassword(String xacmldbPassword) {
595 PolicyController.xacmldbPassword = xacmldbPassword;
598 public static String getAutoPushAvailable() {
599 return autoPushAvailable;
602 public static void setAutoPushAvailable(String autoPushAvailable) {
603 PolicyController.autoPushAvailable = autoPushAvailable;
606 public static String getAutoPushDSClosedLoop() {
607 return autoPushDSClosedLoop;
610 public static void setAutoPushDSClosedLoop(String autoPushDSClosedLoop) {
611 PolicyController.autoPushDSClosedLoop = autoPushDSClosedLoop;
614 public static String getAutoPushDSFirewall() {
615 return autoPushDSFirewall;
618 public static void setAutoPushDSFirewall(String autoPushDSFirewall) {
619 PolicyController.autoPushDSFirewall = autoPushDSFirewall;
622 public static String getAutoPushDSMicroservice() {
623 return autoPushDSMicroservice;
626 public static void setAutoPushDSMicroservice(String autoPushDSMicroservice) {
627 PolicyController.autoPushDSMicroservice = autoPushDSMicroservice;
630 public static String getAutoPushPDPGroup() {
631 return autoPushPDPGroup;
634 public static void setAutoPushPDPGroup(String autoPushPDPGroup) {
635 PolicyController.autoPushPDPGroup = autoPushPDPGroup;
638 public static String getPapUrl() {
642 public static void setPapUrl(String papUrl) {
643 PolicyController.papUrl = papUrl;
646 public static String getMsOnapName() {
650 public static void setMsOnapName(String msOnapName) {
651 PolicyController.msOnapName = msOnapName;
654 public static String getMsPolicyName() {
658 public static void setMsPolicyName(String msPolicyName) {
659 PolicyController.msPolicyName = msPolicyName;
662 public static String getConfigHome() {
666 public static void setConfigHome(String configHome) {
667 PolicyController.configHome = configHome;
670 public static String getActionHome() {
674 public static void setActionHome(String actionHome) {
675 PolicyController.actionHome = actionHome;
678 public static Object getMapaccess() {
682 public static String getPolicydata() {
686 public static String getCharacterencoding() {
687 return characterEncoding;
690 public static String getContenttype() {
694 public static String getFile() {