/SharpSnmpLib/Mib/TextualConvention.cs

# · C# · 207 lines · 161 code · 15 blank · 31 comment · 54 complexity · d3ad6973fe9a47ae6a23f90a56b41113 MD5 · raw file

  1. using System;
  2. namespace Lextm.SharpSnmpLib.Mib
  3. {
  4. internal sealed class TextualConvention : ITypeAssignment
  5. {
  6. private string _name;
  7. private DisplayHint _displayHint;
  8. private Status _status;
  9. private string _description;
  10. private string _reference;
  11. private ITypeAssignment _syntax;
  12. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "module")]
  13. public TextualConvention(string module, string name, Lexer lexer)
  14. {
  15. _name = name;
  16. Symbol temp = lexer.NextNonEOLSymbol;
  17. if (temp == Symbol.DisplayHint)
  18. {
  19. // TODO: this needs decoding to a useful format.
  20. _displayHint = new DisplayHint(lexer.NextNonEOLSymbol.ToString().Trim(new char[] { '"' }));
  21. temp = lexer.NextNonEOLSymbol;
  22. }
  23. temp.Expect(Symbol.Status);
  24. try
  25. {
  26. _status = (Status)Enum.Parse(typeof(Status), lexer.NextNonEOLSymbol.ToString());
  27. temp = lexer.NextNonEOLSymbol;
  28. }
  29. catch (ArgumentException)
  30. {
  31. temp.Validate(true, "Invalid status");
  32. }
  33. temp.Expect(Symbol.Description);
  34. _description = lexer.NextNonEOLSymbol.ToString().Trim(new char[] { '"' });
  35. temp = lexer.NextNonEOLSymbol;
  36. if (temp == Symbol.Reference)
  37. {
  38. _reference = lexer.NextNonEOLSymbol.ToString();
  39. temp = lexer.NextNonEOLSymbol;
  40. }
  41. temp.Expect(Symbol.Syntax);
  42. /*
  43. * RFC2579 definition:
  44. * Syntax ::= -- Must be one of the following:
  45. * -- a base type (or its refinement), or
  46. * -- a BITS pseudo-type
  47. * type
  48. * | "BITS" "{" NamedBits "}"
  49. *
  50. * From section 3.5:
  51. * The data structure must be one of the alternatives defined
  52. * in the ObjectSyntax CHOICE or the BITS construct. Note
  53. * that this means that the SYNTAX clause of a Textual
  54. * Convention can not refer to a previously defined Textual
  55. * Convention.
  56. *
  57. * The SYNTAX clause of a TEXTUAL CONVENTION macro may be
  58. * sub-typed in the same way as the SYNTAX clause of an
  59. * OBJECT-TYPE macro.
  60. *
  61. * Therefore the possible values are (grouped by underlying type):
  62. * INTEGER, Integer32
  63. * OCTET STRING, Opaque
  64. * OBJECT IDENTIFIER
  65. * IpAddress
  66. * Counter64
  67. * Unsigned32, Counter32, Gauge32, TimeTicks
  68. * BITS
  69. * With appropriate sub-typing.
  70. */
  71. temp = lexer.NextNonEOLSymbol;
  72. if (temp == Symbol.Bits)
  73. {
  74. _syntax = new BitsType(module, string.Empty, lexer);
  75. }
  76. else if (temp == Symbol.Integer || temp == Symbol.Integer32)
  77. {
  78. _syntax = new IntegerType(module, string.Empty, lexer);
  79. }
  80. else if (temp == Symbol.Octet)
  81. {
  82. temp = lexer.NextSymbol;
  83. temp.Expect(Symbol.String);
  84. _syntax = new OctetStringType(module, string.Empty, lexer);
  85. }
  86. else if (temp == Symbol.Opaque)
  87. {
  88. _syntax = new OctetStringType(module, string.Empty, lexer);
  89. }
  90. else if (temp == Symbol.IpAddress)
  91. {
  92. _syntax = new IpAddressType(module, string.Empty, lexer);
  93. }
  94. else if (temp == Symbol.Counter64)
  95. {
  96. _syntax = new Counter64Type(module, string.Empty, lexer);
  97. }
  98. else if (temp == Symbol.Unsigned32 || temp == Symbol.Counter32 || temp == Symbol.Gauge32 || temp == Symbol.TimeTicks)
  99. {
  100. _syntax = new UnsignedType(module, string.Empty, lexer);
  101. }
  102. else if (temp == Symbol.Object)
  103. {
  104. temp = lexer.NextSymbol;
  105. temp.Expect(Symbol.Identifier);
  106. _syntax = new ObjectIdentifierType(module, string.Empty, lexer);
  107. }
  108. else
  109. {
  110. temp.Validate(true, "illegal syntax for textual convention");
  111. }
  112. }
  113. public string Name
  114. {
  115. get { return _name; }
  116. }
  117. public string DisplayHint
  118. {
  119. get { return _displayHint == null ? null : _displayHint.ToString(); }
  120. }
  121. public Status Status
  122. {
  123. get { return _status; }
  124. }
  125. public string Description
  126. {
  127. get { return _description; }
  128. }
  129. public string Reference
  130. {
  131. get { return _reference; }
  132. }
  133. public ITypeAssignment Syntax
  134. {
  135. get { return _syntax; }
  136. }
  137. internal object Decode(Variable v)
  138. {
  139. if (_syntax is IntegerType)
  140. {
  141. Integer32 i = v.Data as Integer32;
  142. if (i == null || (_syntax as IntegerType).IsEnumeration)
  143. {
  144. return null;
  145. }
  146. else if (_displayHint != null)
  147. {
  148. return _displayHint.Decode(i.ToInt32());
  149. }
  150. else
  151. {
  152. return i.ToInt32();
  153. }
  154. }
  155. else if (_syntax is UnsignedType)
  156. {
  157. Integer32 i = v.Data as Integer32;
  158. if (i == null)
  159. {
  160. return null;
  161. }
  162. else if (_displayHint != null)
  163. {
  164. return _displayHint.Decode(i.ToInt32());
  165. }
  166. else
  167. {
  168. return i.ToInt32();
  169. }
  170. }
  171. else if (_syntax is OctetStringType)
  172. {
  173. OctetString o = v.Data as OctetString;
  174. if (o == null)
  175. {
  176. return null;
  177. }
  178. else
  179. {
  180. // TODO: Follow the format specifier for octet strings.
  181. return null;
  182. }
  183. }
  184. else
  185. {
  186. return null;
  187. }
  188. }
  189. }
  190. }