blob: 6d86fe9e10b2449058c20d6b438222fc3a79c7fd [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 (
20 "encoding/json"
Jonathan Hart4b110f62020-03-13 17:36:19 -070021 "github.com/donNewtonAlpha/goloxi"
Don Newtone0d34a82019-11-14 10:58:06 -050022 ofp "github.com/donNewtonAlpha/goloxi/of13"
David K. Bainbridgeaea73cd2020-01-27 10:44:50 -080023 "github.com/opencord/voltha-lib-go/v3/pkg/log"
Don Newton98fd8812019-09-23 15:15:02 -040024)
25
Jonathan Hart4b110f62020-03-13 17:36:19 -070026func (ofc *OFConnection) handleRoleRequest(request *ofp.RoleRequest) {
David K. Bainbridge157bdab2020-01-16 14:38:05 -080027 if logger.V(log.DebugLevel) {
Don Newton7577f072020-01-06 12:41:11 -050028 js, _ := json.Marshal(request)
David K. Bainbridge157bdab2020-01-16 14:38:05 -080029 logger.Debugw("handleRoleRequest called",
30 log.Fields{
31 "device-id": ofc.DeviceID,
32 "request": js})
Don Newton7577f072020-01-06 12:41:11 -050033 }
Jonathan Hart4b110f62020-03-13 17:36:19 -070034
35 if request.Role == ofp.OFPCRRoleNochange {
36 reply := ofp.NewRoleReply()
37 reply.SetXid(request.GetXid())
38 reply.SetVersion(request.GetVersion())
39 reply.SetRole(ofp.ControllerRole(ofc.role))
40 reply.SetGenerationId(request.GetGenerationId())
41 if err := ofc.SendMessage(reply); err != nil {
42 logger.Errorw("handle-role-request-send-message", log.Fields{
43 "device-id": ofc.DeviceID,
44 "error": err})
45 }
46 }
47
48 ok := ofc.roleManager.UpdateRoles(ofc.OFControllerEndPoint, request)
49
50 if ok {
51 reply := ofp.NewRoleReply()
52 reply.SetXid(request.GetXid())
53 reply.SetVersion(request.GetVersion())
54 reply.SetRole(request.GetRole())
55 reply.SetGenerationId(request.GetGenerationId())
56 if err := ofc.SendMessage(reply); err != nil {
57 logger.Errorw("handle-role-request-send-message", log.Fields{
58 "device-id": ofc.DeviceID,
59 "error": err})
60 }
61 } else {
62 reply := ofp.NewRoleRequestFailedErrorMsg()
63 reply.SetXid(request.GetXid())
64 reply.SetVersion(request.GetVersion())
65 reply.Code = ofp.OFPRRFCStale
66
67 enc := goloxi.NewEncoder()
68 err := request.Serialize(enc)
69 if err == nil {
70 reply.Data = enc.Bytes()
71 }
72
73 if err := ofc.SendMessage(reply); err != nil {
74 logger.Errorw("handle-role-request-send-message", log.Fields{
75 "device-id": ofc.DeviceID,
76 "error": err})
77 }
78 }
79}
80
81func (ofc *OFConnection) sendRoleSlaveError(request ofp.IHeader) {
82 reply := ofp.NewBadRequestErrorMsg()
Don Newton98fd8812019-09-23 15:15:02 -040083 reply.SetXid(request.GetXid())
84 reply.SetVersion(request.GetVersion())
Jonathan Hart4b110f62020-03-13 17:36:19 -070085 reply.Code = ofp.OFPCRRoleSlave
86
87 enc := goloxi.NewEncoder()
88 err := request.Serialize(enc)
89 if err == nil {
90 reply.Data = enc.Bytes()
91 }
92
David K. Bainbridgecac73ac2020-02-19 07:00:12 -080093 if err := ofc.SendMessage(reply); err != nil {
Jonathan Hart4b110f62020-03-13 17:36:19 -070094 logger.Errorw("send-role-slave-error", log.Fields{
David K. Bainbridgecac73ac2020-02-19 07:00:12 -080095 "device-id": ofc.DeviceID,
96 "error": err})
97 }
Don Newton98fd8812019-09-23 15:15:02 -040098}