packetforge: add packetforge for generic flow to extras
[vpp.git] / extras / packetforge / NodeAttribute.py
1 # Copyright (c) 2022 Intel and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 from InputFormat import *
15 import ExpressionConverter
16
17
18 class NodeAttribute:
19     def __init__(self):
20         self.DefaultValue = None
21
22     def Create(token):
23         if token == None:
24             return None
25
26         attr = NodeAttribute()
27
28         if token["name"] == None:
29             return None
30         if token["size"] == None:
31             return None
32
33         # name
34         attr.Name = token["name"]
35
36         inputFormat = InputFormat.bytearray
37         res, u16 = ExpressionConverter.ToNum(token["size"])
38
39         # size
40         if res:
41             attr.Size = u16
42             if u16 <= 8:
43                 inputFormat = InputFormat.u8
44             elif u16 <= 16:
45                 inputFormat = InputFormat.u16
46             elif u16 <= 32:
47                 inputFormat = InputFormat.u32
48             elif u16 <= 64:
49                 inputFormat = InputFormat.u64
50             else:
51                 inputFormat = InputFormat.bytearray
52         else:
53             return None
54
55         if "format" in token and token["format"] != None:
56             inputFormat = InputFormat[token["format"]]
57
58         attr.Format = inputFormat
59         if "default" in token and token["default"] != None:
60             attr.DefaultValue = token["default"]
61             ret, _ = ExpressionConverter.Verify(attr.Format, attr.DefaultValue)
62             if not ret:
63                 return None
64
65         return attr