Skip to content

Commit

Permalink
redfish-gen: Create RefishBase class with common methods
Browse files Browse the repository at this point in the history
Create common Redfish class with common functionality to
share same Redfish related functionaly in the 'redfish-gen'
tool.

End-User-Impact: None: (Code optimization without change
			functionality)

Signed-off-by: Sergey Gulyaev <[email protected]>
  • Loading branch information
Sergey Gulyaev committed Feb 22, 2023
1 parent b24a80a commit 86166fc
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 139 deletions.
107 changes: 107 additions & 0 deletions tools/redfish-gen/redfish_gen/redfish_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
## SPDX-License-Identifier: Apache-2.0
## Copyright (C) 2023, KNS Group LLC (YADRO)

import json
from shutil import copy
from os import path, makedirs, walk
from xml.dom import minidom

from .globals import __RFG_PATH__
from .globals import __WWW_PATH__

class RedfishBase:
"""
Redfish base class.
Contains all common methods for RedfishNode and RedfishSchema classes.
"""

@staticmethod
def redfish_version():
return "v1"

@staticmethod
def _json_schemas_relative_dir():
return "JsonSchemas"

@staticmethod
def _load_file_fullpath(fullname):
with open(fullname) as f:
data = f.read()
return data

@staticmethod
def _load_file(fullname, schema_path=__RFG_PATH__):
file_path = path.join(schema_path, fullname)
with open(file_path) as f:
data = f.read()
return data

@staticmethod
def _load_bundle_file(fullname):
file_path = path.join("assets", "schemas", "bundle", fullname)
return RedfishBase._load_file(file_path)

@staticmethod
def _load_csdl_file(fullname):
data = RedfishBase._load_bundle_file(fullname)
return minidom.parseString(data)


@staticmethod
def _version_from_filename(filename):
parts = filename.split(".")
if len(parts) > 1:
return parts[1]
return None

@staticmethod
def schema_and_version_from(filename):
filename = path.basename(filename)
filename = path.splitext(filename)[0]
parts = filename.split(".")
if len(parts) > 1:
return parts[0], parts[1]
return parts[0], None

@staticmethod
def write_file(destdir, filename, content):
if not path.isdir(destdir):
makedirs(destdir, exist_ok=True)
filename = path.join(destdir, filename)
if isinstance(content, str):
with open(filename, "w") as file:
file.writelines(content)
else:
with open(filename, "wb") as file:
file.write(content)

def __init__(self, standard: str, schema_name, version) -> None:
if standard != "redfish" and standard != "redfish/swordfish":
raise Exception("API Standard \"%s\" in not supported" % standard)
self._standard = standard
self._schema_name = schema_name
if self._schema_name is None:
raise ValueError("Node config: require field 'Schema'")
self._schema_version = version[1:] if version is not None and version.startswith("v") else version

def standard(self):
return self._standard

def schema_id(self):
return self._schema_name

def version(self):
if self._schema_version:
return "v" + self._schema_version.replace(".", "_")
return None

def _available_schema(self):
if self.schema is not None:
return self.schema
return self.schema_spec

def is_oem(self):
return self._schema_name.startswith("Oem")

def get_json_schema(self):
return self.schema
46 changes: 11 additions & 35 deletions tools/redfish-gen/redfish_gen/redfish_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
import json
from xml.dom import minidom

from .redfish_base import RedfishBase
from .property import OemFragment, Property
from .globals import __RFG_PATH__
from .node_parameter import NodeParameter, StaticNodeParameter


class RedfishNode:
class RedfishNode(RedfishBase):
"""
Redfish node instance.
Contains all relevant configs, provides engine to acquire
Expand All @@ -37,31 +38,20 @@ def build(parent=None, **kwargs):
else:
raise ValueError("Invalid config definition")

# Load configs
@staticmethod
def __load_file(filename, schema_path=__RFG_PATH__):
with open(schema_path + "/assets/schemas/bundle/" + filename) as f:
data = f.read()
return data

def __load_openapi(self):
data = self.__load_file(self.__schema_file_openapi())
data = self._load_bundle_file(self.__schema_file_openapi())
return yaml.safe_load(data)

def __load_schema_json(self):
file = self.__schema_file_json()
if file is not None:
return json.loads(self.__load_file(file))
return json.loads(self._load_bundle_file(file))
return file

def __load_schema_spec_json(self):
data = self.__load_file(self.__schema_spec_file_json())
data = self._load_bundle_file(self.__schema_spec_file_json())
return json.loads(data)

def __load_csdl(self):
data = self.__load_file(self.__schema_file_csdl())
return minidom.parseString(data)

def __schema_file(self):
if self._schema_version:
return self._schema_name + "." + self.version()
Expand Down Expand Up @@ -92,21 +82,20 @@ def __build_id(self):

# Constructor
def __init__(self, parent, segment, **kwargs):
super().__init__(standard="redfish",
schema_name=kwargs.get("Schema", None),
version=str(kwargs.get("Version", "")))
self._parent = parent
self._segment = kwargs.get("Segment", segment)
self._classname = segment
self._prefix = kwargs.get("Prefix", "").split("/")
self.__build_id()
self._schema_name = kwargs.get("Schema", None)
if self._schema_name is None:
raise ValueError("Node config: require field 'Schema'")
self._schema_version = str(kwargs.get("Version", ""))
self._y = kwargs
# Load resourses
self.openapi = self.__load_openapi()
self.schema_spec = self.__load_schema_spec_json()
self.schema = self.__load_schema_json()
self.csdl = self.__load_csdl()
self.csdl = RedfishBase._load_csdl_file(self.__schema_file_csdl())

def is_dynamic(self):
return False
Expand Down Expand Up @@ -136,11 +125,6 @@ def __get(self):
def __modify(self):
return self.__actions()("Modify")

def __available_schema(self):
if self.schema is not None:
return self.schema
return self.schema_spec

def __schema_spec_node(self):
"""
BUG note
Expand All @@ -162,9 +146,6 @@ def __formatting_classname(classname):
def name(self):
return self._y["Name"]

def schema_id(self):
return self._schema_name

def segment(self):
return self._segment

Expand Down Expand Up @@ -192,19 +173,14 @@ def id(self):
return self._id

def odata_type(self):
return self.__available_schema()["title"]
return self._available_schema()["title"]

def classname(self):
return RedfishNode.__formatting_classname(self._classname)

def def_filename(self):
return self._classname

def version(self):
if self._schema_version:
return "v" + self._schema_version.replace(".", "_")
return None

def insertable(self):
return self.__schema_spec_node()["insertable"]

Expand Down Expand Up @@ -299,7 +275,7 @@ def __check_properties_definition(self, source: str) -> bool:
def _properties_expand(self, source: str):
if not self.__check_properties_definition(source):
return []
return Property.build(source, self.schema_id(), self.__available_schema(), self.__get()["Properties"])
return Property.build(source, self.schema_id(), self._available_schema(), self.__get()["Properties"])

def links(self):
return self._properties_expand("Links")
Expand Down
Loading

0 comments on commit 86166fc

Please sign in to comment.