diff --git a/YangParser/SemanticModel/Action.cs b/YangParser/SemanticModel/Action.cs index 397fab2..5fc8e26 100644 --- a/YangParser/SemanticModel/Action.cs +++ b/YangParser/SemanticModel/Action.cs @@ -50,6 +50,7 @@ public override string ToCode() settings.Indent = true; settings.OmitXmlDeclaration = true; settings.NewLineOnAttributes = true; + settings.Async = true; StringBuilder stringBuilder = new StringBuilder(); using XmlWriter writer = XmlWriter.Create(stringBuilder, settings); await writer.WriteStartElementAsync(null,"rpc","urn:ietf:params:xml:ns:netconf:base:1.0"); diff --git a/YangParser/SemanticModel/Container.cs b/YangParser/SemanticModel/Container.cs index 435e242..3b169a0 100644 --- a/YangParser/SemanticModel/Container.cs +++ b/YangParser/SemanticModel/Container.cs @@ -56,19 +56,11 @@ public Container(string argument, Metadata metadata) : base( public override string ToCode() { var nodes = Children.Select(child => child.ToCode()).ToArray(); - var name = Argument; - if (Parent!.Argument == Argument) - { - name = "sub-" + name; - } - - TargetName = MakeName(name); - - string property = $"public{KeywordString}{MakeName(name)}Container? {MakeName(name)} {{ get; set; }}"; + string property = $"public{KeywordString}{TargetName}Container? {TargetName} {{ get; set; }}"; return $$""" {{property}} {{DescriptionString}}{{AttributeString}} - public class {{MakeName(name)}}Container + public class {{TargetName}}Container { {{string.Join("\n\t", nodes.Select(Indent))}} {{Indent(XmlFunction())}} @@ -76,5 +68,17 @@ public class {{MakeName(name)}}Container """; } - public string TargetName { get; private set; } = string.Empty; + public string TargetName + { + get + { + var name = Argument; + if (Parent!.Argument == Argument) + { + name = "sub-" + name; + } + + return MakeName(name); + } + } } \ No newline at end of file diff --git a/YangParser/SemanticModel/Notification.cs b/YangParser/SemanticModel/Notification.cs index 04e3efa..211d623 100644 --- a/YangParser/SemanticModel/Notification.cs +++ b/YangParser/SemanticModel/Notification.cs @@ -10,7 +10,6 @@ public Notification(YangStatement statement) : base(statement) { if (statement.Keyword != Keyword) throw new SemanticError($"Non-matching Keyword '{statement.Keyword}', expected {Keyword}", statement); - } public const string Keyword = "notification"; @@ -36,12 +35,61 @@ public Notification(YangStatement statement) : base(statement) public override string ToCode() { var nodes = Children.Select(child => child.ToCode()).ToArray(); + var xmlWrite = GetXmlWriting(); return $$""" {{DescriptionString}}{{AttributeString}} public class {{MakeName(Argument)}} { {{string.Join("\n\t", nodes.Select(Indent))}} + public async Task ToXML() + { + XmlWriterSettings settings = new XmlWriterSettings(); + settings.Indent = true; + settings.OmitXmlDeclaration = true; + settings.NewLineOnAttributes = true; + settings.Async = true; + StringBuilder stringBuilder = new StringBuilder(); + using XmlWriter writer = XmlWriter.Create(stringBuilder, settings); + await writer.WriteStartElementAsync(null,"notification","urn:ietf:params:xml:ns:netconf:notification:1.0"); + await writer.WriteStartElementAsync(null,"eventTime",null); + await writer.WriteStringAsync(DateTime.UtcNow.ToString("yyyy-MM-ddThh:mm:ssZ")); + await writer.WriteEndElementAsync(); + await writer.WriteStartElementAsync("{{XmlNamespace?.Prefix}}","{{Argument}}","{{XmlNamespace?.Namespace}}"); + {{xmlWrite}} + await writer.WriteEndElementAsync(); + await writer.WriteEndElementAsync(); + return stringBuilder.ToString(); + } + {{Indent(XmlFunction())}} } """; } + + private string GetXmlWriting() + { + if (Parent is Module) + { + return "await WriteXML(writer);"; + } + + var parent = Parent; + while (parent != null) + { + if (parent.Parent is Module or Submodule) + { + break; + } + + parent = parent.Parent; + } + + if (parent is IXMLSource source) + { + return $"await WriteXML(writer);"; + } + + throw new SemanticError( + $"Top level statement of 'notification {Argument}' ({parent?.Source.Keyword} {parent?.Argument}) was not a valid XML source", + Source); + } } \ No newline at end of file diff --git a/YangParser/SemanticModel/Rpc.cs b/YangParser/SemanticModel/Rpc.cs index 3e9eee1..2e02069 100644 --- a/YangParser/SemanticModel/Rpc.cs +++ b/YangParser/SemanticModel/Rpc.cs @@ -60,6 +60,7 @@ public override string ToCode() settings.Indent = true; settings.OmitXmlDeclaration = true; settings.NewLineOnAttributes = true; + settings.Async = true; StringBuilder stringBuilder = new StringBuilder(); using XmlWriter writer = XmlWriter.Create(stringBuilder, settings); await writer.WriteStartElementAsync(null,"rpc","urn:ietf:params:xml:ns:netconf:base:1.0");