Exemplo n.º 1
0
    def __create_violation_link(self, agreement_id, violation, extras):
        """
           Create an OCCI violation link with the proper attribute values
        """
        LOG.debug(
            'Violation instance created for agreement {}'.format(agreement_id))

        now_iso = arrow.utcnow().isoformat()
        id = '/violation_link/' + str(uuid.uuid4())
        myrulesengine = rulesengine.RulesEngine()
        agreement = myrulesengine._registry.get_resource(agreement_id, None)
        agreement.identifier = agreement_id

        res = core_model.Link(id, occi_violation.VIOLATION_LINK, [], agreement,
                              violation)
        res.attributes = {
            'occi.core.source': agreement_id,
            'occi.core.target': violation.identifier
        }

        res.provider = extras["security"].items()[0][0]
        res.customer = extras["customer"]
        res.source = agreement
        res.target = violation.identifier

        # Updating agreement resource with new link
        agreement.links.append(res)
        myrulesengine._registry.resources.__setitem__(agreement_id, agreement)

        LOG.debug('Inserting violation link with ID: {}'.format(id))
        myrulesengine._registry.resources.__setitem__(id, res)
        return res
Exemplo n.º 2
0
    def __create_violation_resource(self, term, metric_name, metric_value,
                                    device_id, violation_metrics, remedy,
                                    extras):
        """
           Create an OCCI violation instance with the proper attribute values
        """
        LOG.debug(
            'Violation instance created for metric {} on device {}'.format(
                metric_name, device_id))

        now_iso = arrow.utcnow().isoformat()
        id = '/violation/' + str(uuid.uuid4())
        res = core_model.Resource(id, occi_violation.VIOLATION, [])
        res.attributes = {
            'occi.violation.timestamp.start': now_iso,
            'occi.violation.term': term,
            'occi.violation.metrics': json.dumps(violation_metrics),
            'occi.violation.device': device_id,
            'occi.violation.remedy': remedy
        }
        res.provider = extras["security"].items()[0][0]
        res.customer = extras["customer"]
        LOG.debug('Inserting resource with ID: {}'.format(id))
        myrulesengine = rulesengine.RulesEngine()
        myrulesengine._registry.resources.__setitem__(id, res)

        return res
Exemplo n.º 3
0
    def notification_event(self, device_id, metric_name, metric_value):
        """
               A notification event carrying the metric from the monitoring infra.
        """

        # from device and metric get agreement_id
        db_records = DB.policies.find({'devices': device_id}, {'policy': 0})
        if db_records.count() > 0:
            for policy in db_records:
                metrics = []
                for term_key, term in policy['terms'].iteritems():
                    for metric in term['metrics']:
                        metrics.append(metric)

                if metric_name in metrics:
                    metrics.remove(metric_name)
                else:
                    LOG.error('Metric {} could not be found in policy record '
                              '{}.'.format(metric_name, policy['_id']))
                    raise AttributeError('Metric {} could not be found in the '
                                         'policy record {}.'.format(
                                             metric_name, policy['_id']))

                metric_values = {metric_name: metric_value}
                for metric in metrics:
                    try:
                        c_api = self.get_collector_class(device_id, metric)
                    except AttributeError as e:
                        LOG.error(
                            'Failed to get collector class for device {}.'.
                            format(device_id))
                        LOG.warn('Loading default collector')
                        c_api = 'DummyCollector'

                    if c_api:
                        try:
                            c_class = getattr(collectors, c_api)
                            collector = c_class()
                            metric_values[metric] = collector. \
                                pull_metric(device_id, metric)
                        except AttributeError:
                            LOG.error(
                                'Collector class {} missing'.format(c_api))
                            raise RuntimeWarning(
                                'Collector class {} missing'.format(c_api))

                LOG.debug("agreement ID is {}.".format(policy['agreement_id']))
                LOG.debug("Metric(s) {} with value(s) {}.".format(
                    metric_values.keys(), metric_values.values()))

                myrulesengine = rulesengine.RulesEngine()
                myrulesengine.reason_agreement(policy['agreement_id'],
                                               metric_values, device_id)
        else:
            LOG.error('Policy record for device {} and metric {} could not be '
                      'found.'.format(device_id, metric_name))
            raise AttributeError('Policy record for device {} and metric {} '
                                 'could not be found.'.format(
                                     device_id, metric_name))
Exemplo n.º 4
0
    def __delete_violation(self, violation, extras):
        """
           Delete an OCCI violation when the violation is no longer active. 
        """
        LOG.debug('Deleting violation: {}.'.format(violation.identifier))

        myrulesengine = rulesengine.RulesEngine()
        myrulesengine._registry.delete_resource(violation.identifier, extras)
Exemplo n.º 5
0
    def agreement_term_apply_remedy(self, agreement_id, term):
        """
            Method for the application of the remedy clause.
        """
        LOG.info(
            "Enforcing remedy for agreement {} called for term {}.".format(
                agreement_id, term))

        myrulesengine = rulesengine.RulesEngine()

        myrulesengine.update_term(agreement_id, term + ".term.state",
                                  "violated")

        remedy = self._slo_terms_metrics[term]['remedy']

        # ToDo: interact with RCBaaS for charging the remedy
        self.__publish_to_rcb_queue(agreement_id, term, '', '', self.device_id,
                                    self._violated_metrics,
                                    self._slo_terms_metrics[term]['remedy'])

        extras = self.__get_extras(agreement_id)
        violation = self.__create_violation_resource(term, '', '',
                                                     self.device_id,
                                                     self._violated_metrics,
                                                     remedy, extras)
        link = self.__create_violation_link(agreement_id, violation, extras)

        LOG.info('Wait for term to become valid.')
        aggrator = aggregator.Aggregator()
        term_slo_metrics = aggrator.pull_term(
            term, agreement_id, self._slo_terms_metrics[term]['metrics'],
            self._device_id)

        for key, value in term_slo_metrics.iteritems():
            self._metrics[key] = value

        while self.agreement_term_violated(agreement_id, term):
            term_slo_metrics = aggrator.pull_term(
                term, agreement_id, self._slo_terms_metrics[term]['metrics'],
                self._device_id)

            for key, value in term_slo_metrics.iteritems():
                self._metrics[key] = value
            time.sleep(15)
        LOG.info('SLO term {} became valid again.'.format(term))
        myrulesengine.update_term(agreement_id, term + ".term.state",
                                  "fulfilled")

        #self.__update_violation_end_time(violation, extras)
        # Deleting the violation resource while is no longer active
        self.__delete_violation(violation, extras)
        self.__delete_violation_link(agreement_id, violation, link, extras)
Exemplo n.º 6
0
    def __update_violation_end_time(self, violation, extras):
        """
           Update an OCCI violation with the proper attribute values.
        """
        LOG.debug('Violation endtime updated for violation {}'.format(
            violation.identifier))

        now_iso = arrow.utcnow().isoformat()
        myrulesengine = rulesengine.RulesEngine()
        # Updating agreement resource with new link
        violation.attributes['occi.violation.timestamp.end'] = now_iso
        myrulesengine._registry.resources.__setitem__(violation.identifier,
                                                      violation)
Exemplo n.º 7
0
    def __delete_violation_link(self, agreement_id, violation, violation_link,
                                extras):
        """
            Remove the violation link resource from the registry and from the agreement resource.
        """
        myrulesengine = rulesengine.RulesEngine()
        myrulesengine._registry.delete_resource(violation_link.identifier,
                                                extras)

        agreement = myrulesengine._registry.get_resource(agreement_id, None)
        agreement.identifier = agreement_id
        if violation_link in agreement.links:
            agreement.links.remove(violation_link)