oasis-open / tosca-community-contributions

OASIS TC Open Repository: Manages TOSCA profiles, tests, and templates that are maintained by the TOSCA community. They are intended to be used as examples to help developers get started with TOSCA and to test compliance of TOSCA implementations with the standard.
https://github.com/oasis-open/tosca-community-contributions
Apache License 2.0
39 stars 25 forks source link

Use condition syntax in node filters and capability filters #121

Open lauwers opened 2 years ago

lauwers commented 2 years ago

I have a need to define more complex requirement fulfillment logic than what can be expressed using node and capability filters. I believe the existing condition syntax could be used for this purpose. The following example shows how condition syntax could express anti-collocation behavior:

tosca_definitions_version: tosca_2_0

capability_types:
  Host:
    description: for hosting software components

relationship_types:
  HostedOn:
    description: relationship to hosting container

node_types:

  Host:
    properties:
      uuid:
        type: string
    capabilities:
      host:
        type: Host

  SoftwareComponent:
    requirements:
      - host:
          capability: Host
          relationship: HostedOn

service_template:

  node_templates:

    host1:
      type: Host
      properties:
        uuid: host.1

    host2:
      type: Host
      properties:
        uuid: host.2

    host3:
      type: Host
      properties:
        uuid: host.3

    software1:
      type: SoftwareComponent
      requirements:
        - host:
            node: Host

    software2:
      type: SoftwareComponent
      requirements:
        - host:
            node: Host
            filter:
              - not:
                  [TARGET, uuid] : { equal: [software1, RELATIONSHIP, host, TARGET, uuid ] }
lauwers commented 2 years ago

My apologies, but I believe I have an error in the filter clause in the above example. I think the condition in the filter needs a get_property function to work correctly, as follows:

tosca_definitions_version: tosca_2_0

capability_types:
  Host:
    description: for hosting software components

relationship_types:
  HostedOn:
    description: relationship to hosting container

node_types:

  Host:
    properties:
      uuid:
        type: string
    capabilities:
      host:
        type: Host

  SoftwareComponent:
    requirements:
      - host:
          capability: Host
          relationship: HostedOn

service_template:

  node_templates:

    host1:
      type: Host
      properties:
        uuid: host.1

    host2:
      type: Host
      properties:
        uuid: host.2

    host3:
      type: Host
      properties:
        uuid: host.3

    software1:
      type: SoftwareComponent
      requirements:
        - host:
            node: Host

    software2:
      type: SoftwareComponent
      requirements:
        - host:
            node: Host
            filter:
              - not:
                  [TARGET, uuid] : { equal:  { get_property: [software1, RELATIONSHIP, host, TARGET, uuid ] } }
lauwers commented 1 year ago

Node filters now use boolean expressions as documented in https://docs.oasis-open.org/tosca/TOSCA/v2.0/csd05/TOSCA-v2.0-csd05.html#_Toc125468604. Using this syntax, the example above would look as follows:

tosca_definitions_version: tosca_2_0

capability_types:
  Host: {}

relationship_types:
  HostedOn: {}

node_types:
  Host:
    properties:
      uuid:
        type: string
    capabilities:
      host:
        type: Host

  SoftwareComponent:
    requirements:
      - host:
          capability: Host
          relationship: HostedOn
          count_range: [1, 1]

service_template:
  node_templates:

    host1:
      type: Host
      properties:
        uuid: host.1

    host2:
      type: Host
      properties:
        uuid: host.2

    host3:
      type: Host
      properties:
        uuid: host.3

    software1:
      type: SoftwareComponent
      requirements:
        - host:
            node: Host

    software2:
      type: SoftwareComponent
      requirements:
        - host:
            node: Host
            node_filter:
              $not:
                $equal:
                  - $get_property: [SELF, TARGET, uuid]
                  - $get_property: [software1, RELATIONSHIP, host, TARGET, uuid] 

Of course, the example itself is a bit convoluted, since anti-collocation behavior should be specified instead using a declarative policy as suggested in Issue https://github.com/oasis-open/tosca-community-contributions/issues/141