blob: 8972e1cd35fc756a406d4055a2617b2901644db4 [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"
22 ofp "github.com/donNewtonAlpha/goloxi/of13"
23 "github.com/golang/protobuf/ptypes/empty"
24 "github.com/opencord/ofagent-go/internal/pkg/openflow"
David K. Bainbridgeaea73cd2020-01-27 10:44:50 -080025 "github.com/opencord/voltha-lib-go/v3/pkg/log"
David K. Bainbridge157bdab2020-01-16 14:38:05 -080026 "google.golang.org/grpc"
27 "net"
28)
29
30func (ofa *OFAgent) receiveChangeEvents(ctx context.Context) {
31 logger.Debug("receive-change-events-started")
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080032 // If we exit, assume disconnected
33 defer func() {
34 ofa.events <- ofaEventVolthaDisconnected
35 logger.Debug("receive-change-events-finished")
36 }()
37 if ofa.volthaClient == nil {
38 logger.Error("no-voltha-connection")
39 return
40 }
David K. Bainbridge157bdab2020-01-16 14:38:05 -080041 opt := grpc.EmptyCallOption{}
42 streamCtx, streamDone := context.WithCancel(context.Background())
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080043 defer streamDone()
David K. Bainbridge157bdab2020-01-16 14:38:05 -080044 stream, err := ofa.volthaClient.ReceiveChangeEvents(streamCtx, &empty.Empty{}, opt)
45 if err != nil {
46 logger.Errorw("Unable to establish Receive Change Event Stream",
47 log.Fields{"error": err})
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080048 return
David K. Bainbridge157bdab2020-01-16 14:38:05 -080049 }
David K. Bainbridge157bdab2020-01-16 14:38:05 -080050
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080051top:
David K. Bainbridge157bdab2020-01-16 14:38:05 -080052 for {
53 select {
54 case <-ctx.Done():
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080055 break top
David K. Bainbridge157bdab2020-01-16 14:38:05 -080056 default:
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080057 ce, err := stream.Recv()
58 if err != nil {
David K. Bainbridge157bdab2020-01-16 14:38:05 -080059 logger.Errorw("error receiving change event",
60 log.Fields{"error": err})
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080061 break top
David K. Bainbridge157bdab2020-01-16 14:38:05 -080062 }
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080063 ofa.changeEventChannel <- ce
64 logger.Debug("receive-change-event-queued")
David K. Bainbridge157bdab2020-01-16 14:38:05 -080065 }
66 }
67}
68
69func (ofa *OFAgent) handleChangeEvents(ctx context.Context) {
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080070 logger.Debug("handle-change-event-started")
71
72top:
David K. Bainbridge157bdab2020-01-16 14:38:05 -080073 for {
74 select {
75 case <-ctx.Done():
David K. Bainbridge9cb404e2020-01-28 14:32:29 -080076 break top
David K. Bainbridge157bdab2020-01-16 14:38:05 -080077 case changeEvent := <-ofa.changeEventChannel:
78 deviceID := changeEvent.GetId()
79 portStatus := changeEvent.GetPortStatus()
80 logger.Debugw("received-change-event",
81 log.Fields{
82 "device-id": deviceID,
83 "port-status": portStatus})
84
85 if portStatus == nil {
86 if logger.V(log.WarnLevel) {
87 js, _ := json.Marshal(changeEvent.GetEvent())
88 logger.Warnw("Received change event that was not port status",
89 log.Fields{"ChangeEvent": js})
90 }
91 break
92 }
93 ofPortStatus := ofp.NewPortStatus()
94 ofPortStatus.SetXid(openflow.GetXid())
95 ofPortStatus.SetVersion(4)
96
97 ofReason := ofp.PortReason(portStatus.GetReason())
98 ofPortStatus.SetReason(ofReason)
99 ofDesc := ofp.NewPortDesc()
100
101 desc := portStatus.GetDesc()
102 ofDesc.SetAdvertised(ofp.PortFeatures(desc.GetAdvertised()))
103 ofDesc.SetConfig(ofp.PortConfig(0))
104 ofDesc.SetCurr(ofp.PortFeatures(desc.GetAdvertised()))
105 ofDesc.SetCurrSpeed(desc.GetCurrSpeed())
106 intArray := desc.GetHwAddr()
107 var octets []byte
108 for _, val := range intArray {
109 octets = append(octets, byte(val))
110 }
111 addr := net.HardwareAddr(octets)
112 ofDesc.SetHwAddr(addr)
113 ofDesc.SetMaxSpeed(desc.GetMaxSpeed())
114 ofDesc.SetName(openflow.PadString(desc.GetName(), 16))
115 ofDesc.SetPeer(ofp.PortFeatures(desc.GetPeer()))
116 ofDesc.SetPortNo(ofp.Port(desc.GetPortNo()))
117 ofDesc.SetState(ofp.PortState(desc.GetState()))
118 ofDesc.SetSupported(ofp.PortFeatures(desc.GetSupported()))
119 ofPortStatus.SetDesc(*ofDesc)
David K. Bainbridgecac73ac2020-02-19 07:00:12 -0800120 if err := ofa.getOFClient(deviceID).SendMessage(ofPortStatus); err != nil {
121 log.Errorw("handle-change-events-send-message", log.Fields{"error": err})
122 }
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800123 }
124 }
David K. Bainbridge9cb404e2020-01-28 14:32:29 -0800125
126 logger.Debug("handle-change-event-finsihed")
David K. Bainbridge157bdab2020-01-16 14:38:05 -0800127}