Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 1 | /* |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 2 | Copyright 2020 the original author or authors. |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 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 | |
| 17 | package openflow |
| 18 | |
| 19 | import ( |
| 20 | "fmt" |
Jonathan Hart | 828908c | 2020-04-15 14:23:45 -0700 | [diff] [blame] | 21 | ofp "github.com/opencord/goloxi/of13" |
David K. Bainbridge | aea73cd | 2020-01-27 10:44:50 -0800 | [diff] [blame] | 22 | "github.com/opencord/voltha-protos/v3/go/openflow_13" |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 23 | "strings" |
| 24 | "sync" |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 25 | ) |
| 26 | |
| 27 | var mu sync.Mutex |
| 28 | var xid uint32 = 1 |
| 29 | |
| 30 | func GetXid() uint32 { |
| 31 | mu.Lock() |
| 32 | defer mu.Unlock() |
| 33 | xid++ |
| 34 | return xid |
| 35 | } |
| 36 | func PadString(value string, padSize int) string { |
| 37 | size := len(value) |
| 38 | nullsNeeded := padSize - size |
| 39 | null := fmt.Sprintf("%c", '\000') |
| 40 | padded := strings.Repeat(null, nullsNeeded) |
| 41 | return fmt.Sprintf("%s%s", value, padded) |
| 42 | } |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 43 | |
| 44 | func extractAction(action ofp.IAction) *openflow_13.OfpAction { |
| 45 | var ofpAction openflow_13.OfpAction |
| 46 | switch action.GetType() { |
| 47 | case ofp.OFPATOutput: |
| 48 | var outputAction openflow_13.OfpAction_Output |
| 49 | loxiOutputAction := action.(*ofp.ActionOutput) |
| 50 | var output openflow_13.OfpActionOutput |
Don Newton | b437c6f | 2019-12-18 11:51:57 -0500 | [diff] [blame] | 51 | output.Port = uint32(loxiOutputAction.GetPort()) |
| 52 | /* |
| 53 | var maxLen uint16 |
| 54 | maxLen = loxiOutputAction.GetMaxLen() |
| 55 | output.MaxLen = uint32(maxLen) |
| 56 | |
| 57 | */ |
| 58 | output.MaxLen = 0 |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 59 | outputAction.Output = &output |
| 60 | ofpAction.Action = &outputAction |
| 61 | ofpAction.Type = openflow_13.OfpActionType_OFPAT_OUTPUT |
| 62 | case ofp.OFPATCopyTtlOut: //CopyTtltOut |
| 63 | case ofp.OFPATCopyTtlIn: //CopyTtlIn |
| 64 | case ofp.OFPATSetMplsTtl: //SetMplsTtl |
| 65 | case ofp.OFPATDecMplsTtl: //DecMplsTtl |
| 66 | case ofp.OFPATPushVLAN: //PushVlan |
| 67 | var pushVlan openflow_13.OfpAction_Push |
| 68 | loxiPushAction := action.(*ofp.ActionPushVlan) |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 69 | var push openflow_13.OfpActionPush |
| 70 | push.Ethertype = uint32(loxiPushAction.Ethertype) //TODO This should be available in the fields |
| 71 | pushVlan.Push = &push |
| 72 | ofpAction.Type = openflow_13.OfpActionType_OFPAT_PUSH_VLAN |
| 73 | ofpAction.Action = &pushVlan |
| 74 | case ofp.OFPATPopVLAN: //PopVlan |
| 75 | ofpAction.Type = openflow_13.OfpActionType_OFPAT_POP_VLAN |
| 76 | case ofp.OFPATPushMpls: //PushMpls |
| 77 | case ofp.OFPATPopMpls: //PopMpls |
| 78 | case ofp.OFPATSetQueue: //SetQueue |
| 79 | case ofp.OFPATGroup: //ActionGroup |
Jonathan Hart | 60c5d77 | 2020-03-30 18:28:40 -0700 | [diff] [blame] | 80 | ofpAction.Type = openflow_13.OfpActionType_OFPAT_GROUP |
| 81 | group := action.(*ofp.ActionGroup) |
| 82 | ofpAction.Action = &openflow_13.OfpAction_Group{ |
| 83 | Group: &openflow_13.OfpActionGroup{ |
| 84 | GroupId: group.GroupId, |
| 85 | }, |
| 86 | } |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 87 | case ofp.OFPATSetNwTtl: //SetNwTtl |
| 88 | case ofp.OFPATDecNwTtl: //DecNwTtl |
| 89 | case ofp.OFPATSetField: //SetField |
| 90 | ofpAction.Type = openflow_13.OfpActionType_OFPAT_SET_FIELD |
| 91 | var ofpAction_SetField openflow_13.OfpAction_SetField |
| 92 | var ofpActionSetField openflow_13.OfpActionSetField |
| 93 | var ofpOxmField openflow_13.OfpOxmField |
| 94 | ofpOxmField.OxmClass = openflow_13.OfpOxmClass_OFPXMC_OPENFLOW_BASIC |
| 95 | var ofpOxmField_OfbField openflow_13.OfpOxmField_OfbField |
| 96 | var ofpOxmOfbField openflow_13.OfpOxmOfbField |
| 97 | loxiSetField := action.(*ofp.ActionSetField) |
| 98 | oxmName := loxiSetField.Field.GetOXMName() |
| 99 | switch oxmName { |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame] | 100 | //TODO handle set field sith other fields |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 101 | case "vlan_vid": |
| 102 | ofpOxmOfbField.Type = openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_VLAN_VID |
| 103 | var vlanVid openflow_13.OfpOxmOfbField_VlanVid |
| 104 | var VlanVid = loxiSetField.Field.GetOXMValue().(uint16) |
| 105 | vlanVid.VlanVid = uint32(VlanVid) |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 106 | ofpOxmOfbField.Value = &vlanVid |
Gamze Abaka | 3e2b2ce | 2020-05-09 10:21:40 +0000 | [diff] [blame] | 107 | case "vlan_pcp": |
| 108 | ofpOxmOfbField.Type = openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_VLAN_PCP |
| 109 | var vlanPcp openflow_13.OfpOxmOfbField_VlanPcp |
| 110 | var VlanPcp = loxiSetField.Field.GetOXMValue().(uint8) |
| 111 | vlanPcp.VlanPcp = uint32(VlanPcp) |
| 112 | ofpOxmOfbField.Value = &vlanPcp |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 113 | } |
| 114 | ofpOxmField_OfbField.OfbField = &ofpOxmOfbField |
| 115 | ofpOxmField.Field = &ofpOxmField_OfbField |
| 116 | ofpActionSetField.Field = &ofpOxmField |
| 117 | ofpAction_SetField.SetField = &ofpActionSetField |
| 118 | ofpAction.Action = &ofpAction_SetField |
Don Newton | b437c6f | 2019-12-18 11:51:57 -0500 | [diff] [blame] | 119 | |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 120 | case ofp.OFPATPushPbb: //PushPbb |
| 121 | case ofp.OFPATPopPbb: //PopPbb |
| 122 | case ofp.OFPATExperimenter: //Experimenter |
| 123 | |
| 124 | } |
| 125 | return &ofpAction |
| 126 | |
| 127 | } |