tosca_definitions_version: tosca_simple_yaml_1_3

metadata:
  template_author: cloudsoft
  template_version: 0.1.0-SNAPSHOT
  template_name: io.cloudsoft.tomcat-member

topology_template:
  inputs:
    deployment:
      default: https://search.maven.org/remotecontent?filepath=org/apache/brooklyn/example/brooklyn-example-hello-world-webapp/0.12.0/brooklyn-example-hello-world-webapp-0.12.0.war # BROOKLYN_VERSION

  node_templates:
    tomcat-host:
      type: tosca.nodes.Compute
      capabilities:
        cloudsoft:
          properties:
            required.ports: { get_property: [ tomcat-instance, app.port ] }
    tomcat-instance:
      type: tosca-tomcat9-node
      requirements:
        - host: tomcat-host
      artifacts:
        ROOT_war:
          file: { get_input: deployment }

  outputs:
    reqs_per_sec: { get_attribute: [ tomcat-instance, request.per_sec ] }
    request_per_sec_windowed_30s: { get_attribute: [ tomcat-instance , request.count.windowed ] }

  groups:
    - add_brooklyn_types:
        members: [ tomcat-instance ]
        type: brooklyn.tosca.groups.initializer
        properties:
          brooklyn.initializers:
            - type: org.apache.brooklyn.core.sensor.ssh.SshCommandSensor
              brooklyn.config:
                name: request.count
                command: 'curl -u test:test http://localhost:8080/manager/jmxproxy | grep requestCount | cut -d " " -f 2 | sort -n | tail -n 1'
                period: 10s
                targetType: int
            - type: org.apache.brooklyn.core.sensor.ssh.SshCommandSensor
              brooklyn.config:
                name: service.process.isHealthy
                command: 'curl http://localhost:8080/health/index.html || echo "false"'
                period: 10s
                targetType: String
          brooklyn.enrichers:
            # emit failure sensor if a failure connecting to the service is sustained for 30s
            - type: org.apache.brooklyn.policy.ha.ServiceFailureDetector
              brooklyn.config:
                name: failure-detector
                entityFailed.stabilizationDelay: 30s
            - type: org.apache.brooklyn.enricher.stock.YamlTimeWeightedDeltaEnricher
              brooklyn.config:
                # derive reqs.per_sec from reqs.count
                enricher.sourceSensor: request.count
                enricher.targetSensor: request.per_sec
                enricher.delta.period: 1s
            - type: org.apache.brooklyn.enricher.stock.YamlRollingTimeWindowMeanEnricher
              brooklyn.config:
                # and take an average over 30s for reqs.per_sec into request.count.windowed
                enricher.sourceSensor: request.per_sec
                enricher.targetSensor: request.count.windowed
                enricher.window.duration: 30s
          brooklyn.policies:
            - type: org.apache.brooklyn.policy.ha.ServiceRestarter
              brooklyn.config:
                # restart if a failure is detected (with a max of one restart in 2m, sensor will propagate otherwise)
                failOnRecurringFailuresInThisDuration: 5m
    - add_brooklyn_types:
        members: [io.cloudsoft.tomcat-member]
        type: brooklyn.tosca.groups.initializer
        properties:
          brooklyn.enrichers:
            - type: org.apache.brooklyn.enricher.stock.Propagator
              brooklyn.config:
                enricher.producer: $brooklyn:component("tomcat-instance")
                enricher.propagating.propagatingAllBut:
                  - $brooklyn:sensor("entity.id")
                  - $brooklyn:sensor("service.isUp")
                  - $brooklyn:sensor("service.notUp.indicators")
                  - $brooklyn:sensor("service.problems")
                  - $brooklyn:sensor("service.process.isHealthy")
                  - $brooklyn:sensor("service.state")
                  - $brooklyn:sensor("service.state.expected")

