aboutsummaryrefslogtreecommitdiffstats
path: root/POLICY-SDK-APP/src/main/java/org/openecomp/policy/controller/PolicyController.java
blob: 42e4483cab4a897bd01a74f70c0795db61c233a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/*-
 * ============LICENSE_START=======================================================
 * ECOMP Policy Engine
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.policy.controller;


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.annotation.PostConstruct;
import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;
import org.openecomp.policy.admin.PolicyNotificationMail;
import org.openecomp.policy.admin.RESTfulPAPEngine;
import org.openecomp.policy.model.PDPGroupContainer;
import org.openecomp.policy.model.Roles;
import org.openecomp.policy.rest.XACMLRestProperties;
import org.openecomp.policy.rest.XacmlAdminAuthorization;
import org.openecomp.policy.rest.dao.CommonClassDao;
import org.openecomp.policy.rest.jpa.Datatype;
import org.openecomp.policy.rest.jpa.FunctionDefinition;
import org.openecomp.policy.rest.jpa.PolicyEntity;
import org.openecomp.policy.rest.jpa.PolicyVersion;
import org.openecomp.policy.rest.jpa.UserInfo;
import org.openecomp.portalsdk.core.controller.RestrictedBaseController;
import org.openecomp.portalsdk.core.web.support.JsonMessage;
import org.openecomp.portalsdk.core.web.support.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import org.openecomp.policy.xacml.api.XACMLErrorConstants;
import org.openecomp.policy.xacml.api.pap.PAPPolicyEngine;

import com.att.research.xacml.util.XACMLProperties;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.openecomp.policy.common.logging.flexlogger.FlexLogger; 
import org.openecomp.policy.common.logging.flexlogger.Logger;


@Controller
@RequestMapping("/")
public class PolicyController extends RestrictedBaseController {
	private static final Logger	LOGGER	= FlexLogger.getLogger(PolicyController.class);

	private static CommonClassDao commonClassDao;
	// Our authorization object
	//
	XacmlAdminAuthorization authorizer = new XacmlAdminAuthorization();
	//
	// The PAP Engine
	//
	private static PAPPolicyEngine papEngine;

	public static String logTableLimit;
	public static String systemAlertTableLimit;
	protected static Map<String, String> dropDownMap = new HashMap<>();
	public static Map<String, String> getDropDownMap() {
		return dropDownMap;
	}

	public static void setDropDownMap(Map<String, String> dropDownMap) {
		PolicyController.dropDownMap = dropDownMap;
	}

	public static String getDomain() {
		return XACMLProperties.getProperty(XACMLRestProperties.PROP_ADMIN_DOMAIN, "urn");
	}

	private static final Object mapAccess = new Object();
	private static Map<Datatype, List<FunctionDefinition>> mapDatatype2Function = null;
	private static Map<String, FunctionDefinition> mapID2Function = null;


	//Smtp Java Mail Properties
	public static String smtpHost = null;
	public static String smtpPort = null;
	public static String smtpUsername = null;
	public static String smtpPassword = null;
	public static String smtpApplicationName = null;
	public static String smtpEmailExtension = null;
	//log db Properties
	public static String logdbDriver = null;
	public static String logdbUrl = null;
	public static String logdbUserName = null;
	public static String logdbPassword = null;
	public static String logdbDialect = null;
	//Xacml db properties
	public static String xacmldbUrl = null;
	public static String xacmldbUserName = null;
	public static String xacmldbPassword = null;

	//AutoPush feature. 
	public static String autoPushAvailable;
	public static String autoPushDSClosedLoop;
	public static String autoPushDSFirewall;
	public static String autoPushDSMicroservice;
	public static String autoPushPDPGroup;
	
	//papURL
	public static String papUrl;
	
	//MicroService Model Properties
	public static String msEcompName;
	public static String msPolicyName;
	
	//WebApp directories
	public static String configHome;
	public static String actionHome;

	@Autowired
	private PolicyController(CommonClassDao commonClassDao){
		PolicyController.commonClassDao = commonClassDao;
	}

	public PolicyController() {
	}

	@PostConstruct
	public void init(){
		Properties prop = new Properties();
		InputStream input = null;
		try {
			input = new FileInputStream("xacml.admin.properties");
			// load a properties file
			prop.load(input);
			//pap url
			papUrl = prop.getProperty("xacml.rest.pap.url"); 
			// get the property values
			smtpHost = prop.getProperty("ecomp.smtp.host");
			smtpPort = prop.getProperty("ecomp.smtp.port");
			smtpUsername = prop.getProperty("ecomp.smtp.userName");
			smtpPassword = prop.getProperty("ecomp.smtp.password");
			smtpApplicationName = prop.getProperty("ecomp.application.name");
			smtpEmailExtension = prop.getProperty("ecomp.smtp.emailExtension");
			//Log Database Properties
			logdbDriver = prop.getProperty("xacml.log.db.driver");
			logdbUrl = prop.getProperty("xacml.log.db.url");
			logdbUserName = prop.getProperty("xacml.log.db.user");
			logdbPassword = prop.getProperty("xacml.log.db.password");
			logdbDialect = prop.getProperty("ecomp.dialect");
			//Xacml Database Properties
			xacmldbUrl = prop.getProperty("javax.persistence.jdbc.url");
			xacmldbUserName = prop.getProperty("javax.persistence.jdbc.user");
			xacmldbPassword = prop.getProperty("javax.persistence.jdbc.password");
			//AutoPuh
			autoPushAvailable=prop.getProperty("xacml.automatic.push");
			autoPushDSClosedLoop=prop.getProperty("xacml.autopush.closedloop");
			autoPushDSFirewall=prop.getProperty("xacml.autopush.firewall");
			autoPushDSMicroservice=prop.getProperty("xacml.autopush.microservice");
			autoPushPDPGroup=prop.getProperty("xacml.autopush.pdpGroup");
			//Micro Service Properties
			msEcompName=prop.getProperty("xacml.policy.msEcompName");
			msPolicyName=prop.getProperty("xacml.policy.msPolicyName");
			//WebApp directories
			configHome = prop.getProperty("xacml.rest.config.webapps") + "Config";
			actionHome = prop.getProperty("xacml.rest.config.webapps") + "Action";
			//Get the Property Values for Dashboard tab Limit 
			try{
				logTableLimit = prop.getProperty("xacml.ecomp.dashboard.logTableLimit");
				systemAlertTableLimit = prop.getProperty("xacml.ecomp.dashboard.systemAlertTableLimit");
			}catch(Exception e){
				LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Dashboard tab Property fields are missing" +e);
				logTableLimit = "5000";
				systemAlertTableLimit = "2000";
			}
			System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME, "xacml.admin.properties");
		} catch (IOException ex) {
			LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Exception Occured while reading the Smtp properties from xacml.admin.properties file" +ex);
		} finally {
			if (input != null) {
				try {
					input.close();
				} catch (IOException e) {
					LOGGER.error(XACMLErrorConstants.ERROR_DATA_ISSUE+"Exception Occured while Closing the xacml.admin.properties file" +e);
				}
			}
		}

		//Initialize the FunctionDefinition table at Server Start up 
		Map<Datatype, List<FunctionDefinition>> functionMap = getFunctionDatatypeMap();
		for (Datatype id : functionMap.keySet()) {
			List<FunctionDefinition> functionDefinations = (List<FunctionDefinition>) functionMap.get(id);
			for (FunctionDefinition functionDef : functionDefinations) {
				dropDownMap.put(functionDef.getShortname(),functionDef.getXacmlid());
			}
		}

	}

	public static  Map<Datatype, List<FunctionDefinition>>	getFunctionDatatypeMap() {				
		synchronized(mapAccess) {
			if (mapDatatype2Function == null) {
				buildFunctionMaps();
			}
		}
		return mapDatatype2Function;
	}

	public static Map<String, FunctionDefinition> getFunctionIDMap() {
		synchronized(mapAccess) {
			if (mapID2Function == null) {
				buildFunctionMaps();
			}
		}
		return mapID2Function;
	}

	private static  void buildFunctionMaps() {
		mapDatatype2Function = new HashMap<>();
		mapID2Function = new  HashMap<>(); 
		List<Object> functiondefinitions = commonClassDao.getData(FunctionDefinition.class);	
		for (int i = 0; i < functiondefinitions.size(); i ++) {
			FunctionDefinition value = (FunctionDefinition) functiondefinitions.get(i);
			mapID2Function.put(value.getXacmlid(), value);
			if (mapDatatype2Function.containsKey(value.getDatatypeBean()) == false) {
				mapDatatype2Function.put(value.getDatatypeBean(), new ArrayList<FunctionDefinition>());
			}
			mapDatatype2Function.get(value.getDatatypeBean()).add(value);
		}
	}

	@RequestMapping(value={"/get_FunctionDefinitionDataByName"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
	public void getFunctionDefinitionData(HttpServletRequest request, HttpServletResponse response){
		try{
			Map<String, Object> model = new HashMap<>();
			ObjectMapper mapper = new ObjectMapper();
			model.put("functionDefinitionDatas", mapper.writeValueAsString(commonClassDao.getDataByColumn(FunctionDefinition.class, "shortname")));
			JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
			JSONObject j = new JSONObject(msg);
			response.getWriter().write(j.toString());
		}
		catch (Exception e){
			LOGGER.equals(XACMLErrorConstants.ERROR_DATA_ISSUE +"Error while retriving the Function Definition data"+e);
		}
	}
	
	public PolicyEntity getPolicyEntityData(String scope, String policyName){
		String key = scope + ":" + policyName;
		List<Object> data = commonClassDao.getDataById(PolicyEntity.class, "scope:policyName", key);
		PolicyEntity entity = (PolicyEntity) data.get(0);
		return entity;
	}

	public static Map<String, Roles> getUserRoles(String userId) {
		Map<String, Roles> scopes = new HashMap<>();
		List<Object> roles = commonClassDao.getDataById(Roles.class, "loginId", userId);
		if (roles != null && roles.size() > 0) {
			for (Object role : roles) {
				scopes.put(((Roles) role).getScope(), (Roles) role);
			}
		}
		return scopes;
	}

	public static  List<String> getRolesOfUser(String userId) {
		List<String> rolesList = new ArrayList<String>();
		List<Object> roles = commonClassDao.getDataById(Roles.class, "loginId", userId);
		for (Object role: roles) {
			rolesList.add(((Roles) role).getRole());
		}
		return rolesList;
	}

	public static List<Object> getRoles(String userId) {
		return commonClassDao.getDataById(Roles.class, "loginId", userId);
	}

	//Get List of User Roles
	@RequestMapping(value={"/get_UserRolesData"}, method={org.springframework.web.bind.annotation.RequestMethod.GET} , produces=MediaType.APPLICATION_JSON_VALUE)
	public void getUserRolesEntityData(HttpServletRequest request, HttpServletResponse response){
		try{
			String userId = UserUtils.getUserSession(request).getOrgUserId();
			Map<String, Object> model = new HashMap<>();
			ObjectMapper mapper = new ObjectMapper();
			model.put("userRolesDatas", mapper.writeValueAsString(getRolesOfUser(userId)));
			JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
			JSONObject j = new JSONObject(msg);
			response.getWriter().write(j.toString());
		}
		catch (Exception e){
			LOGGER.error("Exception Occured"+e);
		}
	}

	//Policy tabs Model and View 
	@RequestMapping(value= {"/policy", "/policy/Editor" } , method = RequestMethod.GET)
	public ModelAndView view(HttpServletRequest request){
		String myRequestURL = request.getRequestURL().toString();
		try {
			//
			// Set the URL for the RESTful PAP Engine
			//	
			setPapEngine((PAPPolicyEngine) new RESTfulPAPEngine(myRequestURL));
			new PDPGroupContainer((PAPPolicyEngine) new RESTfulPAPEngine(myRequestURL));
		} catch (Exception e) {
			LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR+"Exception Occured while loading PAP"+e);
		}	
		Map<String, Object> model = new HashMap<>();
		return new ModelAndView("policy_Editor","model", model);
	}

	public static PAPPolicyEngine getPapEngine() {
		return papEngine;
	}

	public void setPapEngine(PAPPolicyEngine papEngine) {
		PolicyController.papEngine = papEngine;
	}

	public String getUserName(String createdBy) {
		String loginId = createdBy;
		List<Object> data = commonClassDao.getDataById(UserInfo.class, "loginId", loginId);
		return data.get(0).toString();
	}

	public static boolean getActivePolicy(String query) {
		if(commonClassDao.getDataByQuery(query).size() > 0){
			return true;
		}else{
			return false;
		}
	}

	public void executeQuery(String query) {
		commonClassDao.updateQuery(query);	
	}
	
	public void saveData(Object cloneEntity) {
		commonClassDao.save(cloneEntity);
	}

	public void updateData(Object entity) {
		commonClassDao.update(entity);
	}

	public void deleteData(Object entity) {
		commonClassDao.delete(entity);
	}
	
	public List<Object> getData(@SuppressWarnings("rawtypes") Class className){
		return commonClassDao.getData(className);
	}

	public PolicyVersion getPolicyEntityFromPolicyVersion(String query){
		PolicyVersion policyVersionEntity = (PolicyVersion) commonClassDao.getEntityItem(PolicyVersion.class, "policyName", query);
		return policyVersionEntity;	
	}

	public List<Object> getDataByQuery(String query){
		return commonClassDao.getDataByQuery(query);
	}


	@SuppressWarnings("rawtypes")
	public Object getEntityItem(Class className, String columname, String key){
		return commonClassDao.getEntityItem(className, columname, key);
	}
	
	
	public void watchPolicyFunction(PolicyVersion entity, String policyName, String mode){
		PolicyNotificationMail email = new PolicyNotificationMail();
		try {
			email.sendMail(entity, policyName, mode, commonClassDao);
		} catch (MessagingException e) {
			LOGGER.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Excepton Occured while Renaming/Deleting a Policy or Scope" + e);
		}
	}

	//Switch Version
	public JSONObject switchVersionPolicyContent(String policyName) {
		String dbCheckName = policyName.replace("/", ".");
		if(dbCheckName.contains("Config_")){
			dbCheckName = dbCheckName.replace(".Config_", ":Config_");
		}else if(dbCheckName.contains("Action_")){
			dbCheckName = dbCheckName.replace(".Action_", ":Action_");
		}else if(dbCheckName.contains("Decision_")){
			dbCheckName = dbCheckName.replace(".Decision_", ":Decision_");
		}
		String[] splitDBCheckName = dbCheckName.split(":");
		String query =   "FROM PolicyEntity where policyName like'"+splitDBCheckName[1]+"%' and scope ='"+splitDBCheckName[0]+"'";
		List<Object> policyEntity = commonClassDao.getDataByQuery(query);
		List<String> av = new ArrayList<>();
		for(Object entity : policyEntity){
			PolicyEntity pEntity = (PolicyEntity) entity;
			String removeExtension = pEntity.getPolicyName().replace(".xml", "");
			String version = removeExtension.substring(removeExtension.lastIndexOf(".")+1);
			av.add(version);
		}
		if(policyName.contains("/")){
			policyName = policyName.replace("/", File.separator);
		}
		PolicyVersion entity = (PolicyVersion) commonClassDao.getEntityItem(PolicyVersion.class, "policyName", policyName);
		JSONObject el = new JSONObject();
		el.put("activeVersion", entity.getActiveVersion());
		el.put("availableVersions", av);
		el.put("highestVersion", entity.getHigherVersion());
		return el;
	}
}