${scope()} with optional items

Hi!

I need to modify a rule to add more information.
The information I need can be extracted using the ${scope()} formatting since the information is in the tags.

Now to the problem:
The rule covers different vendors, not all vendors support the “extra” metric.
I believe that if I use a ${scope(“something”)} and it does not exist then the rule will fail

Any way to make the ${scope()} optional? Ie if the formatting can’t resolve then we can use a default value, say empty string? Might look a bit funny, but it’s still better than a failure.

Example on a device with the metric:
{scope(\"name\")} ({scope(“peerip”)} - ${scope(“remote-peer-name”)})” would resolve to
Safenet_SAS_VPN (64.26.xx.xx - SafeNet_SAS_NA)
whilst on the devices that the metrics does not exist it would be presented thus
Safenet_SAS_VPN (64.26.xx.xx - )

Or is there another solution?
I see in the same rule(VpnTunnelIsDownRule) that there are two variants, one for vsx and one for non-vsx.
Would it be possible to multiply the derived classes and make a Vsx where the extra tag exist + a Non-Vsx where the extra tag exists? Dirty workaround… :wink:

Unfortunately this feature isn’t supported at the moment :frowning:

The workaround could work, but it would be a fragile and delicate solution.

On the first variation – the one expecting to find the tag – said tag must be explicitly selected in the “select device” part:

    SelectTagsExpression(context.metaDao, Set("device-id", "my-tag"), True)

This is so the rule-engine would know to include said tag in the scope. Note that in many rules "device-id" might be replaced with DeviceKey which is just an alias to the former string literal.

On the second variation – the one expecting not to find the tag – said tag must be explicitly conditioned to not exist in the “select device” part:

    SelectTagsExpression(context.metaDao, Set("device-id"), !Exists("my-tag"))

This is especially important since we do not want devices with the tag to be evaluated by both variants. Also, since Exists was probably not used previously in said rule, you must import it:

import com.indeni.server.common.data.conditions.Exists
1 Like