Showing posts with label Dot Notation. Show all posts
Showing posts with label Dot Notation. Show all posts

Saturday, 27 September 2014

Generate dot notation OIDs - Part 2

Following last post, there are few corrections are required both in term of accuracy and correctness.

  1. Transmission OID
  2. I referred Object Identifier transmission as 0.3.6.1.2.1.10, this clearly wrong as the correct reference is "1.3.6.1.2.1.10". This is due to dependency module RFC1155-SMI from RFC 1155 does not provide an oidnum for "iso".
    RFC1155-SMI.txt
            -- Next original line is commented out for simpler syntax
     -- internet      OBJECT IDENTIFIER ::= { iso org(3) dod(6) 1 }
     iso           OBJECT IDENTIFIER ::= { 0 }
     org           OBJECT IDENTIFIER ::= { iso 3 }
     dod           OBJECT IDENTIFIER ::= { org 6 }
     internet      OBJECT IDENTIFIER ::= { dod 1 }
    

    Earlier I have mistakenly associate iso as '0' hence the error. This should fix it.
    RFC1155-SMI.txt
     iso           OBJECT IDENTIFIER ::= { 1 }
    
    Now this produces following output:

  3. MIB terminology
  4. Referring dot notation oid string as "Dotted Oid" is not accurate as oid generally refers to dot notation itself, hence again I modified the feature name to just refer it as "Oid" (refer to image above)
    Mib.xcore
    class OidValue {
     refers Identifier parent
     int oidnum
     derived String oid get {
      var p = parent
      var doid = oidnum.toString
      while (p != null) {
       doid = p.value.oidnum + "." + doid
       p = p.value.parent
      }
      doid
     }
    }
    
Another thing worth to mention here is the usage of Xcore. Xcore is an alternative to common Ecore editor with additional behaviour definitions (class method, derived feature).

TODO: However in above example "derived String oid get {..}", my initial impression was oid is lazy initialization that is set when getOid() is called, however this is not the case - from the generated code, the oid feature does not exist at all, its purely a method that does the calculation every time its called. Therefore the advantage of this "derived String oid get {..}" vs "op String getOid {..}" is the former available within the class feature.

OidValueImpl.java
 @Override
 public Object eGet(int featureID, boolean resolve, boolean coreType) {
  switch (featureID) {
   case MibPackage.OID_VALUE__PARENT:
    if (resolve) return getParent();
    return basicGetParent();
   case MibPackage.OID_VALUE__OIDNUM:
    return getOidnum();
   case MibPackage.OID_VALUE__OID:
    return getOid();
  }
  return super.eGet(featureID, resolve, coreType);
 }
Hope there is a way to make it work as lazy initialization. However, understandably lazy initialization is not going to be an easy implementation, as notification mechanism should be considered as well i.e any change to referent object should re-trigger the evaluation.

TODO2:  How does this set works i.e "derived String oid set {..}"? how do we access the parameters?

Friday, 26 September 2014

Generate dot notation OIDs - Part 1

Now basic MIB file parsing is working, its always important to get full dotted name and oid.

In order to do this, there are 2 different approaches:
  • Add a feature within Identifier EObject that calculates the dotted name/oid and saves the value as a feature (attribute).
  • Do Model to Model Transformation (MMT).
I will opt for the first as this is rather a natural addition to the existing MIB model. Came across XCore that does provide exactly this and the functionality is called "Derived Feature".

To enable this, need to:
  • Take over the Ecore/Genmodel generation from Xtext 
  • Export this Ecore/Genmodel to Xcore
  • Finally make Xtext to refer to the Xcore instead.
Here are the actual steps:
  • Make sure Xcore is available in Eclipse



  • Before exporting Mib.genmodel to Xcore, make necessary changes for generation directories/id. Because once exported to Xcore, all the files are auto-generated - its messy work to clean up later.
  • Export Mib.genmodel to Xcore. Specify the location to "platform:/resource/com.ravi.mib.xtext/model/xcore/". Noticed it always fail on the first attempt and successful on the second.



  • Import Mib model instead of generating it:

Mib.xtext
//generate mib "http://www.ravi.com/mib/xtext/Mib"
import "http://www.ravi.com/mib/xtext/Mib"

  • Modify MWE to stop generating ECore and re-point the Xcore:

GenerateMib.mwe2
    component = Generator {
        pathRtProject = runtimeProject
        pathUiProject = "${runtimeProject}.ui"
        pathTestProject = "${runtimeProject}.tests"
        projectNameRt = projectName
        projectNameUi = "${projectName}.ui"
        encoding = encoding
        language = auto-inject {
            // switch to xcore
            loadedResource = "platform:/resource/org.eclipse.emf.ecore.xcore.lib/model/XcoreLang.xcore"
            loadedResource = "classpath:/model/Xbase.ecore"
            loadedResource = "classpath:/model/Xbase.genmodel"
            loadedResource = "classpath:/model/Ecore.ecore"
            loadedResource = "classpath:/model/Ecore.genmodel"
            loadedResource = "platform:/resource/${projectName}/model/xcore/Mib.xcore"
            uri = grammarURI
    
            // Java API to access grammar elements (required by several other fragments)
            fragment = grammarAccess.GrammarAccessFragment auto-inject {}
    
            // generates Java API for the generated EPackages
            // switch to xcore
            // fragment = ecore.EMFGeneratorFragment auto-inject {}
    

  • Add Xcore plugin to MANIFEST.MF:

MANIFEST.MF
Require-Bundle: org.eclipse.xtext;visibility:=reexport,
 ...
 org.eclipse.xtext.xbase.lib,
 org.eclipse.emf.ecore.xcore
Import-Package: org.apache.log4j,

  • Now, running MWE should not cause any error (besides maybe need to manual trigger Xcore code generation - by making the Xcore file dirty e.g enter space and save)
TODO: How to add Xcore generation in MWE?

Now add following derive feature to Xcore to generate dotted name (under Identifier) and dotted oid (under OidValue).

Mib.xcore
class Identifier {
 String name
 contains OidValue value
 derived String dottedName get {
  var p = value.parent
  var dname = name
  while (p != null) {
   dname = p.name + "." + dname
   p = p.value.parent
  }
  dname
 }
}

...
class OidValue {
 refers Identifier parent
 int oidnum
 derived String dottedOid get {
  var p = parent
  var doid = oidnum.toString
  while (p != null) {
   doid = p.value.oidnum + "." + doid
   p = p.value.parent
  }
  doid
 }
}
Now, dotted name and oid should be available in the MIB model (open this via Mib Model Editor instead of Xtext Mib editor).