blob: 740616f7e5bcb98ce993426e006b443bbfe01d7d [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"
Jonathan Hart828908c2020-04-15 14:23:45 -070022 "github.com/opencord/goloxi"
23 ofp "github.com/opencord/goloxi/of13"
David K. Bainbridgeaea73cd2020-01-27 10:44:50 -080024 "github.com/opencord/voltha-lib-go/v3/pkg/log"
Don Newton98fd8812019-09-23 15:15:02 -040025)
26
Rohan Agrawalc32d9932020-06-15 11:01:47 +000027func (ofc *OFConnection) handleRoleRequest(ctx context.Context, request *ofp.RoleRequest) {
David K. Bainbridge157bdab2020-01-16 14:38:05 -080028 if logger.V(log.DebugLevel) {
Don Newton7577f072020-01-06 12:41:11 -050029 js, _ := json.Marshal(request)
Rohan Agrawalc32d9932020-06-15 11:01:47 +000030 logger.Debugw(ctx, "handleRoleRequest called",
David K. Bainbridge157bdab2020-01-16 14:38:05 -080031 log.Fields{
32 "device-id": ofc.DeviceID,
33 "request": js})
Don Newton7577f072020-01-06 12:41:11 -050034 }
Jonathan Hart4b110f62020-03-13 17:36:19 -070035
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 Agrawalc32d9932020-06-15 11:01:47 +000042 if err := ofc.SendMessage(ctx, reply); err != nil {
43 logger.Errorw(ctx, "handle-role-request-send-message", log.Fields{
Jonathan Hart4b110f62020-03-13 17:36:19 -070044 "device-id": ofc.DeviceID,
45 "error": err})
46 }
47 }
48
Rohan Agrawalc32d9932020-06-15 11:01:47 +000049 ok := ofc.roleManager.UpdateRoles(ctx, ofc.OFControllerEndPoint, request)
Jonathan Hart4b110f62020-03-13 17:36:19 -070050
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 Agrawalc32d9932020-06-15 11:01:47 +000057 if err := ofc.SendMessage(ctx, reply); err != nil {
58 logger.Errorw(ctx, "handle-role-request-send-message", log.Fields{
Jonathan Hart4b110f62020-03-13 17:36:19 -070059 "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 Agrawalc32d9932020-06-15 11:01:47 +000074 if err := ofc.SendMessage(ctx, reply); err != nil {
75 logger.Errorw(ctx, "handle-role-request-send-message", log.Fields{
Jonathan Hart4b110f62020-03-13 17:36:19 -070076 "device-id": ofc.DeviceID,
77 "error": err})
78 }
79 }
80}
81
Rohan Agrawalc32d9932020-06-15 11:01:47 +000082func (ofc *OFConnection) sendRoleSlaveError(ctx context.Context, request ofp.IHeader) {
Jonathan Hart4b110f62020-03-13 17:36:19 -070083 reply := ofp.NewBadRequestErrorMsg()
Don Newton98fd8812019-09-23 15:15:02 -040084 reply.SetXid(request.GetXid())
85 reply.SetVersion(request.GetVersion())
Jonathan Hart4b110f62020-03-13 17:36:19 -070086 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 Agrawalc32d9932020-06-15 11:01:47 +000094 if err := ofc.SendMessage(ctx, reply); err != nil {
95 logger.Errorw(ctx, "send-role-slave-error", log.Fields{
David K. Bainbridgecac73ac2020-02-19 07:00:12 -080096 "device-id": ofc.DeviceID,
97 "error": err})
98 }
Don Newton98fd8812019-09-23 15:15:02 -040099}