Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MSA-9969 [YANG] YANG enhance generate MS from the YANG XML structure WF #193

Merged
merged 1 commit into from
May 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 3 additions & 15 deletions ...YANG_To_XML/.meta_Convert_YANG_To_XML.xml → ...ce/.meta_Convert_YANG_To_MicroService.xml
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,23 @@
<map>
<entry>
<key>DISPLAYNAME</key>
<value>Convert_YANG_To_XML.xml</value>
<value>Convert_YANG_To_MicroService.xml</value>
</entry>
<entry>
<key>DATE_MODIFICATION</key>
<value>1617358606722</value>
<value>1619793743117</value>
</entry>
<entry>
<key>REPOSITORY</key>
<value>Process</value>
</entry>
<entry>
<key>DATE_CREATION</key>
<value>1617358606714</value>
</entry>
<entry>
<key>MODEL</key>
<value>0</value>
<value>1619793743117</value>
</entry>
<entry>
<key>TAG</key>
</entry>
<entry>
<key>FILE_TYPE</key>
<value>text</value>
</entry>
<entry>
<key>MANUFACTURER</key>
<value>0</value>
</entry>
<entry>
<key>TYPE</key>
<value>FILE</value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
<map>
<entry>
<key>DISPLAYNAME</key>
<value>Process_convert_yang_to_xml</value>
<value>Process_Delete</value>
</entry>
<entry>
<key>DATE_MODIFICATION</key>
<value>1616080840816</value>
<value>1617349254149</value>
</entry>
<entry>
<key>REPOSITORY</key>
<value>Process</value>
</entry>
<entry>
<key>DATE_CREATION</key>
<value>1616080840812</value>
<value>1617349254142</value>
</entry>
<entry>
<key>TAG</key>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<metadata>
<map>
<entry>
<key>DISPLAYNAME</key>
<value>Process_Convert_XML_to_MicroService</value>
</entry>
<entry>
<key>DATE_MODIFICATION</key>
<value>1617785102777</value>
</entry>
<entry>
<key>REPOSITORY</key>
<value>Process</value>
</entry>
<entry>
<key>DATE_CREATION</key>
<value>1617785102709</value>
</entry>
<entry>
<key>TAG</key>
</entry>
<entry>
<key>TYPE</key>
<value>DIRECTORY</value>
</entry>
<entry>
<key>COMMENT</key>
</entry>
</map>
</metadata>
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,41 @@
</values>
</variable>
<variable displayName="Select" name="params.yang_list.0.is_selected" startIncrement="0" type="Boolean" mandatoryArray="false" visible="true" description="" groupSeparator="" groupDisplayName="" displayOrder="3" increment="0" refServiceURI="" keepOnImport="false" editable="false" onlyDetailView="false" localVarNameMatch="" remoteVarNameMatch="" arrayCanAdd="true" arrayCanRemove="true" arrayCanMove="true" arrayCanEdit="true" displayNameHeader="" fullDisplayName="" default="false" isMandatory="false" isUserLocked="false" isGrouped="false" isSearchable="false" isUniqueGlobal="false"/>
<variable displayName="Yang_Main_File" name="params.yang_list.0.is_yangmainfile" startIncrement="0" type="Boolean" mandatoryArray="false" visible="true" description="Yang_Main_File used to select the list of field" groupSeparator="" groupDisplayName="" displayOrder="5" increment="0" refServiceURI="" keepOnImport="false" editable="false" onlyDetailView="false" localVarNameMatch="" remoteVarNameMatch="" arrayCanAdd="true" arrayCanRemove="true" arrayCanMove="true" arrayCanEdit="true" displayNameHeader="" fullDisplayName="" isMandatory="false" isUserLocked="false" isGrouped="false" isSearchable="false" isUniqueGlobal="false"/>
<variable displayName="yang " name="params.yang_list.0.yang" startIncrement="0" type="String" mandatoryArray="false" visible="true" description="" groupSeparator="" groupDisplayName="" displayOrder="2" increment="0" refServiceURI="" keepOnImport="false" editable="false" onlyDetailView="false" localVarNameMatch="" remoteVarNameMatch="" arrayCanAdd="true" arrayCanRemove="true" arrayCanMove="true" arrayCanEdit="true" displayNameHeader="" fullDisplayName="" isMandatory="true" isUserLocked="true" isGrouped="false" isSearchable="false" isUniqueGlobal="false"/>
<variable displayName="Xml Output File" name="params.xml_output_file" startIncrement="0" type="String" mandatoryArray="false" visible="true" description="Xml Output File" groupSeparator="" groupDisplayName="" displayOrder="4" increment="0" refServiceURI="" keepOnImport="false" editable="false" onlyDetailView="false" localVarNameMatch="" remoteVarNameMatch="" arrayCanAdd="true" arrayCanRemove="true" arrayCanMove="true" arrayCanEdit="true" displayNameHeader="" fullDisplayName="" isMandatory="false" isUserLocked="false" isGrouped="false" isSearchable="false" isUniqueGlobal="false"/>
<variable displayName="XML temp file" name="params.xml_output_file" startIncrement="0" type="String" mandatoryArray="false" visible="true" description="XML temp file" groupSeparator="" groupDisplayName="" displayOrder="4" increment="0" refServiceURI="" keepOnImport="false" editable="false" onlyDetailView="false" localVarNameMatch="" remoteVarNameMatch="" arrayCanAdd="true" arrayCanRemove="true" arrayCanMove="true" arrayCanEdit="true" displayNameHeader="" fullDisplayName="" isMandatory="false" isUserLocked="false" isGrouped="false" isSearchable="false" isUniqueGlobal="false"/>
<variable displayName="Micro Service File" name="params.micro_service_file" startIncrement="0" type="String" mandatoryArray="false" visible="true" description="Micro Service File Created" groupSeparator="" groupDisplayName="" displayOrder="4" increment="0" refServiceURI="" keepOnImport="false" editable="false" onlyDetailView="false" localVarNameMatch="" remoteVarNameMatch="" arrayCanAdd="true" arrayCanRemove="true" arrayCanMove="true" arrayCanEdit="true" displayNameHeader="" fullDisplayName="" isMandatory="false" isUserLocked="false" isGrouped="false" isSearchable="false" isUniqueGlobal="false"/>
</variables>
<example/>
<process name="Process/Convert_YANG_To_XML/Process_create_instance">
<process name="Process/Convert_YANG_To_MicroService/Process_create_instance">
<displayName>Create Converter Instance</displayName>
<type>CREATE</type>
<visibility>5</visibility>
<allowSchedule/>
<task name="Task_create_instance.py">
<processPath>/opt/fmc_repository/Process/Convert_YANG_To_XML/Process_create_instance/Tasks</processPath>
<processPath>/opt/fmc_repository/Process/Convert_YANG_To_MicroService/Process_create_instance/Tasks</processPath>
<displayName>create_instance</displayName>
</task>
</process>
<process name="Process/Convert_YANG_To_XML/Process_convert_yang_to_xml">
<displayName>Convert Yang files to XML</displayName>
<process name="Process/Convert_YANG_To_MicroService/Process_convert_yang_to_microservice">
<displayName>Convert Yang files to MicroService</displayName>
<type>UPDATE</type>
<visibility>5</visibility>
<allowSchedule/>
<task name="Task_transform_yang_files_to_xml.py">
<processPath>/opt/fmc_repository/Process/Convert_YANG_To_XML/Process_convert_yang_to_xml/Tasks</processPath>
<processPath>/opt/fmc_repository/Process/Convert_YANG_To_MicroService/Process_convert_yang_to_microservice/Tasks</processPath>
<displayName>Convert Yang files to XML</displayName>
</task>
<task name="Task_Update_XML.py">
<processPath>/opt/fmc_repository/Process/Convert_YANG_To_MicroService/Process_convert_yang_to_microservice/Tasks</processPath>
<displayName>Update XML File for MS</displayName>
</task>
<task name="Task_Convert_xml_to_MicroService.py">
<processPath>/opt/fmc_repository/Process/Convert_YANG_To_MicroService/Process_convert_yang_to_microservice/Tasks</processPath>
<displayName>Convert XML to MicroService</displayName>
</task>
</process>
<process name="Process/Convert_YANG_To_XML/Process_Delete">
<process name="Process/Convert_YANG_To_MicroService/Process_Delete">
<displayName>Delete</displayName>
<type>DELETE</type>
<visibility>5</visibility>
Expand All @@ -47,11 +57,11 @@
<information>
<icon/>
<description>Convert Yang files into xml files</description>
<displayField>yang_list.0.yang</displayField>
<displayField>service_id</displayField>
<serviceTaskType>python</serviceTaskType>
<order>10000</order>
<visibility>5</visibility>
<name>YANG to XML Converter</name>
<name>YANG to MicroService Converter</name>
<group>tool|yang</group>
</information>
</ObjectDefinition>
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
import copy
import json
import re
import os
import subprocess


from xml.etree.ElementTree import ElementTree
from xml.etree.ElementTree import Element
from xml.etree.ElementTree import SubElement
import xml.etree.ElementTree as ET
import numpy as np
#from re import search
from msa_sdk import constants
from msa_sdk.variables import Variables
from msa_sdk.msa_api import MSA_API
import requests
from xml.dom import minidom
#from xml.dom.ext.reader.Sax2 import FromXmlStream
from msa_sdk.device import Device

dev_var = Variables()
context = Variables.task_call(dev_var)
yang_fields_without_child = context['yang_fields_without_child_hash'].keys()

msa_object = MSA_API()
msa_object.action = 'Create MS object'
msa_object.path = "/repository/v2/resource/microservice"


# Build the curl command to create the Micro Service

ms_name = str(os.path.basename(context['main_yang_file']).replace(context['yangs_extension'],''))
ms_filename = "CommandDefinition/microservices/" + ms_name + '.xml'


curl_ms_creation_xml = """ {
"information": {
"displayName": """+ '"'+ ms_name + """",
"icon": "none",
"description": """ +'"'+ ms_name + """",
"category": "Default",
"displayField": "object_id",
"order": 0,
"visibility": "5",
"configType": "netconf"
},
"variables": {
"variable": [
{
"displayName": "object_id",
"fullDisplayName": "",
"displayNameHeader": "",
"displayOrder": 0,
"name": "params.object_id",
"description": "",
"type": "String",
"visible": true,
"userLocked": false,
"onlyDetailView": false,
"grouped": false,
"groupDisplayName": "",
"groupSeparator": "",
"mandatory": false,
"editable": false,
"arrayCanAdd": true,
"arrayCanRemove": true,
"arrayCanMove": true,
"arrayCanEdit": true
} """

for yang_good_field in yang_fields_without_child:
curl_ms_creation_xml = curl_ms_creation_xml + """,
{
"displayName": """ + '"'+ yang_good_field + """",
"fullDisplayName": """ + '"'+ yang_good_field + """",
"displayNameHeader": "",
"displayOrder": 0,
"name": "params.""" + yang_good_field + """",
"description": "",
"type": "String",
"visible": true,
"userLocked": false,
"onlyDetailView": false,
"grouped": false,
"groupDisplayName": "",
"groupSeparator": "",
"mandatory": false,
"editable": false,
"arrayCanAdd": true,
"arrayCanRemove": true,
"arrayCanMove": true,
"arrayCanEdit": true
} """

curl_ms_creation_xml = curl_ms_creation_xml + """
],
"frozen": 0
},
"command": [
{
"operation": null,
"name": "IMPORT",
"postTemplate": null,
"parser": {
"section": [
{
"xpath": ""
}
],
"lines": {}
}
},
{
"operation": " """
yang_create_operations = ''
#get Create part from xml
f = open(context['xml_create_output_file'], "r")
string_create_part = f.read()
string_create_part = string_create_part.replace('\"','\\"') #protect " in the string
string_create_part = string_create_part.replace('\n','\\n') #protect end of line for json convertion
yang_create_operations = yang_create_operations + string_create_part + '" '

curl_ms_creation_xml = curl_ms_creation_xml + yang_create_operations + """,
"name": "CREATE"
},
{
"operation": null,
"name": "READ"
},
{
"operation": " """
curl_ms_creation_xml = curl_ms_creation_xml + yang_create_operations + """,
"name": "IMPORT"
},
{
"operation": null,
"name": "UPDATE"
},
{
"operation": null,
"name": "CONSTRAINT"
},
{
"operation": null,
"name": "DELETE"
},
{
"operation": null,
"name": "LIST"
}
],
"example": {
"content": " """
curl_ms_creation_xml = curl_ms_creation_xml + yang_create_operations + """
},
"metaInformationList": [
{
"type": "FILE",
"uri": """+ '"'+ ms_filename + """",
"file": true,
"name": """+ '"'+ ms_name + '.xml' + """",
"displayName": """+ '"'+ ms_name + '.xml' + """",
"repositoryName": "CommandDefinition",
"parentURI": "CommandDefinition",
"fileType": "text",
"tag": "string",
"comment": "string",
"modelId": 137,
"vendorId": 1
}
]
}
"""


context.update(curl_ms_creation_xml=curl_ms_creation_xml)

#Uncomment following lines to degub, check the contain of following file, it should be convertible into one json array (it it is the curl request parameter)
#fw = open(context['xml_create_output_file']+'_curl_content.json', "w")
#fw.write(curl_ms_creation_xml)
#fw.close()


parameters_json = json.loads(curl_ms_creation_xml)
#Create the MicroService with Culr requests
msa_object.call_post(parameters_json)


context['micro_service_file'] = ms_filename

ret = MSA_API.process_content(constants.ENDED, ' New MicroService "'+ ms_name + '" created', context, True)
print(ret)


Loading