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