<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Premkumar Chalmeti</title>
    <description>The latest articles on DEV Community by Premkumar Chalmeti (@premchalmeti).</description>
    <link>https://dev.to/premchalmeti</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F676334%2F533d9d25-f635-4932-bc3f-087a9946759d.jpeg</url>
      <title>DEV Community: Premkumar Chalmeti</title>
      <link>https://dev.to/premchalmeti</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/premchalmeti"/>
    <language>en</language>
    <item>
      <title>Trees, the essential heroes of data structures</title>
      <dc:creator>Premkumar Chalmeti</dc:creator>
      <pubDate>Mon, 30 Aug 2021 05:21:35 +0000</pubDate>
      <link>https://dev.to/premchalmeti/trees-the-essential-heroes-of-data-structures-2mne</link>
      <guid>https://dev.to/premchalmeti/trees-the-essential-heroes-of-data-structures-2mne</guid>
      <description>&lt;p&gt;Most of the softwares under the hood are data structures and algorithms. These are the essential tools(skills) for software craftsmen. If you are a developer, you must have come across a task where you had to use data structures and apply an algorithm to solve the problem.&lt;/p&gt;

&lt;h3&gt;
  
  
  TL;DR;
&lt;/h3&gt;

&lt;p&gt;This article explains a use case on how trees are useful for organizing hierarchical data and processing them using a recursive algorithm.&lt;br&gt;
The source code is available at &lt;a href="https://github.com/premchalmeti/clish_xml_parser" rel="noopener noreferrer"&gt;https://github.com/premchalmeti/clish_xml_parser&lt;/a&gt;&lt;/p&gt;



&lt;p&gt;I recently started working with a virtualization products company that operates in enterprise data services. A virtual machine contains storage disks attached to it and these VMs collectively called storage clusters.&lt;/p&gt;

&lt;p&gt;The clusters had an admin CLI(Command Line Interface) using CLISH(pronounce: see-lish) framework. My job was to create a safe CLI for users to execute mostly read-only commands.&lt;/p&gt;
&lt;h3&gt;
  
  
  What is CLISH??
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;A CLISH(&lt;strong&gt;C&lt;/strong&gt;ommand &lt;strong&gt;L&lt;/strong&gt;ine &lt;strong&gt;I&lt;/strong&gt;nterface &lt;strong&gt;SH&lt;/strong&gt;ell) is an open-source framework used to create CLI on *nix platforms.&lt;/li&gt;
&lt;li&gt;In CLISH, adding a new menu in the console is as simple as writing an XML(&lt;code&gt;clock.xml&lt;/code&gt;) file. You just have to define all your commands(&lt;em&gt;clock set&lt;/em&gt;, &lt;em&gt;clock timezone&lt;/em&gt;) in an XML file at a single location.&lt;/li&gt;
&lt;li&gt;Now, The CLISH reads these XMLs and generate an instance of CLI like this,&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpg9rb7sdsx81g2weohne.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpg9rb7sdsx81g2weohne.png" title="CLISH Console" alt="CLISH Console"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Problem 💭
&lt;/h3&gt;

&lt;p&gt;Let's explore the problem by taking an example,&lt;/p&gt;

&lt;p&gt;In admin CLI, a network menu has all the commands like &lt;em&gt;network show&lt;/em&gt;, &lt;em&gt;network device add&lt;/em&gt;, etc.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;In user CLI, a network module (&lt;code&gt;user_network.xml&lt;/code&gt;) has only safe commands in it.&lt;br&gt;
A safe command doesn't alter the state (configuration to be precise) of the cluster.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;network device add&lt;/em&gt;: not safe (add a new device hence alters the state of the clusters)&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;network show&lt;/em&gt;: safe&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This safe command's definition is provided in a JSON schema file.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Now, the first step in the process of creating a CLI was to write a parser that does the following things.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Read the JSON schema file&lt;/li&gt;
&lt;li&gt;Loads the corresponding source XML file (&lt;em&gt;source_file&lt;/em&gt; key in the JSON)&lt;/li&gt;
&lt;li&gt;Parse the XML and remove the unsafe commands (The algorithm is explained later)&lt;/li&gt;
&lt;li&gt;Output the safe XML (&lt;code&gt;user_network.xml&lt;/code&gt;) to be used for user CLI&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;However, we can not directly use the schema file for parsing. If you notice in the above XML file the CLI has a particular format to group related commands under a parent command.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;network device&lt;/em&gt; groups &lt;em&gt;network device add&lt;/em&gt;, &lt;em&gt;network device delete&lt;/em&gt;, etc&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;network device configure&lt;/em&gt; groups &lt;em&gt;network device configure rename&lt;/em&gt;, &lt;em&gt;network device configure ip&lt;/em&gt;, etc&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this case, if all child commands are removed (are unsafe) then the parent command has to be removed and so on till the root node.&lt;/p&gt;

&lt;p&gt;Iterating a list of commands without any relation among them is difficult to operate on.&lt;/p&gt;

&lt;h3&gt;
  
  
  Solution 💡
&lt;/h3&gt;

&lt;p&gt;This stringent parsing requires the commands to be organized in a hierarchy then recursively process the nodes from bottom to up. (The image gives more idea).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Trees are best data structures for storing data in hierarchical order&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A simple variant of &lt;strong&gt;general trees&lt;/strong&gt; fits the bill. A general tree contains zero or more child nodes. &lt;/p&gt;

&lt;p&gt;We can tokenize each command and prepare a common general tree definition from the current schema file. We will refer to this tree as &lt;em&gt;CmdTree&lt;/em&gt;. A general tree for the &lt;code&gt;admin_network.xml&lt;/code&gt; constructed using the schema file looks below&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F63if4yndaolqh1f26tu0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F63if4yndaolqh1f26tu0.png" title="Tree to XML Mapping" alt="Tree to XML Mapping"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Each node in the above tree has a cmd token (network, show, device) and has some more metadata used while parsing.&lt;/p&gt;

&lt;p&gt;Finally, we can now derive this recursive algorithm for parsing the XML.&lt;/p&gt;

&lt;h3&gt;
  
  
  Algorithm 📝
&lt;/h3&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The final XML will look like this.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Since this XML only contains safe commands after parsing. These XMLs can now be used for creating a safe CLI for the users.&lt;/p&gt;

&lt;p&gt;The source code is available at &lt;a href="https://github.com/premchalmeti/clish_xml_parser" rel="noopener noreferrer"&gt;https://github.com/premchalmeti/clish_xml_parser&lt;/a&gt;&lt;/p&gt;




</description>
      <category>algorithms</category>
      <category>problemsolving</category>
      <category>cli</category>
      <category>xmlparsing</category>
    </item>
  </channel>
</rss>
