2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.controller;
22 import java.io.PrintWriter;
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.HashSet;
26 import java.util.Iterator;
27 import java.util.LinkedHashMap;
28 import java.util.List;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
35 import org.hibernate.SessionFactory;
36 import org.json.JSONObject;
37 import org.onap.policy.common.logging.flexlogger.FlexLogger;
38 import org.onap.policy.common.logging.flexlogger.Logger;
39 import org.onap.policy.rest.adapter.AddressGroupJson;
40 import org.onap.policy.rest.adapter.AddressJson;
41 import org.onap.policy.rest.adapter.AddressMembers;
42 import org.onap.policy.rest.adapter.DeployNowJson;
43 import org.onap.policy.rest.adapter.IdMap;
44 import org.onap.policy.rest.adapter.PolicyRestAdapter;
45 import org.onap.policy.rest.adapter.PrefixIPList;
46 import org.onap.policy.rest.adapter.ServiceGroupJson;
47 import org.onap.policy.rest.adapter.ServiceListJson;
48 import org.onap.policy.rest.adapter.ServiceMembers;
49 import org.onap.policy.rest.adapter.ServicesJson;
50 import org.onap.policy.rest.adapter.TagDefines;
51 import org.onap.policy.rest.adapter.Tags;
52 import org.onap.policy.rest.adapter.Term;
53 import org.onap.policy.rest.adapter.TermCollector;
54 import org.onap.policy.rest.adapter.VendorSpecificData;
55 import org.onap.policy.rest.dao.CommonClassDao;
56 import org.onap.policy.rest.jpa.AddressGroup;
57 import org.onap.policy.rest.jpa.FWTagPicker;
58 import org.onap.policy.rest.jpa.GroupServiceList;
59 import org.onap.policy.rest.jpa.PolicyEntity;
60 import org.onap.policy.rest.jpa.PrefixList;
61 import org.onap.policy.rest.jpa.SecurityZone;
62 import org.onap.policy.rest.jpa.ServiceList;
63 import org.onap.policy.rest.jpa.TermList;
64 import org.onap.policy.xacml.api.XACMLErrorConstants;
65 import org.openecomp.portalsdk.core.controller.RestrictedBaseController;
66 import org.springframework.beans.factory.annotation.Autowired;
67 import org.springframework.stereotype.Controller;
68 import org.springframework.web.bind.annotation.RequestMapping;
70 import com.fasterxml.jackson.databind.DeserializationFeature;
71 import com.fasterxml.jackson.databind.JsonNode;
72 import com.fasterxml.jackson.databind.ObjectMapper;
73 import com.fasterxml.jackson.databind.ObjectWriter;
75 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
76 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
77 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
78 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
79 import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
80 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
81 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
85 public class CreateFirewallController extends RestrictedBaseController {
86 private static Logger policyLogger = FlexLogger.getLogger(CreateFirewallController.class);
89 SessionFactory sessionFactory;
91 private static CommonClassDao commonClassDao;
93 private List<String> tagCollectorList;
94 private String jsonBody;
95 List<String> expandablePrefixIPList = new ArrayList<>();
96 List<String> expandableServicesList= new ArrayList<>();
98 private CreateFirewallController(CommonClassDao commonClassDao){
99 CreateFirewallController.commonClassDao = commonClassDao;
102 public CreateFirewallController(){}
103 private List<String> termCollectorList;
104 private ArrayList<Object> attributeList;
107 public PolicyRestAdapter setDataToPolicyRestAdapter(PolicyRestAdapter policyData){
109 termCollectorList = new ArrayList<>();
110 tagCollectorList = new ArrayList<>();
111 if(!policyData.getAttributes().isEmpty()){
112 for(Object attribute : policyData.getAttributes()){
113 if(attribute instanceof LinkedHashMap<?, ?>){
114 String key = ((LinkedHashMap<?, ?>) attribute).get("key").toString();
115 termCollectorList.add(key);
117 String tag = ((LinkedHashMap<?, ?>) attribute).get("value").toString();
118 tagCollectorList.add(tag);
122 jsonBody = constructJson(policyData);
123 if (jsonBody != null && !jsonBody.equalsIgnoreCase("")) {
124 policyData.setJsonBody(jsonBody);
126 policyData.setJsonBody("{}");
128 policyData.setJsonBody(jsonBody);
133 private List<String> mapping(String expandableList) {
134 List <String> valueDesc= new ArrayList<>();
135 List<Object> prefixListData = commonClassDao.getData(PrefixList.class);
136 for (int i = 0; i< prefixListData.size(); i++) {
137 PrefixList prefixList = (PrefixList) prefixListData.get(i);
138 if (prefixList.getPrefixListName().equals(expandableList)) {
139 String value = prefixList.getPrefixListValue();
140 valueDesc.add(value);
141 String desc= prefixList.getDescription();
149 private ServiceList mappingServiceList(String expandableList) {
150 ServiceList serviceList=null;
151 List<Object> serviceListData = commonClassDao.getData(ServiceList.class);
152 for (int i = 0; i< serviceListData.size(); i++) {
153 serviceList = (ServiceList) serviceListData.get(i);
154 if (serviceList.getServiceName().equals(expandableList)) {
161 private GroupServiceList mappingServiceGroup(String expandableList) {
163 GroupServiceList serviceGroup=null;
164 List<Object> serviceGroupData = commonClassDao.getData(GroupServiceList.class);
165 for (int i = 0; i< serviceGroupData.size(); i++) {
166 serviceGroup = (GroupServiceList) serviceGroupData.get(i);
167 if (serviceGroup.getGroupName().equals(expandableList)) {
174 private AddressGroup mappingAddressGroup(String expandableList) {
176 AddressGroup addressGroup=null;
177 List<Object> addressGroupData = commonClassDao.getData(AddressGroup.class);
178 for (int i = 0; i< addressGroupData.size(); i++) {
179 addressGroup = (AddressGroup) addressGroupData.get(i);
180 if (addressGroup.getGroupName().equals(expandableList)) {
187 public void prePopulateFWPolicyData(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
188 attributeList = new ArrayList<>();
189 if (policyAdapter.getPolicyData() instanceof PolicyType) {
190 Object policyData = policyAdapter.getPolicyData();
191 PolicyType policy = (PolicyType) policyData;
192 // policy name value is the policy name without any prefix and Extensions.
193 policyAdapter.setOldPolicyFileName(policyAdapter.getPolicyName());
194 String policyNameValue = policyAdapter.getPolicyName().substring(policyAdapter.getPolicyName().indexOf("FW_") +3);
195 if (policyLogger.isDebugEnabled()) {
196 policyLogger.debug("Prepopulating form data for Config Policy selected:"+ policyAdapter.getPolicyName());
198 policyAdapter.setPolicyName(policyNameValue);
199 String description = "";
201 description = policy.getDescription().substring(0, policy.getDescription().indexOf("@CreatedBy:"));
203 policyLogger.info("General error", e);
204 description = policy.getDescription();
206 policyAdapter.setPolicyDescription(description);
208 ObjectMapper mapper = new ObjectMapper();
210 TermCollector tc1=null;
214 SecurityZone jpaSecurityZone;
215 data = entity.getConfigurationData().getConfigBody();
216 tc1 = (TermCollector)mapper.readValue(data, TermCollector.class);
217 List<Object> securityZoneData = commonClassDao.getData(SecurityZone.class);
218 for (int i = 0; i < securityZoneData.size() ; i++) {
219 jpaSecurityZone = (SecurityZone) securityZoneData.get(i);
220 if (jpaSecurityZone.getZoneValue().equals(tc1.getSecurityZoneId())){
221 policyAdapter.setSecurityZone(jpaSecurityZone.getZoneName());
227 policyLogger.error("Exception Caused while Retriving the JSON body data" +e);
230 Map<String, String> termTagMap=null;
232 for(int i=0;i<tc1.getFirewallRuleList().size();i++){
233 termTagMap = new HashMap<String, String>();
234 String ruleName= tc1.getFirewallRuleList().get(i).getRuleName();
235 String tagPickerName=tc1.getRuleToTag().get(i).getTagPickerName();
236 termTagMap.put("key", ruleName);
237 termTagMap.put("value", tagPickerName);
238 attributeList.add(termTagMap);
241 policyAdapter.setAttributes(attributeList);
242 // Get the target data under policy.
243 TargetType target = policy.getTarget();
244 if (target != null) {
245 // Under target we have AnyOFType
246 List<AnyOfType> anyOfList = target.getAnyOf();
247 if (anyOfList != null) {
248 Iterator<AnyOfType> iterAnyOf = anyOfList.iterator();
249 while (iterAnyOf.hasNext()) {
250 AnyOfType anyOf = iterAnyOf.next();
251 // Under AnyOFType we have AllOFType
252 List<AllOfType> allOfList = anyOf.getAllOf();
253 if (allOfList != null) {
254 Iterator<AllOfType> iterAllOf = allOfList.iterator();
255 while (iterAllOf.hasNext()) {
256 AllOfType allOf = iterAllOf.next();
257 // Under AllOFType we have Match
258 List<MatchType> matchList = allOf.getMatch();
259 if (matchList != null) {
261 Iterator<MatchType> iterMatch = matchList.iterator();
262 while (iterMatch.hasNext()) {
263 MatchType match = iterMatch.next();
265 // Under the match we have attribute value and
266 // attributeDesignator. So,finally down to the actual attribute.
268 AttributeValueType attributeValue = match.getAttributeValue();
269 String value = (String) attributeValue.getContent().get(0);
270 AttributeDesignatorType designator = match.getAttributeDesignator();
271 String attributeId = designator.getAttributeId();
272 if (attributeId.equals("ConfigName")) {
273 policyAdapter.setConfigName(value);
275 if (attributeId.equals("RiskType")){
276 policyAdapter.setRiskType(value);
278 if (attributeId.equals("RiskLevel")){
279 policyAdapter.setRiskLevel(value);
281 if (attributeId.equals("guard")){
282 policyAdapter.setGuard(value);
284 if (attributeId.equals("TTLDate") && !value.contains("NA")){
285 PolicyController controller = new PolicyController();
286 String newDate = controller.convertDate(value);
287 policyAdapter.setTtlDate(newDate);
299 @RequestMapping(value={"/policyController/ViewFWPolicyRule.htm"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
300 public void setFWViewRule(HttpServletRequest request, HttpServletResponse response){
302 termCollectorList = new ArrayList<>();
303 ObjectMapper mapper = new ObjectMapper();
304 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
305 JsonNode root = mapper.readTree(request.getReader());
306 PolicyRestAdapter policyData = (PolicyRestAdapter)mapper.readValue(root.get("policyData").toString(), PolicyRestAdapter.class);
307 if(!policyData.getAttributes().isEmpty()){
308 for(Object attribute : policyData.getAttributes()){
309 if(attribute instanceof LinkedHashMap<?, ?>){
310 String key = ((LinkedHashMap<?, ?>) attribute).get("key").toString();
311 termCollectorList.add(key);
315 TermList jpaTermList;
316 String ruleSrcList=null;
317 String ruleDestList=null;
318 String ruleSrcPort=null;
319 String ruleDestPort=null;
320 String ruleAction=null;
321 List <String> valueDesc= new ArrayList<>();
322 StringBuffer displayString = new StringBuffer();
323 for (String id : termCollectorList) {
324 List<Object> tmList = commonClassDao.getDataById(TermList.class, "termName", id);
325 jpaTermList = (TermList) tmList.get(0);
326 if (jpaTermList != null){
327 ruleSrcList= ((TermList) jpaTermList).getSrcIPList();
328 if ((ruleSrcList!= null) && (!ruleSrcList.isEmpty()) && !ruleSrcList.equals("null")){
329 displayString.append("Source IP List: " + ((TermList) jpaTermList).getSrcIPList());
330 displayString.append(" ; \t\n");
331 for(String srcList:ruleSrcList.split(",")){
332 if(srcList.startsWith("Group_")){
333 AddressGroup ag= new AddressGroup();
334 ag= mappingAddressGroup(srcList);
335 displayString.append("\n\t"+"Group has :"+ag.getPrefixList()+"\n");
336 for(String groupItems:ag.getPrefixList().split(",")){
337 valueDesc=mapping(groupItems);
338 displayString.append("\n\t"+"Name: "+groupItems);
339 if(!valueDesc.isEmpty()){
340 displayString.append("\n\t"+"Description: "+valueDesc.get(1));
341 displayString.append("\n\t"+"Value: "+valueDesc.get(0));
343 displayString.append("\n");
346 if(!srcList.equals("ANY")){
347 valueDesc=mapping(srcList);
348 displayString.append("\n\t"+"Name: "+srcList);
349 displayString.append("\n\t"+"Description: "+valueDesc.get(1));
350 displayString.append("\n\t"+"Value: "+valueDesc.get(0));
351 displayString.append("\n");
355 displayString.append("\n");
357 ruleDestList= ((TermList) jpaTermList).getDestIPList();
358 if ( ruleDestList!= null && (!ruleDestList.isEmpty())&& !ruleDestList.equals("null")){
359 displayString.append("Destination IP List: " + ((TermList) jpaTermList).getDestIPList());
360 displayString.append(" ; \t\n");
361 for(String destList:ruleDestList.split(",")){
362 if(destList.startsWith("Group_")){
363 AddressGroup ag= new AddressGroup();
364 ag= mappingAddressGroup(destList);
365 displayString.append("\n\t"+"Group has :"+ag.getPrefixList()+"\n");
366 for(String groupItems:ag.getPrefixList().split(",")){
367 valueDesc=mapping(groupItems);
368 displayString.append("\n\t"+"Name: "+groupItems);
369 displayString.append("\n\t"+"Description: "+valueDesc.get(1));
370 displayString.append("\n\t"+"Value: "+valueDesc.get(0));
371 displayString.append("\n\t");
374 if(!destList.equals("ANY")){
375 valueDesc=mapping(destList);
376 displayString.append("\n\t"+"Name: "+destList);
377 displayString.append("\n\t"+"Description: "+valueDesc.get(1));
378 displayString.append("\n\t"+"Value: "+valueDesc.get(0));
379 displayString.append("\n\t");
383 displayString.append("\n");
386 ruleSrcPort=((TermList) jpaTermList).getSrcPortList();
387 if ( ruleSrcPort!= null && (!ruleSrcPort.isEmpty())&& !ruleSrcPort.equals("null")) {
388 displayString.append("\n"+"Source Port List:"
390 displayString.append(" ; \t\n");
393 ruleDestPort= ((TermList) jpaTermList).getDestPortList();
394 if (ruleDestPort != null && (!ruleDestPort.isEmpty())&& !ruleDestPort.equals("null")) {
395 displayString.append("\n"+"Destination Port List:"
397 displayString.append(" ; \t\n");
398 for(String destServices:ruleDestPort.split(",")){
399 if(destServices.startsWith("Group_")){
400 GroupServiceList sg= new GroupServiceList();
401 sg= mappingServiceGroup(destServices);
402 displayString.append("\n\t"+"Service Group has :"+sg.getServiceList()+"\n");
403 for(String groupItems:sg.getServiceList().split(",")){
404 ServiceList sl= new ServiceList();
405 sl= mappingServiceList(groupItems);
406 displayString.append("\n\t"+"Name: "+
407 sl.getServiceName());
408 displayString.append("\n\t"+"Description: "+
409 sl.getServiceDescription());
410 displayString.append("\n\t"+"Transport-Protocol: "+
411 sl.getServiceTransProtocol());
412 displayString.append("\n\t"+"Ports: "+
413 sl.getServicePorts());
414 displayString.append("\n");
418 if(!destServices.equals("ANY")){
419 ServiceList sl= new ServiceList();
420 sl= mappingServiceList(destServices);
421 displayString.append("\n\t"+"Name: "+
422 sl.getServiceName());
423 displayString.append("\n\t"+"Description: "+
424 sl.getServiceDescription());
425 displayString.append("\n\t"+"Transport-Protocol: "+
426 sl.getServiceTransProtocol());
427 displayString.append("\n\t"+"Ports: "+
428 sl.getServicePorts());
429 displayString.append("\n");
433 displayString.append("\n");
436 ruleAction=(jpaTermList).getAction();
437 if ( ruleAction!= null && (!ruleAction.isEmpty())) {
438 displayString.append("\n"+"Action List:"
440 displayString.append(" ; \t\n");
444 response.setCharacterEncoding("UTF-8");
445 response.setContentType("application / json");
446 request.setCharacterEncoding("UTF-8");
448 PrintWriter out = response.getWriter();
449 String responseString = mapper.writeValueAsString(displayString);
450 JSONObject j = new JSONObject("{policyData: " + responseString + "}");
451 out.write(j.toString());
452 } catch (Exception e) {
453 policyLogger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
457 private String constructJson(PolicyRestAdapter policyData) {
459 //Maps to assosciate the values read from the TermList dictionary
460 Map<Integer, String> srcIP_map =null;
461 Map<Integer, String> destIP_map=null;
462 Map<Integer, String> srcPort_map =null;
463 Map<Integer, String> destPort_map =null;
464 Map<Integer, String> action_map=null;
465 Map<Integer, String> fromZone_map=null;
466 Map<Integer, String> toZone_map=null;
468 String ruleDesc=null;
469 String ruleFromZone=null;
470 String ruleToZone=null;
471 String ruleSrcPrefixList=null;
472 String ruleDestPrefixList=null;
473 String ruleSrcPort=null;
474 String ruleDestPort=null;
475 String ruleAction=null;
480 List<String> expandableList = new ArrayList<>();
481 TermList jpaTermList;
482 TermCollector tc = new TermCollector();
483 SecurityZone jpaSecurityZone;
484 List<Term> termList = new ArrayList<>();
487 List<Tags>tagsList= new ArrayList<>();
489 TagDefines tagDefine= new TagDefines();
490 List<TagDefines> tagList=null;
491 ServiceListJson targetSl=null;
494 String networkRole="";
495 for(String tag:tagCollectorList){
497 List<Object> tagListData = commonClassDao.getData(FWTagPicker.class);
498 for(int tagCounter=0; tagCounter<tagListData.size(); tagCounter++){
499 FWTagPicker jpaTagPickerList=(FWTagPicker) tagListData.get(tagCounter);
500 if (jpaTagPickerList.getTagPickerName().equals(tag) ){
501 String tagValues=jpaTagPickerList.getTagValues();
502 tagList= new ArrayList<>();
503 for(String val:tagValues.split("#")) {
504 int index=val.indexOf(":");
505 String keyToStore=val.substring(0,index);
506 String valueToStore=val.substring(index+1,val.length());
508 tagDefine= new TagDefines();
509 tagDefine.setKey(keyToStore);
510 tagDefine.setValue(valueToStore);
511 //Add to the collection.
512 tagList.add(tagDefine);
515 networkRole=jpaTagPickerList.getNetworkRole();
519 tags.setTags(tagList);
520 tags.setTagPickerName(tag);
521 tags.setRuleName(termCollectorList.get(i));
522 tags.setNetworkRole(networkRole);
526 tc.setRuleToTag(tagsList);
528 for (int tl = 0 ; tl< termCollectorList.size(); tl++) {
529 expandableList.add(termCollectorList.get(tl));
530 Term targetTerm = new Term();
531 //targetSl= new ServiceListJson();
532 targetTerm.setRuleName(termCollectorList.get(tl));
533 List<Object> termListData = commonClassDao.getData(TermList.class);
534 for (int j =0; j < termListData.size(); j++) {
535 jpaTermList = (TermList) termListData.get(j);
536 if (jpaTermList.getTermName().equals(termCollectorList.get(tl))){
537 ruleDesc=jpaTermList.getTermDescription();
538 if ((ruleDesc!=null)&& (!ruleDesc.isEmpty())){
539 targetTerm.setDescription(ruleDesc);
541 ruleFromZone=jpaTermList.getFromZone();
543 if ((ruleFromZone != null) && (!ruleFromZone.isEmpty())){
544 fromZone_map = new HashMap<>();
545 fromZone_map.put(tl, ruleFromZone);
547 ruleToZone=jpaTermList.getToZone();
549 if ((ruleToZone != null) && (!ruleToZone.isEmpty())){
550 toZone_map = new HashMap<>();
551 toZone_map.put(tl, ruleToZone);
553 ruleSrcPrefixList=jpaTermList.getSrcIPList();
555 if ((ruleSrcPrefixList != null) && (!ruleSrcPrefixList.isEmpty())){
556 srcIP_map = new HashMap<>();
557 srcIP_map.put(tl, ruleSrcPrefixList);
560 ruleDestPrefixList= jpaTermList.getDestIPList();
561 if ((ruleDestPrefixList != null) && (!ruleDestPrefixList.isEmpty())){
562 destIP_map = new HashMap<>();
563 destIP_map.put(tl, ruleDestPrefixList);
566 ruleSrcPort=jpaTermList.getSrcPortList();
568 if (ruleSrcPort != null && (!ruleSrcPort.isEmpty())){
569 srcPort_map = new HashMap<>();
570 srcPort_map.put(tl, ruleSrcPort);
573 ruleDestPort= jpaTermList.getDestPortList();
575 if (ruleDestPort!= null && (!jpaTermList.getDestPortList().isEmpty())){
576 destPort_map = new HashMap<>();
577 destPort_map.put(tl, ruleDestPort);
580 ruleAction=jpaTermList.getAction();
582 if (( ruleAction!= null) && (!ruleAction.isEmpty())){
583 action_map = new HashMap<>();
584 action_map.put(tl, ruleAction);
588 targetTerm.setEnabled(true);
589 targetTerm.setLog(true);
590 targetTerm.setNegateSource(false);
591 targetTerm.setNegateDestination(false);
593 if(action_map!=null){
594 targetTerm.setAction(action_map.get(tl));
598 if(fromZone_map!=null){
599 List<String> fromZone= new ArrayList<>();
600 for(String fromZoneStr:fromZone_map.get(tl).split(",") ){
601 fromZone.add(fromZoneStr);
603 targetTerm.setFromZones(fromZone);
607 if(toZone_map!=null){
608 List<String> toZone= new ArrayList<>();
609 for(String toZoneStr:toZone_map.get(tl).split(",") ){
610 toZone.add(toZoneStr);
612 targetTerm.setToZones(toZone);
615 //Destination Services.
616 if(destPort_map!=null){
617 Set<ServicesJson> destServicesJsonList= new HashSet<>();
618 for(String destServices:destPort_map.get(tl).split(",") ){
619 ServicesJson destServicesJson= new ServicesJson();
620 destServicesJson.setType("REFERENCE");
621 if(destServices.equals("ANY")){
622 destServicesJson.setName("any");
623 destServicesJsonList.add(destServicesJson);
626 if(destServices.startsWith("Group_")){
627 destServicesJson.setName(destServices.substring(6,destServices.length()));
629 destServicesJson.setName(destServices);
631 destServicesJsonList.add(destServicesJson);
634 targetTerm.setDestServices(destServicesJsonList);
636 //ExpandableServicesList
637 if((srcPort_map!=null) && (destPort_map!=null)){
638 String servicesCollateString = (srcPort_map.get(tl) + "," + destPort_map.get(tl));
639 expandableServicesList.add(servicesCollateString);
640 }else if (srcPort_map!=null){
641 expandableServicesList.add(srcPort_map.get(tl));
642 }else if (destPort_map!=null){
643 expandableServicesList.add(destPort_map.get(tl));
648 List<AddressJson> sourceListArrayJson= new ArrayList<>();
649 for(String srcList:srcIP_map.get(tl).split(",") ){
650 AddressJson srcListJson= new AddressJson();
651 if(srcList.equals("ANY")){
652 srcListJson.setType("any");
653 sourceListArrayJson.add(srcListJson);
656 srcListJson.setType("REFERENCE");
657 if(srcList.startsWith("Group_")){
658 srcListJson.setName(srcList.substring(6,srcList.length()));
660 srcListJson.setName(srcList);
662 sourceListArrayJson.add(srcListJson);
665 targetTerm.setSourceList(sourceListArrayJson);
667 if(destIP_map!=null){
669 List<AddressJson> destListArrayJson= new ArrayList<>();
670 for(String destList:destIP_map.get(tl).split(",")){
671 AddressJson destListJson= new AddressJson();
672 if(destList.equals("ANY")){
673 destListJson.setType("any");
674 destListArrayJson.add(destListJson);
677 destListJson.setType("REFERENCE");
678 if(destList.startsWith("Group_")){
679 destListJson.setName(destList.substring(6,destList.length()));
681 destListJson.setName(destList);
683 destListArrayJson.add(destListJson);
686 targetTerm.setDestinationList(destListArrayJson);
688 //ExpandablePrefixIPList
689 if ((srcIP_map!=null) && (destIP_map!=null))
691 String collateString = (srcIP_map.get(tl) + "," + destIP_map
693 expandablePrefixIPList.add(collateString);
695 else if(srcIP_map!=null){
696 expandablePrefixIPList.add(srcIP_map.get(tl));
698 else if(destIP_map!=null){
699 expandablePrefixIPList.add(destIP_map.get(tl));
701 termList.add(targetTerm);
702 targetTerm.setPosition("" + (ruleCount++));
705 List<Object> securityZoneData = commonClassDao.getData(SecurityZone.class);
706 for (int j =0 ; j< securityZoneData.size() ; j++){
707 jpaSecurityZone = (SecurityZone) securityZoneData.get(j);
708 if (jpaSecurityZone.getZoneName().equals(policyData.getSecurityZone())){
709 tc.setSecurityZoneId(jpaSecurityZone.getZoneValue());
710 IdMap idMapInstance= new IdMap();
711 idMapInstance.setAstraId(jpaSecurityZone.getZoneValue());
712 idMapInstance.setVendorId("deviceGroup:dev");
714 List<IdMap> idMap = new ArrayList<IdMap>();
715 idMap.add(idMapInstance);
717 VendorSpecificData vendorStructure= new VendorSpecificData();
718 vendorStructure.setIdMap(idMap);
719 tc.setVendorSpecificData(vendorStructure);
724 tc.setServiceTypeId("/v0/firewall/pan");
725 tc.setConfigName(policyData.getConfigName());
726 tc.setVendorServiceId("vipr");
728 DeployNowJson deployNow= new DeployNowJson();
729 deployNow.setDeployNow(false);
731 tc.setDeploymentOption(deployNow);
733 Set<ServiceListJson> servListArray = new HashSet<>();
734 Set<ServiceGroupJson> servGroupArray= new HashSet<>();
735 Set<AddressGroupJson> addrGroupArray= new HashSet<>();
737 ServiceGroupJson targetSg= null;
738 AddressGroupJson addressSg=null;
739 ServiceListJson targetAny= null;
740 ServiceListJson targetAnyTcp=null;
741 ServiceListJson targetAnyUdp=null;
743 for(String serviceList:expandableServicesList){
744 for(String t: serviceList.split(",")){
745 if((!t.startsWith("Group_"))){
746 if(!t.equals("ANY")){
747 ServiceList sl = new ServiceList();
748 targetSl= new ServiceListJson();
749 sl= mappingServiceList(t);
750 targetSl.setName(sl.getServiceName());
751 targetSl.setDescription(sl.getServiceDescription());
752 targetSl.setTransportProtocol(sl.getServiceTransProtocol());
753 targetSl.setType(sl.getServiceType());
754 targetSl.setPorts(sl.getServicePorts());
755 servListArray.add(targetSl);
757 //Any for destinationServices.
758 //Add names any, any-tcp, any-udp to the serviceGroup object.
759 targetAny= new ServiceListJson();
760 targetAny.setName("any");
761 targetAny.setType("SERVICE");
762 targetAny.setTransportProtocol("any");
763 targetAny.setPorts("any");
765 servListArray.add(targetAny);
767 targetAnyTcp= new ServiceListJson();
768 targetAnyTcp.setName("any-tcp");
769 targetAnyTcp.setType("SERVICE");
770 targetAnyTcp.setTransportProtocol("tcp");
771 targetAnyTcp.setPorts("any");
773 servListArray.add(targetAnyTcp);
775 targetAnyUdp= new ServiceListJson();
776 targetAnyUdp.setName("any-udp");
777 targetAnyUdp.setType("SERVICE");
778 targetAnyUdp.setTransportProtocol("udp");
779 targetAnyUdp.setPorts("any");
781 servListArray.add(targetAnyUdp);
783 }else{//This is a group
784 GroupServiceList sg= new GroupServiceList();
785 targetSg= new ServiceGroupJson();
786 sg= mappingServiceGroup(t);
788 String name=sg.getGroupName();
789 //Removing the "Group_" prepending string before packing the JSON
790 targetSg.setName(name.substring(6,name.length()));
791 List<ServiceMembers> servMembersList= new ArrayList<>();
793 for(String groupString: sg.getServiceList().split(",")){
794 ServiceMembers serviceMembers= new ServiceMembers();
795 serviceMembers.setType("REFERENCE");
796 serviceMembers.setName(groupString);
797 servMembersList.add(serviceMembers);
798 //Expand the group Name
799 ServiceList expandGroupSl = new ServiceList();
800 targetSl= new ServiceListJson();
801 expandGroupSl= mappingServiceList(groupString);
803 targetSl.setName(expandGroupSl.getServiceName());
804 targetSl.setDescription(expandGroupSl.getServiceDescription());
805 targetSl.setTransportProtocol(expandGroupSl.getServiceTransProtocol());
806 targetSl.setType(expandGroupSl.getServiceType());
807 targetSl.setPorts(expandGroupSl.getServicePorts());
808 servListArray.add(targetSl);
811 targetSg.setMembers(servMembersList);
812 servGroupArray.add(targetSg);
818 Set<PrefixIPList> prefixIPList = new HashSet<>();
819 for(String prefixList:expandablePrefixIPList){
820 for(String prefixIP: prefixList.split(",")){
821 if((!prefixIP.startsWith("Group_"))){
822 if(!prefixIP.equals("ANY")){
823 List<AddressMembers> addMembersList= new ArrayList<>();
824 List<String> valueDesc= new ArrayList<>();
825 PrefixIPList targetAddressList = new PrefixIPList();
826 AddressMembers addressMembers= new AddressMembers();
827 targetAddressList.setName(prefixIP);
828 policyLogger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "PrefixList value:"+prefixIP);
829 valueDesc = mapping(prefixIP);
830 if(!valueDesc.isEmpty()){
831 policyLogger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "PrefixList description:"+valueDesc.get(1));
832 targetAddressList.setDescription(valueDesc.get(1));
836 addressMembers.setType("SUBNET");
837 if(!valueDesc.isEmpty()) {
838 addressMembers.setValue(valueDesc.get(0));
841 addMembersList.add(addressMembers);
843 targetAddressList.setMembers(addMembersList);
844 prefixIPList.add(targetAddressList);
847 else{//This is a group
848 AddressGroup ag= new AddressGroup();
849 addressSg= new AddressGroupJson();
850 ag= mappingAddressGroup(prefixIP);
852 String name=ag.getGroupName();
853 //Removing the "Group_" prepending string before packing the JSON
854 addressSg.setName(name.substring(6,name.length()));
856 List<AddressMembers> addrMembersList= new ArrayList<>();
857 for(String groupString: ag.getPrefixList().split(",")){
858 List<String> valueDesc= new ArrayList<>();
859 AddressMembers addressMembers= new AddressMembers();
860 valueDesc= mapping (groupString);
861 if(valueDesc.size() > 0){
862 addressMembers.setValue(valueDesc.get(0));
864 addressMembers.setType("SUBNET");
865 addrMembersList.add(addressMembers);
866 //Expand the group Name
868 addressSg.setMembers(addrMembersList);
869 addrGroupArray.add(addressSg);
876 Set<Object> serviceGroup= new HashSet<>();
878 for(Object obj1:servGroupArray){
879 serviceGroup.add(obj1);
882 for(Object obj:servListArray){
883 serviceGroup.add(obj);
886 Set<Object> addressGroup= new HashSet<>();
888 for(Object addObj:prefixIPList){
889 addressGroup.add(addObj);
892 for(Object addObj1:addrGroupArray){
893 addressGroup.add(addObj1);
896 tc.setServiceGroups(serviceGroup);
897 tc.setAddressGroups(addressGroup);
898 tc.setFirewallRuleList(termList);
900 ObjectWriter om = new ObjectMapper().writer();
902 json = om.writeValueAsString(tc);
903 } catch (Exception e) {
904 policyLogger.error("Exception Occured"+e);
907 }catch (Exception e) {
908 policyLogger.error("Exception Occured"+e);