Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 1 | /* |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 2 | Copyright 2020 the original author or authors. |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 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 | |
| 17 | package openflow |
| 18 | |
| 19 | import ( |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 20 | "context" |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 21 | "encoding/json" |
David K. Bainbridge | e05cf0c | 2021-08-19 03:16:50 +0000 | [diff] [blame] | 22 | |
Jonathan Hart | 828908c | 2020-04-15 14:23:45 -0700 | [diff] [blame] | 23 | "github.com/opencord/goloxi" |
| 24 | ofp "github.com/opencord/goloxi/of13" |
David K. Bainbridge | e05cf0c | 2021-08-19 03:16:50 +0000 | [diff] [blame] | 25 | "github.com/opencord/voltha-lib-go/v7/pkg/log" |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 26 | ) |
| 27 | |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 28 | func (ofc *OFConnection) handleRoleRequest(ctx context.Context, request *ofp.RoleRequest) { |
Girish Kumar | 01e0c63 | 2020-08-10 16:48:56 +0000 | [diff] [blame] | 29 | span, ctx := log.CreateChildSpan(ctx, "openflow-role") |
| 30 | defer span.Finish() |
| 31 | |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 32 | if logger.V(log.DebugLevel) { |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame] | 33 | js, _ := json.Marshal(request) |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 34 | logger.Debugw(ctx, "handleRoleRequest called", |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 35 | log.Fields{ |
| 36 | "device-id": ofc.DeviceID, |
| 37 | "request": js}) |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame] | 38 | } |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 39 | |
| 40 | if request.Role == ofp.OFPCRRoleNochange { |
| 41 | reply := ofp.NewRoleReply() |
| 42 | reply.SetXid(request.GetXid()) |
| 43 | reply.SetVersion(request.GetVersion()) |
| 44 | reply.SetRole(ofp.ControllerRole(ofc.role)) |
| 45 | reply.SetGenerationId(request.GetGenerationId()) |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 46 | if err := ofc.SendMessage(ctx, reply); err != nil { |
| 47 | logger.Errorw(ctx, "handle-role-request-send-message", log.Fields{ |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 48 | "device-id": ofc.DeviceID, |
| 49 | "error": err}) |
| 50 | } |
| 51 | } |
| 52 | |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 53 | ok := ofc.roleManager.UpdateRoles(ctx, ofc.OFControllerEndPoint, request) |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 54 | |
| 55 | if ok { |
| 56 | reply := ofp.NewRoleReply() |
| 57 | reply.SetXid(request.GetXid()) |
| 58 | reply.SetVersion(request.GetVersion()) |
| 59 | reply.SetRole(request.GetRole()) |
| 60 | reply.SetGenerationId(request.GetGenerationId()) |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 61 | if err := ofc.SendMessage(ctx, reply); err != nil { |
| 62 | logger.Errorw(ctx, "handle-role-request-send-message", log.Fields{ |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 63 | "device-id": ofc.DeviceID, |
| 64 | "error": err}) |
| 65 | } |
| 66 | } else { |
| 67 | reply := ofp.NewRoleRequestFailedErrorMsg() |
| 68 | reply.SetXid(request.GetXid()) |
| 69 | reply.SetVersion(request.GetVersion()) |
| 70 | reply.Code = ofp.OFPRRFCStale |
| 71 | |
| 72 | enc := goloxi.NewEncoder() |
| 73 | err := request.Serialize(enc) |
| 74 | if err == nil { |
| 75 | reply.Data = enc.Bytes() |
| 76 | } |
| 77 | |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 78 | if err := ofc.SendMessage(ctx, reply); err != nil { |
| 79 | logger.Errorw(ctx, "handle-role-request-send-message", log.Fields{ |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 80 | "device-id": ofc.DeviceID, |
| 81 | "error": err}) |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 86 | func (ofc *OFConnection) sendRoleSlaveError(ctx context.Context, request ofp.IHeader) { |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 87 | reply := ofp.NewBadRequestErrorMsg() |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 88 | reply.SetXid(request.GetXid()) |
| 89 | reply.SetVersion(request.GetVersion()) |
Jonathan Hart | 4b110f6 | 2020-03-13 17:36:19 -0700 | [diff] [blame] | 90 | reply.Code = ofp.OFPCRRoleSlave |
| 91 | |
| 92 | enc := goloxi.NewEncoder() |
| 93 | err := request.Serialize(enc) |
| 94 | if err == nil { |
| 95 | reply.Data = enc.Bytes() |
| 96 | } |
| 97 | |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 98 | if err := ofc.SendMessage(ctx, reply); err != nil { |
| 99 | logger.Errorw(ctx, "send-role-slave-error", log.Fields{ |
David K. Bainbridge | cac73ac | 2020-02-19 07:00:12 -0800 | [diff] [blame] | 100 | "device-id": ofc.DeviceID, |
| 101 | "error": err}) |
| 102 | } |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 103 | } |