summaryrefslogtreecommitdiffstats
path: root/cds-ui/designer-client/src/app/modules/feature-modules/packages/package-creation/template-mapping/utils/ParserFactory
diff options
context:
space:
mode:
Diffstat (limited to 'cds-ui/designer-client/src/app/modules/feature-modules/packages/package-creation/template-mapping/utils/ParserFactory')
-rw-r--r--cds-ui/designer-client/src/app/modules/feature-modules/packages/package-creation/template-mapping/utils/ParserFactory/ASCII-Parser.ts19
-rw-r--r--cds-ui/designer-client/src/app/modules/feature-modules/packages/package-creation/template-mapping/utils/ParserFactory/JinjaXML.ts31
-rw-r--r--cds-ui/designer-client/src/app/modules/feature-modules/packages/package-creation/template-mapping/utils/ParserFactory/JinjaYML.ts43
-rw-r--r--cds-ui/designer-client/src/app/modules/feature-modules/packages/package-creation/template-mapping/utils/ParserFactory/Parser.spec.ts153
-rw-r--r--cds-ui/designer-client/src/app/modules/feature-modules/packages/package-creation/template-mapping/utils/ParserFactory/Parser.ts4
-rw-r--r--cds-ui/designer-client/src/app/modules/feature-modules/packages/package-creation/template-mapping/utils/ParserFactory/ParserFactory.ts73
-rw-r--r--cds-ui/designer-client/src/app/modules/feature-modules/packages/package-creation/template-mapping/utils/ParserFactory/VtlParser.ts53
-rw-r--r--cds-ui/designer-client/src/app/modules/feature-modules/packages/package-creation/template-mapping/utils/ParserFactory/VtlYMLParser.ts35
-rw-r--r--cds-ui/designer-client/src/app/modules/feature-modules/packages/package-creation/template-mapping/utils/ParserFactory/XmlParser.ts33
9 files changed, 444 insertions, 0 deletions
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
index 000000000..c9e0a1891
--- /dev/null
+++ b/cds-ui/designer-client/src/app/modules/feature-modules/packages/package-creation/template-mapping/utils/ParserFactory/ASCII-Parser.ts
@@ -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];
+ }
+
+}
diff --git a/cds-ui/designer-client/src/app/modules/feature-modules/packages/package-creation/template-mapping/utils/ParserFactory/JinjaXML.ts b/cds-ui/designer-client/src/app/modules/feature-modules/packages/package-creation/template-mapping/utils/ParserFactory/JinjaXML.ts
new file mode 100644
index 000000000..cb1359aa0
--- /dev/null
+++ b/cds-ui/designer-client/src/app/modules/feature-modules/packages/package-creation/template-mapping/utils/ParserFactory/JinjaXML.ts
@@ -0,0 +1,31 @@
+import { Parser } from './Parser';
+
+export class JinjaXMLParser 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];
+ }
+
+}
+
+/*
+
+<?xml version="1.0" encoding="UTF-8"?>
+<configuration xmlns:junos="http://xml.juniper.net/junos/17.4R1/junos">
+<system xmlns="http://yang.juniper.net/junos-qfx/conf/system">
+<host-name operation="delete" />
+<host-name operation="create">[hostname]</host-name>
+</system>
+</configuration>
+
+*/
diff --git a/cds-ui/designer-client/src/app/modules/feature-modules/packages/package-creation/template-mapping/utils/ParserFactory/JinjaYML.ts b/cds-ui/designer-client/src/app/modules/feature-modules/packages/package-creation/template-mapping/utils/ParserFactory/JinjaYML.ts
new file mode 100644
index 000000000..6ecee9070
--- /dev/null
+++ b/cds-ui/designer-client/src/app/modules/feature-modules/packages/package-creation/template-mapping/utils/ParserFactory/JinjaYML.ts
@@ -0,0 +1,43 @@
+import { Parser } from './Parser';
+
+export class JinjaYMLParser implements Parser {
+ variables: Set<string> = new Set();
+ getVariables(fileContent: string): string[] {
+ if (fileContent.includes('{{')) {
+ // '[{]+[ ]*.[V-v]alues.' old regex
+ const xmlSplit = fileContent.split(new RegExp('[{]+[ ]*.'));
+ for (const val of xmlSplit) {
+ const res = val.substring(0, val.indexOf('}}'));
+ if (res && res.length > 0) {
+ console.log(res);
+ if (res.includes('Value')) {
+ this.variables.add(this.extractValues(res.trim()));
+ } else {
+ this.variables.add(this.extractParent(res.trim()).toLowerCase());
+ }
+ }
+ }
+ }
+ return [...this.variables];
+ }
+
+ extractValues(value) {
+ return value.split('Values.')[1];
+ }
+ extractParent(value): string {
+ return value.split('.')[0];
+ }
+
+}
+
+/*
+vf-module-name: {{ .Values.vpg_name_0 }}
+<?xml version="1.0" encoding="UTF-8"?>
+<configuration xmlns:junos="http://xml.juniper.net/junos/17.4R1/junos">
+<system xmlns="http://yang.juniper.net/junos-qfx/conf/system">
+<host-name operation="delete" />
+<host-name operation="create">[hostname]</host-name>
+</system>
+</configuration>
+
+*/
diff --git a/cds-ui/designer-client/src/app/modules/feature-modules/packages/package-creation/template-mapping/utils/ParserFactory/Parser.spec.ts b/cds-ui/designer-client/src/app/modules/feature-modules/packages/package-creation/template-mapping/utils/ParserFactory/Parser.spec.ts
new file mode 100644
index 000000000..3a1880c99
--- /dev/null
+++ b/cds-ui/designer-client/src/app/modules/feature-modules/packages/package-creation/template-mapping/utils/ParserFactory/Parser.spec.ts
@@ -0,0 +1,153 @@
+import { XmlParser } from './XmlParser';
+import { ParserFactory } from './ParserFactory';
+import { FileExtension } from '../TemplateType';
+import { JinjaXMLParser } from './JinjaXML';
+
+fdescribe('ImportsTabComponent', () => {
+
+ const parserFactory = new ParserFactory();
+
+
+ beforeEach(() => {
+ });
+
+ it('Test xml Parser', () => {
+ const fileContent = `<vlb-business-vnf-onap-plugin xmlns="urn:opendaylight:params:xml:ns:yang:vlb-business-vnf-onap-plugin">
+ <vdns-instances>
+ <vdns-instance>
+ <ip-addr>$vdns_int_private_ip_0</ip-addr>
+ <oam-ip-addr>$vdns_onap_private_ip_0</oam-ip-addr>
+ <enabled>false</enabled>
+ <tag>dddd</tag>
+ </vdns-instance>
+ </vdns-instances>
+ </vlb-business-vnf-onap-plugin>`;
+
+ const parser = parserFactory.getParser(fileContent, FileExtension.XML);
+ const res = parser.getVariables(fileContent);
+ console.log(res);
+ expect(res.length).toEqual(2);
+ expect(res[0]).toEqual('vdns_int_private_ip_0');
+ expect(res[1]).toEqual('vdns_onap_private_ip_0');
+ });
+
+ it('Test J2 XML Parser', () => {
+ const fileContent = `<?xml version="1.0" encoding="UTF-8"?>
+ <configuration xmlns:junos="http://xml.juniper.net/junos/17.4R1/junos">
+ <system xmlns="http://yang.juniper.net/junos-qfx/conf/system">
+ <host-name operation="delete" />
+ <host-name operation="create">[hostname]</host-name>
+ </system>
+ </configuration>`;
+
+ const parser = parserFactory.getParser(fileContent, FileExtension.Jinja);
+ const res = parser.getVariables(fileContent);
+ console.log(typeof (res));
+ console.log(res);
+ expect(res.length).toEqual(1);
+ expect(res[0]).toEqual('hostname');
+
+ });
+
+ it('Test J2 YML Parser', () => {
+ const fileContent = `apiVersion: v1
+ kind: Service
+ metadata:
+ name: {{ .Values.vpg_name_0 }}-ssh
+ labels:
+ vnf-name: {{ .Values.vnf_name }}
+ vf-module-name: {{ .Values.vpg_name_0 }}
+ release: {{ .Release.Name }}
+ chart: {{ .Chart.Name }}
+ spec:
+ type: NodePort
+ ports:
+ port: 22
+ nodePort: \${vpg-management-port}
+ selector:
+ vf-module-name: {{ .Values.vpg_name_0 }}
+ release: {{ .Release.Name }}
+ chart: {{ .Chart.Name }}`;
+
+ const parser = parserFactory.getParser(fileContent, FileExtension.Jinja);
+ const res = parser.getVariables(fileContent);
+ console.log(res);
+ expect(res.length).toEqual(4);
+ expect(res[0]).toEqual('vpg_name_0');
+ 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');
+
+
+ });
+
+
+
+
+
+ it('Test Velocity YML Parser', () => {
+ const fileContent = `apiVersion: v1
+ kind: Service
+ metadata:
+ name: {{ .Values.vpg_name_0 }}-ssh
+ labels:
+ vnf-name: {{ .Values.vnf_name }}
+ vf-module-name: {{ .Values.vpg_name_0 }}
+ release: {{ .Release.Name }}
+ chart: {{ .Chart.Name }}
+ spec:
+ type: NodePort
+ ports:
+ port: 22
+ nodePort: \${vpg-management-port}
+ selector:
+ vf-module-name: {{ .Values.vpg_name_0 }}
+ release: {{ .Release.Name }}
+ chart: {{ .Chart.Name }}`;
+
+ const parser = parserFactory.getParser(fileContent, FileExtension.Velocity);
+ const res = parser.getVariables(fileContent);
+ console.log(res);
+ expect(res.length).toEqual(1);
+ expect(res[0]).toEqual('vpg-management-port');
+
+ });
+
+
+
+});
diff --git a/cds-ui/designer-client/src/app/modules/feature-modules/packages/package-creation/template-mapping/utils/ParserFactory/Parser.ts b/cds-ui/designer-client/src/app/modules/feature-modules/packages/package-creation/template-mapping/utils/ParserFactory/Parser.ts
new file mode 100644
index 000000000..f189a84ca
--- /dev/null
+++ b/cds-ui/designer-client/src/app/modules/feature-modules/packages/package-creation/template-mapping/utils/ParserFactory/Parser.ts
@@ -0,0 +1,4 @@
+export interface Parser {
+ variables: Set<string>;
+ getVariables(fileContent: string): string[];
+}
diff --git a/cds-ui/designer-client/src/app/modules/feature-modules/packages/package-creation/template-mapping/utils/ParserFactory/ParserFactory.ts b/cds-ui/designer-client/src/app/modules/feature-modules/packages/package-creation/template-mapping/utils/ParserFactory/ParserFactory.ts
new file mode 100644
index 000000000..b64afeed0
--- /dev/null
+++ b/cds-ui/designer-client/src/app/modules/feature-modules/packages/package-creation/template-mapping/utils/ParserFactory/ParserFactory.ts
@@ -0,0 +1,73 @@
+
+import { XmlParser } from './XmlParser';
+import { Parser } from './Parser';
+import { VtlParser } from './VtlParser';
+import { FileExtension } from '../TemplateType';
+import { JinjaXMLParser } from './JinjaXML';
+import { VtlYMLParser } from './VtlYMLParser';
+import { JinjaYMLParser } from './JinjaYML';
+import { ASCIIParser } from './ASCII-Parser';
+
+export class ParserFactory {
+
+ getParser(fileContent: string, fileExtension: string): Parser {
+ let parser: Parser;
+ console.log('file extension =' + fileExtension);
+
+ if (fileExtension === FileExtension.Velocity) {
+
+ if (this.isXML(fileContent)) {
+ parser = new XmlParser();
+ } else if (this.isJSON(fileContent)) {
+ parser = new VtlParser();
+ } else if (this.isASCII(fileContent)) {
+ parser = new ASCIIParser();
+ } else {
+ console.log('Velocity YML parser....');
+ parser = new VtlYMLParser();
+ }
+
+ } else if (fileExtension === FileExtension.Jinja) {
+
+ if (this.isXML(fileContent)) {
+ parser = new JinjaXMLParser();
+ } else if (this.isJSON(fileContent)) {
+ // TODO: implement JSON parser
+ } else if (this.isASCII(fileContent)) {
+ parser = new ASCIIParser();
+ } else {
+ console.log('Jinja YML parser....');
+ parser = new JinjaYMLParser();
+ }
+
+ } else if (fileExtension === FileExtension.XML) {
+ parser = new XmlParser();
+ }
+ return parser;
+ }
+
+ private isXML(fileContent: string): boolean {
+ return fileContent.includes('<?xml version="1.0" encoding="UTF-8"?>');
+ }
+
+ private isJSON(fileContent: string): boolean {
+ try {
+ JSON.parse(fileContent);
+ } catch (e) {
+ return false;
+ }
+ return true;
+ }
+
+ private isASCII(fileContent: string): boolean {
+ if (
+ fileContent.includes('end') &&
+ fileContent.includes('set') &&
+ fileContent.includes('$(')
+ ) {
+ return true;
+ }
+
+ return false;
+ }
+}
diff --git a/cds-ui/designer-client/src/app/modules/feature-modules/packages/package-creation/template-mapping/utils/ParserFactory/VtlParser.ts b/cds-ui/designer-client/src/app/modules/feature-modules/packages/package-creation/template-mapping/utils/ParserFactory/VtlParser.ts
new file mode 100644
index 000000000..ca80a297c
--- /dev/null
+++ b/cds-ui/designer-client/src/app/modules/feature-modules/packages/package-creation/template-mapping/utils/ParserFactory/VtlParser.ts
@@ -0,0 +1,53 @@
+import { Parser } from './Parser';
+
+export class VtlParser implements Parser {
+ variables: Set<string> = new Set();
+ getVariables(fileContent: string): string[] {
+ const variables: string[] = [];
+ const stringsSlittedByBraces = fileContent.split('${');
+ const stringsDefaultByDollarSignOnly = fileContent.split('"$');
+
+ for (let i = 1; i < stringsSlittedByBraces.length; i++) {
+ const element = stringsSlittedByBraces[i];
+ if (element) {
+ const firstElement = element.split('}')[0];
+ if (!variables.includes(firstElement)) {
+ variables.push(firstElement);
+ } else {
+ console.log(firstElement);
+ }
+ }
+ }
+
+ for (let i = 1; i < stringsDefaultByDollarSignOnly.length; i++) {
+ const element = stringsDefaultByDollarSignOnly[i];
+ if (element && !element.includes('$')) {
+ const firstElement = element.split('"')[0]
+ .replace('{', '')
+ .replace('}', '').trim();
+ if (!variables.includes(firstElement)) {
+ variables.push(firstElement);
+ }
+ }
+ }
+ this.variables = new Set(variables);
+ return [...variables];
+ }
+
+}
+
+/*
+
+<vlb-business-vnf-onap-plugin xmlns="urn:opendaylight:params:xml:ns:yang:vlb-business-vnf-onap-plugin">
+ <vdns-instances>
+ <vdns-instance>
+ <ip-addr>$vdns_int_private_ip_0</ip-addr>
+ <oam-ip-addr>$vdns_onap_private_ip_0</oam-ip-addr>
+ <tag>aaaa</tag>
+ <enabled>false</enabled>
+ <tag>dddd</tag>
+ </vdns-instance>
+ </vdns-instances>
+</vlb-business-vnf-onap-plugin>
+
+*/
diff --git a/cds-ui/designer-client/src/app/modules/feature-modules/packages/package-creation/template-mapping/utils/ParserFactory/VtlYMLParser.ts b/cds-ui/designer-client/src/app/modules/feature-modules/packages/package-creation/template-mapping/utils/ParserFactory/VtlYMLParser.ts
new file mode 100644
index 000000000..6c3a0e0fd
--- /dev/null
+++ b/cds-ui/designer-client/src/app/modules/feature-modules/packages/package-creation/template-mapping/utils/ParserFactory/VtlYMLParser.ts
@@ -0,0 +1,35 @@
+import { Parser } from './Parser';
+
+export class VtlYMLParser 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 && !res.includes('{')) {
+ this.variables.add(res);
+ }
+
+ }
+ }
+ return [...this.variables];
+ }
+
+}
+
+/*
+
+<vlb-business-vnf-onap-plugin xmlns="urn:opendaylight:params:xml:ns:yang:vlb-business-vnf-onap-plugin">
+ <vdns-instances>
+ <vdns-instance>
+ <ip-addr>$vdns_int_private_ip_0</ip-addr>
+ <oam-ip-addr>$vdns_onap_private_ip_0</oam-ip-addr>
+ <tag>aaaa</tag>
+ <enabled>false</enabled>
+ <tag>dddd</tag>
+ </vdns-instance>
+ </vdns-instances>
+</vlb-business-vnf-onap-plugin>
+
+*/
diff --git a/cds-ui/designer-client/src/app/modules/feature-modules/packages/package-creation/template-mapping/utils/ParserFactory/XmlParser.ts b/cds-ui/designer-client/src/app/modules/feature-modules/packages/package-creation/template-mapping/utils/ParserFactory/XmlParser.ts
new file mode 100644
index 000000000..69bc8b627
--- /dev/null
+++ b/cds-ui/designer-client/src/app/modules/feature-modules/packages/package-creation/template-mapping/utils/ParserFactory/XmlParser.ts
@@ -0,0 +1,33 @@
+import { Parser } from './Parser';
+
+export class XmlParser implements Parser {
+ variables: Set<string> = new Set();
+ getVariables(fileContent: string): string[] {
+ 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];
+ }
+
+}
+
+/*
+
+<vlb-business-vnf-onap-plugin xmlns="urn:opendaylight:params:xml:ns:yang:vlb-business-vnf-onap-plugin">
+ <vdns-instances>
+ <vdns-instance>
+ <ip-addr>$vdns_int_private_ip_0</ip-addr>
+ <oam-ip-addr>$vdns_onap_private_ip_0</oam-ip-addr>
+ <tag>aaaa</tag>
+ <enabled>false</enabled>
+ <tag>dddd</tag>
+ </vdns-instance>
+ </vdns-instances>
+</vlb-business-vnf-onap-plugin>
+
+*/