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.model.PDPGroupContainer;
44 import org.onap.policy.rest.XACMLRestProperties;
45 import org.onap.policy.rest.XacmlAdminAuthorization;
46 import org.onap.policy.rest.dao.CommonClassDao;
47 import org.onap.policy.rest.jpa.Datatype;
48 import org.onap.policy.rest.jpa.FunctionDefinition;
49 import org.onap.policy.rest.jpa.PolicyEntity;
50 import org.onap.policy.rest.jpa.PolicyVersion;
51 import org.onap.policy.rest.jpa.UserInfo;
52 import org.openecomp.policy.model.Roles;
53 import org.openecomp.portalsdk.core.controller.RestrictedBaseController;
54 import org.openecomp.portalsdk.core.web.support.JsonMessage;
55 import org.openecomp.portalsdk.core.web.support.UserUtils;
56 import org.springframework.beans.factory.annotation.Autowired;
57 import org.springframework.http.MediaType;
58 import org.springframework.stereotype.Controller;
59 import org.springframework.web.bind.annotation.RequestMapping;
60 import org.springframework.web.bind.annotation.RequestMethod;
61 import org.springframework.web.servlet.ModelAndView;
63 import org.onap.policy.xacml.api.XACMLErrorConstants;
64 import org.onap.policy.xacml.api.pap.PAPPolicyEngine;
66 import com.att.research.xacml.util.XACMLProperties;
67 import com.fasterxml.jackson.databind.ObjectMapper;
69 import org.onap.policy.common.logging.flexlogger.FlexLogger;
70 import org.onap.policy.common.logging.flexlogger.Logger;
75 public class PolicyController extends RestrictedBaseController {
76 private static final Logger policyLogger = FlexLogger.getLogger(PolicyController.class);
78 private static CommonClassDao commonClassDao;
80 // Our authorization object
82 XacmlAdminAuthorization authorizer = new XacmlAdminAuthorization();
86 private static PAPPolicyEngine papEngine;
88 private static String logTableLimit;
89 private static String systemAlertTableLimit;
90 protected static Map<String, String> dropDownMap = new HashMap<>();
91 public static Map<String, String> getDropDownMap() {
95 public static void setDropDownMap(Map<String, String> dropDownMap) {
96 PolicyController.dropDownMap = dropDownMap;
99 public static String getDomain() {
100 return XACMLProperties.getProperty(XACMLRestProperties.PROP_ADMIN_DOMAIN, "urn");
103 private static final Object mapAccess = new Object();
104 private static Map<Datatype, List<FunctionDefinition>> mapDatatype2Function = null;
105 private static Map<String, FunctionDefinition> mapID2Function = null;
107 //Constant variables used across Policy-sdk
108 private static final String policyData = "policyData";
109 private static final String characterEncoding = "UTF-8";
110 private static final String contentType = "application/json";
111 private static final String file = "file";
113 //Smtp Java Mail Properties
114 private static String smtpHost = null;
115 private static String smtpPort = null;
116 private static String smtpUsername = null;
117 private static String smtpPassword = null;
118 private static String smtpApplicationName = null;
119 private static String smtpEmailExtension = null;
121 private static String logdbDriver = null;
122 private static String logdbUrl = null;
123 private static String logdbUserName = null;
124 private static String logdbPassword = null;
125 private static String logdbDialect = null;
126 //Xacml db properties
127 private static String xacmldbUrl = null;
128 private static String xacmldbUserName = null;
129 private static String xacmldbPassword = null;
132 private static String autoPushAvailable;
133 private static String autoPushDSClosedLoop;
134 private static String autoPushDSFirewall;
135 private static String autoPushDSMicroservice;
136 private static String autoPushPDPGroup;
139 private static String papUrl;
141 //MicroService Model Properties
142 private static String msOnapName;
143 private static String msPolicyName;
146 private static String configHome;
147 private static String actionHome;
150 private PolicyController(CommonClassDao commonClassDao){
151 PolicyController.commonClassDao = commonClassDao;
154 public PolicyController() {
159 Properties prop = new Properties();
160 InputStream input = null;
162 input = new FileInputStream("xacml.admin.properties");
163 // load a properties file
166 setPapUrl(prop.getProperty("xacml.rest.pap.url"));
167 // get the property values
168 setSmtpHost(prop.getProperty("onap.smtp.host"));
169 setSmtpPort(prop.getProperty("onap.smtp.port"));
170 setSmtpUsername(prop.getProperty("onap.smtp.userName"));
171 setSmtpPassword(prop.getProperty("onap.smtp.password"));
172 setSmtpApplicationName(prop.getProperty("onap.application.name"));
173 setSmtpEmailExtension(prop.getProperty("onap.smtp.emailExtension"));
174 //Log Database Properties
175 setLogdbDriver(prop.getProperty("xacml.log.db.driver"));
176 setLogdbUrl(prop.getProperty("xacml.log.db.url"));
177 setLogdbUserName(prop.getProperty("xacml.log.db.user"));
178 setLogdbPassword(prop.getProperty("xacml.log.db.password"));
179 setLogdbDialect(prop.getProperty("onap.dialect"));
180 //Xacml Database Properties
181 setXacmldbUrl(prop.getProperty("javax.persistence.jdbc.url"));
182 setXacmldbUserName(prop.getProperty("javax.persistence.jdbc.user"));
183 setXacmldbPassword(prop.getProperty("javax.persistence.jdbc.password"));
185 setAutoPushAvailable(prop.getProperty("xacml.automatic.push"));
186 setAutoPushDSClosedLoop(prop.getProperty("xacml.autopush.closedloop"));
187 setAutoPushDSFirewall(prop.getProperty("xacml.autopush.firewall"));
188 setAutoPushDSMicroservice(prop.getProperty("xacml.autopush.microservice"));
189 setAutoPushPDPGroup(prop.getProperty("xacml.autopush.pdpGroup"));
190 //Micro Service Properties
191 setMsOnapName(prop.getProperty("xacml.policy.msOnapName"));
192 setMsPolicyName(prop.getProperty("xacml.policy.msPolicyName"));
194 setConfigHome(prop.getProperty("xacml.rest.config.webapps") + "Config");
195 setActionHome(prop.getProperty("xacml.rest.config.webapps") + "Action");
196 //Get the Property Values for Dashboard tab Limit
198 setLogTableLimit(prop.getProperty("xacml.onap.dashboard.logTableLimit"));
199 setSystemAlertTableLimit(prop.getProperty("xacml.onap.dashboard.systemAlertTableLimit"));
201 policyLogger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Dashboard tab Property fields are missing" +e);
202 setLogTableLimit("5000");
203 setSystemAlertTableLimit("2000");
205 System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME, "xacml.admin.properties");
206 } catch (IOException ex) {
207 policyLogger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Exception Occured while reading the Smtp properties from xacml.admin.properties file" +ex);
212 } catch (IOException e) {
213 policyLogger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Exception Occured while Closing the xacml.admin.properties file" +e);
218 //Initialize the FunctionDefinition table at Server Start up
219 Map<Datatype, List<FunctionDefinition>> functionMap = getFunctionDatatypeMap();
220 for (Datatype id : functionMap.keySet()) {
221 List<FunctionDefinition> functionDefinations = functionMap.get(id);
222 for (FunctionDefinition functionDef : functionDefinations) {
223 dropDownMap.put(functionDef.getShortname(),functionDef.getXacmlid());
229 public static Map<Datatype, List<FunctionDefinition>> getFunctionDatatypeMap() {
230 synchronized(mapAccess) {
231 if (mapDatatype2Function == null) {
235 return mapDatatype2Function;
238 public static Map<String, FunctionDefinition> getFunctionIDMap() {
239 synchronized(mapAccess) {
240 if (mapID2Function == null) {
244 return mapID2Function;
247 private static void buildFunctionMaps() {
248 mapDatatype2Function = new HashMap<>();
249 mapID2Function = new HashMap<>();
250 List<Object> functiondefinitions = commonClassDao.getData(FunctionDefinition.class);
251 for (int i = 0; i < functiondefinitions.size(); i ++) {
252 FunctionDefinition value = (FunctionDefinition) functiondefinitions.get(i);
253 mapID2Function.put(value.getXacmlid(), value);
254 if (!mapDatatype2Function.containsKey(value.getDatatypeBean())) {
255 mapDatatype2Function.put(value.getDatatypeBean(), new ArrayList<FunctionDefinition>());
257 mapDatatype2Function.get(value.getDatatypeBean()).add(value);
261 @RequestMapping(value={"/get_FunctionDefinitionDataByName"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
262 public void getFunctionDefinitionData(HttpServletRequest request, HttpServletResponse response){
264 Map<String, Object> model = new HashMap<>();
265 ObjectMapper mapper = new ObjectMapper();
266 model.put("functionDefinitionDatas", mapper.writeValueAsString(commonClassDao.getDataByColumn(FunctionDefinition.class, "shortname")));
267 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
268 JSONObject j = new JSONObject(msg);
269 response.getWriter().write(j.toString());
272 policyLogger.error(XACMLErrorConstants.ERROR_DATA_ISSUE +"Error while retriving the Function Definition data"+e);
276 public PolicyEntity getPolicyEntityData(String scope, String policyName){
277 String key = scope + ":" + policyName;
278 List<Object> data = commonClassDao.getDataById(PolicyEntity.class, "scope:policyName", key);
279 return (PolicyEntity) data.get(0);
282 public static Map<String, Roles> getUserRoles(String userId) {
283 Map<String, Roles> scopes = new HashMap<>();
284 List<Object> roles = commonClassDao.getDataById(Roles.class, "loginId", userId);
285 if (roles != null && !roles.isEmpty()) {
286 for (Object role : roles) {
287 scopes.put(((Roles) role).getScope(), (Roles) role);
293 public List<String> getRolesOfUser(String userId) {
294 List<String> rolesList = new ArrayList<>();
295 List<Object> roles = commonClassDao.getDataById(Roles.class, "loginId", userId);
296 for (Object role: roles) {
297 rolesList.add(((Roles) role).getRole());
302 public List<Object> getRoles(String userId) {
303 return commonClassDao.getDataById(Roles.class, "loginId", userId);
306 //Get List of User Roles
307 @RequestMapping(value={"/get_UserRolesData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
308 public void getUserRolesEntityData(HttpServletRequest request, HttpServletResponse response){
310 String userId = UserUtils.getUserSession(request).getOrgUserId();
311 Map<String, Object> model = new HashMap<>();
312 ObjectMapper mapper = new ObjectMapper();
313 model.put("userRolesDatas", mapper.writeValueAsString(getRolesOfUser(userId)));
314 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
315 JSONObject j = new JSONObject(msg);
316 response.getWriter().write(j.toString());
319 policyLogger.error("Exception Occured"+e);
323 //Policy tabs Model and View
324 @RequestMapping(value= {"/policy", "/policy/Editor" } , method = RequestMethod.GET)
325 public ModelAndView view(HttpServletRequest request){
326 String myRequestURL = request.getRequestURL().toString();
329 // Set the URL for the RESTful PAP Engine
331 setPapEngine((PAPPolicyEngine) new RESTfulPAPEngine(myRequestURL));
332 new PDPGroupContainer((PAPPolicyEngine) new RESTfulPAPEngine(myRequestURL));
333 } catch (Exception e) {
334 policyLogger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR+"Exception Occured while loading PAP"+e);
336 Map<String, Object> model = new HashMap<>();
337 return new ModelAndView("policy_Editor","model", model);
340 public PAPPolicyEngine getPapEngine() {
344 public void setPapEngine(PAPPolicyEngine papEngine) {
345 PolicyController.papEngine = papEngine;
348 public String getUserName(String createdBy) {
349 String loginId = createdBy;
350 List<Object> data = commonClassDao.getDataById(UserInfo.class, "loginId", loginId);
351 return data.get(0).toString();
354 public static boolean getActivePolicy(String query) {
355 if(commonClassDao.getDataByQuery(query, new SimpleBindings()).size() > 0){
362 public void executeQuery(String query) {
363 commonClassDao.updateQuery(query);
366 public void saveData(Object cloneEntity) {
367 commonClassDao.save(cloneEntity);
370 public void updateData(Object entity) {
371 commonClassDao.update(entity);
374 public void deleteData(Object entity) {
375 commonClassDao.delete(entity);
378 public List<Object> getData(@SuppressWarnings("rawtypes") Class className){
379 return commonClassDao.getData(className);
382 public PolicyVersion getPolicyEntityFromPolicyVersion(String query){
383 return (PolicyVersion) commonClassDao.getEntityItem(PolicyVersion.class, "policyName", query);
386 public List<Object> getDataByQuery(String query, SimpleBindings params){
387 return commonClassDao.getDataByQuery(query, params);
391 @SuppressWarnings("rawtypes")
392 public Object getEntityItem(Class className, String columname, String key){
393 return commonClassDao.getEntityItem(className, columname, key);
397 public void watchPolicyFunction(PolicyVersion entity, String policyName, String mode){
398 PolicyNotificationMail email = new PolicyNotificationMail();
400 email.sendMail(entity, policyName, mode, commonClassDao);
401 } catch (MessagingException e) {
402 policyLogger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Excepton Occured while Renaming/Deleting a Policy or Scope" + e);
407 public JSONObject switchVersionPolicyContent(String policyName) {
408 String dbCheckName = policyName.replace("/", ".");
409 if(dbCheckName.contains("Config_")){
410 dbCheckName = dbCheckName.replace(".Config_", ":Config_");
411 }else if(dbCheckName.contains("Action_")){
412 dbCheckName = dbCheckName.replace(".Action_", ":Action_");
413 }else if(dbCheckName.contains("Decision_")){
414 dbCheckName = dbCheckName.replace(".Decision_", ":Decision_");
416 String[] splitDBCheckName = dbCheckName.split(":");
417 String query = "FROM PolicyEntity where policyName like :splitDBCheckName1 and scope = :splitDBCheckName0";
418 SimpleBindings params = new SimpleBindings();
419 params.put("splitDBCheckName1", splitDBCheckName[1] + "%");
420 params.put("splitDBCheckName0", splitDBCheckName[0]);
421 List<Object> policyEntity = commonClassDao.getDataByQuery(query, params);
422 List<String> av = new ArrayList<>();
423 for(Object entity : policyEntity){
424 PolicyEntity pEntity = (PolicyEntity) entity;
425 String removeExtension = pEntity.getPolicyName().replace(".xml", "");
426 String version = removeExtension.substring(removeExtension.lastIndexOf(".")+1);
429 if(policyName.contains("/")){
430 policyName = policyName.replace("/", File.separator);
432 PolicyVersion entity = (PolicyVersion) commonClassDao.getEntityItem(PolicyVersion.class, "policyName", policyName);
433 JSONObject el = new JSONObject();
434 el.put("activeVersion", entity.getActiveVersion());
435 el.put("availableVersions", av);
436 el.put("highestVersion", entity.getHigherVersion());
440 public static String getLogTableLimit() {
441 return logTableLimit;
444 public static void setLogTableLimit(String logTableLimit) {
445 PolicyController.logTableLimit = logTableLimit;
448 public static String getSystemAlertTableLimit() {
449 return systemAlertTableLimit;
452 public static void setSystemAlertTableLimit(String systemAlertTableLimit) {
453 PolicyController.systemAlertTableLimit = systemAlertTableLimit;
456 public static CommonClassDao getCommonClassDao() {
457 return commonClassDao;
460 public static void setCommonClassDao(CommonClassDao commonClassDao) {
461 PolicyController.commonClassDao = commonClassDao;
464 public XacmlAdminAuthorization getAuthorizer() {
468 public void setAuthorizer(XacmlAdminAuthorization authorizer) {
469 this.authorizer = authorizer;
472 public static Map<Datatype, List<FunctionDefinition>> getMapDatatype2Function() {
473 return mapDatatype2Function;
476 public static void setMapDatatype2Function(Map<Datatype, List<FunctionDefinition>> mapDatatype2Function) {
477 PolicyController.mapDatatype2Function = mapDatatype2Function;
480 public static Map<String, FunctionDefinition> getMapID2Function() {
481 return mapID2Function;
484 public static void setMapID2Function(Map<String, FunctionDefinition> mapID2Function) {
485 PolicyController.mapID2Function = mapID2Function;
488 public static String getSmtpHost() {
492 public static void setSmtpHost(String smtpHost) {
493 PolicyController.smtpHost = smtpHost;
496 public static String getSmtpPort() {
500 public static void setSmtpPort(String smtpPort) {
501 PolicyController.smtpPort = smtpPort;
504 public static String getSmtpUsername() {
508 public static void setSmtpUsername(String smtpUsername) {
509 PolicyController.smtpUsername = smtpUsername;
512 public static String getSmtpPassword() {
516 public static void setSmtpPassword(String smtpPassword) {
517 PolicyController.smtpPassword = smtpPassword;
520 public static String getSmtpApplicationName() {
521 return smtpApplicationName;
524 public static void setSmtpApplicationName(String smtpApplicationName) {
525 PolicyController.smtpApplicationName = smtpApplicationName;
528 public static String getSmtpEmailExtension() {
529 return smtpEmailExtension;
532 public static void setSmtpEmailExtension(String smtpEmailExtension) {
533 PolicyController.smtpEmailExtension = smtpEmailExtension;
536 public static String getLogdbDriver() {
540 public static void setLogdbDriver(String logdbDriver) {
541 PolicyController.logdbDriver = logdbDriver;
544 public static String getLogdbUrl() {
548 public static void setLogdbUrl(String logdbUrl) {
549 PolicyController.logdbUrl = logdbUrl;
552 public static String getLogdbUserName() {
553 return logdbUserName;
556 public static void setLogdbUserName(String logdbUserName) {
557 PolicyController.logdbUserName = logdbUserName;
560 public static String getLogdbPassword() {
561 return logdbPassword;
564 public static void setLogdbPassword(String logdbPassword) {
565 PolicyController.logdbPassword = logdbPassword;
568 public static String getLogdbDialect() {
572 public static void setLogdbDialect(String logdbDialect) {
573 PolicyController.logdbDialect = logdbDialect;
576 public static String getXacmldbUrl() {
580 public static void setXacmldbUrl(String xacmldbUrl) {
581 PolicyController.xacmldbUrl = xacmldbUrl;
584 public static String getXacmldbUserName() {
585 return xacmldbUserName;
588 public static void setXacmldbUserName(String xacmldbUserName) {
589 PolicyController.xacmldbUserName = xacmldbUserName;
592 public static String getXacmldbPassword() {
593 return xacmldbPassword;
596 public static void setXacmldbPassword(String xacmldbPassword) {
597 PolicyController.xacmldbPassword = xacmldbPassword;
600 public static String getAutoPushAvailable() {
601 return autoPushAvailable;
604 public static void setAutoPushAvailable(String autoPushAvailable) {
605 PolicyController.autoPushAvailable = autoPushAvailable;
608 public static String getAutoPushDSClosedLoop() {
609 return autoPushDSClosedLoop;
612 public static void setAutoPushDSClosedLoop(String autoPushDSClosedLoop) {
613 PolicyController.autoPushDSClosedLoop = autoPushDSClosedLoop;
616 public static String getAutoPushDSFirewall() {
617 return autoPushDSFirewall;
620 public static void setAutoPushDSFirewall(String autoPushDSFirewall) {
621 PolicyController.autoPushDSFirewall = autoPushDSFirewall;
624 public static String getAutoPushDSMicroservice() {
625 return autoPushDSMicroservice;
628 public static void setAutoPushDSMicroservice(String autoPushDSMicroservice) {
629 PolicyController.autoPushDSMicroservice = autoPushDSMicroservice;
632 public static String getAutoPushPDPGroup() {
633 return autoPushPDPGroup;
636 public static void setAutoPushPDPGroup(String autoPushPDPGroup) {
637 PolicyController.autoPushPDPGroup = autoPushPDPGroup;
640 public static String getPapUrl() {
644 public static void setPapUrl(String papUrl) {
645 PolicyController.papUrl = papUrl;
648 public static String getMsOnapName() {
652 public static void setMsOnapName(String msOnapName) {
653 PolicyController.msOnapName = msOnapName;
656 public static String getMsPolicyName() {
660 public static void setMsPolicyName(String msPolicyName) {
661 PolicyController.msPolicyName = msPolicyName;
664 public static String getConfigHome() {
668 public static void setConfigHome(String configHome) {
669 PolicyController.configHome = configHome;
672 public static String getActionHome() {
676 public static void setActionHome(String actionHome) {
677 PolicyController.actionHome = actionHome;
680 public static Object getMapaccess() {
684 public static String getPolicydata() {
688 public static String getCharacterencoding() {
689 return characterEncoding;
692 public static String getContenttype() {
696 public static String getFile() {