blob: 7994fd2a071724484381adb579f3866f0e036df5 [file] [log] [blame]
Don Newton98fd8812019-09-23 15:15:02 -04001/*
2 Copyright 2017 the original author or authors.
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
17package openflow
18
19import (
Don Newtone0d34a82019-11-14 10:58:06 -050020 "encoding/json"
Don Newton98fd8812019-09-23 15:15:02 -040021 "fmt"
Don Newtone0d34a82019-11-14 10:58:06 -050022 "log"
Don Newton98fd8812019-09-23 15:15:02 -040023 "strings"
24 "sync"
Don Newtone0d34a82019-11-14 10:58:06 -050025
26 ofp "github.com/donNewtonAlpha/goloxi/of13"
Don Newtonb437c6f2019-12-18 11:51:57 -050027 "github.com/opencord/voltha-protos/v2/go/openflow_13"
Don Newton98fd8812019-09-23 15:15:02 -040028)
29
30var mu sync.Mutex
31var xid uint32 = 1
32
33func GetXid() uint32 {
34 mu.Lock()
35 defer mu.Unlock()
36 xid++
37 return xid
38}
39func PadString(value string, padSize int) string {
40 size := len(value)
41 nullsNeeded := padSize - size
42 null := fmt.Sprintf("%c", '\000')
43 padded := strings.Repeat(null, nullsNeeded)
44 return fmt.Sprintf("%s%s", value, padded)
45}
Don Newtone0d34a82019-11-14 10:58:06 -050046
47func extractAction(action ofp.IAction) *openflow_13.OfpAction {
48 var ofpAction openflow_13.OfpAction
49 switch action.GetType() {
50 case ofp.OFPATOutput:
51 var outputAction openflow_13.OfpAction_Output
52 loxiOutputAction := action.(*ofp.ActionOutput)
53 var output openflow_13.OfpActionOutput
Don Newtonb437c6f2019-12-18 11:51:57 -050054 output.Port = uint32(loxiOutputAction.GetPort())
55 /*
56 var maxLen uint16
57 maxLen = loxiOutputAction.GetMaxLen()
58 output.MaxLen = uint32(maxLen)
59
60 */
61 output.MaxLen = 0
Don Newtone0d34a82019-11-14 10:58:06 -050062 outputAction.Output = &output
63 ofpAction.Action = &outputAction
64 ofpAction.Type = openflow_13.OfpActionType_OFPAT_OUTPUT
Don Newtonb437c6f2019-12-18 11:51:57 -050065 js, _ := json.Marshal(outputAction)
66 log.Printf("EXTRACT ACTION %s", js)
Don Newtone0d34a82019-11-14 10:58:06 -050067 case ofp.OFPATCopyTtlOut: //CopyTtltOut
68 case ofp.OFPATCopyTtlIn: //CopyTtlIn
69 case ofp.OFPATSetMplsTtl: //SetMplsTtl
70 case ofp.OFPATDecMplsTtl: //DecMplsTtl
71 case ofp.OFPATPushVLAN: //PushVlan
72 var pushVlan openflow_13.OfpAction_Push
73 loxiPushAction := action.(*ofp.ActionPushVlan)
74 fields := loxiPushAction.GetActionFields()
75 fieldsJS, _ := json.Marshal(fields)
76 log.Printf("\n\nPushVlan fields %s\n\n", fieldsJS)
77 var push openflow_13.OfpActionPush
78 push.Ethertype = uint32(loxiPushAction.Ethertype) //TODO This should be available in the fields
79 pushVlan.Push = &push
80 ofpAction.Type = openflow_13.OfpActionType_OFPAT_PUSH_VLAN
81 ofpAction.Action = &pushVlan
82 case ofp.OFPATPopVLAN: //PopVlan
83 ofpAction.Type = openflow_13.OfpActionType_OFPAT_POP_VLAN
84 case ofp.OFPATPushMpls: //PushMpls
85 case ofp.OFPATPopMpls: //PopMpls
86 case ofp.OFPATSetQueue: //SetQueue
87 case ofp.OFPATGroup: //ActionGroup
88 case ofp.OFPATSetNwTtl: //SetNwTtl
89 case ofp.OFPATDecNwTtl: //DecNwTtl
90 case ofp.OFPATSetField: //SetField
91 ofpAction.Type = openflow_13.OfpActionType_OFPAT_SET_FIELD
92 var ofpAction_SetField openflow_13.OfpAction_SetField
93 var ofpActionSetField openflow_13.OfpActionSetField
94 var ofpOxmField openflow_13.OfpOxmField
95 ofpOxmField.OxmClass = openflow_13.OfpOxmClass_OFPXMC_OPENFLOW_BASIC
96 var ofpOxmField_OfbField openflow_13.OfpOxmField_OfbField
97 var ofpOxmOfbField openflow_13.OfpOxmOfbField
98 loxiSetField := action.(*ofp.ActionSetField)
99 oxmName := loxiSetField.Field.GetOXMName()
100 switch oxmName {
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)
106
107 ofpOxmOfbField.Value = &vlanVid
108 default:
109 log.Printf("UNHANDLED SET FIELD %s", oxmName)
110 }
111 ofpOxmField_OfbField.OfbField = &ofpOxmOfbField
112 ofpOxmField.Field = &ofpOxmField_OfbField
113 ofpActionSetField.Field = &ofpOxmField
114 ofpAction_SetField.SetField = &ofpActionSetField
115 ofpAction.Action = &ofpAction_SetField
Don Newtonb437c6f2019-12-18 11:51:57 -0500116
Don Newtone0d34a82019-11-14 10:58:06 -0500117 case ofp.OFPATPushPbb: //PushPbb
118 case ofp.OFPATPopPbb: //PopPbb
119 case ofp.OFPATExperimenter: //Experimenter
120
121 }
122 return &ofpAction
123
124}