blob: c361b644c2c6eb4fe0cab0f9afcc09b18cefb7cf [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
Zack Williams9a42f872019-02-15 17:56:04 -070015from __future__ import absolute_import
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080016import ast
17
Zack Williams045b63d2019-01-22 16:30:57 -070018
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080019def xproto_check_synchronizer(m):
20 try:
Zack Williams045b63d2019-01-22 16:30:57 -070021 sync_step_path = "synchronizer/steps/sync_%s.py" % m["name"].lower()
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080022 sync_step = open(sync_step_path).read()
23 except IOError:
Zack Williams045b63d2019-01-22 16:30:57 -070024 return "510 Model needs a sync step %s" % sync_step_path
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080025
26 try:
27 sync_step_ast = ast.parse(sync_step)
28 except SyntaxError:
Zack Williams045b63d2019-01-22 16:30:57 -070029 return "511 Could not parse sync step %s" % sync_step_path
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080030
Zack Williams9a42f872019-02-15 17:56:04 -070031 classes = [x for x in sync_step_ast.body if isinstance(x, ast.ClassDef)]
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080032 found_sync_step_class = False
33
34 for c in classes:
35 base_names = [v.id for v in c.bases]
Zack Williams045b63d2019-01-22 16:30:57 -070036 if "SyncStep" in base_names or "SyncInstanceUsingAnsible" in base_names:
Zack Williams9a42f872019-02-15 17:56:04 -070037 attributes = [x for x in c.body if isinstance(x, ast.Assign)]
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080038 for a in attributes:
39 target_names = [t.id for t in a.targets]
40 values = a.value.elts if isinstance(a.value, ast.List) else [a.value]
41 value_names = [v.id for v in values]
42
Zack Williams045b63d2019-01-22 16:30:57 -070043 if "observes" in target_names and m["name"] in value_names:
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080044 found_sync_step_class = True
45 break
46
47 if not found_sync_step_class:
Zack Williams045b63d2019-01-22 16:30:57 -070048 return (
49 "512 Synchronizer needs a sync step class with an observes field containing %s"
50 % m["name"]
51 )
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080052 else:
Zack Williams045b63d2019-01-22 16:30:57 -070053 return "200 OK"
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080054
55
56def xproto_check_policy(m):
57 try:
Zack Williams045b63d2019-01-22 16:30:57 -070058 model_policy_path = (
59 "synchronizer/model_policies/model_policy_%s.py" % m["name"].lower()
60 )
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080061 model_policy = open(model_policy_path).read()
62 except IOError:
Zack Williams045b63d2019-01-22 16:30:57 -070063 return "510 Model needs a model policy %s" % model_policy_path
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080064
65 try:
66 model_policy_ast = ast.parse(model_policy)
67 except SyntaxError:
Zack Williams045b63d2019-01-22 16:30:57 -070068 return "511 Could not parse sync step %s" % model_policy_path
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080069
Zack Williams9a42f872019-02-15 17:56:04 -070070 classes = [x for x in model_policy_ast.body if isinstance(x, ast.ClassDef)]
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080071 found_model_policy_class = False
72 for c in classes:
73 base_names = [v.id for v in c.bases]
Zack Williams045b63d2019-01-22 16:30:57 -070074 if "Policy" in base_names or "TenantWithContainerPolicy" in base_names:
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080075 found_model_policy_class = True
76 break
77
78 if not found_model_policy_class:
Zack Williams045b63d2019-01-22 16:30:57 -070079 return "513 Synchronizer needs a model policy class"
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080080 else:
Zack Williams045b63d2019-01-22 16:30:57 -070081 return "200 OK"