Scott Baker | 083a3b6 | 2018-03-09 20:48:30 -0800 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 16 | import ast |
| 17 | |
| 18 | def xproto_check_synchronizer(m): |
| 19 | try: |
| 20 | sync_step_path = 'synchronizer/steps/sync_%s.py'%m['name'].lower() |
| 21 | sync_step = open(sync_step_path).read() |
| 22 | except IOError: |
| 23 | return '510 Model needs a sync step %s'%sync_step_path |
| 24 | |
| 25 | try: |
| 26 | sync_step_ast = ast.parse(sync_step) |
| 27 | except SyntaxError: |
| 28 | return '511 Could not parse sync step %s'%sync_step_path |
| 29 | |
| 30 | classes = filter(lambda x:isinstance(x, ast.ClassDef), sync_step_ast.body) |
| 31 | found_sync_step_class = False |
| 32 | |
| 33 | for c in classes: |
| 34 | base_names = [v.id for v in c.bases] |
| 35 | if 'SyncStep' in base_names or 'SyncInstanceUsingAnsible' in base_names: |
| 36 | attributes = filter(lambda x:isinstance(x, ast.Assign), c.body) |
| 37 | for a in attributes: |
| 38 | target_names = [t.id for t in a.targets] |
| 39 | values = a.value.elts if isinstance(a.value, ast.List) else [a.value] |
| 40 | value_names = [v.id for v in values] |
| 41 | |
| 42 | if 'observes' in target_names and m['name'] in value_names: |
| 43 | found_sync_step_class = True |
| 44 | break |
| 45 | |
| 46 | if not found_sync_step_class: |
| 47 | return '512 Synchronizer needs a sync step class with an observes field containing %s'%m['name'] |
| 48 | else: |
| 49 | return '200 OK' |
| 50 | |
| 51 | |
| 52 | def xproto_check_policy(m): |
| 53 | try: |
| 54 | model_policy_path = 'synchronizer/model_policies/model_policy_%s.py'%m['name'].lower() |
| 55 | model_policy = open(model_policy_path).read() |
| 56 | except IOError: |
| 57 | return '510 Model needs a model policy %s'%model_policy_path |
| 58 | |
| 59 | try: |
| 60 | model_policy_ast = ast.parse(model_policy) |
| 61 | except SyntaxError: |
| 62 | return '511 Could not parse sync step %s'%model_policy_path |
| 63 | |
| 64 | classes = filter(lambda x:isinstance(x, ast.ClassDef), model_policy_ast.body) |
| 65 | found_model_policy_class = False |
| 66 | for c in classes: |
| 67 | base_names = [v.id for v in c.bases] |
| 68 | if 'Policy' in base_names or 'TenantWithContainerPolicy' in base_names: |
| 69 | found_model_policy_class = True |
| 70 | break |
| 71 | |
| 72 | if not found_model_policy_class: |
| 73 | return '513 Synchronizer needs a model policy class' |
| 74 | else: |
| 75 | return '200 OK' |
| 76 | |