blob: bd72a0c62bd31538ed4174a9d790ac31f4cecafa [file] [log] [blame]
Don Newton98fd8812019-09-23 15:15:02 -04001/*
David K. Bainbridge157bdab2020-01-16 14:38:05 -08002 Copyright 2020 the original author or authors.
Don Newton98fd8812019-09-23 15:15:02 -04003
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 (
Rohan Agrawalc32d9932020-06-15 11:01:47 +000020 "context"
Don Newton98fd8812019-09-23 15:15:02 -040021 "encoding/json"
David K. Bainbridgee05cf0c2021-08-19 03:16:50 +000022
Jonathan Hart828908c2020-04-15 14:23:45 -070023 "github.com/opencord/goloxi"
24 ofp "github.com/opencord/goloxi/of13"
David K. Bainbridgee05cf0c2021-08-19 03:16:50 +000025 "github.com/opencord/voltha-lib-go/v7/pkg/log"
Don Newton98fd8812019-09-23 15:15:02 -040026)
27
Rohan Agrawalc32d9932020-06-15 11:01:47 +000028func (ofc *OFConnection) handleRoleRequest(ctx context.Context, request *ofp.RoleRequest) {
Girish Kumar01e0c632020-08-10 16:48:56 +000029 span, ctx := log.CreateChildSpan(ctx, "openflow-role")
30 defer span.Finish()
31
David K. Bainbridge157bdab2020-01-16 14:38:05 -080032 if logger.V(log.DebugLevel) {
Don Newton7577f072020-01-06 12:41:11 -050033 js, _ := json.Marshal(request)
Rohan Agrawalc32d9932020-06-15 11:01:47 +000034 logger.Debugw(ctx, "handleRoleRequest called",
David K. Bainbridge157bdab2020-01-16 14:38:05 -080035 log.Fields{
36 "device-id": ofc.DeviceID,
37 "request": js})
Don Newton7577f072020-01-06 12:41:11 -050038 }
Jonathan Hart4b110f62020-03-13 17:36:19 -070039
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 Agrawalc32d9932020-06-15 11:01:47 +000046 if err := ofc.SendMessage(ctx, reply); err != nil {
47 logger.Errorw(ctx, "handle-role-request-send-message", log.Fields{
Jonathan Hart4b110f62020-03-13 17:36:19 -070048 "device-id": ofc.DeviceID,
49 "error": err})
50 }
51 }
52
Rohan Agrawalc32d9932020-06-15 11:01:47 +000053 ok := ofc.roleManager.UpdateRoles(ctx, ofc.OFControllerEndPoint, request)
Jonathan Hart4b110f62020-03-13 17:36:19 -070054
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 Agrawalc32d9932020-06-15 11:01:47 +000061 if err := ofc.SendMessage(ctx, reply); err != nil {
62 logger.Errorw(ctx, "handle-role-request-send-message", log.Fields{
Jonathan Hart4b110f62020-03-13 17:36:19 -070063 "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 Agrawalc32d9932020-06-15 11:01:47 +000078 if err := ofc.SendMessage(ctx, reply); err != nil {
79 logger.Errorw(ctx, "handle-role-request-send-message", log.Fields{
Jonathan Hart4b110f62020-03-13 17:36:19 -070080 "device-id": ofc.DeviceID,
81 "error": err})
82 }
83 }
84}
85
Rohan Agrawalc32d9932020-06-15 11:01:47 +000086func (ofc *OFConnection) sendRoleSlaveError(ctx context.Context, request ofp.IHeader) {
Jonathan Hart4b110f62020-03-13 17:36:19 -070087 reply := ofp.NewBadRequestErrorMsg()
Don Newton98fd8812019-09-23 15:15:02 -040088 reply.SetXid(request.GetXid())
89 reply.SetVersion(request.GetVersion())
Jonathan Hart4b110f62020-03-13 17:36:19 -070090 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 Agrawalc32d9932020-06-15 11:01:47 +000098 if err := ofc.SendMessage(ctx, reply); err != nil {
99 logger.Errorw(ctx, "send-role-slave-error", log.Fields{
David K. Bainbridgecac73ac2020-02-19 07:00:12 -0800100 "device-id": ofc.DeviceID,
101 "error": err})
102 }
Don Newton98fd8812019-09-23 15:15:02 -0400103}