blob: 68f87cde200dcdea0584dd0e1e65ce56f7fe1e8f [file] [log] [blame]
Don Newton98fd8812019-09-23 15:15:02 -04001/*
2 Copyright 2017 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 grpc
18
19import (
20 "context"
21 "encoding/json"
Don Newton7577f072020-01-06 12:41:11 -050022
Don Newtone0d34a82019-11-14 10:58:06 -050023 ofp "github.com/donNewtonAlpha/goloxi/of13"
Don Newton98fd8812019-09-23 15:15:02 -040024 "github.com/golang/protobuf/ptypes/empty"
25 "github.com/opencord/ofagent-go/openflow"
Don Newton7577f072020-01-06 12:41:11 -050026 "github.com/opencord/ofagent-go/settings"
27 l "github.com/opencord/voltha-lib-go/v2/pkg/log"
Don Newtonb437c6f2019-12-18 11:51:57 -050028 pb "github.com/opencord/voltha-protos/v2/go/voltha"
Don Newton98fd8812019-09-23 15:15:02 -040029 "google.golang.org/grpc"
Don Newton7577f072020-01-06 12:41:11 -050030
Don Newton98fd8812019-09-23 15:15:02 -040031 "net"
32 "time"
33)
34
35func receiveChangeEvent(client pb.VolthaServiceClient) {
Don Newton7577f072020-01-06 12:41:11 -050036 if settings.GetDebug(grpcDeviceID) {
37 logger.Debugln("GrpcClient receiveChangeEvent called")
38 }
Don Newton98fd8812019-09-23 15:15:02 -040039 opt := grpc.EmptyCallOption{}
40 stream, err := client.ReceiveChangeEvents(context.Background(), &empty.Empty{}, opt)
41 if err != nil {
Don Newton7577f072020-01-06 12:41:11 -050042 logger.Fatalln("Unable to establish Receive Change Event Stream")
Don Newton98fd8812019-09-23 15:15:02 -040043 }
44 for {
45 changeEvent, err := stream.Recv()
46 if err != nil {
Don Newton7577f072020-01-06 12:41:11 -050047 logger.Errorw("Error receiving change event", l.Fields{"Error": err})
Don Newton98fd8812019-09-23 15:15:02 -040048 }
Don Newton7577f072020-01-06 12:41:11 -050049 deviceID := changeEvent.GetId()
Don Newton98fd8812019-09-23 15:15:02 -040050 portStatus := changeEvent.GetPortStatus()
Don Newton7577f072020-01-06 12:41:11 -050051 if settings.GetDebug(grpcDeviceID) {
52 //js, _ := json.Marshal(changeEvent)
53
54 logger.Debugw("Received Change Event", l.Fields{"DeviceID": deviceID, "PortStatus": portStatus})
55 }
56
Don Newton98fd8812019-09-23 15:15:02 -040057 if portStatus == nil {
Don Newton7577f072020-01-06 12:41:11 -050058 js, _ := json.Marshal(changeEvent.GetEvent())
59 logger.Warnw("Received change event that was not port status", l.Fields{"ChangeEvent": js})
Don Newton98fd8812019-09-23 15:15:02 -040060 break
61 }
Don Newton98fd8812019-09-23 15:15:02 -040062 ofPortStatus := ofp.NewPortStatus()
63 ofPortStatus.SetXid(openflow.GetXid())
64 ofPortStatus.SetVersion(4)
65
66 ofReason := ofp.PortReason(portStatus.GetReason())
67 ofPortStatus.SetReason(ofReason)
68 ofDesc := ofp.NewPortDesc()
69
70 desc := portStatus.GetDesc()
71 ofDesc.SetAdvertised(ofp.PortFeatures(desc.GetAdvertised()))
72 ofDesc.SetConfig(ofp.PortConfig(0))
73 ofDesc.SetCurr(ofp.PortFeatures(desc.GetAdvertised()))
74 ofDesc.SetCurrSpeed(desc.GetCurrSpeed())
75 intArray := desc.GetHwAddr()
76 var octets []byte
77 for i := 0; i < len(intArray); i++ {
78 octets = append(octets, byte(intArray[i]))
79 }
80 addr := net.HardwareAddr(octets)
81 ofDesc.SetHwAddr(addr)
82 ofDesc.SetMaxSpeed(desc.GetMaxSpeed())
83 ofDesc.SetName(openflow.PadString(desc.GetName(), 16))
84 ofDesc.SetPeer(ofp.PortFeatures(desc.GetPeer()))
85 ofDesc.SetPortNo(ofp.Port(desc.GetPortNo()))
86 ofDesc.SetState(ofp.PortState(desc.GetState()))
87 ofDesc.SetSupported(ofp.PortFeatures(desc.GetSupported()))
88 ofPortStatus.SetDesc(*ofDesc)
Don Newton7577f072020-01-06 12:41:11 -050089 var client = clientMap[deviceID]
Don Newton98fd8812019-09-23 15:15:02 -040090 if client == nil {
Don Newton7577f072020-01-06 12:41:11 -050091 client = addClient(deviceID)
Don Newton98fd8812019-09-23 15:15:02 -040092 time.Sleep(2 * time.Second)
93 }
94 client.SendMessage(ofPortStatus)
95 }
96}