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