blob: 0b316bd8fe146fd32f600c946442b831513c6d5f [file] [log] [blame]
Jonathan Hart60c5d772020-03-30 18:28:40 -07001/*
2 Copyright 2020 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 (
20 "context"
Jonathan Hart828908c2020-04-15 14:23:45 -070021 "github.com/opencord/goloxi"
22 ofp "github.com/opencord/goloxi/of13"
Maninder12b909f2020-10-23 14:23:36 +053023 "github.com/opencord/voltha-lib-go/v4/pkg/log"
24 "github.com/opencord/voltha-protos/v4/go/openflow_13"
25 "github.com/opencord/voltha-protos/v4/go/voltha"
Jonathan Hart60c5d772020-03-30 18:28:40 -070026)
27
Rohan Agrawalc32d9932020-06-15 11:01:47 +000028func (ofc *OFConnection) handleGroupMod(ctx context.Context, groupMod ofp.IGroupMod) {
Girish Kumar01e0c632020-08-10 16:48:56 +000029 span, ctx := log.CreateChildSpan(ctx, "openflow-group-modification")
30 defer span.Finish()
Jonathan Hart60c5d772020-03-30 18:28:40 -070031
32 volthaClient := ofc.VolthaClient.Get()
33 if volthaClient == nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +000034 logger.Errorw(ctx, "no-voltha-connection",
Jonathan Hart60c5d772020-03-30 18:28:40 -070035 log.Fields{"device-id": ofc.DeviceID})
36 return
37 }
38
39 groupUpdate := &openflow_13.FlowGroupTableUpdate{
40 Id: ofc.DeviceID,
41 GroupMod: &voltha.OfpGroupMod{
Rohan Agrawalc32d9932020-06-15 11:01:47 +000042 Command: openflowGroupModCommandToVoltha(ctx, groupMod.GetCommand()),
43 Type: openflowGroupTypeToVoltha(ctx, groupMod.GetGroupType()),
Jonathan Hart60c5d772020-03-30 18:28:40 -070044 GroupId: groupMod.GetGroupId(),
45 Buckets: openflowBucketsToVoltha(groupMod.GetBuckets()),
46 },
47 }
48
Girish Kumar01e0c632020-08-10 16:48:56 +000049 _, err := volthaClient.UpdateLogicalDeviceFlowGroupTable(log.WithSpanFromContext(context.Background(), ctx), groupUpdate)
Jonathan Hart60c5d772020-03-30 18:28:40 -070050 if err != nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +000051 logger.Errorw(ctx, "Error updating group table",
Jonathan Hart60c5d772020-03-30 18:28:40 -070052 log.Fields{"device-id": ofc.DeviceID, "error": err})
53 }
54
55}
56
Rohan Agrawalc32d9932020-06-15 11:01:47 +000057func openflowGroupModCommandToVoltha(ctx context.Context, command ofp.GroupModCommand) openflow_13.OfpGroupModCommand {
Jonathan Hart60c5d772020-03-30 18:28:40 -070058 switch command {
59 case ofp.OFPGCAdd:
60 return openflow_13.OfpGroupModCommand_OFPGC_ADD
61 case ofp.OFPGCModify:
62 return openflow_13.OfpGroupModCommand_OFPGC_MODIFY
63 case ofp.OFPGCDelete:
64 return openflow_13.OfpGroupModCommand_OFPGC_DELETE
65 }
Rohan Agrawalc32d9932020-06-15 11:01:47 +000066 logger.Errorw(ctx, "Unknown group mod command", log.Fields{"command": command})
Jonathan Hart60c5d772020-03-30 18:28:40 -070067 return 0
68}
69
Rohan Agrawalc32d9932020-06-15 11:01:47 +000070func openflowGroupTypeToVoltha(ctx context.Context, t ofp.GroupType) openflow_13.OfpGroupType {
Jonathan Hart60c5d772020-03-30 18:28:40 -070071 switch t {
72 case ofp.OFPGTAll:
73 return openflow_13.OfpGroupType_OFPGT_ALL
74 case ofp.OFPGTSelect:
75 return openflow_13.OfpGroupType_OFPGT_SELECT
76 case ofp.OFPGTIndirect:
77 return openflow_13.OfpGroupType_OFPGT_INDIRECT
78 case ofp.OFPGTFf:
79 return openflow_13.OfpGroupType_OFPGT_FF
80 }
Rohan Agrawalc32d9932020-06-15 11:01:47 +000081 logger.Errorw(ctx, "Unknown openflow group type", log.Fields{"type": t})
Jonathan Hart60c5d772020-03-30 18:28:40 -070082 return 0
83}
84
Rohan Agrawalc32d9932020-06-15 11:01:47 +000085func volthaGroupTypeToOpenflow(ctx context.Context, t openflow_13.OfpGroupType) ofp.GroupType {
Jonathan Hart60c5d772020-03-30 18:28:40 -070086 switch t {
87 case openflow_13.OfpGroupType_OFPGT_ALL:
88 return ofp.OFPGTAll
89 case openflow_13.OfpGroupType_OFPGT_SELECT:
90 return ofp.OFPGTSelect
91 case openflow_13.OfpGroupType_OFPGT_INDIRECT:
92 return ofp.OFPGTIndirect
93 case openflow_13.OfpGroupType_OFPGT_FF:
94 return ofp.OFPGTFf
95 }
Rohan Agrawalc32d9932020-06-15 11:01:47 +000096 logger.Errorw(ctx, "Unknown voltha group type", log.Fields{"type": t})
Jonathan Hart60c5d772020-03-30 18:28:40 -070097 return 0
98}
99
100func openflowBucketsToVoltha(buckets []*ofp.Bucket) []*openflow_13.OfpBucket {
101 outBuckets := make([]*openflow_13.OfpBucket, len(buckets))
102
103 for i, bucket := range buckets {
104 b := &openflow_13.OfpBucket{
105 Weight: uint32(bucket.Weight),
106 WatchPort: uint32(bucket.WatchPort),
107 WatchGroup: bucket.WatchGroup,
108 Actions: openflowActionsToVoltha(bucket.Actions),
109 }
110 outBuckets[i] = b
111 }
112
113 return outBuckets
114}
115
116func openflowActionsToVoltha(actions []goloxi.IAction) []*openflow_13.OfpAction {
117 outActions := make([]*openflow_13.OfpAction, len(actions))
118
119 for i, action := range actions {
120 outActions[i] = extractAction(action)
121 }
122
123 return outActions
124}
125
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000126func volthaBucketsToOpenflow(ctx context.Context, buckets []*openflow_13.OfpBucket) []*ofp.Bucket {
Jonathan Hart60c5d772020-03-30 18:28:40 -0700127 outBuckets := make([]*ofp.Bucket, len(buckets))
128
129 for i, bucket := range buckets {
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000130 actions := volthaActionsToOpenflow(ctx, bucket.Actions)
Jonathan Hart60c5d772020-03-30 18:28:40 -0700131 b := &ofp.Bucket{
Jonathan Hart60c5d772020-03-30 18:28:40 -0700132 Weight: uint16(bucket.Weight),
133 WatchPort: ofp.Port(bucket.WatchPort),
134 WatchGroup: bucket.WatchGroup,
135 Actions: actions,
136 }
137 outBuckets[i] = b
138 }
139
140 return outBuckets
141}
142
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000143func volthaActionsToOpenflow(ctx context.Context, actions []*openflow_13.OfpAction) []goloxi.IAction {
Jonathan Hart60c5d772020-03-30 18:28:40 -0700144 outActions := make([]goloxi.IAction, len(actions))
145
Jonathan Hart60c5d772020-03-30 18:28:40 -0700146 for i, action := range actions {
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000147 outActions[i] = parseAction(ctx, action)
Jonathan Hart60c5d772020-03-30 18:28:40 -0700148 }
149
Jonathan Hart828908c2020-04-15 14:23:45 -0700150 return outActions
Jonathan Hart60c5d772020-03-30 18:28:40 -0700151}