blob: 6a79d4200285388ba89fd1ff2f48e056c1e67f4a [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) {
Girish Kumar01e0c632020-08-10 16:48:56 +000032 span, ctx := log.CreateChildSpan(ctx, "receive-change-events")
33 defer span.Finish()
34
Rohan Agrawalc32d9932020-06-15 11:01:47 +000035 logger.Debug(ctx, "receive-change-events-started")
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080036 // If we exit, assume disconnected
37 defer func() {
38 ofa.events <- ofaEventVolthaDisconnected
Rohan Agrawalc32d9932020-06-15 11:01:47 +000039 logger.Debug(ctx, "receive-change-events-finished")
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080040 }()
41 if ofa.volthaClient == nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +000042 logger.Error(ctx, "no-voltha-connection")
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080043 return
44 }
David K. Bainbridge157bdab2020-01-16 14:38:05 -080045 opt := grpc.EmptyCallOption{}
Girish Kumarcd402012020-08-18 12:17:38 +000046 streamCtx, streamDone := context.WithCancel(log.WithSpanFromContext(context.Background(), ctx))
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080047 defer streamDone()
David Bainbridgef8ce7d22020-04-08 12:49:41 -070048 stream, err := ofa.volthaClient.Get().ReceiveChangeEvents(streamCtx, &empty.Empty{}, opt)
David K. Bainbridge157bdab2020-01-16 14:38:05 -080049 if err != nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +000050 logger.Errorw(ctx, "Unable to establish Receive Change Event Stream",
David K. Bainbridge157bdab2020-01-16 14:38:05 -080051 log.Fields{"error": err})
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080052 return
David K. Bainbridge157bdab2020-01-16 14:38:05 -080053 }
David K. Bainbridge157bdab2020-01-16 14:38:05 -080054
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080055top:
David K. Bainbridge157bdab2020-01-16 14:38:05 -080056 for {
57 select {
58 case <-ctx.Done():
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080059 break top
David K. Bainbridge157bdab2020-01-16 14:38:05 -080060 default:
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080061 ce, err := stream.Recv()
62 if err != nil {
Rohan Agrawalc32d9932020-06-15 11:01:47 +000063 logger.Errorw(ctx, "error receiving change event",
David K. Bainbridge157bdab2020-01-16 14:38:05 -080064 log.Fields{"error": err})
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080065 break top
David K. Bainbridge157bdab2020-01-16 14:38:05 -080066 }
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080067 ofa.changeEventChannel <- ce
Rohan Agrawalc32d9932020-06-15 11:01:47 +000068 logger.Debug(ctx, "receive-change-event-queued")
David K. Bainbridge157bdab2020-01-16 14:38:05 -080069 }
70 }
71}
72
73func (ofa *OFAgent) handleChangeEvents(ctx context.Context) {
Rohan Agrawalc32d9932020-06-15 11:01:47 +000074 logger.Debug(ctx, "handle-change-event-started")
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080075
76top:
David K. Bainbridge157bdab2020-01-16 14:38:05 -080077 for {
78 select {
79 case <-ctx.Done():
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080080 break top
David K. Bainbridge157bdab2020-01-16 14:38:05 -080081 case changeEvent := <-ofa.changeEventChannel:
82 deviceID := changeEvent.GetId()
83 portStatus := changeEvent.GetPortStatus()
Rohan Agrawalc32d9932020-06-15 11:01:47 +000084 logger.Debugw(ctx, "received-change-event",
David K. Bainbridge157bdab2020-01-16 14:38:05 -080085 log.Fields{
86 "device-id": deviceID,
87 "port-status": portStatus})
88
89 if portStatus == nil {
90 if logger.V(log.WarnLevel) {
91 js, _ := json.Marshal(changeEvent.GetEvent())
Rohan Agrawalc32d9932020-06-15 11:01:47 +000092 logger.Warnw(ctx, "Received change event that was not port status",
David K. Bainbridge157bdab2020-01-16 14:38:05 -080093 log.Fields{"ChangeEvent": js})
94 }
95 break
96 }
97 ofPortStatus := ofp.NewPortStatus()
98 ofPortStatus.SetXid(openflow.GetXid())
99 ofPortStatus.SetVersion(4)
100
101 ofReason := ofp.PortReason(portStatus.GetReason())
102 ofPortStatus.SetReason(ofReason)
103 ofDesc := ofp.NewPortDesc()
104
105 desc := portStatus.GetDesc()
106 ofDesc.SetAdvertised(ofp.PortFeatures(desc.GetAdvertised()))
107 ofDesc.SetConfig(ofp.PortConfig(0))
108 ofDesc.SetCurr(ofp.PortFeatures(desc.GetAdvertised()))
109 ofDesc.SetCurrSpeed(desc.GetCurrSpeed())
110 intArray := desc.GetHwAddr()
111 var octets []byte
112 for _, val := range intArray {
113 octets = append(octets, byte(val))
114 }
115 addr := net.HardwareAddr(octets)
116 ofDesc.SetHwAddr(addr)
117 ofDesc.SetMaxSpeed(desc.GetMaxSpeed())
118 ofDesc.SetName(openflow.PadString(desc.GetName(), 16))
119 ofDesc.SetPeer(ofp.PortFeatures(desc.GetPeer()))
120 ofDesc.SetPortNo(ofp.Port(desc.GetPortNo()))
121 ofDesc.SetState(ofp.PortState(desc.GetState()))
122 ofDesc.SetSupported(ofp.PortFeatures(desc.GetSupported()))
123 ofPortStatus.SetDesc(*ofDesc)
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000124 if err := ofa.getOFClient(ctx, deviceID).SendMessage(ctx, ofPortStatus); err != nil {
125 logger.Errorw(ctx, "handle-change-events-send-message", log.Fields{"error": err})
David K. Bainbridgecac73ac2020-02-19 07:00:12 -0800126 }
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800127 }
128 }
David K. Bainbridge9cb404e2020-01-28 14:32:29 -0800129
Rohan Agrawalc32d9932020-06-15 11:01:47 +0000130 logger.Debug(ctx, "handle-change-event-finsihed")
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800131}