Policy 1707 commit to LF
[policy/engine.git] / ECOMP-PAP-REST / src / main / java / org / openecomp / policy / pap / xacml / rest / components / FirewallConfigPolicy.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP-PAP-REST
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.policy.pap.xacml.rest.components;
22
23 import java.io.BufferedWriter;
24 import java.io.File;
25 import java.io.FileWriter;
26 import java.io.IOException;
27 import java.io.StringReader;
28 import java.net.URI;
29 import java.net.URISyntaxException;
30 import java.nio.charset.Charset;
31 import java.nio.file.Files;
32 import java.nio.file.Path;
33 import java.nio.file.Paths;
34 import java.sql.Connection;
35 import java.sql.DriverManager;
36 import java.sql.ResultSet;
37 import java.sql.SQLException;
38 import java.sql.Statement;
39 import java.util.HashMap;
40 import java.util.List;
41 import java.util.Map;
42
43 import javax.json.Json;
44 import javax.json.JsonArray;
45 import javax.json.JsonObject;
46 import javax.json.JsonReader;
47 import javax.persistence.EntityManager;
48 import javax.persistence.Query;
49
50 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AdviceExpressionType;
51 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AdviceExpressionsType;
52 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
53 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
54 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeAssignmentExpressionType;
55 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
56 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
57 import oasis.names.tc.xacml._3_0.core.schema.wd_17.EffectType;
58 import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
59 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObjectFactory;
60 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
61 import oasis.names.tc.xacml._3_0.core.schema.wd_17.RuleType;
62 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
63
64 import org.apache.commons.io.FilenameUtils;
65 import org.openecomp.policy.common.logging.eelf.MessageCodes;
66 import org.openecomp.policy.common.logging.eelf.PolicyLogger;
67 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
68 import org.openecomp.policy.common.logging.flexlogger.Logger;
69 import org.openecomp.policy.pap.xacml.rest.XACMLPapServlet;
70 import org.openecomp.policy.rest.XACMLRestProperties;
71 import org.openecomp.policy.rest.adapter.PolicyRestAdapter;
72 import org.openecomp.policy.rest.jpa.PolicyEntity;
73
74 import com.att.research.xacml.std.IdentifierImpl;
75 import com.att.research.xacml.util.XACMLProperties;
76 import com.fasterxml.jackson.databind.JsonNode;
77 import com.github.fge.jackson.JsonLoader;
78 import com.github.fge.jsonpatch.JsonPatch;
79 import com.github.fge.jsonpatch.diff.JsonDiff;
80
81 public class FirewallConfigPolicy extends Policy {
82
83         private static final Logger LOGGER = FlexLogger.getLogger(FirewallConfigPolicy.class);
84         
85         /*
86          * These are the parameters needed for DB access from the PAP
87          */
88         private static String papDbDriver = null;
89         private static String papDbUrl = null;
90         private static String papDbUser = null;
91         private static String papDbPassword = null;
92
93         public FirewallConfigPolicy() {
94                 super();
95         }
96         
97         public FirewallConfigPolicy(PolicyRestAdapter policyAdapter) {
98                 this.policyAdapter = policyAdapter;
99                 this.policyAdapter.setConfigType(policyAdapter.getConfigType());
100                 
101         }
102         
103         // Saving the Configurations file at server location for config policy.
104         protected void saveConfigurations(String policyName, String jsonBody) {
105                 try{
106                         if(policyName.endsWith(".xml")){
107                                 policyName = policyName.replace(".xml", "");
108                         }
109                         FileWriter fw = new FileWriter(CONFIG_HOME + File.separator + policyName + ".json");
110                         BufferedWriter bw = new BufferedWriter(fw);
111                         bw.write(jsonBody);
112                         bw.close();
113                         if (LOGGER.isDebugEnabled()) {
114                                 LOGGER.debug("Configuration is succesfully saved");
115                         }
116                 } catch (IOException e) {
117                         e.printStackTrace();
118                 }
119         }
120         
121         //Utility to read json data from the existing file to a string
122         static String readFile(String path, Charset encoding) throws IOException {
123                 
124                 byte[] encoded = Files.readAllBytes(Paths.get(path));
125                 return new String(encoded, encoding);
126                 
127         }
128         
129         // Validations for Config form
130         public boolean validateConfigForm() {
131
132                 // Validating mandatory Fields.
133                 isValidForm = true;     
134                 return isValidForm;
135
136         }
137
138         @Override
139         public Map<String, String> savePolicies() throws Exception {
140
141                 Map<String, String> successMap = new HashMap<String,String>();
142                 if(isPolicyExists()){
143                         successMap.put("EXISTS", "This Policy already exist on the PAP");
144                         return successMap;
145                 }
146
147                 if(!isPreparedToSave()){
148                         prepareToSave();
149                 }
150
151                 // Until here we prepared the data and here calling the method to create xml.
152                 Path newPolicyPath = null;
153                 newPolicyPath = Paths.get(policyAdapter.getNewFileName());
154                 Boolean dbIsUpdated = false;
155                 if (policyAdapter.getApiflag() != null && policyAdapter.getApiflag().equalsIgnoreCase("admin")){
156                         if (policyAdapter.isEditPolicy()) {
157                                 dbIsUpdated = updateFirewallDictionaryData(policyAdapter.getJsonBody(), policyAdapter.getPrevJsonBody());
158                         } else {
159                                 dbIsUpdated = insertFirewallDicionaryData(policyAdapter.getJsonBody());
160                         }
161                 } else {
162                         dbIsUpdated = true;
163                 }
164
165                 if(dbIsUpdated) {
166                         successMap = createPolicy(newPolicyPath,getCorrectPolicyDataObject());  
167                 } else {
168                         PolicyLogger.error("Failed to Update the Database Dictionary Tables.");
169
170                         //remove the new json file 
171                         String jsonBody = policyAdapter.getPrevJsonBody();
172                         if (jsonBody!=null){
173                                 saveConfigurations(policyName, jsonBody);
174                         } else {
175                                 saveConfigurations(policyName, "");
176                         }
177                         successMap.put("fwdberror", "DB UPDATE");
178                 }
179         
180                 return successMap;              
181         }
182
183         //This is the method for preparing the policy for saving.  We have broken it out
184         //separately because the fully configured policy is used for multiple things
185         @Override
186         public boolean prepareToSave() throws Exception{
187                 
188                 if(isPreparedToSave()){
189                         //we have already done this
190                         return true;
191                 }
192                 
193                 int version = 0;
194                 String policyID = policyAdapter.getPolicyID();
195                 version = policyAdapter.getHighestVersion();
196                 
197                 // Create the Instance for pojo, PolicyType object is used in marshalling.
198                 if (policyAdapter.getPolicyType().equals("Config")) {
199                         PolicyType policyConfig = new PolicyType();
200
201                         policyConfig.setVersion(Integer.toString(version));
202                         policyConfig.setPolicyId(policyID);
203                         policyConfig.setTarget(new TargetType());
204                         policyAdapter.setData(policyConfig);
205                 }
206                 policyName = policyAdapter.getNewFileName();
207                 
208                 //String oldPolicyName = policyName.replace(".xml", "");
209                 String scope = policyName.substring(0, policyName.indexOf("."));
210                 String dbPolicyName = policyName.substring(policyName.indexOf(".")+1).replace(".xml", "");
211                 
212                 int oldversion = Integer.parseInt(dbPolicyName.substring(dbPolicyName.lastIndexOf(".")+1));
213                 dbPolicyName = dbPolicyName.substring(0, dbPolicyName.lastIndexOf(".")+1);
214                 //String scope = oldPolicyName.substring(0, oldPolicyName.lastIndexOf("."));
215                 //scope = scope.substring(0, scope.lastIndexOf("."));
216                 if(oldversion > 1){
217                         oldversion = oldversion - 1; 
218                         dbPolicyName = dbPolicyName + oldversion + ".xml";
219                 }
220                 EntityManager em = XACMLPapServlet.getEmf().createEntityManager();
221                 Query createPolicyQuery = em.createQuery("SELECT p FROM PolicyEntity p WHERE p.scope=:scope AND p.policyName=:policyName");                     
222                 createPolicyQuery.setParameter("scope", scope);
223                 createPolicyQuery.setParameter("policyName", dbPolicyName);
224                 List<?> createPolicyQueryList = createPolicyQuery.getResultList();
225                 if(!createPolicyQueryList.isEmpty()){
226                         PolicyEntity entitydata = (PolicyEntity) createPolicyQueryList.get(0);
227                         policyAdapter.setPrevJsonBody(entitydata.getConfigurationData().getConfigBody());
228                 }
229                 em.close();
230                 if (policyAdapter.getData() != null) {
231                         String jsonBody = policyAdapter.getJsonBody();
232                         saveConfigurations(policyName, jsonBody);
233                         
234                         // Make sure the filename ends with an extension
235                         if (policyName.endsWith(".xml") == false) {
236                                 policyName = policyName + ".xml";
237                         }
238
239                         PolicyType configPolicy = (PolicyType) policyAdapter.getData();
240                         
241                         configPolicy.setDescription(policyAdapter.getPolicyDescription());
242
243                         configPolicy.setRuleCombiningAlgId(policyAdapter.getRuleCombiningAlgId());
244
245                         AllOfType allOfOne = new AllOfType();
246                         String fileName = policyAdapter.getNewFileName();
247                         String name = fileName.substring(fileName.lastIndexOf("\\") + 1, fileName.length());
248                         if ((name == null) || (name.equals(""))) {
249                                 name = fileName.substring(fileName.lastIndexOf("/") + 1, fileName.length());
250                         }
251                         allOfOne.getMatch().add(createMatch("PolicyName", name));
252                         AllOfType allOf = new AllOfType();
253                         
254                         // Match for ConfigName
255                         allOf.getMatch().add(createMatch("ConfigName", policyAdapter.getConfigName()));
256                         // Match for riskType
257                         allOf.getMatch().add(
258                                         createDynamicMatch("RiskType", policyAdapter.getRiskType()));
259                         // Match for riskLevel
260                         allOf.getMatch().add(
261                                         createDynamicMatch("RiskLevel", String.valueOf(policyAdapter.getRiskLevel())));
262                         // Match for riskguard
263                         allOf.getMatch().add(
264                                         createDynamicMatch("guard", policyAdapter.getGuard()));
265                         // Match for ttlDate
266                         allOf.getMatch().add(
267                                         createDynamicMatch("TTLDate", policyAdapter.getTtlDate()));
268                         AnyOfType anyOf = new AnyOfType();
269                         anyOf.getAllOf().add(allOfOne);
270                         anyOf.getAllOf().add(allOf);
271
272                         TargetType target = new TargetType();
273                         ((TargetType) target).getAnyOf().add(anyOf);
274                         
275                         // Adding the target to the policy element
276                         configPolicy.setTarget((TargetType) target);
277
278                         RuleType rule = new RuleType();
279                         rule.setRuleId(policyAdapter.getRuleID());
280                         
281                         rule.setEffect(EffectType.PERMIT);
282                         
283                         // Create Target in Rule
284                         AllOfType allOfInRule = new AllOfType();
285                         
286                         // Creating match for ACCESS in rule target
287                         MatchType accessMatch = new MatchType();
288                         AttributeValueType accessAttributeValue = new AttributeValueType();
289                         accessAttributeValue.setDataType(STRING_DATATYPE);
290                         accessAttributeValue.getContent().add("ACCESS");
291                         accessMatch.setAttributeValue(accessAttributeValue);
292                         AttributeDesignatorType accessAttributeDesignator = new AttributeDesignatorType();
293                         URI accessURI = null;
294                         try {
295                                 accessURI = new URI(ACTION_ID);
296                         } catch (URISyntaxException e) {
297                                 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "FirewallConfigPolicy", "Exception creating ACCESS URI");
298                         }
299                         accessAttributeDesignator.setCategory(CATEGORY_ACTION);
300                         accessAttributeDesignator.setDataType(STRING_DATATYPE);
301                         accessAttributeDesignator.setAttributeId(new IdentifierImpl(accessURI).stringValue());
302                         accessMatch.setAttributeDesignator(accessAttributeDesignator);
303                         accessMatch.setMatchId(FUNCTION_STRING_EQUAL_IGNORE);
304                         
305                         // Creating Config Match in rule Target
306                         MatchType configMatch = new MatchType();
307                         AttributeValueType configAttributeValue = new AttributeValueType();
308                         configAttributeValue.setDataType(STRING_DATATYPE);
309                         
310                         configAttributeValue.getContent().add("Config");
311                         
312                         configMatch.setAttributeValue(configAttributeValue);
313                         AttributeDesignatorType configAttributeDesignator = new AttributeDesignatorType();
314                         URI configURI = null;
315                         try {
316                                 configURI = new URI(RESOURCE_ID);
317                         } catch (URISyntaxException e) {
318                                 PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "FirewallConfigPolicy", "Exception creating Config URI");
319                         }
320                         
321                         configAttributeDesignator.setCategory(CATEGORY_RESOURCE);
322                         configAttributeDesignator.setDataType(STRING_DATATYPE);
323                         configAttributeDesignator.setAttributeId(new IdentifierImpl(configURI).stringValue());
324                         configMatch.setAttributeDesignator(configAttributeDesignator);
325                         configMatch.setMatchId(FUNCTION_STRING_EQUAL_IGNORE);
326
327                         allOfInRule.getMatch().add(accessMatch);
328                         allOfInRule.getMatch().add(configMatch);
329
330                         AnyOfType anyOfInRule = new AnyOfType();
331                         anyOfInRule.getAllOf().add(allOfInRule);
332                         
333                         TargetType targetInRule = new TargetType();
334                         targetInRule.getAnyOf().add(anyOfInRule);
335
336                         rule.setTarget(targetInRule);
337                         rule.setAdviceExpressions(getAdviceExpressions(version, policyName));
338
339                         configPolicy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().add(rule);
340                         policyAdapter.setPolicyData(configPolicy);
341
342                 } else {
343                         PolicyLogger.error("Unsupported data object." + policyAdapter.getData().getClass().getCanonicalName());
344                 }
345                 setPreparedToSave(true);
346                 return true;
347         }
348
349         // Data required for Advice part is setting here.
350         private AdviceExpressionsType getAdviceExpressions(int version, String fileName) {
351                 
352                 //Firewall Config ID Assignment
353                 AdviceExpressionsType advices = new AdviceExpressionsType();
354                 AdviceExpressionType advice = new AdviceExpressionType();
355                 advice.setAdviceId("firewallConfigID");
356                 advice.setAppliesTo(EffectType.PERMIT);
357                 // For Configuration
358                 AttributeAssignmentExpressionType assignment1 = new AttributeAssignmentExpressionType();
359                 assignment1.setAttributeId("type");
360                 assignment1.setCategory(CATEGORY_RESOURCE);
361                 assignment1.setIssuer("");
362                 AttributeValueType configNameAttributeValue = new AttributeValueType();
363                 configNameAttributeValue.setDataType(STRING_DATATYPE);
364                 configNameAttributeValue.getContent().add("Configuration");
365                 assignment1.setExpression(new ObjectFactory().createAttributeValue(configNameAttributeValue));
366                 advice.getAttributeAssignmentExpression().add(assignment1);
367                 
368                 // For Config file Url if configurations are provided.
369                 //URL ID Assignment
370                 AttributeAssignmentExpressionType assignment2 = new AttributeAssignmentExpressionType();
371                 assignment2.setAttributeId("URLID");
372                 assignment2.setCategory(CATEGORY_RESOURCE);
373                 assignment2.setIssuer("");
374                 AttributeValueType AttributeValue = new AttributeValueType();
375                 AttributeValue.setDataType(URI_DATATYPE);       
376                 String content = CONFIG_URL + "/Config/" + policyName + ".json";
377
378                 AttributeValue.getContent().add(content);
379                 assignment2.setExpression(new ObjectFactory().createAttributeValue(AttributeValue));
380                 advice.getAttributeAssignmentExpression().add(assignment2);
381                 
382                 //Policy Name Assignment
383                 AttributeAssignmentExpressionType assignment3 = new AttributeAssignmentExpressionType();
384                 assignment3.setAttributeId("PolicyName");
385                 assignment3.setCategory(CATEGORY_RESOURCE);
386                 assignment3.setIssuer("");
387                 AttributeValueType attributeValue3 = new AttributeValueType();
388                 attributeValue3.setDataType(STRING_DATATYPE);
389                 fileName = FilenameUtils.removeExtension(fileName);
390                 fileName = fileName + ".xml";
391                 String name = fileName.substring(fileName.lastIndexOf("\\") + 1, fileName.length());
392                 if ((name == null) || (name.equals(""))) {
393                         name = fileName.substring(fileName.lastIndexOf("/") + 1, fileName.length());
394                 }
395                 attributeValue3.getContent().add(name);
396                 assignment3.setExpression(new ObjectFactory().createAttributeValue(attributeValue3));
397                 advice.getAttributeAssignmentExpression().add(assignment3);
398                 
399                 //Version Number Assignment
400                 AttributeAssignmentExpressionType assignment4 = new AttributeAssignmentExpressionType();
401                 assignment4.setAttributeId("VersionNumber");
402                 assignment4.setCategory(CATEGORY_RESOURCE);
403                 assignment4.setIssuer("");
404                 AttributeValueType configNameAttributeValue4 = new AttributeValueType();
405                 configNameAttributeValue4.setDataType(STRING_DATATYPE);
406                 configNameAttributeValue4.getContent().add(Integer.toString(version));
407                 assignment4.setExpression(new ObjectFactory().createAttributeValue(configNameAttributeValue4));
408                 advice.getAttributeAssignmentExpression().add(assignment4);
409                 
410                 //Ecomp Name Assignment
411                 AttributeAssignmentExpressionType assignment5 = new AttributeAssignmentExpressionType();
412                 assignment5.setAttributeId("matching:" + ECOMPID);
413                 assignment5.setCategory(CATEGORY_RESOURCE);
414                 assignment5.setIssuer("");
415                 AttributeValueType configNameAttributeValue5 = new AttributeValueType();
416                 configNameAttributeValue5.setDataType(STRING_DATATYPE);
417                 assignment5.setExpression(new ObjectFactory().createAttributeValue(configNameAttributeValue5));
418                 advice.getAttributeAssignmentExpression().add(assignment5);
419                 
420                 //Config Name Assignment
421                 AttributeAssignmentExpressionType assignment6 = new AttributeAssignmentExpressionType();
422                 assignment6.setAttributeId("matching:" + CONFIGID);
423                 assignment6.setCategory(CATEGORY_RESOURCE);
424                 assignment6.setIssuer("");
425                 AttributeValueType configNameAttributeValue6 = new AttributeValueType();
426                 configNameAttributeValue6.setDataType(STRING_DATATYPE);
427                 configNameAttributeValue6.getContent().add(policyAdapter.getConfigName());
428                 assignment6.setExpression(new ObjectFactory().createAttributeValue(configNameAttributeValue6));
429                 advice.getAttributeAssignmentExpression().add(assignment6);
430
431                 //Risk Attributes
432                 AttributeAssignmentExpressionType assignment7 = new AttributeAssignmentExpressionType();
433                 assignment7.setAttributeId("RiskType");
434                 assignment7.setCategory(CATEGORY_RESOURCE);
435                 assignment7.setIssuer("");
436
437                 AttributeValueType configNameAttributeValue7 = new AttributeValueType();
438                 configNameAttributeValue7.setDataType(STRING_DATATYPE);
439                 configNameAttributeValue7.getContent().add(policyAdapter.getRiskType());
440                 assignment7.setExpression(new ObjectFactory().createAttributeValue(configNameAttributeValue7));
441
442                 advice.getAttributeAssignmentExpression().add(assignment7);
443                                 
444                 AttributeAssignmentExpressionType assignment8 = new AttributeAssignmentExpressionType();
445                 assignment8.setAttributeId("RiskLevel");
446                 assignment8.setCategory(CATEGORY_RESOURCE);
447                 assignment8.setIssuer("");
448
449                 AttributeValueType configNameAttributeValue8 = new AttributeValueType();
450                 configNameAttributeValue8.setDataType(STRING_DATATYPE);
451                 configNameAttributeValue8.getContent().add(policyAdapter.getRiskLevel());
452                 assignment8.setExpression(new ObjectFactory().createAttributeValue(configNameAttributeValue8));
453
454                 advice.getAttributeAssignmentExpression().add(assignment8);     
455
456                 AttributeAssignmentExpressionType assignment9 = new AttributeAssignmentExpressionType();
457                 assignment9.setAttributeId("guard");
458                 assignment9.setCategory(CATEGORY_RESOURCE);
459                 assignment9.setIssuer("");
460
461                 AttributeValueType configNameAttributeValue9 = new AttributeValueType();
462                 configNameAttributeValue9.setDataType(STRING_DATATYPE);
463                 configNameAttributeValue9.getContent().add(policyAdapter.getGuard());
464                 assignment9.setExpression(new ObjectFactory().createAttributeValue(configNameAttributeValue9));
465
466                 advice.getAttributeAssignmentExpression().add(assignment9);
467                                 
468                 AttributeAssignmentExpressionType assignment10 = new AttributeAssignmentExpressionType();
469                 assignment10.setAttributeId("TTLDate");
470                 assignment10.setCategory(CATEGORY_RESOURCE);
471                 assignment10.setIssuer("");
472
473                 AttributeValueType configNameAttributeValue10 = new AttributeValueType();
474                 configNameAttributeValue10.setDataType(STRING_DATATYPE);
475                 configNameAttributeValue10.getContent().add(policyAdapter.getTtlDate());
476                 assignment10.setExpression(new ObjectFactory().createAttributeValue(configNameAttributeValue10));
477
478                 advice.getAttributeAssignmentExpression().add(assignment10);
479                 advices.getAdviceExpression().add(advice);
480                 return advices;
481         }
482         
483         
484         private Boolean insertFirewallDicionaryData (String jsonBody) throws SQLException {
485
486                 
487                 JsonObject json = null;
488                 if (jsonBody != null) {
489                         
490                         //Read jsonBody to JsonObject
491                         json = stringToJson(jsonBody);
492                         
493                         JsonArray firewallRules = null;
494                         JsonArray serviceGroup = null;
495                         JsonArray addressGroup = null;
496                         
497                         Connection con = null;
498                         Statement st = null;
499                         ResultSet rs = null;
500                         
501                         /*
502                          * Retrieve the property values for db access from the xacml.pap.properties
503                          */
504                         papDbDriver = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_DB_DRIVER);
505                         papDbUrl = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_DB_URL);
506                         papDbUser = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_DB_USER);
507                         papDbPassword = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_DB_PASSWORD);
508
509                         //insert data into tables
510                         try {
511                                 //Get DB Connection
512                                 Class.forName(papDbDriver);
513                                 con = DriverManager.getConnection(papDbUrl,papDbUser,papDbPassword);
514                                 st = con.createStatement();
515                                 
516                                 firewallRules = json.getJsonArray("firewallRuleList");
517                                 serviceGroup = json.getJsonArray("serviceGroups");
518                                 addressGroup = json.getJsonArray("addressGroups");      
519                                 
520                                 String insertQuery = null;
521                                 
522                                 /*
523                                  * Inserting firewallRuleList data into the Terms, SecurityZone, and Action tables
524                                  */
525                                 if (firewallRules != null) {
526                                         
527                                         int termID = 0;
528                                         int zoneID = 0;
529                                         int actionID = 0;
530                                         
531                                         rs = st.executeQuery("SELECT MAX(ID) AS ID FROM TERM;");
532                                         if(rs.next()){
533                                                 termID = rs.getInt("ID");
534                                         }
535                                         rs.close();
536                                         
537                                         rs = st.executeQuery("SELECT MAX(ID) AS ID FROM ZONE;");
538                                         if(rs.next()){
539                                                 zoneID = rs.getInt("ID");
540                                         }
541                                         rs.close();
542                                         
543                                         rs = st.executeQuery("SELECT MAX(ID) AS ID FROM ACTIONLIST;");
544                                         if(rs.next()){
545                                                 actionID = rs.getInt("ID");
546                                         }
547                                         rs.close();
548                                         for(int i = 0;i<firewallRules.size();i++) {
549                                                 
550                                                 //increment ID Primary Keys
551                                                 termID = termID + 1;
552                                                 zoneID = zoneID + 1;
553                                                 actionID = actionID + 1;
554                                                 
555                                                 /*
556                                                  * Populate ArrayLists with values from the JSON
557                                                  */
558                                                 //create the JSON object from the JSON Array for each iteration through the for loop
559                                                 JsonObject ruleListobj = firewallRules.getJsonObject(i);
560                                                                                                 
561                                                 //get values from JSON fields of firewallRulesList Array
562                                                 String ruleName = ruleListobj.get("ruleName").toString();
563                                                 String action = ruleListobj.get("action").toString();
564                                                 String description = ruleListobj.get("description").toString();
565                                                 
566                                                 rs = st.executeQuery("SELECT * FROM TERM WHERE TERMNAME = "+ ruleName + ";");
567                                                 
568                                                 if (rs.next()) {
569                                                         st.executeUpdate("DELETE FROM TERM WHERE TERMNAME = "+ ruleName + ";");
570                                                 }
571                                                 rs.close();
572                                                 
573                                                 //getting fromZone Array field from the firewallRulesList
574                                                 JsonArray fromZoneArray = ruleListobj.getJsonArray("fromZones");
575                                                 String fromZoneString = null;
576                                                 
577                                                 
578                                                 for (int fromZoneIndex = 0;fromZoneIndex<fromZoneArray.size(); fromZoneIndex++) {
579                                                         String value = fromZoneArray.get(fromZoneIndex).toString();
580                                                         value = value.replace("\"", "");
581                                                         
582                                                         if (fromZoneString != null) {
583                                                                 fromZoneString = fromZoneString.concat(",").concat(value);
584                                                                 
585                                                         } else {
586                                                                 fromZoneString = value;
587                                                         }
588                                                         
589                                                 }
590                                                 String fromZoneInsert = "'"+fromZoneString+"'";
591                                                 
592                                                 //getting toZone Array field from the firewallRulesList
593                                                 JsonArray toZoneArray = ruleListobj.getJsonArray("toZones");
594                                                 String toZoneString = null;
595                                                 for (int toZoneIndex = 0; toZoneIndex<toZoneArray.size(); toZoneIndex++) {
596                                                         String value = toZoneArray.get(toZoneIndex).toString();
597                                                         value = value.replace("\"", "");
598                                                         
599                                                         if (toZoneString != null) {
600                                                                 toZoneString = toZoneString.concat(",").concat(value);
601                                                                 
602                                                         } else {
603                                                                 toZoneString = value;
604                                                         }
605                                                         
606                                                 }
607                                                 String toZoneInsert = "'"+toZoneString+"'";
608                                                 
609                                                 //getting sourceList Array fields from the firewallRulesList
610                                                 JsonArray srcListArray = ruleListobj.getJsonArray("sourceList");
611                                                 String srcListString = null;
612                                                 for (int srcListIndex = 0; srcListIndex< srcListArray.size(); srcListIndex++) {
613                                                         JsonObject srcListObj = srcListArray.getJsonObject(srcListIndex);
614                                                         String type = srcListObj.get("type").toString().replace("\"", "");
615                                                         
616                                                         String value = null;
617                                                         if(type.equals("REFERENCE")||type.equals("GROUP")){
618                                                                 value = srcListObj.get("name").toString();
619                                                         } else if (type.equalsIgnoreCase("ANY")){
620                                                                 value = null;
621                                                         } else {
622                                                                 value = srcListObj.get("value").toString();
623                                                         }
624                                                         
625                                                         if (value!=null){
626                                                                 value = value.replace("\"", "");
627                                                         }
628                                                         
629                                                         if (srcListString != null) {
630                                                                 srcListString = srcListString.concat(",").concat(value);
631                                                                 
632                                                         } else {
633                                                                 srcListString = value;
634                                                         }
635                                                         
636                                                 }
637                                                 String srcListInsert = "'"+srcListString+"'";
638                                                 
639                                                 //getting destinationList Array fields from the firewallRulesList
640                                                 JsonArray destListArray = ruleListobj.getJsonArray("destinationList");
641                                                 String destListString = null;
642                                                 for (int destListIndex = 0; destListIndex <destListArray.size(); destListIndex++) {
643                                                         JsonObject destListObj = destListArray.getJsonObject(destListIndex);
644                                                         String type = destListObj.get("type").toString().replace("\"", "");
645                                                         
646                                                         String value = null;
647                                                         if(type.equals("REFERENCE")||type.equals("GROUP")){
648                                                                 value = destListObj.get("name").toString();
649                                                         } else if (type.equalsIgnoreCase("ANY")){
650                                                                 value = null;
651                                                         } else {
652                                                                 value = destListObj.get("value").toString();
653                                                         }
654                                                         
655                                                         if (value!=null){
656                                                                 value = value.replace("\"", "");
657                                                         }
658                                                         
659                                                         if (destListString != null) {
660                                                                 destListString = destListString.concat(",").concat(value);
661                                                         } else {
662                                                                 destListString = value;
663                                                         }
664                                                 }
665                                                 String destListInsert = "'"+destListString+"'";
666                                                 
667                                                 //getting destServices Array fields from the firewallRulesList
668                                                 JsonArray destServicesArray = ruleListobj.getJsonArray("destServices");
669                                                 String destPortListString = null;
670                                                 for (int destPortListIndex = 0; destPortListIndex < destServicesArray.size(); destPortListIndex++) {
671                                                         JsonObject destServicesObj = destServicesArray.getJsonObject(destPortListIndex);
672                                                         String type = destServicesObj.get("type").toString().replace("\"", "");
673                                                         
674                                                         String value = null;
675                                                         if(type.equals("REFERENCE")||type.equals("GROUP")){
676                                                                 value = destServicesObj.get("name").toString();
677                                                         } else if (type.equalsIgnoreCase("ANY")){
678                                                                 value = null;
679                                                         } else {
680                                                                 value = destServicesObj.get("value").toString();
681                                                         }
682                                                         
683                                                         if (value!=null){
684                                                                 value = value.replace("\"", "");
685                                                         }
686                                                         
687                                                         if (destPortListString != null) {
688                                                                 destPortListString = destPortListString.concat(",").concat(value);
689                                                         } else {
690                                                                 destPortListString = value;
691                                                         }
692                                                 }
693                                                 String destPortListInsert = "'"+destPortListString+"'";                                 
694                                                 
695                                                 /*
696                                                  * Create Queries to INSERT data into database tables and execute 
697                                                  */                                     
698                         String termSql = "INSERT INTO Term (ID, TERMNAME, SRCIPLIST, DESTIPLIST, PROTOCOLLIST, PORTLIST, SRCPORTLIST,"
699                                  + " DESTPORTLIST, ACTION, DESCRIPTION, FROMZONE, TOZONE, CREATED_BY, MODIFIED_DATE) VALUES ("+termID+","
700                                  +ruleName+","+srcListInsert+","+destListInsert+","+ "null"+","+"null"+","+"null"+","+destPortListInsert+","
701                                  +action+","+description+","+fromZoneInsert+","+toZoneInsert+",'API',"+ "null"+ "); ";
702                                                 termSql = termSql.replace('"', '\'');
703                                                 st.addBatch(termSql);
704
705                                                 String actionSql = "INSERT INTO ACTIONLIST (ID, ACTIONNAME, DESCRIPTION) VALUES ("+actionID+","+action+","+action+"); ";
706                                                 actionSql = actionSql.replace('"', '\'');
707                                                 st.addBatch(actionSql);
708                                                 
709                                                 st.executeBatch();
710                                         }
711                                         
712                                 }
713                                 
714                                 /*
715                                  * Inserting serviceGroups data into the ServiceGroup, ServiceList, ProtocolList, and PortList tables
716                                  */
717                                 if (serviceGroup != null) {
718                                         
719                                         int serviceGroupID = 0;
720                                         int serviceListID = 0;
721                                         int protocolID = 0;
722                                         int portID = 0;
723
724                                         
725                                         rs = st.executeQuery("SELECT MAX(ID) AS ID FROM SERVICEGROUP;");
726                                         if(rs.next()){
727                                                 serviceGroupID = rs.getInt("ID");
728                                         }
729                                         rs.close();
730                                         
731                                         rs = st.executeQuery("SELECT MAX(ID) AS ID FROM GROUPSERVICELIST;");
732                                         if(rs.next()){
733                                                 serviceListID = rs.getInt("ID");
734                                         }
735                                         rs.close();
736                                         
737                                         rs = st.executeQuery("SELECT MAX(ID) AS ID FROM PROTOCOLLIST;");
738                                         if(rs.next()){
739                                                 protocolID = rs.getInt("ID");
740                                         }
741                                         rs.close();
742                                         
743                                         rs = st.executeQuery("SELECT MAX(ID) AS ID FROM PORTLIST;");
744                                         if(rs.next()){
745                                                 portID = rs.getInt("ID");
746                                         }
747                                         rs.close();
748                                         for(int i = 0; i < serviceGroup.size() ; i++) {
749                                                 
750                                                 /*
751                                                  * Populate ArrayLists with values from the JSON
752                                                  */
753                                                 //create the JSON object from the JSON Array for each iteration through the for loop
754                                                 JsonObject svcGroupListobj = serviceGroup.getJsonObject(i);
755                                                                                                 
756                                                 String serviceListName = svcGroupListobj.get("name").toString();
757                                                 
758                                                 String description = null;
759                                                 if (svcGroupListobj.containsKey("description")){
760                                                         description = svcGroupListobj.get("description").toString();
761                                                 }
762                                                 
763                                                 //getting members Array from the serviceGroup
764                                                 JsonArray membersArray = svcGroupListobj.getJsonArray("members");
765
766                                                 //String type = svcGroupListobj.get("type").toString();
767                                                 Boolean isServiceGroup = false;
768                                                 if (membersArray!=null){
769                                                         String membersType = membersArray.getJsonObject(0).get("type").toString();
770                                                         if (membersType.contains("REFERENCE")) {
771                                                                 isServiceGroup = true;
772                                                         }
773                                                 }
774                                                                                                 
775                                                 //Insert values into GROUPSERVICELIST table if name begins with Group 
776                                                 if (isServiceGroup) {
777                                                         
778                                                         //increment ID Primary Keys
779                                                         serviceListID = serviceListID + 1;
780
781                                                         String name = null;
782                                                         for (int membersIndex = 0; membersIndex< membersArray.size(); membersIndex++) {
783                                                                 JsonObject membersObj = membersArray.getJsonObject(membersIndex);
784                                                                 //String value = membersObj.get("name").toString();
785                                                                 String type = membersObj.get("type").toString().replace("\"", "");
786                                                                 
787                                                                 String value = null;
788                                                                 if(type.equals("REFERENCE")||type.equals("GROUP")||type.equals("SERVICE")){
789                                                                         value = membersObj.get("name").toString();
790                                                                 } else if (type.equalsIgnoreCase("ANY")){
791                                                                         value = null;
792                                                                 } else {
793                                                                         value = membersObj.get("value").toString();
794                                                                 }
795                                                                 
796                                                                 if(value != null){
797                                                                         value = value.replace("\"", "");
798                                                                 }
799                                                                 
800                                                                 if (name != null) {
801                                                                         name = name.concat(",").concat(value);
802                                                                 } else {
803                                                                         name = value;
804                                                                 }
805                                                         }
806                                                         String nameInsert = "'"+name+"'";               
807                                                         
808                                                         insertQuery = "INSERT INTO GROUPSERVICELIST (ID, NAME, SERVICELIST) "
809                                                                         + "VALUES("+serviceListID+","+serviceListName+","+nameInsert+")";
810                                                         
811                                                         //Replace double quote with single quote
812                                                         insertQuery = insertQuery.replace('"', '\'');
813                                                         
814                                                         //Execute the queries to Insert data
815                                             st.executeUpdate(insertQuery);
816                                                                                                         
817                                                 } else { //Insert JSON data serviceList table, protollist table, and portlist table
818
819                                                         //increment ID Primary Keys
820                                                         protocolID = protocolID + 1;
821                                                         portID = portID + 1;
822                                                         serviceGroupID = serviceGroupID + 1;
823                                                         
824                                                         String type = svcGroupListobj.get("type").toString();
825                                                         String transportProtocol = svcGroupListobj.get("transportProtocol").toString();
826                                                         String ports = svcGroupListobj.get("ports").toString();
827                                                         
828                                                         /*
829                                                          * Create Queries to INSERT data into database table and execute 
830                                                          */
831                                                         String serviceSql = "INSERT INTO SERVICEGROUP (ID, NAME, DESCRIPTION, TYPE, TRANSPORTPROTOCOL, APPPROTOCOL, PORTS) "
832                                                                         + "VALUES("+serviceGroupID+","+serviceListName+","+description+","+type+","
833                                                                         + transportProtocol+","+"null,"+ports+"); ";
834                                                         serviceSql = serviceSql.replace('"', '\'');
835                                                         st.addBatch(serviceSql);
836                                                         
837                                                         String protSql = "INSERT INTO PROTOCOLLIST (ID, PROTOCOLNAME, DESCRIPTION) VALUES("+protocolID+","+transportProtocol+","+transportProtocol+"); ";
838                                                         protSql = protSql.replace('"', '\'');
839                                                         st.addBatch(protSql);
840                                                         
841                                                         String portSql = "INSERT INTO PORTLIST (ID, PORTNAME, DESCRIPTION) VALUES("+portID+","+ports+","+ports+");";
842                                                         portSql = portSql.replace('"', '\'');
843                                                         st.addBatch(portSql);
844                                                         
845                                                         st.executeBatch();
846
847                                                 }
848                                         }
849                                 }
850                                 
851                                 /*
852                                  * Inserting addressGroup data into the ADDRESSGROUP table
853                                  */
854                                 if (addressGroup != null) {
855                                         int prefixID = 0;
856                                         int addressID = 0;
857                                         
858                                         rs = st.executeQuery("SELECT MAX(ID) AS ID FROM PREFIXLIST;");
859                                         if(rs.next()){
860                                                 prefixID = rs.getInt("ID");
861                                         }
862                                         rs.close();
863                                         
864                                         rs = st.executeQuery("SELECT MAX(ID) AS ID FROM ADDRESSGROUP;");
865                                         if(rs.next()){
866                                                 addressID = rs.getInt("ID");
867                                         }
868                                         rs.close();
869                                         for(int i = 0; i < addressGroup.size(); i++) {
870                                                 /*
871                                                  * Populate ArrayLists with values from the JSON
872                                                  */
873                                                 //create the JSON object from the JSON Array for each iteration through the for loop
874                                                 JsonObject addressGroupObj = addressGroup.getJsonObject(i);
875                                                 
876                                                 //create JSON array for members
877                                                 JsonArray membersArray = addressGroupObj.getJsonArray("members");
878                                                 String addressGroupName = addressGroupObj.get("name").toString();
879                                                 
880                                                 String description = null;
881                                                 if (addressGroupObj.containsKey("description")){
882                                                         description = addressGroupObj.get("description").toString();
883                                                 }
884                                                 
885                                                 String prefixIP = null;
886                                                 String type = null;
887                                                 for (int membersIndex = 0; membersIndex < membersArray.size(); membersIndex++) {
888                                                         JsonObject membersObj = membersArray.getJsonObject(membersIndex);
889                                                         //String value = membersObj.get("value").toString();
890                                                         type = membersObj.get("type").toString().replace("\"", "");
891                                                         
892                                                         String value = null;
893                                                         if(type.equals("REFERENCE")||type.equals("GROUP")||type.equals("SERVICE")){
894                                                                 value = membersObj.get("name").toString();
895                                                         } else if (type.equalsIgnoreCase("ANY")){
896                                                                 value = null;
897                                                         } else {
898                                                                 value = membersObj.get("value").toString();
899                                                         }
900                                                         
901                                                         if(value != null){
902                                                                 value = value.replace("\"", "");
903                                                         }
904                                                         
905                                                         if (prefixIP != null) {
906                                                                 prefixIP = prefixIP.concat(",").concat(value);
907                                                         } else {
908                                                                 prefixIP = value;
909                                                         }
910                                                 }
911                                                 String prefixList = "'"+prefixIP+"'";
912                                                 
913                                                 Boolean isAddressGroup = type.contains("REFERENCE");
914                                                 
915                                                 if (isAddressGroup) {                                                           
916                                                         //increment ID Primary Keys
917                                                         addressID = addressID + 1;
918                                                         
919                                                         insertQuery = "INSERT INTO ADDRESSGROUP (ID, NAME, DESCRIPTION, PREFIXLIST) "
920                                                                                 + "VALUES("+addressID+","+addressGroupName+","+description+","+prefixList+")";  
921                                                 } else {
922                                                          //increment ID Primary Key
923                                                         prefixID = prefixID + 1;
924                                                         
925                                                         insertQuery = "INSERT INTO PREFIXLIST (ID, PL_NAME, PL_VALUE, DESCRIPTION) "
926                                                                                 + "VALUES("+prefixID+","+addressGroupName+","+prefixList+","+description+")";
927                                                         
928                                                 }
929                 
930                                                 
931                                                 //Replace double quote with single quote
932                                                 insertQuery = insertQuery.replace('"', '\'');
933                                                 
934                                                 //Execute the queries to Insert data
935                                     st.executeUpdate(insertQuery);
936                                         }
937                                         
938                                 }
939                                 
940                                 /*
941                                  * Remove duplicate values from 'lookup' dictionary tables
942                                  */
943                                 //ProtocolList Table
944                                 String protoDelete = "DELETE FROM protocollist USING protocollist, protocollist p1 "
945                                                 + "WHERE protocollist.id > p1.id AND protocollist.protocolname = p1.protocolname;";
946                                 st.addBatch(protoDelete);
947                                 
948                                 //PortList Table
949                                 String portListDelete = "DELETE FROM portlist USING portlist, portlist p1 "
950                                                 + "WHERE portlist.id > p1.id AND portlist.portname = p1.portname; ";
951                                 st.addBatch(portListDelete);
952                                 
953                                 //PrefixList Table
954                                 String prefixListDelete = "DELETE FROM prefixlist USING prefixlist, prefixlist p1 "
955                                                 + "WHERE prefixlist.id > p1.id AND prefixlist.pl_name = p1.pl_name AND "
956                                                 + "prefixlist.pl_value = p1.pl_value AND prefixlist.description = p1.description; ";
957                                 st.addBatch(prefixListDelete);
958                                 
959                                 //GroupServiceList
960                                 String groupServiceDelete = "DELETE FROM groupservicelist USING groupservicelist, groupservicelist g1 "
961                                                 + "WHERE groupservicelist.id > g1.id AND groupservicelist.name = g1.name AND "
962                                                 + "groupservicelist.serviceList = g1.serviceList; ";
963                                 st.addBatch(groupServiceDelete);
964                                 
965                                 st.executeBatch();
966                                 
967                         } catch (ClassNotFoundException e) {
968                                 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "FirewallConfigPolicy", "Exception building Firewall queries ");
969                                 System.out.println(e.getMessage());
970                                 return false;
971         
972                         } catch (SQLException e) {
973                                 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "FirewallConfigPolicy", "Exception executing Firewall queries");
974                                 System.out.println(e.getMessage());
975                                 return false;
976                         } catch (Exception e) {
977                                 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "FirewallConfigPolicy", "Exception getting Json values");
978                                 System.out.println(e.getMessage());
979                                 return false;
980                         } finally {
981                                 try{
982                                         if (con!=null) con.close();
983                                         if (rs!=null) rs.close();
984                                         if (st!=null) st.close();
985                                 } catch (Exception ex){}
986                         }
987                         return true;
988
989                 } else {
990                         return false;
991                 }
992                 
993         }
994
995
996         private Boolean updateFirewallDictionaryData(String jsonBody, String prevJsonBody) {
997                 
998                 JsonObject oldJson = null;
999                 JsonObject newJson = null;
1000                 
1001                 if (jsonBody != null || prevJsonBody != null) {
1002                         
1003                         oldJson = stringToJson(prevJsonBody);
1004                         newJson = stringToJson(jsonBody);
1005                         
1006                         //if no changes to the json then return true
1007                         if (oldJson.equals(newJson)) {
1008                                 return true;
1009                         }
1010                         
1011                         JsonArray firewallRules = null;
1012                         JsonArray serviceGroup = null;
1013                         JsonArray addressGroup = null;
1014                         
1015                         firewallRules = newJson.getJsonArray("firewallRuleList");
1016                         serviceGroup = newJson.getJsonArray("serviceGroups");
1017                         addressGroup = newJson.getJsonArray("addressGroups");   
1018                         
1019                         Connection con = null;
1020                         Statement st = null;
1021                         ResultSet rs = null;
1022                         
1023                         /*
1024                          * Retrieve the property values for db access from the xacml.pap.properties
1025                          */
1026                         papDbDriver = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_DB_DRIVER);
1027                         papDbUrl = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_DB_URL);
1028                         papDbUser = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_DB_USER);
1029                         papDbPassword = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_DB_PASSWORD);
1030         
1031                         //insert data into tables
1032                         try {
1033                                 
1034                                 //Get DB Connection
1035                                 Class.forName(papDbDriver);
1036                                 con = DriverManager.getConnection(papDbUrl,papDbUser,papDbPassword);
1037                                 st = con.createStatement();
1038                                 
1039                                 JsonNode jsonDiff = createPatch(jsonBody, prevJsonBody);
1040                                 
1041                                 
1042                                 for (int i = 0; i<jsonDiff.size(); i++) {
1043                                         //String path = jsonDiff.get(i).asText();
1044                                         String jsonpatch = jsonDiff.get(i).toString();
1045                                         
1046                                         JsonObject patchObj = stringToJson(jsonpatch);
1047                                         
1048                                         String path = patchObj.get("path").toString().replace('"', ' ').trim();
1049                                                                                 
1050                                         if (path.contains("firewallRuleList")) {
1051                                                 int termID = 0;
1052                                                 int zoneID = 0;
1053                                                 int actionID = 0;
1054                                                 
1055                                                 rs = st.executeQuery("SELECT MAX(ID) AS ID FROM TERM;");
1056                                                 if(rs.next()){
1057                                                         termID = rs.getInt("ID");
1058                                                 }
1059                                                 rs.close();
1060                                                 
1061                                                 rs = st.executeQuery("SELECT MAX(ID) AS ID FROM ZONE;");
1062                                                 if(rs.next()){
1063                                                         zoneID = rs.getInt("ID");
1064                                                 }
1065                                                 rs.close();
1066                                                 
1067                                                 rs = st.executeQuery("SELECT MAX(ID) AS ID FROM ACTIONLIST;");
1068                                                 if(rs.next()){
1069                                                         actionID = rs.getInt("ID");
1070                                                 }
1071                                                 rs.close();
1072                                                 
1073                                                 /*
1074                                                  * Inserting firewallRuleList data into the Terms, SecurityZone, and Action tables
1075                                                  */
1076                                                 for(int ri = 0; ri < firewallRules.size(); ri++) {
1077                                                         
1078                                                         //increment ID Primary Keys
1079                                                         termID = termID + 1;
1080                                                         zoneID = zoneID + 1;
1081                                                         actionID = actionID + 1;
1082                                                         
1083                                                         /*
1084                                                          * Populate ArrayLists with values from the JSON
1085                                                          */
1086                                                         //create the JSON object from the JSON Array for each iteration through the for loop
1087                                                         JsonObject ruleListobj = firewallRules.getJsonObject(ri);
1088                                                         
1089                                                         //get values from JSON fields of firewallRulesList Array
1090                                                         String ruleName = ruleListobj.get("ruleName").toString().replace('"', '\'');
1091                                                         String action = ruleListobj.get("action").toString().replace('"', '\'');
1092                                                         String description = ruleListobj.get("description").toString().replace('"', '\'');
1093                                                         
1094                                                         rs = st.executeQuery("SELECT * FROM TERM WHERE TERMNAME = "+ ruleName + ";");
1095                                                         
1096                                                         if (rs.next()) {
1097                                                                 st.executeUpdate("DELETE FROM TERM WHERE TERMNAME = "+ ruleName + ";");
1098                                                         }
1099                                                         rs.close();
1100                                                         
1101                                                         //getting fromZone Array field from the firewallRulesList
1102                                                         JsonArray fromZoneArray = ruleListobj.getJsonArray("fromZones");
1103                                                         String fromZoneString = null;
1104                                                         
1105                                                         for (int fromZoneIndex = 0; fromZoneIndex<fromZoneArray.size() ; fromZoneIndex++) {
1106                                                                 String value = fromZoneArray.get(fromZoneIndex).toString();
1107                                                                 value = value.replace("\"", "");
1108                                                                 
1109                                                                 if (fromZoneString != null) {
1110                                                                         fromZoneString = fromZoneString.concat(",").concat(value);
1111                                                                         
1112                                                                 } else {
1113                                                                         fromZoneString = value;
1114                                                                 }
1115                                                                 
1116                                                         }
1117                                                         String fromZoneInsert = "'"+fromZoneString+"'";
1118                                                         
1119                                                         //getting toZone Array field from the firewallRulesList
1120                                                         JsonArray toZoneArray = ruleListobj.getJsonArray("toZones");
1121                                                         String toZoneString = null;
1122                                                         
1123                                                         
1124                                                         for (int toZoneIndex = 0; toZoneIndex < toZoneArray.size(); toZoneIndex++) {
1125                                                                 String value = toZoneArray.get(toZoneIndex).toString();
1126                                                                 value = value.replace("\"", "");
1127                                                                 
1128                                                                 if (toZoneString != null) {
1129                                                                         toZoneString = toZoneString.concat(",").concat(value);
1130                                                                         
1131                                                                 } else {
1132                                                                         toZoneString = value;
1133                                                                 }
1134                                                                 
1135                                                         }
1136                                                         String toZoneInsert = "'"+toZoneString+"'";
1137                                                         //getting sourceList Array fields from the firewallRulesList
1138                                                         JsonArray srcListArray = ruleListobj.getJsonArray("sourceList");
1139                                                         String srcListString = null;
1140                                                         for (int srcListIndex = 0; srcListIndex<srcListArray.size(); srcListIndex++) {
1141                                                                 JsonObject srcListObj = srcListArray.getJsonObject(srcListIndex);
1142                                                                 String type = srcListObj.get("type").toString().replace("\"", "");
1143                                                                 
1144                                                                 String value = null;
1145                                                                 if(type.equals("REFERENCE")||type.equals("GROUP")){
1146                                                                         value = srcListObj.get("name").toString();
1147                                                                 } else if (type.equalsIgnoreCase("ANY")){
1148                                                                         value = null;
1149                                                                 } else {
1150                                                                         value = srcListObj.get("value").toString();
1151                                                                 }
1152                                                                 
1153                                                                 if(value != null){
1154                                                                         value = value.replace("\"", "");
1155                                                                 }
1156                                                                 
1157                                                                 if (srcListString != null) {
1158                                                                         srcListString = srcListString.concat(",").concat(value);
1159                                                                         
1160                                                                 } else {
1161                                                                         srcListString = value;
1162                                                                 }
1163                                                                 
1164                                                         }
1165                                                         String srcListInsert = "'"+srcListString+"'";
1166                                                         
1167                                                         //getting destinationList Array fields from the firewallRulesList
1168                                                         JsonArray destListArray = ruleListobj.getJsonArray("destinationList");
1169                                                         String destListString = null;
1170                                                         for (int destListIndex = 0; destListIndex<destListArray.size(); destListIndex ++) {
1171                                                                 JsonObject destListObj = destListArray.getJsonObject(destListIndex);
1172                                                                 String type = destListObj.get("type").toString().replace("\"", "");
1173                                                                 
1174                                                                 String value = null;
1175                                                                 if(type.equals("REFERENCE")||type.equals("GROUP")){
1176                                                                         value = destListObj.get("name").toString();
1177                                                                 } else if (type.equalsIgnoreCase("ANY")){
1178                                                                         value = null;
1179                                                                 } else {
1180                                                                         value = destListObj.get("value").toString();
1181                                                                 }
1182                                                                 
1183                                                                 if(value != null){
1184                                                                         value = value.replace("\"", "");
1185                                                                 }
1186                                                                 
1187                                                                 if (destListString != null) {
1188                                                                         destListString = destListString.concat(",").concat(value);
1189                                                                 } else {
1190                                                                         destListString = value;
1191                                                                 }
1192                                                         }
1193                                                         String destListInsert = "'"+destListString+"'";
1194                                                         
1195                                                         //getting destServices Array fields from the firewallRulesList
1196                                                         JsonArray destServicesArray = ruleListobj.getJsonArray("destServices");
1197                                                         String destPortListString = null;
1198                                                         for (int destPortListIndex = 0; destPortListIndex < destServicesArray.size(); destPortListIndex++) {
1199                                                                 JsonObject destServicesObj = destServicesArray.getJsonObject(destPortListIndex);
1200                                                                 String type = destServicesObj.get("type").toString().replace("\"", "");
1201                                                                 
1202                                                                 String value = null;
1203                                                                 if(type.equals("REFERENCE")||type.equals("GROUP")){
1204                                                                         value = destServicesObj.get("name").toString();
1205                                                                 } else if (type.equalsIgnoreCase("ANY")){
1206                                                                         value = null;
1207                                                                 } else {
1208                                                                         value = destServicesObj.get("value").toString();
1209                                                                 }
1210                                                                 
1211                                                                 if(value != null){
1212                                                                         value = value.replace("\"", "");
1213                                                                 }
1214                                                                 
1215                                                                 if (destPortListString != null) {
1216                                                                         destPortListString = destPortListString.concat(",").concat(value);
1217                                                                 } else {
1218                                                                         destPortListString = value;
1219                                                                 }
1220                                                         }
1221                                                         String destPortListInsert = "'"+destPortListString+"'";                                 
1222                                                         
1223                                                         /*
1224                                                          * Create Queries to INSERT data into database tables and execute 
1225                                                          */
1226                                                         
1227                                                         //Insert Into Terms table
1228                                 String termSql = "INSERT INTO Term (ID, TERMNAME, SRCIPLIST, DESTIPLIST, PROTOCOLLIST, PORTLIST, SRCPORTLIST,"
1229                                          + " DESTPORTLIST, ACTION, DESCRIPTION, FROMZONE, TOZONE, CREATED_BY, MODIFIED_DATE) VALUES ("+termID+","
1230                                          +ruleName+","+srcListInsert+","+destListInsert+","+ "null"+","+"null"+","+"null"+","+destPortListInsert+","
1231                                          +action+","+description+","+fromZoneInsert+","+toZoneInsert+",'API',"+ "null"+ "); ";
1232                                                         
1233                                                         termSql = termSql.replace('"', '\'');
1234                                                         st.addBatch(termSql);
1235                                                         
1236                                                         rs = st.executeQuery("SELECT * FROM ACTIONLIST WHERE ACTIONNAME = " + action + ";");
1237                                                         
1238                                                         String actionSql = null;
1239                                                         if (rs.next()) {
1240                                                                 //do nothing
1241                                                         } else {
1242                                                                 actionSql = "INSERT INTO ACTIONLIST (ID, ACTIONNAME, DESCRIPTION) VALUES ("+actionID+","+action+","+action+") ";
1243                                                                 actionSql = actionSql.replace('"', '\'');
1244                                                                 st.addBatch(actionSql);
1245                                                         }
1246                                                         st.executeBatch();
1247                                                 }
1248                                                 
1249                                         }
1250                                         
1251                                         if (path.contains("serviceGroups")) {
1252                                                 int serviceGroupID = 0;
1253                                                 int serviceListID = 0;
1254                                                 int protocolID = 0;
1255                                                 int portID = 0;
1256
1257                                                 
1258                                                 rs = st.executeQuery("SELECT MAX(ID) AS ID FROM SERVICEGROUP;");
1259                                                 if(rs.next()){
1260                                                         serviceGroupID = rs.getInt("ID");
1261                                                 }
1262                                                 rs.close();
1263                                                 
1264                                                 rs = st.executeQuery("SELECT MAX(ID) AS ID FROM GROUPSERVICELIST;");
1265                                                 if(rs.next()){
1266                                                         serviceListID = rs.getInt("ID");
1267                                                 }
1268                                                 rs.close();
1269                                                 
1270                                                 rs = st.executeQuery("SELECT MAX(ID) AS ID FROM PROTOCOLLIST;");
1271                                                 if(rs.next()){
1272                                                         protocolID = rs.getInt("ID");
1273                                                 }
1274                                                 rs.close();
1275                                                 
1276                                                 rs = st.executeQuery("SELECT MAX(ID) AS ID FROM PORTLIST;");
1277                                                 if(rs.next()){
1278                                                         portID = rs.getInt("ID");
1279                                                 }
1280                                                 rs.close();
1281                                                 
1282                                                 String insertQuery = null;
1283                                                 
1284                                                 /*
1285                                                  * Inserting serviceGroups data into the ServiceGroup, ServiceList, ProtocolList, and PortList tables
1286                                                  */
1287                                                 for(int si = 0; si < serviceGroup.size(); si++) {
1288                                                         /*
1289                                                          * Populate ArrayLists with values from the JSON
1290                                                          */
1291                                                         //create the JSON object from the JSON Array for each iteration through the for loop
1292                                                         JsonObject svcGroupListobj = serviceGroup.getJsonObject(si);
1293                                                         
1294                                                         String groupName = svcGroupListobj.get("name").toString().replace('"', '\''); 
1295                                                         
1296                                                         String description = null;
1297                                                         if (svcGroupListobj.containsKey("description")){
1298                                                                 description = svcGroupListobj.get("description").toString().replace('"', '\'');
1299                                                         }
1300                                                         
1301                                                         JsonArray membersArray = svcGroupListobj.getJsonArray("members");
1302
1303                                                         Boolean isServiceGroup = false;
1304                                                         if (membersArray!=null){
1305                                                                 String membersType = membersArray.getJsonObject(0).get("type").toString();
1306                                                                 if (membersType.contains("REFERENCE")) {
1307                                                                         isServiceGroup = true;
1308                                                                 }
1309                                                         }
1310                                                         
1311                                                         //Insert values into GROUPSERVICELIST table if name begins with Group 
1312                                                         if (isServiceGroup) {
1313                                                                 
1314                                                                 rs = st.executeQuery("SELECT * FROM GROUPSERVICELIST WHERE NAME = "+ groupName + ";");
1315                                                                 
1316                                                                 if (rs.next()) {
1317                                                                         st.executeUpdate("DELETE FROM GROUPSERVICELIST WHERE NAME = "+ groupName + ";");
1318                                                                 }
1319                                                                 rs.close();
1320                                                                 //increment ID Primary Keys
1321                                                                 serviceListID = serviceListID + 1;
1322                                                                 String name = null;
1323                                                                 for (int membersIndex = 0; membersIndex < membersArray.size(); membersIndex++) {
1324                                                                         JsonObject membersObj = membersArray.getJsonObject(membersIndex);
1325                                                                         String type = membersObj.get("type").toString().replace("\"", "");
1326                                                                         
1327                                                                         String value = null;
1328                                                                         if(type.equals("REFERENCE")||type.equals("GROUP")||type.equals("SERVICE")){
1329                                                                                 value = membersObj.get("name").toString();
1330                                                                         } else if (type.equalsIgnoreCase("ANY")){
1331                                                                                 value = null;
1332                                                                         } else {
1333                                                                                 value = membersObj.get("value").toString();
1334                                                                         }
1335                                                                         
1336                                                                         if(value != null){
1337                                                                                 value = value.replace("\"", "");
1338                                                                         }
1339                                                                         
1340                                                                         if (name != null) {
1341                                                                                 name = name.concat(",").concat(value);
1342                                                                         } else {
1343                                                                                 name = value;
1344                                                                         }
1345                                                                 }
1346                                                                 String nameInsert = "'"+name+"'";               
1347                                                                 
1348                                                                 insertQuery = "INSERT INTO GROUPSERVICELIST (ID, NAME, SERVICELIST) "
1349                                                                                 + "VALUES("+serviceListID+","+groupName+","+nameInsert+")";
1350                                                                 
1351                                                                 //Replace double quote with single quote
1352                                                                 insertQuery = insertQuery.replace('"', '\'');
1353                                                                 
1354                                                                 //Execute the queries to Insert data
1355                                                     st.executeUpdate(insertQuery);
1356                                                                                                                 
1357                                                         } else { //Insert JSON data serviceGroup table, protocollist table, and portlist table
1358                                                                                                                                 
1359                                                                 //increment ID Primary Keys
1360                                                                 protocolID = protocolID + 1;
1361                                                                 portID = portID + 1;
1362                                                                 serviceGroupID = serviceGroupID + 1;
1363                                                                 
1364                                                                 String type = svcGroupListobj.get("type").toString().replace('"', '\'');
1365                                                                 String transportProtocol = svcGroupListobj.get("transportProtocol").toString().replace('"', '\'');
1366                                                                 String ports = svcGroupListobj.get("ports").toString().replace('"', '\'');
1367                                                                 
1368                                                                 rs = st.executeQuery("SELECT * FROM SERVICEGROUP WHERE NAME = "+ groupName + ";");
1369                                                                 
1370                                                                 if (rs.next()) {
1371                                                                         st.executeUpdate("DELETE FROM SERVICEGROUP WHERE NAME = "+ groupName + ";");
1372                                                                 }
1373                                                                 rs.close();
1374                                                                 
1375                                                                 String svcGroupSql = "INSERT INTO SERVICEGROUP (ID, NAME, DESCRIPTION, TYPE, TRANSPORTPROTOCOL, APPPROTOCOL, PORTS) "
1376                                                                                         + "VALUES("+serviceGroupID+","+groupName+","+description+","+type+","
1377                                                                                         + transportProtocol+","+"null,"+ports+"); ";
1378                                                                 svcGroupSql = svcGroupSql.replace('"', '\'');
1379                                                                 st.addBatch(svcGroupSql);
1380                                                                 
1381                                                                 rs = st.executeQuery("SELECT * FROM PROTOCOLLIST WHERE PROTOCOLNAME = " + transportProtocol + ";");
1382                                                                 
1383                                                                 String protoSql = null;
1384                                                                 if (rs.next()) {
1385                                                                         //do nothing
1386                                                                 } else {
1387                                                                         protoSql = "INSERT INTO PROTOCOLLIST (ID, PROTOCOLNAME, DESCRIPTION) "
1388                                                                                         + "VALUES("+protocolID+","+transportProtocol+","+transportProtocol+"); ";
1389                                                                         protoSql = protoSql.replace('"', '\'');
1390                                                                         st.addBatch(protoSql);
1391
1392                                                                 }
1393                                                                 rs.close();
1394
1395                                                                 rs = st.executeQuery("SELECT * FROM PORTLIST WHERE PORTNAME = " + ports + ";");
1396                                                                 
1397                                                                 String portSql = null;
1398                                                                 if (rs.next()) {
1399                                                                         //do nothing
1400                                                                 } else {
1401                                                                         portSql = "INSERT INTO PORTLIST (ID, PORTNAME, DESCRIPTION) VALUES("+portID+","+ports+","+ports+"); ";
1402                                                                         portSql = portSql.replace('"', '\'');
1403                                                                         st.addBatch(portSql);
1404                                                                 }
1405                                                                 rs.close();
1406                                                                 st.executeBatch();
1407                                                         }
1408                                                 }
1409                                         }
1410                                         
1411                                         if (path.contains("addressGroups")) {
1412                                                 /*
1413                                                  * Inserting addressGroup data into the ADDRESSGROUP table
1414                                                  */
1415                                                 int prefixID = 0;
1416                                                 int addressID = 0;
1417                                                 
1418                                                 rs = st.executeQuery("SELECT MAX(ID) AS ID FROM PREFIXLIST;");
1419                                                 if(rs.next()){
1420                                                         prefixID = rs.getInt("ID");
1421                                                 }
1422                                                 rs.close();
1423                                                 
1424                                                 rs = st.executeQuery("SELECT MAX(ID) AS ID FROM ADDRESSGROUP;");
1425                                                 if(rs.next()){
1426                                                         addressID = rs.getInt("ID");
1427                                                 }
1428                                                 rs.close();
1429                                                 
1430                                                 String insertQuery = null;
1431                                                 for(int ai=0; ai < addressGroup.size() ; ai++) {
1432                                                         
1433                                                         /*
1434                                                          * Populate ArrayLists with values from the JSON
1435                                                          */
1436                                                         //create the JSON object from the JSON Array for each iteration through the for loop
1437                                                         JsonObject addressGroupObj = addressGroup.getJsonObject(ai);
1438                                                         
1439                                                         //create JSON array for members
1440                                                         JsonArray membersArray = addressGroupObj.getJsonArray("members");
1441                                                         String addressGroupName = addressGroupObj.get("name").toString().replace('"', '\'');
1442                                                         
1443                                                         String description = null;
1444                                                         if (addressGroupObj.containsKey("description")){
1445                                                                 description = addressGroupObj.get("description").toString().replace('"', '\'');
1446                                                         }
1447                                                         
1448                                                         String prefixIP = null;
1449                                                         String type = null;
1450                                                         for (int membersIndex=0; membersIndex < membersArray.size(); membersIndex++) {
1451                                                                 JsonObject membersObj = membersArray.getJsonObject(membersIndex);
1452                                                                 type = membersObj.get("type").toString().replace("\"", "");
1453                                                                 
1454                                                                 String value = null;
1455                                                                 if(type.equals("REFERENCE")||type.equals("GROUP")||type.equals("SERVICE")){
1456                                                                         value = membersObj.get("name").toString();
1457                                                                 } else if (type.equalsIgnoreCase("ANY")){
1458                                                                         value = null;
1459                                                                 } else {
1460                                                                         value = membersObj.get("value").toString();
1461                                                                 }
1462                                                                 
1463                                                                 if(value != null){
1464                                                                         value = value.replace("\"", "");
1465                                                                 }
1466                                                                 
1467                                                                 if (prefixIP != null) {
1468                                                                         prefixIP = prefixIP.concat(",").concat(value);
1469                                                                 } else {
1470                                                                         prefixIP = value;
1471                                                                 }
1472                                                         }
1473                                                         String prefixList = "'"+prefixIP+"'";
1474                                                         
1475                                                         Boolean isAddressGroup = type.contains("REFERENCE");
1476                                                         
1477                                                         if (isAddressGroup) {           
1478                                                                 
1479                                                                 rs = st.executeQuery("SELECT * FROM ADDRESSGROUP WHERE NAME = "+ addressGroupName + ";");
1480                                                                 
1481                                                                 if (rs.next()) {
1482                                                                         st.executeUpdate("DELETE FROM ADDRESSGROUP WHERE NAME = "+ addressGroupName + ";");
1483                                                                 }
1484                                                                 rs.close();
1485                                                                 //increment ID Primary Keys
1486                                                                 addressID = addressID + 1;
1487                                                                 
1488                                                                 insertQuery = "INSERT INTO ADDRESSGROUP (ID, NAME, DESCRIPTION, PREFIXLIST) "
1489                                                                                         + "VALUES("+addressID+","+addressGroupName+","+description+","+prefixList+")";  
1490                                                                 
1491                                                                 
1492                                                                 
1493                                                         } else {
1494                                                                 
1495                                                                 rs = st.executeQuery("SELECT * FROM PREFIXLIST WHERE PL_NAME = "+ addressGroupName + ";");
1496                                                                 
1497                                                                 if (rs.next()) {
1498                                                                         st.executeUpdate("DELETE FROM PREFIXLIST WHERE PL_NAME = "+ addressGroupName + ";");
1499                                                                 }
1500                                                                 rs.close();
1501                                                                  //increment ID Primary Key
1502                                                                 prefixID = prefixID + 1;
1503                                                                 
1504                                                                 insertQuery = "INSERT INTO PREFIXLIST (ID, PL_NAME, PL_VALUE, DESCRIPTION) "
1505                                                                                         + "VALUES("+prefixID+","+addressGroupName+","+prefixList+","+description+")";
1506                                                                 
1507                                                         }
1508                                                         //Replace double quote with single quote
1509                                                         insertQuery = insertQuery.replace('"', '\'');
1510                                                         
1511                                                         //Execute the queries to Insert data
1512                                             st.executeUpdate(insertQuery);
1513                                                 }                                               
1514                                         }
1515                                 }
1516                                 
1517                                 /*
1518                                  * Remove duplicate values from 'lookup' dictionary tables
1519                                  */
1520                                 //ProtocolList Table
1521                                 String protoDelete = "DELETE FROM protocollist USING protocollist, protocollist p1 "
1522                                                 + "WHERE protocollist.id > p1.id AND protocollist.protocolname = p1.protocolname;";
1523                                 st.addBatch(protoDelete);
1524                                 
1525                                 //PortList Table
1526                                 String portListDelete = "DELETE FROM portlist USING portlist, portlist p1 "
1527                                                 + "WHERE portlist.id > p1.id AND portlist.portname = p1.portname; ";
1528                                 st.addBatch(portListDelete);
1529                                 
1530                                 //PrefixList Table
1531                                 String prefixListDelete = "DELETE FROM prefixlist USING prefixlist, prefixlist p1 "
1532                                                 + "WHERE prefixlist.id > p1.id AND prefixlist.pl_name = p1.pl_name AND "
1533                                                 + "prefixlist.pl_value = p1.pl_value AND prefixlist.description = p1.description; ";
1534                                 st.addBatch(prefixListDelete);
1535                                 
1536                                 //GroupServiceList
1537                                 String groupServiceDelete = "DELETE FROM groupservicelist USING groupservicelist, groupservicelist g1 "
1538                                                 + "WHERE groupservicelist.id > g1.id AND groupservicelist.name = g1.name AND "
1539                                                 + "groupservicelist.serviceList = g1.serviceList; ";
1540                                 st.addBatch(groupServiceDelete);
1541                                 
1542                                 st.executeBatch();
1543                                 
1544                         } catch (ClassNotFoundException e) {
1545                                 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "FirewallConfigPolicy", "Exception building Firewall queries");
1546                                 System.out.println(e.getMessage());
1547                                 return false;
1548         
1549                         } catch (SQLException e) {
1550                                 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "FirewallConfigPolicy", "Exception executing Firewall queries");
1551                                 System.out.println(e.getMessage());
1552                                 return false;
1553                         } finally {
1554                                 try{
1555                                         if (con!=null) con.close();
1556                                         if (rs!=null) rs.close();
1557                                         if (st!=null) st.close();
1558                                 } catch (Exception ex){}
1559                         }
1560                         return true;
1561         
1562                 } else {
1563                         return false;
1564                 }
1565         
1566 }
1567         
1568         private JsonObject stringToJson(String jsonString) {
1569                 
1570                 JsonObject json = null;
1571                 if (jsonString != null) {
1572                         
1573                         //Read jsonBody to JsonObject
1574                         StringReader in = null;
1575                         
1576                         in = new StringReader(jsonString);
1577                         
1578                         JsonReader jsonReader = Json.createReader(in);
1579                         json = jsonReader.readObject();
1580                 }
1581                 
1582                 return json;
1583         }
1584                 
1585                 
1586         private JsonNode createPatch(String json, String oldJson) {
1587                 JsonNode oldJason = null;
1588                 JsonNode updatedJason = null;
1589
1590                 try {
1591                 oldJason = JsonLoader.fromString(oldJson);
1592                 updatedJason = JsonLoader.fromString(json);
1593                 } catch (IOException e) {
1594                 e.printStackTrace();
1595                 }
1596
1597                 JsonPatch jsonPatch = JsonDiff.asJsonPatch(oldJason, updatedJason);
1598                 JsonNode patchNode = JsonDiff.asJson(oldJason, updatedJason);
1599                 System.out.println("Sending Patch:" + jsonPatch);
1600                 return patchNode;
1601
1602                 }
1603
1604         @Override
1605         public Object getCorrectPolicyDataObject() {
1606                 return policyAdapter.getPolicyData();
1607         }
1608
1609 }
1610         
1611         
1612