Junos Space Developer
Highlighted
Junos Space Developer

Check if an array contains a value, in a configlet

[ Edited ]
‎06-26-2020 08:13 AM

I've got a configlet to apply particular SCREENs to particular zones.  When the number of zones is small a chain of ORs aren't too ugly, and this works:

 

security {
    zones {
      #foreach ( $SecurityZone in $SecurityZones )
        security-zone $SecurityZone {
            #if ( $SecurityZone == "untrust" || $SecurityZone == "mobile" ) ## <========================
            screen Outside;
            #elseif( $SecurityZone == "Admin" || $SecurityZone == "Engineering" || $SecurityZone == "Finance" || $SecurityZone == "trust" ) ## <========================
            screen Inside;
            #end
        }
      #end
    }
}

But when the list of zones gets longer, it would be tidier to put them in array/lists and check if the $SecurityZone is in the lists.  Something like this:

#set ( $zones.Outside = ["untrust", "mobile"] )
#set ( $zones.Inside = ["Admin", "Engineering", "Finance", "trust"] ) security { zones { #foreach ( $SecurityZone in $SecurityZones ) security-zone $SecurityZone { #if( $zones.Outside.Contains($SecurityZone) ) ## <======================= screen Outside; #elseif( $zones.Inside.Contains($SecurityZone) ) ## <======================= screen Inside; #end } #end } }

From what I could find on VTL (without trawling through too much text) that ".Contains" should return TRUE if the array contains the value $SecurityZone, but it's not working.  Have I got the command wrong?

1 REPLY 1
Highlighted
Junos Space Developer
Solution
Accepted by topic author atinglin
‎06-29-2020 08:48 AM

Re: Check if an array contains a value, in a configlet

[ Edited ]
‎06-29-2020 07:21 AM

".contains" works, with a list name without fullstops:

 

 

#set ( $Outside = ["untrust", "mobile"] )
#set ( $Inside = ["Admin", "Engineering", "Finance", "trust"] )
security {
    zones {
      #foreach ( $SecurityZone in $SecurityZones )
        security-zone $SecurityZone {
            #if( $Outside.contains($SecurityZone) ) ## <=======================
            screen Outside;
            #elseif( $Inside.contains($SecurityZone) ) ## <=======================
            screen Inside;
            #end
        }
      #end
    }
}

I found a link in the Junos Space Network Management Platform Complete Software Guide to http://velocity.apache.org/engine/1.7/user-guide.html#methods, where it says "A method is defined in the Java code", which led to a web search and https://docs.oracle.com/javase/7/docs/api/java/util/AbstractList.html, which has the "contains" method: https://docs.oracle.com/javase/7/docs/api/java/util/AbstractCollection.html#contains(java.lang.Objec...

 

Hopefully this will help someone else.

 

 

Feedback