blob: aba74003635eec8614bcd3d4707ed19c294a0973 [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"
27 "github.com/opencord/voltha-protos/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
54 output.Port = uint32(loxiOutputAction.Port)
55 output.MaxLen = uint32(loxiOutputAction.MaxLen)
56 outputAction.Output = &output
57 ofpAction.Action = &outputAction
58 ofpAction.Type = openflow_13.OfpActionType_OFPAT_OUTPUT
59 case ofp.OFPATCopyTtlOut: //CopyTtltOut
60 case ofp.OFPATCopyTtlIn: //CopyTtlIn
61 case ofp.OFPATSetMplsTtl: //SetMplsTtl
62 case ofp.OFPATDecMplsTtl: //DecMplsTtl
63 case ofp.OFPATPushVLAN: //PushVlan
64 var pushVlan openflow_13.OfpAction_Push
65 loxiPushAction := action.(*ofp.ActionPushVlan)
66 fields := loxiPushAction.GetActionFields()
67 fieldsJS, _ := json.Marshal(fields)
68 log.Printf("\n\nPushVlan fields %s\n\n", fieldsJS)
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
80 case ofp.OFPATSetNwTtl: //SetNwTtl
81 case ofp.OFPATDecNwTtl: //DecNwTtl
82 case ofp.OFPATSetField: //SetField
83 ofpAction.Type = openflow_13.OfpActionType_OFPAT_SET_FIELD
84 var ofpAction_SetField openflow_13.OfpAction_SetField
85 var ofpActionSetField openflow_13.OfpActionSetField
86 var ofpOxmField openflow_13.OfpOxmField
87 ofpOxmField.OxmClass = openflow_13.OfpOxmClass_OFPXMC_OPENFLOW_BASIC
88 var ofpOxmField_OfbField openflow_13.OfpOxmField_OfbField
89 var ofpOxmOfbField openflow_13.OfpOxmOfbField
90 loxiSetField := action.(*ofp.ActionSetField)
91 oxmName := loxiSetField.Field.GetOXMName()
92 switch oxmName {
93 case "vlan_vid":
94 ofpOxmOfbField.Type = openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_VLAN_VID
95 var vlanVid openflow_13.OfpOxmOfbField_VlanVid
96 var VlanVid = loxiSetField.Field.GetOXMValue().(uint16)
97 vlanVid.VlanVid = uint32(VlanVid)
98
99 ofpOxmOfbField.Value = &vlanVid
100 default:
101 log.Printf("UNHANDLED SET FIELD %s", oxmName)
102 }
103 ofpOxmField_OfbField.OfbField = &ofpOxmOfbField
104 ofpOxmField.Field = &ofpOxmField_OfbField
105 ofpActionSetField.Field = &ofpOxmField
106 ofpAction_SetField.SetField = &ofpActionSetField
107 ofpAction.Action = &ofpAction_SetField
108 case ofp.OFPATPushPbb: //PushPbb
109 case ofp.OFPATPopPbb: //PopPbb
110 case ofp.OFPATExperimenter: //Experimenter
111
112 }
113 return &ofpAction
114
115}