corenova / yang-js

YANG parser and composer
Apache License 2.0
56 stars 18 forks source link

Model CUD events raised even when a model change does not comply with its schema #34

Closed ramukima closed 8 years ago

ramukima commented 8 years ago

Model (alongside example/petstore.yang)

bash-4.3$ cat example/foo.yang
module foo {
  prefix foo;
  import petstore {
    prefix ps;
  }
  list foo {
    key "name";
    leaf name {
      type string;
    }
    leaf petid {
      type leafref {
        path "/ps:pet/ps:id";
      }
      mandatory true;
    }
  }
}
bash-4.3$

Create a foo:foo with wrong leafref value:

bash-4.3$ curl -i -X  GET http://localhost:5000/pet
HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: application/json; charset=utf-8
Content-Length: 172
ETag: W/"ac-sBDpdXrQXR+j1xDWHpGmuQ"
Date: Fri, 26 Aug 2016 20:30:04 GMT
Connection: keep-alive

{
  "petstore:pet": [
    {
      "id": 1,
      "name": "happy",
      "tag": "friendly"
    },
    {
      "id": 2,
      "name": "boba",
      "tag": "hyper"
    }
  ]
curl -i -X POST http://localhost:5000/foo:foo -H 'content-type: application/json' -d '{ "name": "foo1", "petid": 3 }'
HTTP/1.1 201 Created
X-Powered-By: Express
Content-Type: application/json; charset=utf-8
Content-Length: 433
ETag: W/"1b1-dL2bxXGURlemyRmBIUU0bg"
Date: Fri, 26 Aug 2016 20:30:11 GMT
Connection: keep-alive

{
  "foo:foo": [
    {
      "name": "foo1",
      "petid": {
        "error-tag": "data-missing",
        "error-app-tag": "instance-required",
        "err-path": {
          "kind": "xpath",
          "tag": "/",
          "xpath": {
            "kind": "xpath",
            "tag": "ps:pet",
            "xpath": {
              "kind": "xpath",
              "tag": "ps:id"
            }
          }
        }
      }
    }
  ]
}

Petstore Server Log

[yang-express] initializing...
[openapi] enabling...
[openapi] enabled ok
[yangapi] enabling...
[yangapi] enabled ok
[restjson] enabling...
[restjson] enabled ok
[websocket] enabling...
[petstore] importing 'petstore:pet' from model to the store
{ data: { 'petstore:pet': [ [Object], [Object] ] } }
[petstore] importing 'foo:foo' from model to the store
[yang-express] start of a new journey
[websocket] binding to server
[websocket] enabled ok
[restjson:petstore] calling GET on /pet
[restjson:petstore] calling GET on /pet
[restjson:petstore] calling POST on /foo:foo
[@name] update for /foo:foo
ramukima commented 8 years ago

Should the event be raised only after 'the change in model is validated against the schema' ?

sekur commented 8 years ago

To be honest, I wasn't really sure what the behavior for missing leafref should be. Because it is a relational dependency, it may be valid at the time it's being set but it CAN also become invalid if the data it's pointing at changes. So I opted to compute it at the Getter side instead of the Setter side. Which basically means that it currently accepts an invalid/missing leafref while you're setting it (since it may become valid at a future time when the referencing leaf can be resolved).

What I can do is to have it trigger an error - but still accept the data? Not too sure yet on the best approach for this...

ramukima commented 8 years ago

The missing leafref is reported perfectly fine as seen above. I am not sure whether an 'CU' event must be raised on such things. Every subscription will then need to parse such errors on every event raised and that too for every field in the schema, which is not so good.

How does the system reject an invalid model when I do not pass any value for 'mandatory true' attributes ? Does that as well raise an event so that the model subscriber receives it?

sekur commented 8 years ago

Well, the CU events are raised because the input configuration was not rejected during Setter (despite the fact that missing leafref does make that configuration invalid). Basically we are implicitly treating it as a pseudo-valid configuration. I can also consider having it enforce during Setter so the data itself is rejected?

Except for the leafref case, all other constraint validations take effect during Setter - which means that an error will be thrown and no CUD events will be triggered.

sekur commented 8 years ago

Latest yang-js handles leafref resolution during Setter and will reject invalid value appropriately and not raise any CU events. Closing issue.