blob: b114d367fa097834b447c9a58e39baf7fc9d86b2 [file] [log] [blame]
David K. Bainbridge157bdab2020-01-16 14:38:05 -08001/*
2 Copyright 2020 the original author or authors.
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
17package ofagent
18
19import (
20 "context"
21 "encoding/json"
David Bainbridgef8ce7d22020-04-08 12:49:41 -070022 "net"
23
David K. Bainbridge157bdab2020-01-16 14:38:05 -080024 "github.com/golang/protobuf/ptypes/empty"
Jonathan Hart828908c2020-04-15 14:23:45 -070025 ofp "github.com/opencord/goloxi/of13"
David K. Bainbridge157bdab2020-01-16 14:38:05 -080026 "github.com/opencord/ofagent-go/internal/pkg/openflow"
David K. Bainbridgeaea73cd2020-01-27 10:44:50 -080027 "github.com/opencord/voltha-lib-go/v3/pkg/log"
David K. Bainbridge157bdab2020-01-16 14:38:05 -080028 "google.golang.org/grpc"
David K. Bainbridge157bdab2020-01-16 14:38:05 -080029)
30
31func (ofa *OFAgent) receiveChangeEvents(ctx context.Context) {
32 logger.Debug("receive-change-events-started")
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080033 // If we exit, assume disconnected
34 defer func() {
35 ofa.events <- ofaEventVolthaDisconnected
36 logger.Debug("receive-change-events-finished")
37 }()
38 if ofa.volthaClient == nil {
39 logger.Error("no-voltha-connection")
40 return
41 }
David K. Bainbridge157bdab2020-01-16 14:38:05 -080042 opt := grpc.EmptyCallOption{}
43 streamCtx, streamDone := context.WithCancel(context.Background())
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080044 defer streamDone()
David Bainbridgef8ce7d22020-04-08 12:49:41 -070045 stream, err := ofa.volthaClient.Get().ReceiveChangeEvents(streamCtx, &empty.Empty{}, opt)
David K. Bainbridge157bdab2020-01-16 14:38:05 -080046 if err != nil {
47 logger.Errorw("Unable to establish Receive Change Event Stream",
48 log.Fields{"error": err})
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080049 return
David K. Bainbridge157bdab2020-01-16 14:38:05 -080050 }
David K. Bainbridge157bdab2020-01-16 14:38:05 -080051
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080052top:
David K. Bainbridge157bdab2020-01-16 14:38:05 -080053 for {
54 select {
55 case <-ctx.Done():
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080056 break top
David K. Bainbridge157bdab2020-01-16 14:38:05 -080057 default:
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080058 ce, err := stream.Recv()
59 if err != nil {
David K. Bainbridge157bdab2020-01-16 14:38:05 -080060 logger.Errorw("error receiving change event",
61 log.Fields{"error": err})
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080062 break top
David K. Bainbridge157bdab2020-01-16 14:38:05 -080063 }
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080064 ofa.changeEventChannel <- ce
65 logger.Debug("receive-change-event-queued")
David K. Bainbridge157bdab2020-01-16 14:38:05 -080066 }
67 }
68}
69
70func (ofa *OFAgent) handleChangeEvents(ctx context.Context) {
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080071 logger.Debug("handle-change-event-started")
72
73top:
David K. Bainbridge157bdab2020-01-16 14:38:05 -080074 for {
75 select {
76 case <-ctx.Done():
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080077 break top
David K. Bainbridge157bdab2020-01-16 14:38:05 -080078 case changeEvent := <-ofa.changeEventChannel:
79 deviceID := changeEvent.GetId()
80 portStatus := changeEvent.GetPortStatus()
81 logger.Debugw("received-change-event",
82 log.Fields{
83 "device-id": deviceID,
84 "port-status": portStatus})
85
86 if portStatus == nil {
87 if logger.V(log.WarnLevel) {
88 js, _ := json.Marshal(changeEvent.GetEvent())
89 logger.Warnw("Received change event that was not port status",
90 log.Fields{"ChangeEvent": js})
91 }
92 break
93 }
94 ofPortStatus := ofp.NewPortStatus()
95 ofPortStatus.SetXid(openflow.GetXid())
96 ofPortStatus.SetVersion(4)
97
98 ofReason := ofp.PortReason(portStatus.GetReason())
99 ofPortStatus.SetReason(ofReason)
100 ofDesc := ofp.NewPortDesc()
101
102 desc := portStatus.GetDesc()
103 ofDesc.SetAdvertised(ofp.PortFeatures(desc.GetAdvertised()))
104 ofDesc.SetConfig(ofp.PortConfig(0))
105 ofDesc.SetCurr(ofp.PortFeatures(desc.GetAdvertised()))
106 ofDesc.SetCurrSpeed(desc.GetCurrSpeed())
107 intArray := desc.GetHwAddr()
108 var octets []byte
109 for _, val := range intArray {
110 octets = append(octets, byte(val))
111 }
112 addr := net.HardwareAddr(octets)
113 ofDesc.SetHwAddr(addr)
114 ofDesc.SetMaxSpeed(desc.GetMaxSpeed())
115 ofDesc.SetName(openflow.PadString(desc.GetName(), 16))
116 ofDesc.SetPeer(ofp.PortFeatures(desc.GetPeer()))
117 ofDesc.SetPortNo(ofp.Port(desc.GetPortNo()))
118 ofDesc.SetState(ofp.PortState(desc.GetState()))
119 ofDesc.SetSupported(ofp.PortFeatures(desc.GetSupported()))
120 ofPortStatus.SetDesc(*ofDesc)
David K. Bainbridgecac73ac2020-02-19 07:00:12 -0800121 if err := ofa.getOFClient(deviceID).SendMessage(ofPortStatus); err != nil {
divyadesai81bb7ba2020-03-11 11:45:23 +0000122 logger.Errorw("handle-change-events-send-message", log.Fields{"error": err})
David K. Bainbridgecac73ac2020-02-19 07:00:12 -0800123 }
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800124 }
125 }
David K. Bainbridge9cb404e2020-01-28 14:32:29 -0800126
Girish Kumare9d76172020-03-20 20:26:04 +0000127 logger.Debug("handle-change-event-finsihed")
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800128}