cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Announcements
177
Views
0
Helpful
1
Replies
khgrant
Cisco Employee

Un-Escaping Special Characters while applying on devices

 

Hi Team,

 

 

How to can we make the com.tailf.ncs.template.Template.apply method  interpret the escaped special characters and not escape them while applying them on the device?

 

 

The example listed below gets applied on the device along with escaped special characters.

 

 

TemplateVariables variables = new TemplateVariables();

 

  Template template = new Template(context, myTemplateName);

 

 

   String key = “SPEC-CHARS”;

 

   String value = "CISCO\\SIO\"ASPASSWORD”;

 

   // Backslash and Double quotes are escaped

 

   variables.setProperty(key, value);

 

   template.apply(service, variables);

 

 

 

Thanks,

 

 

Krishna

 

1 ACCEPTED SOLUTION

Accepted Solutions
khgrant
Cisco Employee

 

Krishna,

 

 

How to can we make the com.tailf.ncs.template.Template.apply method  interpret the escaped special characters and not escape them while applying them on the device?

 

 

The example listed below gets applied on the device along with escaped special characters.

 

 

TemplateVariables variables = new TemplateVariables();

 

  Template template = new Template(context, myTemplateName);

 

 

   String key = “SPEC-CHARS”;

 

   String value = "CISCO\\SIO\"ASPASSWORD”;

 

   // Backslash and Double quotes are escaped

 

   variables.setProperty(key, value);

 

   template.apply(service, variables);

 

 

Just as you have realized, you can handle your own quoting of the value by using the .setProperty() method. So by having some code like the one below, you can handle both strings with single quotes or strings with double quotes embedded in the string. In order to handle strings with both types of quote characters embedded, something a little more elaborate is required, using the xpath concat() function.

 

 

        Template myTemplate = new Template(context, "quote");

 

        TemplateVariables myVars = new TemplateVariables();

 

 

        try {

 

            String quotedValue;

 

            String description = service.leaf("desc").valueAsString();

 

           

 

            if (description.indexOf('\'') == -1) {

 

                quotedValue = "'"+description+"'";

 

            } else {

 

                // We have single quotes in the string

 

                // Remove any double quotes and replace them with single ones

 

                quotedValue = "\""+description.replace('"', '\'')+"\"";

 

            }

 

            myVars.setProperty("DESCRIPTION", quotedValue);

 

            myTemplate.apply(service, myVars);

 

 

When I use run this code it might look like this:

 

 

admin@ncs(config-quote-x)# desc Hello"World

 

admin@ncs(config-quote-x)# commit dry-run

 

device ios0

 

   config {

 

  -    ios:hostname Hello;

 

  +    ios:hostname "Hello\"World";

 

   }

 

admin@ncs(config-quote-x)# commit

 

Commit complete.

 

 

Ok, so for some reason I got some double quotes around and a backslash inserted in my string?! So what's wrong with my java? Nothing actually. The correct string was received by NSO and send to the device. It's just the NSO CLI that prefers to put quotes around strings with spaces and other odd characters, and escape double quote characters with a backslash. Don't believe me? So let's ask the device.

 

 

$ netconf-console --get-config --port=12022|grep hostname

 

    <hostname xmlns="urn:ios">Hello"World</hostname>

 

 

Well I guess it also depends on the device. Here I was using netsim, i.e. ConfD. Other devices might interpret this differently. If so that's bad luck. Maybe we have to do something in the NED. With the service java above, we did what we could, sending down the right thing.

 

 

Best Regards,

 

/jan

 

View solution in original post

1 REPLY 1
khgrant
Cisco Employee

 

Krishna,

 

 

How to can we make the com.tailf.ncs.template.Template.apply method  interpret the escaped special characters and not escape them while applying them on the device?

 

 

The example listed below gets applied on the device along with escaped special characters.

 

 

TemplateVariables variables = new TemplateVariables();

 

  Template template = new Template(context, myTemplateName);

 

 

   String key = “SPEC-CHARS”;

 

   String value = "CISCO\\SIO\"ASPASSWORD”;

 

   // Backslash and Double quotes are escaped

 

   variables.setProperty(key, value);

 

   template.apply(service, variables);

 

 

Just as you have realized, you can handle your own quoting of the value by using the .setProperty() method. So by having some code like the one below, you can handle both strings with single quotes or strings with double quotes embedded in the string. In order to handle strings with both types of quote characters embedded, something a little more elaborate is required, using the xpath concat() function.

 

 

        Template myTemplate = new Template(context, "quote");

 

        TemplateVariables myVars = new TemplateVariables();

 

 

        try {

 

            String quotedValue;

 

            String description = service.leaf("desc").valueAsString();

 

           

 

            if (description.indexOf('\'') == -1) {

 

                quotedValue = "'"+description+"'";

 

            } else {

 

                // We have single quotes in the string

 

                // Remove any double quotes and replace them with single ones

 

                quotedValue = "\""+description.replace('"', '\'')+"\"";

 

            }

 

            myVars.setProperty("DESCRIPTION", quotedValue);

 

            myTemplate.apply(service, myVars);

 

 

When I use run this code it might look like this:

 

 

admin@ncs(config-quote-x)# desc Hello"World

 

admin@ncs(config-quote-x)# commit dry-run

 

device ios0

 

   config {

 

  -    ios:hostname Hello;

 

  +    ios:hostname "Hello\"World";

 

   }

 

admin@ncs(config-quote-x)# commit

 

Commit complete.

 

 

Ok, so for some reason I got some double quotes around and a backslash inserted in my string?! So what's wrong with my java? Nothing actually. The correct string was received by NSO and send to the device. It's just the NSO CLI that prefers to put quotes around strings with spaces and other odd characters, and escape double quote characters with a backslash. Don't believe me? So let's ask the device.

 

 

$ netconf-console --get-config --port=12022|grep hostname

 

    <hostname xmlns="urn:ios">Hello"World</hostname>

 

 

Well I guess it also depends on the device. Here I was using netsim, i.e. ConfD. Other devices might interpret this differently. If so that's bad luck. Maybe we have to do something in the NED. With the service java above, we did what we could, sending down the right thing.

 

 

Best Regards,

 

/jan

 

View solution in original post

Create
Recognize Your Peers
Content for Community-Ad