VoyenceControl: How can I manipulate an IPv4 Address template variable?
search cancel

VoyenceControl: How can I manipulate an IPv4 Address template variable?


Article ID: 303503


Updated On:




VMware Smart Assurance - NCM


How can I manipulate an IPv4 Address template variable in VoyenceControl?

VoyenceControl supports some very basic Velocity-style syntax (see Note statements) to provide the string manipulation logic required to accomplish this task. This functionality allows you to capture, parse and modify an IP address in one way or another. 

As an example, consider a situation where you need to use an address in one place and a paired address (the address after) in a second. The IPv4 Address template begins with the following config lines:

logging <<myAddr>>
logging x.x.x.x+1

To make this change, you want to increment the last octet of myAddr by 1 to provide the paired address to the logging line for the paired address. To accomplish this, Velocity syntax can be used as follows (see Note statements):

## get a new reference to the template variable's content
#set ($ma = "<<myAddr>>")

## the value in $ma is a string, when we split it to get at each octet, those will be strings
## we need an Integer variable to enable parsing integers from the string values
#set ($Integer = 0)

## now to split the addr and parse out each octet into $a..d
#foreach ($sp in $ma.split('\.'))
#if ($velocityCount == 1) #set ($a = $Integer.parseInt($sp)) #end
#if ($velocityCount == 2) #set ($b = $Integer.parseInt($sp)) #end
#if ($velocityCount == 3) #set ($c = $Integer.parseInt($sp)) #end
#if ($velocityCount == 4) #set ($d = $Integer.parseInt($sp)) #end

## in the above foreach loop the $velocityCount varibale is used
## $velocityCount is a special velocity variable that tracks the loop counters
## this lets us avoid having to create and increment our own counter variable

## this is where you would have your formulas to get the other addr
## we only want to increment the last octet by 1
#set ($d = $d + 1)

## you could modify every octet if needed, be it simple math or conditional logic
##set ($a = 255 - $a)
##set ($b = 255 - $b)
##set ($c = 255 - $c)

## build the new address
#set ($newAddr = "$a.$b.$c.$d")

## now we have the content of the push

logging <<myAddr>>
logging $newAddr

Additional Information

The above example has been annotated using Velocity-style comments, or "##".  These lines can be removed from the example.
Spacing is very important when using Velocity-style syntax in templates.  Take special note of the spaces around parentheses, conditional values and operators.  Take note of the lack of spaces after the number signs.

Apache Velocity (formerly known as Jakarta Velocity) is an open source software project directed by the Apache Software Foundation. Velocity is a Java-based template engine that provides a simple yet powerful template language to reference objects defined in Java code.  There are many resources available on the web that go into further detail concerning syntax and code examples.