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.servlet.http.HttpServletRequest;
37 import javax.servlet.http.HttpServletResponse;
39 import org.json.JSONObject;
40 import org.onap.policy.admin.PolicyNotificationMail;
41 import org.onap.policy.admin.RESTfulPAPEngine;
42 import org.onap.policy.model.PDPGroupContainer;
43 import org.onap.policy.rest.XACMLRestProperties;
44 import org.onap.policy.rest.XacmlAdminAuthorization;
45 import org.onap.policy.rest.dao.CommonClassDao;
46 import org.onap.policy.rest.jpa.Datatype;
47 import org.onap.policy.rest.jpa.FunctionDefinition;
48 import org.onap.policy.rest.jpa.PolicyEntity;
49 import org.onap.policy.rest.jpa.PolicyVersion;
50 import org.onap.policy.rest.jpa.UserInfo;
51 import org.openecomp.policy.model.Roles;
52 import org.openecomp.portalsdk.core.controller.RestrictedBaseController;
53 import org.openecomp.portalsdk.core.web.support.JsonMessage;
54 import org.openecomp.portalsdk.core.web.support.UserUtils;
55 import org.springframework.beans.factory.annotation.Autowired;
56 import org.springframework.http.MediaType;
57 import org.springframework.stereotype.Controller;
58 import org.springframework.web.bind.annotation.RequestMapping;
59 import org.springframework.web.bind.annotation.RequestMethod;
60 import org.springframework.web.servlet.ModelAndView;
62 import org.onap.policy.xacml.api.XACMLErrorConstants;
63 import org.onap.policy.xacml.api.pap.PAPPolicyEngine;
65 import com.att.research.xacml.util.XACMLProperties;
66 import com.fasterxml.jackson.databind.ObjectMapper;
68 import org.onap.policy.common.logging.flexlogger.FlexLogger;
69 import org.onap.policy.common.logging.flexlogger.Logger;
74 public class PolicyController extends RestrictedBaseController {
75 private static final Logger policyLogger = FlexLogger.getLogger(PolicyController.class);
77 private static CommonClassDao commonClassDao;
79 // Our authorization object
81 XacmlAdminAuthorization authorizer = new XacmlAdminAuthorization();
85 private static PAPPolicyEngine papEngine;
87 private static String logTableLimit;
88 private static String systemAlertTableLimit;
89 protected static Map<String, String> dropDownMap = new HashMap<>();
90 public static Map<String, String> getDropDownMap() {
94 public static void setDropDownMap(Map<String, String> dropDownMap) {
95 PolicyController.dropDownMap = dropDownMap;
98 public static String getDomain() {
99 return XACMLProperties.getProperty(XACMLRestProperties.PROP_ADMIN_DOMAIN, "urn");
102 private static final Object mapAccess = new Object();
103 private static Map<Datatype, List<FunctionDefinition>> mapDatatype2Function = null;
104 private static Map<String, FunctionDefinition> mapID2Function = null;
106 //Constant variables used across Policy-sdk
107 private static final String policyData = "policyData";
108 private static final String characterEncoding = "UTF-8";
109 private static final String contentType = "application/json";
110 private static final String file = "file";
112 //Smtp Java Mail Properties
113 private static String smtpHost = null;
114 private static String smtpPort = null;
115 private static String smtpUsername = null;
116 private static String smtpPassword = null;
117 private static String smtpApplicationName = null;
118 private static String smtpEmailExtension = null;
120 private static String logdbDriver = null;
121 private static String logdbUrl = null;
122 private static String logdbUserName = null;
123 private static String logdbPassword = null;
124 private static String logdbDialect = null;
125 //Xacml db properties
126 private static String xacmldbUrl = null;
127 private static String xacmldbUserName = null;
128 private static String xacmldbPassword = null;
131 private static String autoPushAvailable;
132 private static String autoPushDSClosedLoop;
133 private static String autoPushDSFirewall;
134 private static String autoPushDSMicroservice;
135 private static String autoPushPDPGroup;
138 private static String papUrl;
140 //MicroService Model Properties
141 private static String msOnapName;
142 private static String msPolicyName;
145 private static String configHome;
146 private static String actionHome;
149 private PolicyController(CommonClassDao commonClassDao){
150 PolicyController.commonClassDao = commonClassDao;
153 public PolicyController() {
158 Properties prop = new Properties();
159 InputStream input = null;
161 input = new FileInputStream("xacml.admin.properties");
162 // load a properties file
165 setPapUrl(prop.getProperty("xacml.rest.pap.url"));
166 // get the property values
167 setSmtpHost(prop.getProperty("onap.smtp.host"));
168 setSmtpPort(prop.getProperty("onap.smtp.port"));
169 setSmtpUsername(prop.getProperty("onap.smtp.userName"));
170 setSmtpPassword(prop.getProperty("onap.smtp.password"));
171 setSmtpApplicationName(prop.getProperty("onap.application.name"));
172 setSmtpEmailExtension(prop.getProperty("onap.smtp.emailExtension"));
173 //Log Database Properties
174 setLogdbDriver(prop.getProperty("xacml.log.db.driver"));
175 setLogdbUrl(prop.getProperty("xacml.log.db.url"));
176 setLogdbUserName(prop.getProperty("xacml.log.db.user"));
177 setLogdbPassword(prop.getProperty("xacml.log.db.password"));
178 setLogdbDialect(prop.getProperty("onap.dialect"));
179 //Xacml Database Properties
180 setXacmldbUrl(prop.getProperty("javax.persistence.jdbc.url"));
181 setXacmldbUserName(prop.getProperty("javax.persistence.jdbc.user"));
182 setXacmldbPassword(prop.getProperty("javax.persistence.jdbc.password"));
184 setAutoPushAvailable(prop.getProperty("xacml.automatic.push"));
185 setAutoPushDSClosedLoop(prop.getProperty("xacml.autopush.closedloop"));
186 setAutoPushDSFirewall(prop.getProperty("xacml.autopush.firewall"));
187 setAutoPushDSMicroservice(prop.getProperty("xacml.autopush.microservice"));
188 setAutoPushPDPGroup(prop.getProperty("xacml.autopush.pdpGroup"));
189 //Micro Service Properties
190 setMsOnapName(prop.getProperty("xacml.policy.msOnapName"));
191 setMsPolicyName(prop.getProperty("xacml.policy.msPolicyName"));
193 setConfigHome(prop.getProperty("xacml.rest.config.webapps") + "Config");
194 setActionHome(prop.getProperty("xacml.rest.config.webapps") + "Action");
195 //Get the Property Values for Dashboard tab Limit
197 setLogTableLimit(prop.getProperty("xacml.onap.dashboard.logTableLimit"));
198 setSystemAlertTableLimit(prop.getProperty("xacml.onap.dashboard.systemAlertTableLimit"));
200 policyLogger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Dashboard tab Property fields are missing" +e);
201 setLogTableLimit("5000");
202 setSystemAlertTableLimit("2000");
204 System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME, "xacml.admin.properties");
205 } catch (IOException ex) {
206 policyLogger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Exception Occured while reading the Smtp properties from xacml.admin.properties file" +ex);
211 } catch (IOException e) {
212 policyLogger.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Exception Occured while Closing the xacml.admin.properties file" +e);
217 //Initialize the FunctionDefinition table at Server Start up
218 Map<Datatype, List<FunctionDefinition>> functionMap = getFunctionDatatypeMap();
219 for (Datatype id : functionMap.keySet()) {
220 List<FunctionDefinition> functionDefinations = functionMap.get(id);
221 for (FunctionDefinition functionDef : functionDefinations) {
222 dropDownMap.put(functionDef.getShortname(),functionDef.getXacmlid());
228 public static Map<Datatype, List<FunctionDefinition>> getFunctionDatatypeMap() {
229 synchronized(mapAccess) {
230 if (mapDatatype2Function == null) {
234 return mapDatatype2Function;
237 public static Map<String, FunctionDefinition> getFunctionIDMap() {
238 synchronized(mapAccess) {
239 if (mapID2Function == null) {
243 return mapID2Function;
246 private static void buildFunctionMaps() {
247 mapDatatype2Function = new HashMap<>();
248 mapID2Function = new HashMap<>();
249 List<Object> functiondefinitions = commonClassDao.getData(FunctionDefinition.class);
250 for (int i = 0; i < functiondefinitions.size(); i ++) {
251 FunctionDefinition value = (FunctionDefinition) functiondefinitions.get(i);
252 mapID2Function.put(value.getXacmlid(), value);
253 if (!mapDatatype2Function.containsKey(value.getDatatypeBean())) {
254 mapDatatype2Function.put(value.getDatatypeBean(), new ArrayList<FunctionDefinition>());
256 mapDatatype2Function.get(value.getDatatypeBean()).add(value);
260 @RequestMapping(value={"/get_FunctionDefinitionDataByName"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
261 public void getFunctionDefinitionData(HttpServletRequest request, HttpServletResponse response){
263 Map<String, Object> model = new HashMap<>();
264 ObjectMapper mapper = new ObjectMapper();
265 model.put("functionDefinitionDatas", mapper.writeValueAsString(commonClassDao.getDataByColumn(FunctionDefinition.class, "shortname")));
266 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
267 JSONObject j = new JSONObject(msg);
268 response.getWriter().write(j.toString());
271 policyLogger.error(XACMLErrorConstants.ERROR_DATA_ISSUE +"Error while retriving the Function Definition data"+e);
275 public PolicyEntity getPolicyEntityData(String scope, String policyName){
276 String key = scope + ":" + policyName;
277 List<Object> data = commonClassDao.getDataById(PolicyEntity.class, "scope:policyName", key);
278 return (PolicyEntity) data.get(0);
281 public static Map<String, Roles> getUserRoles(String userId) {
282 Map<String, Roles> scopes = new HashMap<>();
283 List<Object> roles = commonClassDao.getDataById(Roles.class, "loginId", userId);
284 if (roles != null && !roles.isEmpty()) {
285 for (Object role : roles) {
286 scopes.put(((Roles) role).getScope(), (Roles) role);
292 public List<String> getRolesOfUser(String userId) {
293 List<String> rolesList = new ArrayList<>();
294 List<Object> roles = commonClassDao.getDataById(Roles.class, "loginId", userId);
295 for (Object role: roles) {
296 rolesList.add(((Roles) role).getRole());
301 public List<Object> getRoles(String userId) {
302 return commonClassDao.getDataById(Roles.class, "loginId", userId);
305 //Get List of User Roles
306 @RequestMapping(value={"/get_UserRolesData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
307 public void getUserRolesEntityData(HttpServletRequest request, HttpServletResponse response){
309 String userId = UserUtils.getUserSession(request).getOrgUserId();
310 Map<String, Object> model = new HashMap<>();
311 ObjectMapper mapper = new ObjectMapper();
312 model.put("userRolesDatas", mapper.writeValueAsString(getRolesOfUser(userId)));
313 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
314 JSONObject j = new JSONObject(msg);
315 response.getWriter().write(j.toString());
318 policyLogger.error("Exception Occured"+e);
322 //Policy tabs Model and View
323 @RequestMapping(value= {"/policy", "/policy/Editor" } , method = RequestMethod.GET)
324 public ModelAndView view(HttpServletRequest request){
325 String myRequestURL = request.getRequestURL().toString();
328 // Set the URL for the RESTful PAP Engine
330 setPapEngine((PAPPolicyEngine) new RESTfulPAPEngine(myRequestURL));
331 new PDPGroupContainer((PAPPolicyEngine) new RESTfulPAPEngine(myRequestURL));
332 } catch (Exception e) {
333 policyLogger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR+"Exception Occured while loading PAP"+e);
335 Map<String, Object> model = new HashMap<>();
336 return new ModelAndView("policy_Editor","model", model);
339 public PAPPolicyEngine getPapEngine() {
343 public void setPapEngine(PAPPolicyEngine papEngine) {
344 PolicyController.papEngine = papEngine;
347 public String getUserName(String createdBy) {
348 String loginId = createdBy;
349 List<Object> data = commonClassDao.getDataById(UserInfo.class, "loginId", loginId);
350 return data.get(0).toString();
353 public static boolean getActivePolicy(String query) {
354 if(commonClassDao.getDataByQuery(query).size() > 0){
361 public void executeQuery(String query) {
362 commonClassDao.updateQuery(query);
365 public void saveData(Object cloneEntity) {
366 commonClassDao.save(cloneEntity);
369 public void updateData(Object entity) {
370 commonClassDao.update(entity);
373 public void deleteData(Object entity) {
374 commonClassDao.delete(entity);
377 public List<Object> getData(@SuppressWarnings("rawtypes") Class className){
378 return commonClassDao.getData(className);
381 public PolicyVersion getPolicyEntityFromPolicyVersion(String query){
382 return (PolicyVersion) commonClassDao.getEntityItem(PolicyVersion.class, "policyName", query);
385 public List<Object> getDataByQuery(String query){
386 return commonClassDao.getDataByQuery(query);
390 @SuppressWarnings("rawtypes")
391 public Object getEntityItem(Class className, String columname, String key){
392 return commonClassDao.getEntityItem(className, columname, key);
396 public void watchPolicyFunction(PolicyVersion entity, String policyName, String mode){
397 PolicyNotificationMail email = new PolicyNotificationMail();
399 email.sendMail(entity, policyName, mode, commonClassDao);
400 } catch (MessagingException e) {
401 policyLogger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Excepton Occured while Renaming/Deleting a Policy or Scope" + e);
406 public JSONObject switchVersionPolicyContent(String policyName) {
407 String dbCheckName = policyName.replace("/", ".");
408 if(dbCheckName.contains("Config_")){
409 dbCheckName = dbCheckName.replace(".Config_", ":Config_");
410 }else if(dbCheckName.contains("Action_")){
411 dbCheckName = dbCheckName.replace(".Action_", ":Action_");
412 }else if(dbCheckName.contains("Decision_")){
413 dbCheckName = dbCheckName.replace(".Decision_", ":Decision_");
415 String[] splitDBCheckName = dbCheckName.split(":");
416 String query = "FROM PolicyEntity where policyName like'"+splitDBCheckName[1]+"%' and scope ='"+splitDBCheckName[0]+"'";
417 List<Object> policyEntity = commonClassDao.getDataByQuery(query);
418 List<String> av = new ArrayList<>();
419 for(Object entity : policyEntity){
420 PolicyEntity pEntity = (PolicyEntity) entity;
421 String removeExtension = pEntity.getPolicyName().replace(".xml", "");
422 String version = removeExtension.substring(removeExtension.lastIndexOf(".")+1);
425 if(policyName.contains("/")){
426 policyName = policyName.replace("/", File.separator);
428 PolicyVersion entity = (PolicyVersion) commonClassDao.getEntityItem(PolicyVersion.class, "policyName", policyName);
429 JSONObject el = new JSONObject();
430 el.put("activeVersion", entity.getActiveVersion());
431 el.put("availableVersions", av);
432 el.put("highestVersion", entity.getHigherVersion());
436 public static String getLogTableLimit() {
437 return logTableLimit;
440 public static void setLogTableLimit(String logTableLimit) {
441 PolicyController.logTableLimit = logTableLimit;
444 public static String getSystemAlertTableLimit() {
445 return systemAlertTableLimit;
448 public static void setSystemAlertTableLimit(String systemAlertTableLimit) {
449 PolicyController.systemAlertTableLimit = systemAlertTableLimit;
452 public static CommonClassDao getCommonClassDao() {
453 return commonClassDao;
456 public static void setCommonClassDao(CommonClassDao commonClassDao) {
457 PolicyController.commonClassDao = commonClassDao;
460 public XacmlAdminAuthorization getAuthorizer() {
464 public void setAuthorizer(XacmlAdminAuthorization authorizer) {
465 this.authorizer = authorizer;
468 public static Map<Datatype, List<FunctionDefinition>> getMapDatatype2Function() {
469 return mapDatatype2Function;
472 public static void setMapDatatype2Function(Map<Datatype, List<FunctionDefinition>> mapDatatype2Function) {
473 PolicyController.mapDatatype2Function = mapDatatype2Function;
476 public static Map<String, FunctionDefinition> getMapID2Function() {
477 return mapID2Function;
480 public static void setMapID2Function(Map<String, FunctionDefinition> mapID2Function) {
481 PolicyController.mapID2Function = mapID2Function;
484 public static String getSmtpHost() {
488 public static void setSmtpHost(String smtpHost) {
489 PolicyController.smtpHost = smtpHost;
492 public static String getSmtpPort() {
496 public static void setSmtpPort(String smtpPort) {
497 PolicyController.smtpPort = smtpPort;
500 public static String getSmtpUsername() {
504 public static void setSmtpUsername(String smtpUsername) {
505 PolicyController.smtpUsername = smtpUsername;
508 public static String getSmtpPassword() {
512 public static void setSmtpPassword(String smtpPassword) {
513 PolicyController.smtpPassword = smtpPassword;
516 public static String getSmtpApplicationName() {
517 return smtpApplicationName;
520 public static void setSmtpApplicationName(String smtpApplicationName) {
521 PolicyController.smtpApplicationName = smtpApplicationName;
524 public static String getSmtpEmailExtension() {
525 return smtpEmailExtension;
528 public static void setSmtpEmailExtension(String smtpEmailExtension) {
529 PolicyController.smtpEmailExtension = smtpEmailExtension;
532 public static String getLogdbDriver() {
536 public static void setLogdbDriver(String logdbDriver) {
537 PolicyController.logdbDriver = logdbDriver;
540 public static String getLogdbUrl() {
544 public static void setLogdbUrl(String logdbUrl) {
545 PolicyController.logdbUrl = logdbUrl;
548 public static String getLogdbUserName() {
549 return logdbUserName;
552 public static void setLogdbUserName(String logdbUserName) {
553 PolicyController.logdbUserName = logdbUserName;
556 public static String getLogdbPassword() {
557 return logdbPassword;
560 public static void setLogdbPassword(String logdbPassword) {
561 PolicyController.logdbPassword = logdbPassword;
564 public static String getLogdbDialect() {
568 public static void setLogdbDialect(String logdbDialect) {
569 PolicyController.logdbDialect = logdbDialect;
572 public static String getXacmldbUrl() {
576 public static void setXacmldbUrl(String xacmldbUrl) {
577 PolicyController.xacmldbUrl = xacmldbUrl;
580 public static String getXacmldbUserName() {
581 return xacmldbUserName;
584 public static void setXacmldbUserName(String xacmldbUserName) {
585 PolicyController.xacmldbUserName = xacmldbUserName;
588 public static String getXacmldbPassword() {
589 return xacmldbPassword;
592 public static void setXacmldbPassword(String xacmldbPassword) {
593 PolicyController.xacmldbPassword = xacmldbPassword;
596 public static String getAutoPushAvailable() {
597 return autoPushAvailable;
600 public static void setAutoPushAvailable(String autoPushAvailable) {
601 PolicyController.autoPushAvailable = autoPushAvailable;
604 public static String getAutoPushDSClosedLoop() {
605 return autoPushDSClosedLoop;
608 public static void setAutoPushDSClosedLoop(String autoPushDSClosedLoop) {
609 PolicyController.autoPushDSClosedLoop = autoPushDSClosedLoop;
612 public static String getAutoPushDSFirewall() {
613 return autoPushDSFirewall;
616 public static void setAutoPushDSFirewall(String autoPushDSFirewall) {
617 PolicyController.autoPushDSFirewall = autoPushDSFirewall;
620 public static String getAutoPushDSMicroservice() {
621 return autoPushDSMicroservice;
624 public static void setAutoPushDSMicroservice(String autoPushDSMicroservice) {
625 PolicyController.autoPushDSMicroservice = autoPushDSMicroservice;
628 public static String getAutoPushPDPGroup() {
629 return autoPushPDPGroup;
632 public static void setAutoPushPDPGroup(String autoPushPDPGroup) {
633 PolicyController.autoPushPDPGroup = autoPushPDPGroup;
636 public static String getPapUrl() {
640 public static void setPapUrl(String papUrl) {
641 PolicyController.papUrl = papUrl;
644 public static String getMsOnapName() {
648 public static void setMsOnapName(String msOnapName) {
649 PolicyController.msOnapName = msOnapName;
652 public static String getMsPolicyName() {
656 public static void setMsPolicyName(String msPolicyName) {
657 PolicyController.msPolicyName = msPolicyName;
660 public static String getConfigHome() {
664 public static void setConfigHome(String configHome) {
665 PolicyController.configHome = configHome;
668 public static String getActionHome() {
672 public static void setActionHome(String actionHome) {
673 PolicyController.actionHome = actionHome;
676 public static Object getMapaccess() {
680 public static String getPolicydata() {
684 public static String getCharacterencoding() {
685 return characterEncoding;
688 public static String getContenttype() {
692 public static String getFile() {