*/
package org.openecomp.policy.controller;
-
-
-import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import org.openecomp.policy.rest.adapter.AddressJson;
import org.openecomp.policy.rest.adapter.AddressMembers;
import org.openecomp.policy.rest.adapter.DeployNowJson;
+import org.openecomp.policy.rest.adapter.IdMap;
import org.openecomp.policy.rest.adapter.PolicyRestAdapter;
import org.openecomp.policy.rest.adapter.PrefixIPList;
import org.openecomp.policy.rest.adapter.ServiceGroupJson;
import org.openecomp.policy.rest.adapter.Tags;
import org.openecomp.policy.rest.adapter.Term;
import org.openecomp.policy.rest.adapter.TermCollector;
+import org.openecomp.policy.rest.adapter.VendorSpecificData;
import org.openecomp.policy.rest.dao.CommonClassDao;
import org.openecomp.policy.rest.jpa.AddressGroup;
import org.openecomp.policy.rest.jpa.FWTagPicker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.servlet.ModelAndView;
-import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.DeserializationFeature;
-import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
+import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
@Controller
@RequestMapping("/")
public class CreateFirewallController extends RestrictedBaseController {
- private static Logger logger = FlexLogger.getLogger(CreateFirewallController.class);
+ private static Logger policyLogger = FlexLogger.getLogger(CreateFirewallController.class);
@Autowired
SessionFactory sessionFactory;
private List<String> tagCollectorList;
private String jsonBody;
- List<String> expandablePrefixIPList = new ArrayList<String>();
- List<String> expandableServicesList= new ArrayList<String>();
+ List<String> expandablePrefixIPList = new ArrayList<>();
+ List<String> expandableServicesList= new ArrayList<>();
@Autowired
private CreateFirewallController(CommonClassDao commonClassDao){
CreateFirewallController.commonClassDao = commonClassDao;
public PolicyRestAdapter setDataToPolicyRestAdapter(PolicyRestAdapter policyData){
- termCollectorList = new ArrayList<String>();
- tagCollectorList = new ArrayList<String>();
- if(policyData.getAttributes().size() > 0){
+ termCollectorList = new ArrayList<>();
+ tagCollectorList = new ArrayList<>();
+ if(!policyData.getAttributes().isEmpty()){
for(Object attribute : policyData.getAttributes()){
if(attribute instanceof LinkedHashMap<?, ?>){
String key = ((LinkedHashMap<?, ?>) attribute).get("key").toString();
}
}
jsonBody = constructJson(policyData);
- if (jsonBody != null || jsonBody.equalsIgnoreCase("")) {
+ if (jsonBody != null && !jsonBody.equalsIgnoreCase("")) {
policyData.setJsonBody(jsonBody);
-
} else {
policyData.setJsonBody("{}");
}
}
private List<String> mapping(String expandableList) {
- String value = new String();
- String desc = new String();
- List <String> valueDesc= new ArrayList<String>();
+ List <String> valueDesc= new ArrayList<>();
List<Object> prefixListData = commonClassDao.getData(PrefixList.class);
for (int i = 0; i< prefixListData.size(); i++) {
PrefixList prefixList = (PrefixList) prefixListData.get(i);
if (prefixList.getPrefixListName().equals(expandableList)) {
- value = prefixList.getPrefixListValue();
+ String value = prefixList.getPrefixListValue();
valueDesc.add(value);
- desc= prefixList.getDescription();
+ String desc= prefixList.getDescription();
valueDesc.add(desc);
break;
}
}
public void prePopulateFWPolicyData(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
- attributeList = new ArrayList<Object>();
+ attributeList = new ArrayList<>();
if (policyAdapter.getPolicyData() instanceof PolicyType) {
Object policyData = policyAdapter.getPolicyData();
PolicyType policy = (PolicyType) policyData;
// policy name value is the policy name without any prefix and Extensions.
policyAdapter.setOldPolicyFileName(policyAdapter.getPolicyName());
String policyNameValue = policyAdapter.getPolicyName().substring(policyAdapter.getPolicyName().indexOf("FW_") +3);
- if (logger.isDebugEnabled()) {
- logger.debug("Prepopulating form data for Config Policy selected:"+ policyAdapter.getPolicyName());
+ if (policyLogger.isDebugEnabled()) {
+ policyLogger.debug("Prepopulating form data for Config Policy selected:"+ policyAdapter.getPolicyName());
}
policyAdapter.setPolicyName(policyNameValue);
String description = "";
try{
description = policy.getDescription().substring(0, policy.getDescription().indexOf("@CreatedBy:"));
}catch(Exception e){
+ policyLogger.info("General error", e);
description = policy.getDescription();
}
policyAdapter.setPolicyDescription(description);
}
}
catch(Exception e) {
- logger.error("Exception Caused while Retriving the JSON body data" +e);
+ policyLogger.error("Exception Caused while Retriving the JSON body data" +e);
}
Map<String, String> termTagMap=null;
-
- for(int i=0;i<tc1.getFirewallRuleList().size();i++){
- termTagMap = new HashMap<String, String>();
- String ruleName= tc1.getFirewallRuleList().get(i).getRuleName();
- String tagPickerName=tc1.getRuleToTag().get(i).getTagPickerName();
- termTagMap.put("key", ruleName);
- termTagMap.put("value", tagPickerName);
- attributeList.add(termTagMap);
+ if(tc1 != null){
+ for(int i=0;i<tc1.getFirewallRuleList().size();i++){
+ termTagMap = new HashMap<String, String>();
+ String ruleName= tc1.getFirewallRuleList().get(i).getRuleName();
+ String tagPickerName=tc1.getRuleToTag().get(i).getTagPickerName();
+ termTagMap.put("key", ruleName);
+ termTagMap.put("value", tagPickerName);
+ attributeList.add(termTagMap);
+ }
}
policyAdapter.setAttributes(attributeList);
// Get the target data under policy.
// Under target we have AnyOFType
List<AnyOfType> anyOfList = target.getAnyOf();
if (anyOfList != null) {
- int index = 0;
Iterator<AnyOfType> iterAnyOf = anyOfList.iterator();
while (iterAnyOf.hasNext()) {
AnyOfType anyOf = iterAnyOf.next();
//
AttributeValueType attributeValue = match.getAttributeValue();
String value = (String) attributeValue.getContent().get(0);
- if (index == 1) {
+ AttributeDesignatorType designator = match.getAttributeDesignator();
+ String attributeId = designator.getAttributeId();
+ if (attributeId.equals("ConfigName")) {
policyAdapter.setConfigName(value);
}
- if (index == 2){
+ if (attributeId.equals("RiskType")){
policyAdapter.setRiskType(value);
}
-
- if (index == 3){
+ if (attributeId.equals("RiskLevel")){
policyAdapter.setRiskLevel(value);
}
-
- if (index == 4){
+ if (attributeId.equals("guard")){
policyAdapter.setGuard(value);
}
- if (index == 5 && !value.contains("NA")){
+ if (attributeId.equals("TTLDate") && !value.contains("NA")){
String newDate = convertDate(value, true);
policyAdapter.setTtlDate(newDate);
}
- index++;
}
}
}
}
@RequestMapping(value={"/policyController/ViewFWPolicyRule.htm"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
- public ModelAndView setFWViewRule(HttpServletRequest request, HttpServletResponse response) throws Exception{
+ public void setFWViewRule(HttpServletRequest request, HttpServletResponse response){
try {
- termCollectorList = new ArrayList<String>();
+ termCollectorList = new ArrayList<>();
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
JsonNode root = mapper.readTree(request.getReader());
PolicyRestAdapter policyData = (PolicyRestAdapter)mapper.readValue(root.get("policyData").toString(), PolicyRestAdapter.class);
- if(policyData.getAttributes().size() > 0){
+ if(!policyData.getAttributes().isEmpty()){
for(Object attribute : policyData.getAttributes()){
if(attribute instanceof LinkedHashMap<?, ?>){
String key = ((LinkedHashMap<?, ?>) attribute).get("key").toString();
String ruleSrcPort=null;
String ruleDestPort=null;
String ruleAction=null;
- List <String> valueDesc= new ArrayList<String>();
+ List <String> valueDesc= new ArrayList<>();
StringBuffer displayString = new StringBuffer();
for (String id : termCollectorList) {
List<Object> tmList = commonClassDao.getDataById(TermList.class, "termName", id);
}
}
}
- displayString.append("\n");
+ displayString.append("\n");
}
ruleAction=(jpaTermList).getAction();
String responseString = mapper.writeValueAsString(displayString);
JSONObject j = new JSONObject("{policyData: " + responseString + "}");
out.write(j.toString());
- return null;
} catch (Exception e) {
- logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
+ policyLogger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + e);
}
- return null;
}
private String constructJson(PolicyRestAdapter policyData) {
String json = null;
- List<String> expandableList = new ArrayList<String>();
+ List<String> expandableList = new ArrayList<>();
TermList jpaTermList;
TermCollector tc = new TermCollector();
SecurityZone jpaSecurityZone;
- List<Term> termList = new ArrayList<Term>();
+ List<Term> termList = new ArrayList<>();
Tags tags=null;
- List<Tags>tagsList= new ArrayList<Tags>();
+ List<Tags>tagsList= new ArrayList<>();
TagDefines tagDefine= new TagDefines();
List<TagDefines> tagList=null;
ServiceListJson targetSl=null;
int i=0;
try{
-
+ String networkRole="";
for(String tag:tagCollectorList){
tags= new Tags();
List<Object> tagListData = commonClassDao.getData(FWTagPicker.class);
FWTagPicker jpaTagPickerList=(FWTagPicker) tagListData.get(tagCounter);
if (jpaTagPickerList.getTagPickerName().equals(tag) ){
String tagValues=jpaTagPickerList.getTagValues();
- tagList= new ArrayList<TagDefines>();
+ tagList= new ArrayList<>();
for(String val:tagValues.split("#")) {
int index=val.indexOf(":");
String keyToStore=val.substring(0,index);
tagList.add(tagDefine);
}
-
+ networkRole=jpaTagPickerList.getNetworkRole();
+ break;
}
}
tags.setTags(tagList);
tags.setTagPickerName(tag);
tags.setRuleName(termCollectorList.get(i));
+ tags.setNetworkRole(networkRole);
tagsList.add(tags);
i++;
}
ruleFromZone=jpaTermList.getFromZone();
if ((ruleFromZone != null) && (!ruleFromZone.isEmpty())){
- fromZone_map = new HashMap<Integer, String>();
+ fromZone_map = new HashMap<>();
fromZone_map.put(tl, ruleFromZone);
}
ruleToZone=jpaTermList.getToZone();
if ((ruleToZone != null) && (!ruleToZone.isEmpty())){
- toZone_map = new HashMap<Integer, String>();
+ toZone_map = new HashMap<>();
toZone_map.put(tl, ruleToZone);
}
ruleSrcPrefixList=jpaTermList.getSrcIPList();
if ((ruleSrcPrefixList != null) && (!ruleSrcPrefixList.isEmpty())){
- srcIP_map = new HashMap<Integer, String>();
+ srcIP_map = new HashMap<>();
srcIP_map.put(tl, ruleSrcPrefixList);
}
ruleDestPrefixList= jpaTermList.getDestIPList();
if ((ruleDestPrefixList != null) && (!ruleDestPrefixList.isEmpty())){
- destIP_map = new HashMap<Integer, String>();
+ destIP_map = new HashMap<>();
destIP_map.put(tl, ruleDestPrefixList);
}
ruleSrcPort=jpaTermList.getSrcPortList();
if (ruleSrcPort != null && (!ruleSrcPort.isEmpty())){
- srcPort_map = new HashMap<Integer, String>();
+ srcPort_map = new HashMap<>();
srcPort_map.put(tl, ruleSrcPort);
}
ruleDestPort= jpaTermList.getDestPortList();
if (ruleDestPort!= null && (!jpaTermList.getDestPortList().isEmpty())){
- destPort_map = new HashMap<Integer, String>();
+ destPort_map = new HashMap<>();
destPort_map.put(tl, ruleDestPort);
}
ruleAction=jpaTermList.getAction();
if (( ruleAction!= null) && (!ruleAction.isEmpty())){
- action_map = new HashMap<Integer, String>();
+ action_map = new HashMap<>();
action_map.put(tl, ruleAction);
}
}
//FromZone arrays
if(fromZone_map!=null){
- List<String> fromZone= new ArrayList<String>();
+ List<String> fromZone= new ArrayList<>();
for(String fromZoneStr:fromZone_map.get(tl).split(",") ){
fromZone.add(fromZoneStr);
}
//ToZone arrays
if(toZone_map!=null){
- List<String> toZone= new ArrayList<String>();
+ List<String> toZone= new ArrayList<>();
for(String toZoneStr:toZone_map.get(tl).split(",") ){
toZone.add(toZoneStr);
}
//Destination Services.
if(destPort_map!=null){
- Set<ServicesJson> destServicesJsonList= new HashSet<ServicesJson>();
+ Set<ServicesJson> destServicesJsonList= new HashSet<>();
for(String destServices:destPort_map.get(tl).split(",") ){
ServicesJson destServicesJson= new ServicesJson();
destServicesJson.setType("REFERENCE");
if(srcIP_map!=null){
//Source List
- List<AddressJson> sourceListArrayJson= new ArrayList<AddressJson>();
+ List<AddressJson> sourceListArrayJson= new ArrayList<>();
for(String srcList:srcIP_map.get(tl).split(",") ){
AddressJson srcListJson= new AddressJson();
if(srcList.equals("ANY")){
}
if(destIP_map!=null){
//Destination List
- List<AddressJson> destListArrayJson= new ArrayList<AddressJson>();
+ List<AddressJson> destListArrayJson= new ArrayList<>();
for(String destList:destIP_map.get(tl).split(",")){
AddressJson destListJson= new AddressJson();
if(destList.equals("ANY")){
jpaSecurityZone = (SecurityZone) securityZoneData.get(j);
if (jpaSecurityZone.getZoneName().equals(policyData.getSecurityZone())){
tc.setSecurityZoneId(jpaSecurityZone.getZoneValue());
- //setParentSecurityZone(jpaSecurityZone.getZoneValue());//For storing the securityZone IDs to the DB
+ IdMap idMapInstance= new IdMap();
+ idMapInstance.setAstraId(jpaSecurityZone.getZoneValue());
+ idMapInstance.setVendorId("deviceGroup:dev");
+
+ List<IdMap> idMap = new ArrayList<IdMap>();
+ idMap.add(idMapInstance);
+
+ VendorSpecificData vendorStructure= new VendorSpecificData();
+ vendorStructure.setIdMap(idMap);
+ tc.setVendorSpecificData(vendorStructure);
break;
}
}
tc.setServiceTypeId("/v0/firewall/pan");
tc.setConfigName(policyData.getConfigName());
+ tc.setVendorServiceId("vipr");
- //Astra is rejecting the packet when it sees a new JSON field, so removing it for now.
- //tc.setTemplateVersion(XACMLProperties.getProperty(XACMLRestProperties.TemplateVersion_FW));
-
DeployNowJson deployNow= new DeployNowJson();
deployNow.setDeployNow(false);
tc.setDeploymentOption(deployNow);
- Set<ServiceListJson> servListArray = new HashSet<ServiceListJson>();
- Set<ServiceGroupJson> servGroupArray= new HashSet<ServiceGroupJson>();
- Set<AddressGroupJson> addrGroupArray= new HashSet<AddressGroupJson>();
+ Set<ServiceListJson> servListArray = new HashSet<>();
+ Set<ServiceGroupJson> servGroupArray= new HashSet<>();
+ Set<AddressGroupJson> addrGroupArray= new HashSet<>();
ServiceGroupJson targetSg= null;
AddressGroupJson addressSg=null;
String name=sg.getGroupName();
//Removing the "Group_" prepending string before packing the JSON
targetSg.setName(name.substring(6,name.length()));
- List<ServiceMembers> servMembersList= new ArrayList<ServiceMembers>();
+ List<ServiceMembers> servMembersList= new ArrayList<>();
for(String groupString: sg.getServiceList().split(",")){
ServiceMembers serviceMembers= new ServiceMembers();
}
}
- Set<PrefixIPList> prefixIPList = new HashSet<PrefixIPList>();
+ Set<PrefixIPList> prefixIPList = new HashSet<>();
for(String prefixList:expandablePrefixIPList){
for(String prefixIP: prefixList.split(",")){
if((!prefixIP.startsWith("Group_"))){
if(!prefixIP.equals("ANY")){
- List<AddressMembers> addMembersList= new ArrayList<AddressMembers>();
- List<String> valueDesc= new ArrayList<String>();
+ List<AddressMembers> addMembersList= new ArrayList<>();
+ List<String> valueDesc= new ArrayList<>();
PrefixIPList targetAddressList = new PrefixIPList();
AddressMembers addressMembers= new AddressMembers();
targetAddressList.setName(prefixIP);
- logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "PrefixList value:"+prefixIP);
+ policyLogger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "PrefixList value:"+prefixIP);
valueDesc = mapping(prefixIP);
if(!valueDesc.isEmpty()){
- logger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "PrefixList description:"+valueDesc.get(1));
+ policyLogger.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + "PrefixList description:"+valueDesc.get(1));
targetAddressList.setDescription(valueDesc.get(1));
}
//Removing the "Group_" prepending string before packing the JSON
addressSg.setName(name.substring(6,name.length()));
- List<AddressMembers> addrMembersList= new ArrayList<AddressMembers>();
+ List<AddressMembers> addrMembersList= new ArrayList<>();
for(String groupString: ag.getPrefixList().split(",")){
- List<String> valueDesc= new ArrayList<String>();
+ List<String> valueDesc= new ArrayList<>();
AddressMembers addressMembers= new AddressMembers();
valueDesc= mapping (groupString);
if(valueDesc.size() > 0){
}
}
- Set<Object> serviceGroup= new HashSet<Object>();
+ Set<Object> serviceGroup= new HashSet<>();
for(Object obj1:servGroupArray){
serviceGroup.add(obj1);
serviceGroup.add(obj);
}
- Set<Object> addressGroup= new HashSet<Object>();
+ Set<Object> addressGroup= new HashSet<>();
for(Object addObj:prefixIPList){
addressGroup.add(addObj);
ObjectWriter om = new ObjectMapper().writer();
try {
json = om.writeValueAsString(tc);
- } catch (JsonGenerationException e) {
- e.printStackTrace();
- } catch (JsonMappingException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
+ } catch (Exception e) {
+ policyLogger.error("Exception Occured"+e);
}
}catch (Exception e) {
- e.printStackTrace();
+ policyLogger.error("Exception Occured"+e);
}
return json;