2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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;
23 import java.io.PrintWriter;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.HashMap;
27 import java.util.Iterator;
28 import java.util.LinkedHashMap;
29 import java.util.List;
31 import java.util.Map.Entry;
33 import java.util.regex.Matcher;
34 import java.util.regex.Pattern;
36 import javax.servlet.http.HttpServletRequest;
37 import javax.servlet.http.HttpServletResponse;
38 import javax.xml.bind.JAXBElement;
40 import org.json.JSONObject;
41 import org.onap.policy.common.logging.flexlogger.FlexLogger;
42 import org.onap.policy.common.logging.flexlogger.Logger;
43 import org.onap.policy.rest.adapter.PolicyRestAdapter;
44 import org.onap.policy.rest.dao.CommonClassDao;
45 import org.onap.policy.rest.jpa.BRMSParamTemplate;
46 import org.onap.policy.rest.jpa.PolicyEntity;
47 import org.onap.policy.xacml.api.XACMLErrorConstants;
48 import org.onap.portalsdk.core.controller.RestrictedBaseController;
49 import org.springframework.beans.factory.annotation.Autowired;
50 import org.springframework.stereotype.Controller;
51 import org.springframework.web.bind.annotation.RequestMapping;
53 import com.fasterxml.jackson.databind.DeserializationFeature;
54 import com.fasterxml.jackson.databind.JsonNode;
55 import com.fasterxml.jackson.databind.ObjectMapper;
57 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AdviceExpressionType;
58 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AdviceExpressionsType;
59 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
60 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
61 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeAssignmentExpressionType;
62 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
63 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
64 import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
65 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
66 import oasis.names.tc.xacml._3_0.core.schema.wd_17.RuleType;
67 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
71 public class CreateBRMSParamController extends RestrictedBaseController {
72 private static final Logger policyLogger = FlexLogger.getLogger(CreateBRMSParamController.class);
74 private static CommonClassDao commonClassDao;
76 public static CommonClassDao getCommonClassDao() {
77 return commonClassDao;
80 public static void setCommonClassDao(CommonClassDao commonClassDao) {
81 CreateBRMSParamController.commonClassDao = commonClassDao;
85 private CreateBRMSParamController(CommonClassDao commonClassDao){
86 CreateBRMSParamController.commonClassDao = commonClassDao;
89 public CreateBRMSParamController(){
92 protected PolicyRestAdapter policyAdapter = null;
94 private HashMap<String, String> dynamicLayoutMap;
96 private static String brmsTemplateVlaue = "<$%BRMSParamTemplate=";
97 private static String string = "String";
100 @RequestMapping(value={"/policyController/getBRMSTemplateData.htm"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
101 public void getBRMSParamPolicyRuleData(HttpServletRequest request, HttpServletResponse response){
103 dynamicLayoutMap = new HashMap<>();
104 ObjectMapper mapper = new ObjectMapper();
105 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
106 JsonNode root = mapper.readTree(request.getReader());
107 String rule = findRule(root.get(PolicyController.getPolicydata()).toString().replaceAll("^\"|\"$", ""));
109 response.setCharacterEncoding(PolicyController.getCharacterencoding());
110 response.setContentType(PolicyController.getContenttype());
111 request.setCharacterEncoding(PolicyController.getCharacterencoding());
113 PrintWriter out = response.getWriter();
114 String responseString = mapper.writeValueAsString(dynamicLayoutMap);
115 JSONObject j = new JSONObject("{policyData: " + responseString + "}");
116 out.write(j.toString());
118 policyLogger.error("Exception Occured while getting BRMS Rule data" , e);
122 protected String findRule(String ruleTemplate) {
123 List<Object> datas = commonClassDao.getDataById(BRMSParamTemplate.class, "ruleName", ruleTemplate);
124 if(datas != null && !datas.isEmpty()){
125 BRMSParamTemplate bRMSParamTemplate = (BRMSParamTemplate) datas.get(0);
126 return bRMSParamTemplate.getRule();
131 protected void generateUI(String rule) {
134 StringBuilder params = new StringBuilder("");
135 Boolean flag = false;
136 Boolean comment = false;
137 String[] lines = rule.split("\n");
138 for(String line : lines){
139 if (line.isEmpty() || line.startsWith("//")) {
142 if (line.startsWith("/*")) {
146 if (line.contains("//")) {
147 line = line.split("\\/\\/")[0];
149 if (line.contains("/*")) {
151 if (line.contains("*/")) {
154 line = line.split("\\/\\*")[0]
155 + line.split("\\*\\/")[1].replace("*/", "");
156 } catch (Exception e) {
157 policyLogger.info("Just for Logging"+e);
158 line = line.split("\\/\\*")[0];
161 line = line.split("\\/\\*")[0];
164 if (line.contains("*/")) {
167 line = line.split("\\*\\/")[1].replace("*/", "");
168 } catch (Exception e) {
169 policyLogger.info("Just for Logging"+e);
179 if (line.contains("declare Params")) {
183 if (line.contains("end") && flag) {
187 params = new StringBuilder(params.toString().replace("declare Params", "").replace("end", "").replaceAll("\\s+", ""));
188 String[] components = params.toString().split(":");
190 for (int i = 0; i < components.length; i++) {
193 caption = components[i];
195 if("".equals(caption)){
198 String nextComponent = "";
200 nextComponent = components[i + 1];
201 } catch (Exception e) {
202 policyLogger.info("Just for Logging"+e);
203 nextComponent = components[i];
205 if (nextComponent.startsWith(string)) {
207 createField(caption, type);
208 caption = nextComponent.replace(string, "");
209 } else if (nextComponent.startsWith("int")) {
211 createField(caption, type);
212 caption = nextComponent.replace("int", "");
215 } catch (Exception e) {
216 policyLogger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + e);
221 private void createField(String caption, String type) {
222 dynamicLayoutMap.put(caption, type);
226 * When the User Click Edit or View Policy the following method will get invoked for setting the data to PolicyRestAdapter.
227 * Which is used to bind the data in GUI
229 public void prePopulateBRMSParamPolicyData(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
230 dynamicLayoutMap = new HashMap<>();
231 if (policyAdapter.getPolicyData() instanceof PolicyType) {
232 PolicyType policy = (PolicyType) policyAdapter.getPolicyData();
233 policyAdapter.setOldPolicyFileName(policyAdapter.getPolicyName());
234 // policy name value is the policy name without any prefix and
236 String policyNameValue = policyAdapter.getPolicyName().substring(policyAdapter.getPolicyName().indexOf("BRMS_Param_") + 11);
237 if (policyLogger.isDebugEnabled()) {
238 policyLogger.debug("Prepopulating form data for BRMS RAW Policy selected:" + policyAdapter.getPolicyName());
240 policyAdapter.setPolicyName(policyNameValue);
241 String description = "";
243 description = policy.getDescription().substring(0, policy.getDescription().indexOf("@CreatedBy:"));
245 policyLogger.info("Just for Logging"+e);
246 description = policy.getDescription();
248 policyAdapter.setPolicyDescription(description);
249 setDataAdapterFromAdviceExpressions(policy, policyAdapter);
250 paramUIGenerate(policyAdapter, entity);
251 // Get the target data under policy.
252 policyAdapter.setDynamicLayoutMap(dynamicLayoutMap);
253 if(policyAdapter.getDynamicLayoutMap().size() > 0){
254 LinkedHashMap<String,String> drlRule = new LinkedHashMap<>();
255 for(Object keyValue: policyAdapter.getDynamicLayoutMap().keySet()){
256 drlRule.put(keyValue.toString(), policyAdapter.getDynamicLayoutMap().get(keyValue));
258 policyAdapter.setRuleData(drlRule);
260 TargetType target = policy.getTarget();
261 if (target != null) {
262 setDataToAdapterFromTarget(target, policyAdapter);
267 private void setDataAdapterFromAdviceExpressions(PolicyType policy, PolicyRestAdapter policyAdapter){
268 ArrayList<Object> attributeList = new ArrayList<>();
270 AdviceExpressionsType expressionTypes = ((RuleType)policy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().get(0)).getAdviceExpressions();
271 for( AdviceExpressionType adviceExpression: expressionTypes.getAdviceExpression()){
272 for(AttributeAssignmentExpressionType attributeAssignment: adviceExpression.getAttributeAssignmentExpression()){
273 if(attributeAssignment.getAttributeId().startsWith("key:")){
274 Map<String, String> attribute = new HashMap<>();
275 String key = attributeAssignment.getAttributeId().replace("key:", "");
276 attribute.put("key", key);
277 @SuppressWarnings("unchecked")
278 JAXBElement<AttributeValueType> attributevalue = (JAXBElement<AttributeValueType>) attributeAssignment.getExpression();
279 String value = (String) attributevalue.getValue().getContent().get(0);
280 attribute.put("value", value);
281 attributeList.add(attribute);
282 }else if(attributeAssignment.getAttributeId().startsWith("dependencies:")){
283 ArrayList<String> dependencies = new ArrayList<>(Arrays.asList(attributeAssignment.getAttributeId().replace("dependencies:", "").split(",")));
284 if(dependencies.contains("")){
285 dependencies.remove("");
287 policyAdapter.setBrmsDependency(dependencies);
288 }else if(attributeAssignment.getAttributeId().startsWith("controller:")){
289 policyAdapter.setBrmsController(attributeAssignment.getAttributeId().replace("controller:", ""));
292 policyAdapter.setAttributes(attributeList);
296 private void setDataToAdapterFromTarget(TargetType target, PolicyRestAdapter policyAdapter){
297 // Under target we have AnyOFType
298 List<AnyOfType> anyOfList = target.getAnyOf();
299 if (anyOfList != null) {
300 Iterator<AnyOfType> iterAnyOf = anyOfList.iterator();
301 while (iterAnyOf.hasNext()) {
302 AnyOfType anyOf = iterAnyOf.next();
303 // Under AnyOFType we have AllOFType
304 List<AllOfType> allOfList = anyOf.getAllOf();
305 if (allOfList != null) {
306 Iterator<AllOfType> iterAllOf = allOfList.iterator();
307 while (iterAllOf.hasNext()) {
308 AllOfType allOf = iterAllOf.next();
309 // Under AllOFType we have Match
310 List<MatchType> matchList = allOf.getMatch();
311 if (matchList != null) {
312 setDataToAdapterFromMatchList(matchList, policyAdapter);
320 private void setDataToAdapterFromMatchList(List<MatchType> matchList, PolicyRestAdapter policyAdapter){
321 Iterator<MatchType> iterMatch = matchList.iterator();
322 while (iterMatch.hasNext()) {
323 MatchType match = iterMatch.next();
325 // Under the match we have attribute value and
326 // attributeDesignator. So,finally down to the actual attribute.
328 AttributeValueType attributeValue = match.getAttributeValue();
329 String value = (String) attributeValue.getContent().get(0);
330 AttributeDesignatorType designator = match.getAttributeDesignator();
331 String attributeId = designator.getAttributeId();
333 if ("RiskType".equals(attributeId)){
334 policyAdapter.setRiskType(value);
336 if ("RiskLevel".equals(attributeId)){
337 policyAdapter.setRiskLevel(value);
339 if ("guard".equals(attributeId)){
340 policyAdapter.setGuard(value);
342 if ("TTLDate".equals(attributeId) && !value.contains("NA")){
343 PolicyController controller = new PolicyController();
344 String newDate = controller.convertDate(value);
345 policyAdapter.setTtlDate(newDate);
350 // This method generates the UI from rule configuration
351 public void paramUIGenerate(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
352 String data = entity.getConfigurationData().getConfigBody();
355 StringBuilder params = new StringBuilder("");
356 Boolean flag = false;
357 Boolean comment = false;
358 for (String line : data.split("\n")) {
359 if (line.isEmpty() || line.startsWith("//")) {
362 if(line.contains(brmsTemplateVlaue)){
363 String value = line.substring(line.indexOf("<$%"),line.indexOf("%$>"));
364 value = value.replace(brmsTemplateVlaue, "");
365 policyAdapter.setRuleName(value);
367 if(line.contains("<%$Values=")) {
368 String value = line.substring(line.indexOf("<%$"), line.indexOf("$%>"));
369 value = value.replaceAll("<%\\$Values=", "");
370 for( String keyValue : value.split(":\\|:")) {
371 String[] pair = keyValue.split(":-:");
372 if (pair != null && pair.length > 0) {
373 dynamicLayoutMap.put(pair[0], (pair.length > 1) ? pair[1] : "");
378 if (line.startsWith("/*")) {
382 if ((line.contains("//"))&&(!(line.contains("http://") || line.contains("https://")))){
383 line = line.split("\\/\\/")[0];
385 if (line.contains("/*")) {
387 if (line.contains("*/")) {
390 line = line.split("\\/\\*")[0]
391 + line.split("\\*\\/")[1].replace(
393 } catch (Exception e) {
394 policyLogger.info("Just for Logging"+e);
395 line = line.split("\\/\\*")[0];
398 line = line.split("\\/\\*")[0];
401 if (line.contains("*/")) {
404 line = line.split("\\*\\/")[1]
406 } catch (Exception e) {
407 policyLogger.info("Just for Logging"+e);
417 if (line.contains("rule") && line.contains(".Params\"")) {
421 if (line.contains("end") && flag) {
425 params = new StringBuilder(params.substring(params.indexOf(".Params\"")+ 11));
426 params = new StringBuilder(params.toString().replaceAll("\\s+", "").replace("salience1000whenthenParamsparams=newParams();","")
427 .replace("insert(params);end", "")
428 .replace("params.set", ""));
429 String[] components = params.toString().split("\\);");
430 if(components!= null && components.length > 0){
431 for (int i = 0; i < components.length; i++) {
433 components[i] = components[i]+")";
434 String caption = components[i].substring(0,
435 components[i].indexOf('('));
436 caption = caption.substring(0, 1).toLowerCase() + caption.substring(1);
437 if (components[i].contains("(\"")) {
438 value = components[i]
439 .substring(components[i].indexOf("(\""),
440 components[i].indexOf("\")"))
441 .replace("(\"", "").replace("\")", "");
443 value = components[i]
444 .substring(components[i].indexOf('('),
445 components[i].indexOf(')'))
446 .replace("(", "").replace(")", "");
448 dynamicLayoutMap.put(caption, value);
452 } catch (Exception e) {
453 policyLogger.error(XACMLErrorConstants.ERROR_DATA_ISSUE + e.getMessage() + e);
460 @SuppressWarnings("unchecked")
461 @RequestMapping(value={"/policyController/ViewBRMSParamPolicyRule.htm"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
462 public void setViewRule(HttpServletRequest request, HttpServletResponse response){
464 ObjectMapper mapper = new ObjectMapper();
465 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
466 JsonNode root = mapper.readTree(request.getReader());
467 PolicyRestAdapter policyData = mapper.readValue(root.get(PolicyController.getPolicydata()).get("policy").toString(), PolicyRestAdapter.class);
468 policyData.setDomainDir(root.get(PolicyController.getPolicydata()).get("model").get("name").toString().replace("\"", ""));
469 if(root.get(PolicyController.getPolicydata()).get("model").get("type").toString().replace("\"", "").equals(PolicyController.getFile())){
470 policyData.setEditPolicy(true);
474 body = findRule((String) policyData.getRuleName()) + "\n";
475 StringBuilder generatedMetadata = new StringBuilder();
476 generatedMetadata.append("/* Autogenerated Code Please Don't change/remove this comment section. This is for the UI purpose. \n\t " +
477 brmsTemplateVlaue + policyData.getRuleName() + "%$> \n */ \n");
479 if(policyData.getDynamicLayoutMap().size() > 0){
480 generatedMetadata.append("/* <%$Values=");
481 for (Entry<?, ?> entry : policyData.getRuleData().entrySet()) {
482 String uiKey = (String) entry.getKey();
483 if(!"templateName".equals(uiKey)) {
484 generatedMetadata.append(uiKey+":-:"+entry.getValue()+":|:");
487 generatedMetadata.append("$%> \n*/ \n");
489 policyLogger.info("Metadata generated with :" + generatedMetadata.toString());
490 body = generatedMetadata.toString() + body;
492 Map<String,String> copyMap=new HashMap<>();
493 copyMap.putAll((Map<? extends String, ? extends String>) policyData.getRuleData());
494 copyMap.put("policyName", policyData.getDomainDir().replace("\\", ".") +".Config_BRMS_Param_" + policyData.getPolicyName());
495 copyMap.put("policyScope", policyData.getDomainDir().replace("\\", "."));
496 copyMap.put("policyVersion", "1");
497 //Finding all the keys in the Map data-structure.
498 Set<String> keySet= copyMap.keySet();
499 Iterator<String> iterator = keySet.iterator();
502 while(iterator.hasNext()) {
503 //Converting the first character of the key into a lower case.
504 String input= iterator.next();
505 String output = Character.toLowerCase(input.charAt(0)) +
506 (input.length() > 1 ? input.substring(1) : "");
507 //Searching for a pattern in the String using the key.
508 p=Pattern.compile("\\$\\{"+output+"\\}");
510 //Replacing the value with the inputs provided by the user in the editor.
511 body=m.replaceAll(copyMap.get(input));
513 response.setCharacterEncoding("UTF-8");
514 response.setContentType("application / json");
515 request.setCharacterEncoding("UTF-8");
517 PrintWriter out = response.getWriter();
518 String responseString = mapper.writeValueAsString(body);
519 JSONObject j = new JSONObject("{policyData: " + responseString + "}");
520 out.write(j.toString());
521 } catch (Exception e) {
522 policyLogger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);