Support parsing of ASCII/CLI files within Velocity and Jinja Template 40/113240/1
authorAhmedeldeeb50 <ahmed.eldeeb.ext@orange.com>
Sun, 27 Sep 2020 11:33:17 +0000 (13:33 +0200)
committerAhmedeldeeb50 <ahmed.eldeeb.ext@orange.com>
Sun, 27 Sep 2020 11:33:17 +0000 (13:33 +0200)
Issue-ID: CCSDK-2772

Signed-off-by: Ahmedeldeeb50 <ahmed.eldeeb.ext@orange.com>
Change-Id: I71e48d3c49ed1adc40ec4edef2df6d7413ed0300

cds-ui/designer-client/src/app/modules/feature-modules/packages/package-creation/template-mapping/utils/ParserFactory/ASCII-Parser.ts [new file with mode: 0644]
cds-ui/designer-client/src/app/modules/feature-modules/packages/package-creation/template-mapping/utils/ParserFactory/Parser.spec.ts
cds-ui/designer-client/src/app/modules/feature-modules/packages/package-creation/template-mapping/utils/ParserFactory/ParserFactory.ts

diff --git a/cds-ui/designer-client/src/app/modules/feature-modules/packages/package-creation/template-mapping/utils/ParserFactory/ASCII-Parser.ts b/cds-ui/designer-client/src/app/modules/feature-modules/packages/package-creation/template-mapping/utils/ParserFactory/ASCII-Parser.ts
new file mode 100644 (file)
index 0000000..c9e0a18
--- /dev/null
@@ -0,0 +1,19 @@
+import { Parser } from './Parser';
+
+export class ASCIIParser implements Parser {
+    variables: Set<string> = new Set();
+    getVariables(fileContent: string): string[] {
+        if (fileContent.includes('$(')) {
+            const xmlSplit = fileContent.split('$(');
+            for (const val of xmlSplit) {
+                const res = val.substring(0, val.indexOf(')'));
+                if (res && res.length > 0) {
+                    this.variables.add(res);
+                }
+
+            }
+        }
+        return [...this.variables];
+    }
+
+}
index d9c4c2b..aab37c7 100644 (file)
@@ -77,4 +77,42 @@ fdescribe('ImportsTabComponent', () => {
         expect(res[1]).toEqual('vnf_name');
 
     });
+
+    it('Test ASCII Parser', () => {
+        const fileContent = `
+        config system interface
+        edit "internal"
+        set vdom "root"
+        set ip $(subnet1_fgt_ip) 255.255.255.0 #1
+        set allowaccess ping https ssh http fgfm capwap
+        set type hard-switch
+        set stp enable
+        set role lan
+        next
+        end
+        config system dhcp server
+        edit 1
+        set dns-service default
+        set default-gateway $(subnet1_fgt_ip) #2
+        set netmask 255.255.255.0
+        set interface "internal"
+        config ip-range
+        edit 1
+        set start-ip $(subnet1_fgt_ip)4,150 #3
+        set end-ip $(subnet1_fgt_ip)4,200 #4
+        next
+        end
+        next
+        end
+        Options
+        `;
+
+        const parser = parserFactory.getParser(fileContent, FileExtension.Jinja);
+        const res = parser.getVariables(fileContent);
+        console.log(res);
+        expect(res.length).toEqual(1);
+        expect(res[0]).toEqual('subnet1_fgt_ip');
+
+
+    });
 });
index d8607c7..a5c92e4 100644 (file)
@@ -6,6 +6,7 @@ import { FileExtension } from '../TemplateType';
 import { JinjaXMLParser } from './JinjaXML';
 import { VtlYMLParser } from './VtlYMLParser';
 import { JinjaYMLParser } from './JinjaYML';
+import { ASCIIParser } from './ASCII-Parser';
 
 export class ParserFactory {
 
@@ -19,6 +20,8 @@ export class ParserFactory {
                 parser = new XmlParser();
             } else if (this.isJSON(fileContent)) {
                 parser = new VtlParser();
+            } else if (this.isASCII(fileContent)) {
+                parser = new ASCIIParser();
             } else {
                 parser = new VtlYMLParser();
             }
@@ -29,6 +32,8 @@ export class ParserFactory {
                 parser = new JinjaXMLParser();
             } else if (this.isJSON(fileContent)) {
                 // TODO: implement JSON parser
+            } else if (this.isASCII(fileContent)) {
+                parser = new ASCIIParser();
             } else {
                 parser = new JinjaYMLParser();
             }
@@ -51,4 +56,16 @@ export class ParserFactory {
         }
         return true;
     }
+
+    private isASCII(fileContent: string): boolean {
+        if (
+            fileContent.includes('end') &&
+            fileContent.includes('set') &&
+            fileContent.includes('$(')
+        ) {
+            return true;
+        }
+
+        return false;
+    }
 }