fix package path and move files under doctor_tests
[doctor.git] / doctor_tests / inspector / congress.py
1 ##############################################################################
2 # Copyright (c) 2017 ZTE Corporation and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9 from doctor_tests.identity_auth import get_identity_auth
10 from doctor_tests.identity_auth import get_session
11 from doctor_tests.os_clients import congress_client
12
13 from doctor_tests.inspector.base import BaseInspector
14
15
16 class CongressInspector(BaseInspector):
17     nova_api_min_version = '2.11'
18     doctor_driver = 'doctor'
19     doctor_datasource = 'doctor'
20     policy = 'classification'
21     rules = {
22         'host_down':
23             'host_down(host) :- doctor:events(hostname=host, type="compute.host.down", status="down")',
24         'active_instance_in_host':
25             'active_instance_in_host(vmid, host) :- nova:servers(id=vmid, host_name=host, status="ACTIVE")',
26         'host_force_down':
27             'execute[nova:services.force_down(host, "nova-compute", "True")] :- host_down(host)',
28         'error_vm_states':
29             'execute[nova:servers.reset_state(vmid, "error")] :- host_down(host), active_instance_in_host(vmid, host)'
30     }
31
32     def __init__(self, conf, log):
33         super(CongressInspector, self).__init__(conf, log)
34         self.auth = get_identity_auth()
35         self.congress = congress_client(get_session(auth=self.auth))
36         self._init_driver_and_ds()
37         self.inspector_url = self.get_inspector_url()
38
39     def _init_driver_and_ds(self):
40         datasources = \
41             {ds['name']: ds for ds in self.congress.list_datasources()['results']}
42
43         # check nova_api version
44         nova_api_version = datasources['nova']['config'].get('api_version')
45         if nova_api_version and nova_api_version < self.nova_api_min_version:
46             raise Exception('Congress Nova datasource API version < nova_api_min_version(%s)'
47                             % self.nova_api_min_version)
48
49         # create doctor datasource if it's not exist
50         if self.doctor_datasource not in datasources:
51             self.congress.create_datasource(
52                 body={'driver': self.doctor_driver,
53                       'name': self.doctor_datasource})
54
55         # check whether doctor driver exist
56         drivers = \
57             {driver['id']: driver for driver in self.congress.list_drivers()['results']}
58         if self.doctor_driver not in drivers:
59             raise Exception('Do not support doctor driver in congress')
60
61         self.policy_rules = \
62             {rule['name']: rule for rule in
63              self.congress.list_policy_rules(self.policy)['results']}
64
65     def get_inspector_url(self):
66         ds = self.congress.list_datasources()['results']
67         doctor_ds = next((item for item in ds if item['driver'] == 'doctor'),
68                          None)
69         congress_endpoint = self.congress.httpclient.get_endpoint(auth=self.auth)
70         return ('%s/v1/data-sources/%s/tables/events/rows' %
71                 (congress_endpoint, doctor_ds['id']))
72
73     def start(self):
74         self.log.info('congress inspector start......')
75
76         for rule_name, rule in self.rules.items():
77             self._add_rule(rule_name, rule)
78
79     def stop(self):
80         self.log.info('congress inspector stop......')
81
82         for rule_name in self.rules.keys():
83             self._del_rule(rule_name)
84
85     def _add_rule(self, rule_name, rule):
86         if rule_name not in self.policy_rules:
87             self.congress.create_policy_rule(self.policy,
88                                              body={'name': rule_name,
89                                                    'rule': rule})
90
91     def _del_rule(self, rule_name):
92         if rule_name in self.policy_rules:
93             rule_id = self.policy_rules[rule_name]['id']
94             self.congress.delete_policy_rule(self.policy, rule_id)