How does instance-identifier looks like in yang model? - ietf-netmod-yang

AS far as I understand, instance-identifier type has an XPath statement which points to some node is a tree. And what's next? How does instance-identifier identify this node? How do I apply instance-identifier to the node it points to? Or do I get it totally wrong...
I also don't have any example of this except those found in google like
leaf instance-identifier-leaf {
type instance-identifier;
}

An instance-identifier can be a reference to any data node in the system.
Think of it as a pointer; it doesn't contain the data itself, just a reference to it (e.g. an address)
It is useful for example to represent a reference to an object that is modeled as a YANG container or list instance..
Given that YANG data can be expressed as an XML document, the way you 'point' to a specific element within that is therefore similar to 'pointing' to a specific XML element. The way you do that in XML is by using XPath, which allows to use a
Here's an example:
container a {
list b {
key x;
leaf x { type int8; }
list c {
key y;
leaf y { type int8; }
}
}
}
leaf ref {
type instance-identifier;
}
So imagine that a real system has its datastore containing this data (for simplification, I'm using XML format, and ignoring namespaces; a real system doesn't need to keep its datastore in XML format):
<a>
<b>
<x>1</x>
</b>
<b>
<x>5</x>
<c>
<y>1</y>
</c>
<c>
<y>2</y>
</c>
</b>
<b>
<x>10</x>
<c>
<y>5</y>
</c>
</b>
</a>
So basically we have a bunch of list entries, some of them cascaded.
If you'd represent all these nodes in xpath, you'd get a list with:
/a
/a/b[x=1]
/a/b[x=5]
/a/b[x=5]/c[y=1]
/a/b[x=5]/c[y=2]
/a/b[x=10]
/a/b[x=10]/c[y=5]
If you had an instance identifier outside this hierarchy called ref, it could take any of these xpaths as possible values, as a string value. So it contain a reference to one of those nodes; it would not contain the node itself, just a reference to it.
One final note is that it is not mandatory that the node referenced by the instance-identifier actually exists in the datastore; you can have an instance-identifier that is pointing to a non-existent node. There is a yang statement (requires-instance) that can be added as a substatement of the type statement, that allows to control whether only existing instances can be referenced, or whether non-existing ones can also be accepted.
Regarding the format of the value, note that the way the instance-identifier is represented depends on the protocol you are using. An instance identifier in NETCONF is different from one in RESTCONF (although they are very similar).
You could imagine a CLI to have a custom way to represent YANG objects, for example:
A data node is defined with the xpath /a/b[x=5]/c[y=1]
In CLI, that node address is viewed as c-5-1 (just an example)
If you had an instance-identifier pointing to that object, its value would be the string 'c-5-1' in CLI, but in NETCONF it would still be the xpath.
In short, the format depends on the protocol you are using.

Related

Drools : applying same rules on all attributes

I am new to Drools, we are trying to create basic validation rules like a NULL check, etc. using the Drools n Scala framework.
I have a source file which has 200 attributes, need to apply NULL-check rule on all these attributes,
is there any easy way to do this? or do I need to create 200 rules for each attribute?
Thanks in advance.
Assuming you have a POJO ("plain old java object", getters/setters and some private variables to hold values) or modern java Records (effectively the same thing), then the answer is no: you need separate rules. For this scenario, the only way to check that field "name" is null is to actually assert against that field like this:
rule "example - name is null"
when
ExampleObject( name == null )
then
System.out.println("Name is null.");
end
However there exist other data structures -- for example, Map and its sibling types -- where you can reference the fields by name. In this case you could theoretically iterate through all of the field names and find the one whose value is empty.
So, for example, Map has a keySet() method which returns a set of fields -- you could iterate through this keyset and for each key check that there is a non-null value present in the map.
rule "example with map"
when
$map: Map()
$keys: Set() from $map.keySet()
$key: String() from $keys
String( this == null ) from $map.get($key)
// or this might work, not sure if the "this" keyword allows this syntax:
// Map( this[$key] == null ) from $map
then
System.out.println($key + " is missing/null");
end
This would require converting your Java object into a Map before passing into the rules.
However I DO NOT RECOMMEND this approach. Maps are extremely un-performant in rules because of how they serialize/deserialize. You will use a ton of unnecessary heap when firing them. If you look at how a HashMap serializes, for example, by peeking at its source code you'll see that it actually contains a bunch of "child" data structures like entryset and keyset and things like that. When using "new", those child structures are only initialized if and when you need them; but when serializing/deserializing, they're created immediately even if you don't need them.
Another solution would be to use Java reflection to get the list of declared field names, and then iterate through those names using reflection to get the value out for that field. In your place I'd do this in Java (reflection is problematic enough without trying to do it in Drools) and then if necessary invoke such a utility function from Drools.

Unique symbol value on type level

Is it possible to have some kind of unique symbol value on the type level, that could be used to distinct (tag) some record without the need to supply a unique string value?
In JS there is Symbol often used for such things. But I would like to have it without using Effect, in pure context.
Well, it could even like accessing Full qualified module name (which is quite unique for the task), but I'm not sure if this is a really relevant/possible thing in the Purescript context.
Example:
Say There is some module that exposes:
type Worker value state =
{ tag :: String
, work :: value -> state -> Effect state
}
makeWorker :: forall value state. Worker value state
performWork :: forall value state. woker -> Worker value state -> value -> Unit
This module is used to manage the state of workers, it passes them value and current state value, and gets Effect with new state value, and puts in state map where keys are tags.
Users of the module:
In one module:
worker = makeWorker { tag: "WorkerOne", work }
-- Then this tagged `worker` is used to performWork:
-- performWork worker "Some value"
In another module we use worker with another tag:
worker = makeWorker { tag: "WorkerTwo", work }
So it would be nice if there would be no need to supply a unique string ("WorkerOne", "WorkerTwo") as a tag but use some "generated" unique value. But the task is that worker should be created on the top level of the module in pure context.
Semantics of PureScript as such is pure and pretty much incompatible with this sort of thing. Same expression always produces same result. The results can be represented differently at a lower level, but in the language semantics they're the same.
And this is a feature, not a bug. In my experience, more often than not, a requirement like yours is an indication of a flawed design somewhere upstream.
An exception to this rule is FFI: if you have to interact with the underlying platform, there is no choice but to play by that platform's rules. One example I can give is React, which uses the JavaScript's implicit object identity as a way to tell components apart.
So the bottom line is: I urge you to reconsider the requirement. Chances are, you don't really need it. And even if you do, manually specified strings might actually be better than automatically generated ones, because they may help you troubleshoot later.
But if you really insist on doing it this way, good news: you can cheat! :-)
You can generate your IDs effectfully and then wrap them in unsafePerformEffect to make it look pure to the compiler. For example:
import Effect.Unsafe (unsafePerformEffect)
import Data.UUID (toString, genUUID)
workerTag :: String
workerTag = toString $ unsafePerformEffect genUUID

Get children of Dom_html.element

In js_of_ocaml, is it possible to get the child nodes of Dom_html.element?
I know that the class inherits Dom.node, and thus has the childNodes method. But since it is a method from Dom.node, it returns values of Dom.node types. And I need those nodes to still be Dom_html.element, or else most methods will not be available.
Since downcasting is not possible in OCaml, I do not find any possible solution for this issue. Am I missing something or is this really impossible?
childNodes cant't be typed as a collection of Dom_html.elements because the nodes returned can, and are likely to, include nodes that are not elements, such as text nodes.
The DOM standard defines a property children on Element which would only return the elements, but that still wouldn't get you to Dom_html.element. And unfortunately it also does not seem to be included in JSOO's Dom.element.
You can use the element function of Dom.CoerceTo to safely coerce Dom.nodes to Dom.elements, but I don't think there is any generally reliable way to go from Dom.element to Dom_html.element, because the DOM is unfortunately too dynamically typed.
You might have to check the tagName manually and (unsafely) cast it using Js.Unsafe.coerce.

XSD import into another XSD file

I have a problem with imported xsd's.
i have 3 xsd service.xsd, header.xsd and inputmessage.xsd
inputmessage.xsd contains the root element.
service.xsd imports header.xsd and inputmessage xsd.
while generating sample xml of service.xsd in eclipse i get the following error "No root element exists since the scheme provided has no global elements".
The error you are seeing is typically due to use of schema documents which do not declare an outer element (a 'root element'). The schemas with which you are working may define only complex types (likely with enclosed elements). The significance of the element w.r.t. file creation is that an element defines the concrete implementation of a type in an xml file (i.e. the name of the element from the schema becomes the tag name in the xml file). The complex type defines the structure that would apply to an element which is of that type.
In your service.xsd file, try inserting the following (you may need to work with the prefix binding to be consistent with your schema file):
<element name="rootElement" type="tns:LocallyDefinedType" />
where 'tns' is bound to the schema target namespace and 'LocallyDefinedType' is the name of a complex type defined in the schema document (the type you are hoping to see in the generated xml document).
If this does not help, post your schema documents (or some appropriate dummied-up examples) and a more targeted element declaration can be provided.

How to workaround the XmlSerialization issue with type name aliases (in .NET)?

I have a collection of objects and for each object I have its type FullName and either its value (as string) or a subtree of inner objects details (again type FullName and value or a subtree).
For each root object I need to create a piece of XML that will be xml de-serializable.
The problem with XmlSerializer is that i.e. following object
int age = 33;
will be serialized to
<int>33</int>
At first this seems to be perfectly ok, however when working with reflection you will be using System.Int32 as type name and int is an alias and this
<System.Int32>33</System.Int32>
will not deserialize.
Now additional complexity comes from the fact that I need to handle any possible data type.
So solutions that utilize System.Activator.CreateInstance(..) and casting won't work unless I go down the path of code gen & compilation as a way of achieving this (which I would rather avoid)
Notes:
A quick research with .NET Reflector revealed that XmlSerializer uses internal class TypeScope, look at its static constructor to see that it initializes an inner HashTable with mappings.
So the question is what is the best (and I mean elegant and solid) way to workaround this sad fact?
I don't exactly see, where your problem originally stems from. XmlSerializer will use the same syntax/mapping for serializing as for deserializing, so there is no conflict when using it for both serializing and deserializing.
Probably the used type-tags are some xml-standard thing, but not sure about that.
I guess the problem is more your usage of reflection. Do you instantiate your
imported/deserialized objects by calling Activator.CreateInstance ?
I would recommend the following instead, if you have some type Foo to be created from the xml in xmlReader:
Foo DeserializedObject = (Foo)Serializer(typeof(Foo)).Deserialize(xmlReader);
Alternatively, if you don't want to switch to the XmlSerializer completely, you could do some preprocessing of your input. The standard way would then be to create some XSLT, in which you transform all those type-elements to their aliases or vice versa. then before processing the XML, you apply your transformation using System.Xml.Xsl.XslCompiledTransform and use your (or the reflector's) mappings for each type.
Why don't you serialize all the field's type as an attribute?
Instead of
<age>
<int>33</int>
</age>
you could do
<age type="System.Int32">
33
</age>