blob: 425e99b7628f1d15306e04830adf3246260792cf [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"
Jonathan Hart60c5d772020-03-30 18:28:40 -070023 "github.com/opencord/voltha-lib-go/v3/pkg/log"
24 "github.com/opencord/voltha-protos/v3/go/openflow_13"
25 "github.com/opencord/voltha-protos/v3/go/voltha"
26)
27
Rohan Agrawalc32d9932020-06-15 11:01:47 +000028func (ofc *OFConnection) handleGroupMod(ctx context.Context, groupMod ofp.IGroupMod) {
Jonathan Hart60c5d772020-03-30 18:28:40 -070029
30 volthaClient := ofc.VolthaClient.Get()
31 if volthaClient == nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +000032 logger.Errorw(ctx, "no-voltha-connection",
Jonathan Hart60c5d772020-03-30 18:28:40 -070033 log.Fields{"device-id": ofc.DeviceID})
34 return
35 }
36
37 groupUpdate := &openflow_13.FlowGroupTableUpdate{
38 Id: ofc.DeviceID,
39 GroupMod: &voltha.OfpGroupMod{
Rohan Agrawalc32d9932020-06-15 11:01:47 +000040 Command: openflowGroupModCommandToVoltha(ctx, groupMod.GetCommand()),
41 Type: openflowGroupTypeToVoltha(ctx, groupMod.GetGroupType()),
Jonathan Hart60c5d772020-03-30 18:28:40 -070042 GroupId: groupMod.GetGroupId(),
43 Buckets: openflowBucketsToVoltha(groupMod.GetBuckets()),
44 },
45 }
46
47 _, err := volthaClient.UpdateLogicalDeviceFlowGroupTable(context.Background(), groupUpdate)
48 if err != nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +000049 logger.Errorw(ctx, "Error updating group table",
Jonathan Hart60c5d772020-03-30 18:28:40 -070050 log.Fields{"device-id": ofc.DeviceID, "error": err})
51 }
52
53}
54
Rohan Agrawalc32d9932020-06-15 11:01:47 +000055func openflowGroupModCommandToVoltha(ctx context.Context, command ofp.GroupModCommand) openflow_13.OfpGroupModCommand {
Jonathan Hart60c5d772020-03-30 18:28:40 -070056 switch command {
57 case ofp.OFPGCAdd:
58 return openflow_13.OfpGroupModCommand_OFPGC_ADD
59 case ofp.OFPGCModify:
60 return openflow_13.OfpGroupModCommand_OFPGC_MODIFY
61 case ofp.OFPGCDelete:
62 return openflow_13.OfpGroupModCommand_OFPGC_DELETE
63 }
Rohan Agrawalc32d9932020-06-15 11:01:47 +000064 logger.Errorw(ctx, "Unknown group mod command", log.Fields{"command": command})
Jonathan Hart60c5d772020-03-30 18:28:40 -070065 return 0
66}
67
Rohan Agrawalc32d9932020-06-15 11:01:47 +000068func openflowGroupTypeToVoltha(ctx context.Context, t ofp.GroupType) openflow_13.OfpGroupType {
Jonathan Hart60c5d772020-03-30 18:28:40 -070069 switch t {
70 case ofp.OFPGTAll:
71 return openflow_13.OfpGroupType_OFPGT_ALL
72 case ofp.OFPGTSelect:
73 return openflow_13.OfpGroupType_OFPGT_SELECT
74 case ofp.OFPGTIndirect:
75 return openflow_13.OfpGroupType_OFPGT_INDIRECT
76 case ofp.OFPGTFf:
77 return openflow_13.OfpGroupType_OFPGT_FF
78 }
Rohan Agrawalc32d9932020-06-15 11:01:47 +000079 logger.Errorw(ctx, "Unknown openflow group type", log.Fields{"type": t})
Jonathan Hart60c5d772020-03-30 18:28:40 -070080 return 0
81}
82
Rohan Agrawalc32d9932020-06-15 11:01:47 +000083func volthaGroupTypeToOpenflow(ctx context.Context, t openflow_13.OfpGroupType) ofp.GroupType {
Jonathan Hart60c5d772020-03-30 18:28:40 -070084 switch t {
85 case openflow_13.OfpGroupType_OFPGT_ALL:
86 return ofp.OFPGTAll
87 case openflow_13.OfpGroupType_OFPGT_SELECT:
88 return ofp.OFPGTSelect
89 case openflow_13.OfpGroupType_OFPGT_INDIRECT:
90 return ofp.OFPGTIndirect
91 case openflow_13.OfpGroupType_OFPGT_FF:
92 return ofp.OFPGTFf
93 }
Rohan Agrawalc32d9932020-06-15 11:01:47 +000094 logger.Errorw(ctx, "Unknown voltha group type", log.Fields{"type": t})
Jonathan Hart60c5d772020-03-30 18:28:40 -070095 return 0
96}
97
98func openflowBucketsToVoltha(buckets []*ofp.Bucket) []*openflow_13.OfpBucket {
99 outBuckets := make([]*openflow_13.OfpBucket, len(buckets))
100
101 for i, bucket := range buckets {
102 b := &openflow_13.OfpBucket{
103 Weight: uint32(bucket.Weight),
104 WatchPort: uint32(bucket.WatchPort),
105 WatchGroup: bucket.WatchGroup,
106 Actions: openflowActionsToVoltha(bucket.Actions),
107 }
108 outBuckets[i] = b
109 }
110
111 return outBuckets
112}
113
114func openflowActionsToVoltha(actions []goloxi.IAction) []*openflow_13.OfpAction {
115 outActions := make([]*openflow_13.OfpAction, len(actions))
116
117 for i, action := range actions {
118 outActions[i] = extractAction(action)
119 }
120
121 return outActions
122}
123
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000124func volthaBucketsToOpenflow(ctx context.Context, buckets []*openflow_13.OfpBucket) []*ofp.Bucket {
Jonathan Hart60c5d772020-03-30 18:28:40 -0700125 outBuckets := make([]*ofp.Bucket, len(buckets))
126
127 for i, bucket := range buckets {
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000128 actions := volthaActionsToOpenflow(ctx, bucket.Actions)
Jonathan Hart60c5d772020-03-30 18:28:40 -0700129 b := &ofp.Bucket{
Jonathan Hart60c5d772020-03-30 18:28:40 -0700130 Weight: uint16(bucket.Weight),
131 WatchPort: ofp.Port(bucket.WatchPort),
132 WatchGroup: bucket.WatchGroup,
133 Actions: actions,
134 }
135 outBuckets[i] = b
136 }
137
138 return outBuckets
139}
140
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000141func volthaActionsToOpenflow(ctx context.Context, actions []*openflow_13.OfpAction) []goloxi.IAction {
Jonathan Hart60c5d772020-03-30 18:28:40 -0700142 outActions := make([]goloxi.IAction, len(actions))
143
Jonathan Hart60c5d772020-03-30 18:28:40 -0700144 for i, action := range actions {
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000145 outActions[i] = parseAction(ctx, action)
Jonathan Hart60c5d772020-03-30 18:28:40 -0700146 }
147
Jonathan Hart828908c2020-04-15 14:23:45 -0700148 return outActions
Jonathan Hart60c5d772020-03-30 18:28:40 -0700149}