"""
Copyright (c) 2015 SONATA-NFV
ALL RIGHTS RESERVED.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Neither the name of the SONATA-NFV [, ANY ADDITIONAL AFFILIATION]
nor the names of its contributors may be used to endorse or promote
products derived from this software without specific prior written
permission.

This work has been performed in the framework of the SONATA project,
funded by the European Commission under Grant number 671517 through
the Horizon 2020 and 5G-PPP programmes. The authors would like to
acknowledge the contributions of their colleagues of the SONATA
partner consortium (www.sonata-nfv.eu).
"""
import logging
import time
import yaml
import requests
import os
import json
import time
from sonmanobase import messaging

logging.basicConfig(level=logging.INFO)
LOG = logging.getLogger("ssmsmart")
LOG.setLevel(logging.DEBUG)
logging.getLogger("son-mano-base:messaging").setLevel(logging.INFO)


class ManoSSM(object):

    def __init__(self):

        self.name = 'ssmsmart'
        self.version = 'v0.2'
        self.description = 'Reconfigures vFW'
        self.uuid = None

        LOG.info("Starting %r ..." % self.name)

        # create and initialize broker connection
        self.manoconn = messaging.ManoBrokerRequestResponseConnection(self.name)

        self.timeout = 0
        # register to Specific Manager Registry
        self.publish()

        # jump to run
        self.run()

    def run(self):

        # go into infinity loop

        while True:
            time.sleep(1)

    def on_registration_ok(self):

        LOG.debug("Received registration ok event.")
        self.manoconn.subscribe(self.on_alert_recieved,'son.monitoring')
        self.manoconn.publish(topic='specific.manager.registry.ssm.status',
                                message=yaml.dump({'status':'Subscribed to son.monitoring topic, waiting for alert message'}))

    def on_alert_recieved(self, ch, method, props, response):

        alert = json.loads(response)
        if time.time() > self.timeout:

            if alert['alertname'] == 'mon_rule_vm_cpu_usage_85_perc' and alert['exported_job'] == "vnf":

                LOG.info('Alert message received')
                LOG.info('Start reconfiguring vFW ...')

                self.manoconn.publish(topic='specific.manager.registry.ssm.status',
                                  message=yaml.dump({'status': 'cpu usage 85% alert message received, start reconfiguring vFW'}))

                # retrieve vFW IP address
                endpoint = os.environ['HOST']
                self.manoconn.publish(topic='specific.manager.registry.ssm.status',
                                  message=yaml.dump({'status': 'vFW IP address ==> "{0}"'.format(str(endpoint))}))

                entry1 = ''
                try:
                    self.timeout = time.time() + 60 * 5
                    entry1 = requests.post(url='http://' + endpoint + ':8080/stats/flowentry/add',
                                   data=json.dumps({"dpid": 1, "cookie": 200, "priority": 1000,
                                                    "match": {"dl_type": 0x0800, "nw_proto": 17, "udp_dst": 5001}}))
                    if (entry1.status_code == 200):

                        LOG.info('vFW reconfiguration succeeded')
                        self.manoconn.publish(topic='specific.manager.registry.ssm.status',
                                              message=yaml.dump({'status': 'vFW reconfiguration succeeded'}))
                    else:
                        LOG.info('vFW reconfiguration failed')
                        self.manoconn.publish(topic='specific.manager.registry.ssm.status',
                                              message=yaml.dump({'status': 'vFW reconfiguration failed ==> "{0}"'.format(str(entry1))}))
                except BaseException as err:
                    LOG.info('vFW reconfiguration failed')
                    self.manoconn.publish(topic='specific.manager.registry.ssm.status',
                                          message=yaml.dump(
                                              {'status': 'vFW reconfiguration failed ==> "{0}"'.format(str(err))}))

        else:
            LOG.info('repetitive alert')
            self.manoconn.publish(topic='specific.manager.registry.ssm.status',
                                  message=yaml.dump({'status': 'repetitive alert'}))

    def publish(self):

        """
        Send a register request to the Specific Manager registry to announce this SSM.
        """

        message = {'name': self.name,
                   'version': self.version,
                   'description': self.description}

        self.manoconn.call_async(self._on_publish_response,
                                 'specific.manager.registry.ssm.registration',
                                 yaml.dump(message))

    def _on_publish_response(self, ch, method, props, response):

        response = yaml.load(str(response))

        if response['status'] != "running":
            LOG.error("SSM registration failed. Exit.")
            exit(1)
        else:
            self.uuid = response['uuid']
            LOG.info("SSM registered with uuid: %r" % self.uuid)
            # jump to on_registration_ok()
            self.on_registration_ok()

def main():
    ManoSSM()

if __name__ == '__main__':
    main()

@@ -4651,229 +4651,8 @@
 ))%0A%0A
-        else:%0A            LOG.info('repetitive alert')%0A            self.manoconn.publish(topic='specific.manager.registry.ssm.status',%0A                                  message=yaml.dump(%7B'status': 'repetitive alert'%7D))%0A%0A
     

add code to SSM to react only on the first alert message.