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