| 22 | |
| 23 | |
| 24 | def Forge(pattern, actions, file_flag, show_result_only): |
| 25 | pg = ParseGraph.Create(parsegraph_path) |
| 26 | if pg == None: |
| 27 | print("error: create parsegraph failed") |
| 28 | return None |
| 29 | |
| 30 | if not file_flag: |
| 31 | token = ParsePattern(pattern) |
| 32 | if token == None: |
| 33 | return None |
| 34 | else: |
| 35 | if not os.path.exists(pattern): |
| 36 | print("error: file not exist '%s' " % (pattern)) |
| 37 | return |
| 38 | f = open(pattern, "r", encoding="utf-8") |
| 39 | token = json.load(f) |
| 40 | if "actions" in token: |
| 41 | actions = token["actions"] |
| 42 | |
| 43 | path = Path.Create(token) |
| 44 | if path == None: |
| 45 | print("error: path not exit") |
| 46 | return None |
| 47 | |
| 48 | result = pg.Forge(path) |
| 49 | if result == None: |
| 50 | print("error: result not available") |
| 51 | return None |
| 52 | |
| 53 | spec, mask = GetBinary(result.ToJSON()) |
| 54 | |
| 55 | # create generic flow |
| 56 | my_flow = { |
| 57 | "flow": { |
| 58 | "generic": { |
| 59 | "pattern": {"spec": bytes(spec.encode()), "mask": bytes(mask.encode())} |
| 60 | } |
| 61 | }, |
| 62 | } |
| 63 | |
| 64 | if show_result_only: |
| 65 | return my_flow |
| 66 | |
| 67 | my_flow.update( |
| 68 | { |
| 69 | "type": VppEnum.vl_api_flow_type_v2_t.FLOW_TYPE_GENERIC_V2, |
| 70 | } |
| 71 | ) |
| 72 | |
| 73 | # update actions entry |
| 74 | my_flow = GetAction(actions, my_flow) |
| 75 | |
| 76 | return my_flow |
| 77 | |
| 78 | |
| 79 | def GetAction(actions, flow): |