blob: ffe4e520a8d4d7da28858b43186d2a2094dd7da3 [file] [log] [blame]
Don Newton98fd8812019-09-23 15:15:02 -04001/*
David K. Bainbridge157bdab2020-01-16 14:38:05 -08002 Copyright 2020 the original author or authors.
Don Newton98fd8812019-09-23 15:15:02 -04003
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
17package openflow
18
19import (
20 "fmt"
Jonathan Hart828908c2020-04-15 14:23:45 -070021 ofp "github.com/opencord/goloxi/of13"
David K. Bainbridgeaea73cd2020-01-27 10:44:50 -080022 "github.com/opencord/voltha-protos/v3/go/openflow_13"
David K. Bainbridge157bdab2020-01-16 14:38:05 -080023 "strings"
24 "sync"
Don Newton98fd8812019-09-23 15:15:02 -040025)
26
27var mu sync.Mutex
28var xid uint32 = 1
29
30func GetXid() uint32 {
31 mu.Lock()
32 defer mu.Unlock()
33 xid++
34 return xid
35}
36func 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 Newtone0d34a82019-11-14 10:58:06 -050043
44func 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 Newtonb437c6f2019-12-18 11:51:57 -050051 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 Newtone0d34a82019-11-14 10:58:06 -050059 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 Newtone0d34a82019-11-14 10:58:06 -050069 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 Hart60c5d772020-03-30 18:28:40 -070080 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 Newtone0d34a82019-11-14 10:58:06 -050087 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 Newton7577f072020-01-06 12:41:11 -0500100 //TODO handle set field sith other fields
Don Newtone0d34a82019-11-14 10:58:06 -0500101 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 Newtone0d34a82019-11-14 10:58:06 -0500106 ofpOxmOfbField.Value = &vlanVid
Gamze Abaka3e2b2ce2020-05-09 10:21:40 +0000107 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 Newtone0d34a82019-11-14 10:58:06 -0500113 }
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 Newtonb437c6f2019-12-18 11:51:57 -0500119
Don Newtone0d34a82019-11-14 10:58:06 -0500120 case ofp.OFPATPushPbb: //PushPbb
121 case ofp.OFPATPopPbb: //PopPbb
122 case ofp.OFPATExperimenter: //Experimenter
123
124 }
125 return &ofpAction
126
127}