blob: db61f016861980bfe5ee3750a0a06e1715179512 [file] [log] [blame]
Scott Baker083a3b62018-03-09 20:48:30 -08001# 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 Bhatiabfb233a2018-02-09 14:53:09 -080015import ast
16
Zack Williams045b63d2019-01-22 16:30:57 -070017
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080018def xproto_check_synchronizer(m):
19 try:
Zack Williams045b63d2019-01-22 16:30:57 -070020 sync_step_path = "synchronizer/steps/sync_%s.py" % m["name"].lower()
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080021 sync_step = open(sync_step_path).read()
22 except IOError:
Zack Williams045b63d2019-01-22 16:30:57 -070023 return "510 Model needs a sync step %s" % sync_step_path
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080024
25 try:
26 sync_step_ast = ast.parse(sync_step)
27 except SyntaxError:
Zack Williams045b63d2019-01-22 16:30:57 -070028 return "511 Could not parse sync step %s" % sync_step_path
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080029
Zack Williams045b63d2019-01-22 16:30:57 -070030 classes = filter(lambda x: isinstance(x, ast.ClassDef), sync_step_ast.body)
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080031 found_sync_step_class = False
32
33 for c in classes:
34 base_names = [v.id for v in c.bases]
Zack Williams045b63d2019-01-22 16:30:57 -070035 if "SyncStep" in base_names or "SyncInstanceUsingAnsible" in base_names:
36 attributes = filter(lambda x: isinstance(x, ast.Assign), c.body)
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080037 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 Williams045b63d2019-01-22 16:30:57 -070042 if "observes" in target_names and m["name"] in value_names:
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080043 found_sync_step_class = True
44 break
45
46 if not found_sync_step_class:
Zack Williams045b63d2019-01-22 16:30:57 -070047 return (
48 "512 Synchronizer needs a sync step class with an observes field containing %s"
49 % m["name"]
50 )
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080051 else:
Zack Williams045b63d2019-01-22 16:30:57 -070052 return "200 OK"
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080053
54
55def xproto_check_policy(m):
56 try:
Zack Williams045b63d2019-01-22 16:30:57 -070057 model_policy_path = (
58 "synchronizer/model_policies/model_policy_%s.py" % m["name"].lower()
59 )
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080060 model_policy = open(model_policy_path).read()
61 except IOError:
Zack Williams045b63d2019-01-22 16:30:57 -070062 return "510 Model needs a model policy %s" % model_policy_path
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080063
64 try:
65 model_policy_ast = ast.parse(model_policy)
66 except SyntaxError:
Zack Williams045b63d2019-01-22 16:30:57 -070067 return "511 Could not parse sync step %s" % model_policy_path
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080068
Zack Williams045b63d2019-01-22 16:30:57 -070069 classes = filter(lambda x: isinstance(x, ast.ClassDef), model_policy_ast.body)
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080070 found_model_policy_class = False
71 for c in classes:
72 base_names = [v.id for v in c.bases]
Zack Williams045b63d2019-01-22 16:30:57 -070073 if "Policy" in base_names or "TenantWithContainerPolicy" in base_names:
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080074 found_model_policy_class = True
75 break
76
77 if not found_model_policy_class:
Zack Williams045b63d2019-01-22 16:30:57 -070078 return "513 Synchronizer needs a model policy class"
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080079 else:
Zack Williams045b63d2019-01-22 16:30:57 -070080 return "200 OK"