Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [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 | |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame^] | 15 | from __future__ import absolute_import, print_function |
Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [diff] [blame] | 16 | |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame^] | 17 | import copy |
| 18 | import sys |
| 19 | |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 20 | import plyxproto.model as m |
Sapan Bhatia | db183c2 | 2017-06-23 02:47:42 -0700 | [diff] [blame] | 21 | from plyxproto.helpers import Visitor |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame^] | 22 | from six.moves import map, range |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 23 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 24 | |
Sapan Bhatia | d3fcb66 | 2017-07-25 21:13:48 -0400 | [diff] [blame] | 25 | class MissingPolicyException(Exception): |
| 26 | pass |
| 27 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 28 | |
Sapan Bhatia | d3fcb66 | 2017-07-25 21:13:48 -0400 | [diff] [blame] | 29 | def find_missing_policy_calls(name, policies, policy): |
| 30 | if isinstance(policy, dict): |
| 31 | (k, lst), = policy.items() |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 32 | if k == "policy": |
Sapan Bhatia | d3fcb66 | 2017-07-25 21:13:48 -0400 | [diff] [blame] | 33 | policy_name = lst[0] |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 34 | if policy_name not in policies: |
| 35 | raise MissingPolicyException( |
| 36 | "Policy %s invoked missing policy %s" % (name, policy_name) |
| 37 | ) |
Sapan Bhatia | d3fcb66 | 2017-07-25 21:13:48 -0400 | [diff] [blame] | 38 | else: |
| 39 | for p in lst: |
| 40 | find_missing_policy_calls(name, policies, p) |
| 41 | elif isinstance(policy, list): |
| 42 | for p in lst: |
| 43 | find_missing_policy_calls(name, policies, p) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 44 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 45 | |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 46 | def dotname_to_fqn(dotname): |
| 47 | b_names = [part.pval for part in dotname] |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 48 | package = ".".join(b_names[:-1]) |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 49 | name = b_names[-1] |
| 50 | if package: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 51 | fqn = package + "." + name |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 52 | else: |
| 53 | fqn = name |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 54 | return {"name": name, "fqn": fqn, "package": package} |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 55 | |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 56 | |
| 57 | def dotname_to_name(dotname): |
| 58 | b_names = [part.pval for part in dotname] |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 59 | return ".".join(b_names) |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 60 | |
| 61 | |
Sapan Bhatia | 504cc97 | 2017-04-27 01:56:28 +0200 | [diff] [blame] | 62 | def count_messages(body): |
| 63 | count = 0 |
| 64 | for e in body: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 65 | if isinstance(e, m.MessageDefinition): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 66 | count += 1 |
Sapan Bhatia | 504cc97 | 2017-04-27 01:56:28 +0200 | [diff] [blame] | 67 | return count |
| 68 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 69 | |
Sapan Bhatia | 504cc97 | 2017-04-27 01:56:28 +0200 | [diff] [blame] | 70 | def count_fields(body): |
| 71 | count = 0 |
| 72 | for e in body: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 73 | if type(e) in [m.LinkDefinition, m.FieldDefinition, m.LinkSpec]: |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 74 | count += 1 |
Sapan Bhatia | 504cc97 | 2017-04-27 01:56:28 +0200 | [diff] [blame] | 75 | return count |
| 76 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 77 | |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 78 | def name_to_value(obj): |
| 79 | try: |
| 80 | value = obj.value.value.pval |
| 81 | except AttributeError: |
| 82 | try: |
| 83 | value = obj.value.value |
| 84 | except AttributeError: |
| 85 | value = obj.value.pval |
| 86 | |
| 87 | return value |
| 88 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 89 | |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 90 | class Stack(list): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 91 | def push(self, x): |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 92 | self.append(x) |
| 93 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 94 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 95 | """ XOS2Jinja overrides the underlying visitor pattern to transform the tree |
| 96 | in addition to traversing it """ |
| 97 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 98 | |
Sapan Bhatia | db183c2 | 2017-06-23 02:47:42 -0700 | [diff] [blame] | 99 | class XOS2Jinja(Visitor): |
Scott Baker | c237f88 | 2018-09-28 14:12:47 -0700 | [diff] [blame] | 100 | def __init__(self, args): |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 101 | super(XOS2Jinja, self).__init__() |
| 102 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 103 | self.stack = Stack() |
| 104 | self.models = {} |
| 105 | self.options = {} |
| 106 | self.package = None |
| 107 | self.message_options = {} |
| 108 | self.count_stack = Stack() |
Sapan Bhatia | db183c2 | 2017-06-23 02:47:42 -0700 | [diff] [blame] | 109 | self.policies = {} |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 110 | self.content = "" |
| 111 | self.offset = 0 |
| 112 | self.current_message_name = None |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 113 | self.verbose = 0 |
| 114 | self.first_field = True |
| 115 | self.first_method = True |
Scott Baker | c237f88 | 2018-09-28 14:12:47 -0700 | [diff] [blame] | 116 | self.args = args |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 117 | |
Sapan Bhatia | db183c2 | 2017-06-23 02:47:42 -0700 | [diff] [blame] | 118 | def visit_PolicyDefinition(self, obj): |
| 119 | if self.package: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 120 | pname = ".".join([self.package, obj.name.value.pval]) |
Sapan Bhatia | db183c2 | 2017-06-23 02:47:42 -0700 | [diff] [blame] | 121 | else: |
| 122 | pname = obj.name.value.pval |
| 123 | |
| 124 | self.policies[pname] = obj.body |
Sapan Bhatia | d3fcb66 | 2017-07-25 21:13:48 -0400 | [diff] [blame] | 125 | find_missing_policy_calls(pname, self.policies, obj.body) |
Sapan Bhatia | 3e3c1cd | 2017-07-15 01:35:44 -0400 | [diff] [blame] | 126 | |
| 127 | return True |
| 128 | |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 129 | def visit_PackageStatement(self, obj): |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 130 | dotlist = obj.name.value |
| 131 | dotlist2 = [f.pval for f in dotlist] |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 132 | dotstr = ".".join(dotlist2) |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 133 | self.package = dotstr |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 134 | return True |
| 135 | |
| 136 | def visit_ImportStatement(self, obj): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 137 | """Ignore""" |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 138 | return True |
| 139 | |
| 140 | def visit_OptionStatement(self, obj): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 141 | if not hasattr(obj, "mark_for_deletion"): |
| 142 | if self.current_message_name: |
Sapan Bhatia | ae9645c | 2017-05-05 15:35:54 +0200 | [diff] [blame] | 143 | self.message_options[obj.name.value.pval] = obj.value.value.pval |
| 144 | else: |
| 145 | self.options[obj.name.value.pval] = obj.value.value.pval |
Sapan Bhatia | 504cc97 | 2017-04-27 01:56:28 +0200 | [diff] [blame] | 146 | |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 147 | return True |
| 148 | |
| 149 | def visit_LU(self, obj): |
| 150 | return True |
| 151 | |
| 152 | def visit_default(self, obj): |
| 153 | return True |
| 154 | |
| 155 | def visit_FieldDirective(self, obj): |
| 156 | return True |
| 157 | |
| 158 | def visit_FieldDirective_post(self, obj): |
| 159 | |
| 160 | try: |
| 161 | name = obj.name.value.pval |
| 162 | except AttributeError: |
| 163 | name = obj.name.value |
| 164 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 165 | if isinstance(obj.value, list): |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 166 | value = dotname_to_name(obj.value) |
| 167 | else: |
| 168 | value = name_to_value(obj) |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 169 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 170 | self.stack.push([name, value]) |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 171 | return True |
| 172 | |
| 173 | def visit_FieldType(self, obj): |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame^] | 174 | """ |
| 175 | Field type, if type is name, then it may need refactoring consistent |
| 176 | with refactoring rules according to the table |
| 177 | """ |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 178 | return True |
| 179 | |
| 180 | def visit_LinkDefinition(self, obj): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 181 | s = {} |
Sapan Bhatia | 8a57c66 | 2017-04-11 10:39:47 -0700 | [diff] [blame] | 182 | |
Sapan Bhatia | ae9645c | 2017-05-05 15:35:54 +0200 | [diff] [blame] | 183 | try: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 184 | s["link_type"] = obj.link_type.pval |
Sapan Bhatia | ae9645c | 2017-05-05 15:35:54 +0200 | [diff] [blame] | 185 | except AttributeError: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 186 | s["link_type"] = obj.link_type |
Sapan Bhatia | ae9645c | 2017-05-05 15:35:54 +0200 | [diff] [blame] | 187 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 188 | s["src_port"] = obj.src_port.value.pval |
| 189 | s["name"] = obj.src_port.value.pval |
Sapan Bhatia | db183c2 | 2017-06-23 02:47:42 -0700 | [diff] [blame] | 190 | try: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 191 | s["policy"] = obj.policy.pval |
Sapan Bhatia | db183c2 | 2017-06-23 02:47:42 -0700 | [diff] [blame] | 192 | except AttributeError: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 193 | s["policy"] = None |
Sapan Bhatia | c4f803f | 2017-04-21 11:50:39 +0200 | [diff] [blame] | 194 | |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 195 | try: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 196 | s["dst_port"] = obj.dst_port.value.pval |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 197 | except AttributeError: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 198 | s["dst_port"] = obj.dst_port |
Sapan Bhatia | c4f803f | 2017-04-21 11:50:39 +0200 | [diff] [blame] | 199 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 200 | if isinstance(obj.through, list): |
| 201 | s["through"] = dotname_to_fqn(obj.through) |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 202 | else: |
| 203 | try: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 204 | s["through"] = obj.through.pval |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 205 | except AttributeError: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 206 | s["through"] = obj.through |
Sapan Bhatia | c4f803f | 2017-04-21 11:50:39 +0200 | [diff] [blame] | 207 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 208 | if isinstance(obj.name, list): |
| 209 | s["peer"] = dotname_to_fqn(obj.name) |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 210 | else: |
| 211 | try: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 212 | s["peer"] = obj.name.pval |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 213 | except AttributeError: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 214 | s["peer"] = obj.name |
Sapan Bhatia | ae9645c | 2017-05-05 15:35:54 +0200 | [diff] [blame] | 215 | |
Scott Baker | c237f88 | 2018-09-28 14:12:47 -0700 | [diff] [blame] | 216 | try: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 217 | s["reverse_id"] = obj.reverse_id.pval |
Scott Baker | c237f88 | 2018-09-28 14:12:47 -0700 | [diff] [blame] | 218 | except AttributeError: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 219 | s["reverse_id"] = obj.reverse_id |
Scott Baker | c237f88 | 2018-09-28 14:12:47 -0700 | [diff] [blame] | 220 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 221 | s["_type"] = "link" |
| 222 | s["options"] = {"modifier": "optional"} |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 223 | |
| 224 | self.stack.push(s) |
| 225 | return True |
| 226 | |
| 227 | def visit_FieldDefinition(self, obj): |
| 228 | self.count_stack.push(len(obj.fieldDirective)) |
| 229 | return True |
| 230 | |
| 231 | def visit_FieldDefinition_post(self, obj): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 232 | s = {} |
| 233 | |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 234 | if isinstance(obj.ftype, m.Name): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 235 | s["type"] = obj.ftype.value |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 236 | else: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 237 | s["type"] = obj.ftype.name.pval |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 238 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 239 | s["name"] = obj.name.value.pval |
Sapan Bhatia | db183c2 | 2017-06-23 02:47:42 -0700 | [diff] [blame] | 240 | |
| 241 | try: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 242 | s["policy"] = obj.policy.pval |
Sapan Bhatia | db183c2 | 2017-06-23 02:47:42 -0700 | [diff] [blame] | 243 | except AttributeError: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 244 | s["policy"] = None |
Sapan Bhatia | db183c2 | 2017-06-23 02:47:42 -0700 | [diff] [blame] | 245 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 246 | s["modifier"] = obj.field_modifier.pval |
| 247 | s["id"] = obj.fieldId.pval |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 248 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 249 | opts = {"modifier": s["modifier"]} |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 250 | n = self.count_stack.pop() |
| 251 | for i in range(0, n): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 252 | k, v = self.stack.pop() |
Sapan Bhatia | 5769d93 | 2017-05-17 11:10:54 +0200 | [diff] [blame] | 253 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 254 | # The two lines below may be added to eliminate "" around an option. |
Sapan Bhatia | 5769d93 | 2017-05-17 11:10:54 +0200 | [diff] [blame] | 255 | # Right now, this is handled in targets. FIXME |
| 256 | # |
| 257 | # if (v.startswith('"') and v.endswith('"')): |
| 258 | # v = v[1:-1] |
| 259 | |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 260 | opts[k] = v |
| 261 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 262 | s["options"] = opts |
Sapan Bhatia | 8a57c66 | 2017-04-11 10:39:47 -0700 | [diff] [blame] | 263 | try: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 264 | last_link = self.stack[-1]["_type"] |
| 265 | if last_link == "link": |
| 266 | s["link"] = True |
| 267 | except BaseException: |
Sapan Bhatia | 8a57c66 | 2017-04-11 10:39:47 -0700 | [diff] [blame] | 268 | pass |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 269 | s["_type"] = "field" |
Sapan Bhatia | 8a57c66 | 2017-04-11 10:39:47 -0700 | [diff] [blame] | 270 | |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 271 | self.stack.push(s) |
| 272 | return True |
| 273 | |
| 274 | def visit_EnumFieldDefinition(self, obj): |
| 275 | if self.verbose > 4: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 276 | print("\tEnumField: name=%s, %s" % (obj.name, obj)) |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 277 | |
| 278 | return True |
| 279 | |
| 280 | def visit_EnumDefinition(self, obj): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 281 | """New enum definition, refactor name""" |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 282 | if self.verbose > 3: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 283 | print("Enum, [%s] body=%s\n\n" % (obj.name, obj.body)) |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 284 | |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 285 | return True |
| 286 | |
| 287 | def visit_MessageDefinition(self, obj): |
Sapan Bhatia | 504cc97 | 2017-04-27 01:56:28 +0200 | [diff] [blame] | 288 | self.current_message_name = obj.name.value.pval |
| 289 | self.message_options = {} |
| 290 | self.count_stack.push(count_fields(obj.body)) |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 291 | return True |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 292 | |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 293 | def visit_MessageDefinition_post(self, obj): |
| 294 | stack_num = self.count_stack.pop() |
| 295 | fields = [] |
| 296 | links = [] |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame^] | 297 | last_field = {} |
Sapan Bhatia | ae9645c | 2017-05-05 15:35:54 +0200 | [diff] [blame] | 298 | try: |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame^] | 299 | obj.bases = list(map(dotname_to_fqn, obj.bases)) |
Sapan Bhatia | ae9645c | 2017-05-05 15:35:54 +0200 | [diff] [blame] | 300 | except AttributeError: |
| 301 | pass |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 302 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 303 | for i in range(0, stack_num): |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 304 | f = self.stack.pop() |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 305 | if f["_type"] == "link": |
| 306 | f["options"] = { |
| 307 | i: d[i] for d in [f["options"], last_field["options"]] for i in d |
| 308 | } |
| 309 | assert last_field == fields[0] |
| 310 | fields[0].setdefault("options", {})["link_type"] = f["link_type"] |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 311 | links.insert(0, f) |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 312 | else: |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 313 | fields.insert(0, f) |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 314 | last_field = f |
| 315 | |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 316 | if self.package: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 317 | model_name = ".".join([self.package, obj.name.value.pval]) |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 318 | else: |
| 319 | model_name = obj.name.value.pval |
| 320 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 321 | model_def = { |
| 322 | "name": obj.name.value.pval, |
| 323 | "fields": fields, |
| 324 | "links": links, |
| 325 | "bases": obj.bases, |
| 326 | "options": self.message_options, |
| 327 | "package": self.package, |
| 328 | "fqn": model_name, |
| 329 | "rlinks": [], |
| 330 | } |
Sapan Bhatia | db183c2 | 2017-06-23 02:47:42 -0700 | [diff] [blame] | 331 | try: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 332 | model_def["policy"] = obj.policy.pval |
Sapan Bhatia | db183c2 | 2017-06-23 02:47:42 -0700 | [diff] [blame] | 333 | except AttributeError: |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 334 | model_def["policy"] = None |
Sapan Bhatia | db183c2 | 2017-06-23 02:47:42 -0700 | [diff] [blame] | 335 | |
Sapan Bhatia | 943dad5 | 2017-05-19 18:41:01 +0200 | [diff] [blame] | 336 | self.stack.push(model_def) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 337 | |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 338 | self.models[model_name] = model_def |
| 339 | |
| 340 | # Set message options |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame^] | 341 | for k, v in sorted(self.options.items(), key=lambda t: t[0]): # sorted by key |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 342 | try: |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 343 | if k not in self.message_options: |
| 344 | self.message_options[k] = v |
Sapan Bhatia | 3cfdf63 | 2017-06-08 05:14:03 +0200 | [diff] [blame] | 345 | except KeyError: |
| 346 | pass |
| 347 | |
Sapan Bhatia | 504cc97 | 2017-04-27 01:56:28 +0200 | [diff] [blame] | 348 | self.current_message_name = None |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 349 | return True |
| 350 | |
| 351 | def visit_MessageExtension(self, obj): |
| 352 | return True |
| 353 | |
| 354 | def visit_MethodDefinition(self, obj): |
| 355 | return True |
| 356 | |
| 357 | def visit_ServiceDefinition(self, obj): |
| 358 | return True |
| 359 | |
| 360 | def visit_ExtensionsDirective(self, obj): |
| 361 | return True |
| 362 | |
| 363 | def visit_Literal(self, obj): |
| 364 | return True |
| 365 | |
| 366 | def visit_Name(self, obj): |
| 367 | return True |
| 368 | |
| 369 | def visit_DotName(self, obj): |
| 370 | return True |
| 371 | |
| 372 | def visit_Proto(self, obj): |
Sapan Bhatia | 504cc97 | 2017-04-27 01:56:28 +0200 | [diff] [blame] | 373 | self.count_stack.push(count_messages(obj.body)) |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 374 | return True |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 375 | |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 376 | def visit_Proto_post(self, obj): |
| 377 | count = self.count_stack.pop() |
| 378 | messages = [] |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 379 | for i in range(0, count): |
Sapan Bhatia | c4f803f | 2017-04-21 11:50:39 +0200 | [diff] [blame] | 380 | try: |
| 381 | m = self.stack.pop() |
| 382 | except IndexError: |
Sapan Bhatia | 504cc97 | 2017-04-27 01:56:28 +0200 | [diff] [blame] | 383 | pass |
Sapan Bhatia | c4f803f | 2017-04-21 11:50:39 +0200 | [diff] [blame] | 384 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 385 | messages.insert(0, m) |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 386 | |
Scott Baker | c237f88 | 2018-09-28 14:12:47 -0700 | [diff] [blame] | 387 | self.compute_rlinks(messages, self.models) |
Sapan Bhatia | cb35e7f | 2017-05-24 12:17:28 +0200 | [diff] [blame] | 388 | |
Sapan Bhatia | ff1b8fa | 2017-04-10 19:44:38 -0700 | [diff] [blame] | 389 | self.messages = messages |
| 390 | return True |
| 391 | |
| 392 | def visit_LinkSpec(self, obj): |
| 393 | count = self.count_stack.pop() |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 394 | self.count_stack.push(count + 1) |
Sapan Bhatia | db183c2 | 2017-06-23 02:47:42 -0700 | [diff] [blame] | 395 | return True |
Scott Baker | c237f88 | 2018-09-28 14:12:47 -0700 | [diff] [blame] | 396 | |
| 397 | def compute_rlinks(self, messages, message_dict): |
| 398 | rev_links = {} |
| 399 | |
| 400 | link_opposite = { |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 401 | "manytomany": "manytomany", |
| 402 | "manytoone": "onetomany", |
| 403 | "onetoone": "onetoone", |
| 404 | "onetomany": "manytoone", |
Scott Baker | c237f88 | 2018-09-28 14:12:47 -0700 | [diff] [blame] | 405 | } |
| 406 | |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame^] | 407 | for msg in messages: |
| 408 | for lnk in msg["links"]: |
| 409 | rlink = copy.deepcopy(lnk) |
Scott Baker | c237f88 | 2018-09-28 14:12:47 -0700 | [diff] [blame] | 410 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 411 | rlink["_type"] = "rlink" # An implicit link, not declared in the model |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame^] | 412 | rlink["src_port"] = lnk["dst_port"] |
| 413 | rlink["dst_port"] = lnk["src_port"] |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 414 | rlink["peer"] = { |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame^] | 415 | "name": msg["name"], |
| 416 | "package": msg["package"], |
| 417 | "fqn": msg["fqn"], |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 418 | } |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame^] | 419 | rlink["link_type"] = link_opposite[lnk["link_type"]] |
| 420 | rlink["reverse_id"] = lnk["reverse_id"] |
Scott Baker | c237f88 | 2018-09-28 14:12:47 -0700 | [diff] [blame] | 421 | |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame^] | 422 | if (not lnk["reverse_id"]) and (self.args.verbosity >= 1): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 423 | print( |
| 424 | "WARNING: Field %s in model %s has no reverse_id" |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame^] | 425 | % (lnk["src_port"], msg["name"]), |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 426 | file=sys.stderr, |
| 427 | ) |
Scott Baker | c237f88 | 2018-09-28 14:12:47 -0700 | [diff] [blame] | 428 | |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame^] | 429 | if lnk["reverse_id"] and ( |
| 430 | (int(lnk["reverse_id"]) < 1000) or (int(lnk["reverse_id"]) >= 1900) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 431 | ): |
| 432 | raise Exception( |
| 433 | "reverse id for field %s in model %s should be between 1000 and 1899" |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame^] | 434 | % (lnk["src_port"], msg["name"]) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 435 | ) |
Scott Baker | c237f88 | 2018-09-28 14:12:47 -0700 | [diff] [blame] | 436 | |
| 437 | try: |
| 438 | try: |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame^] | 439 | rev_links[lnk["peer"]["fqn"]].append(rlink) |
Scott Baker | c237f88 | 2018-09-28 14:12:47 -0700 | [diff] [blame] | 440 | except TypeError: |
| 441 | pass |
| 442 | except KeyError: |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame^] | 443 | rev_links[lnk["peer"]["fqn"]] = [rlink] |
Scott Baker | c237f88 | 2018-09-28 14:12:47 -0700 | [diff] [blame] | 444 | |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame^] | 445 | for msg in messages: |
Scott Baker | c237f88 | 2018-09-28 14:12:47 -0700 | [diff] [blame] | 446 | try: |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame^] | 447 | msg["rlinks"] = rev_links[msg["name"]] |
| 448 | message_dict[msg["name"]]["rlinks"] = msg["rlinks"] |
Scott Baker | c237f88 | 2018-09-28 14:12:47 -0700 | [diff] [blame] | 449 | except KeyError: |
| 450 | pass |