Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package grpc |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "encoding/json" |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame] | 22 | |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 23 | ofp "github.com/donNewtonAlpha/goloxi/of13" |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 24 | "github.com/golang/protobuf/ptypes/empty" |
| 25 | "github.com/opencord/ofagent-go/openflow" |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame] | 26 | "github.com/opencord/ofagent-go/settings" |
| 27 | l "github.com/opencord/voltha-lib-go/v2/pkg/log" |
Don Newton | b437c6f | 2019-12-18 11:51:57 -0500 | [diff] [blame] | 28 | pb "github.com/opencord/voltha-protos/v2/go/voltha" |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 29 | "google.golang.org/grpc" |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame] | 30 | |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 31 | "net" |
| 32 | "time" |
| 33 | ) |
| 34 | |
| 35 | func receiveChangeEvent(client pb.VolthaServiceClient) { |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame] | 36 | if settings.GetDebug(grpcDeviceID) { |
| 37 | logger.Debugln("GrpcClient receiveChangeEvent called") |
| 38 | } |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 39 | opt := grpc.EmptyCallOption{} |
| 40 | stream, err := client.ReceiveChangeEvents(context.Background(), &empty.Empty{}, opt) |
| 41 | if err != nil { |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame] | 42 | logger.Fatalln("Unable to establish Receive Change Event Stream") |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 43 | } |
| 44 | for { |
| 45 | changeEvent, err := stream.Recv() |
| 46 | if err != nil { |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame] | 47 | logger.Errorw("Error receiving change event", l.Fields{"Error": err}) |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 48 | } |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame] | 49 | deviceID := changeEvent.GetId() |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 50 | portStatus := changeEvent.GetPortStatus() |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame] | 51 | 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 Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 57 | if portStatus == nil { |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame] | 58 | js, _ := json.Marshal(changeEvent.GetEvent()) |
| 59 | logger.Warnw("Received change event that was not port status", l.Fields{"ChangeEvent": js}) |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 60 | break |
| 61 | } |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 62 | 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 Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame] | 89 | var client = clientMap[deviceID] |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 90 | if client == nil { |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame] | 91 | client = addClient(deviceID) |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 92 | time.Sleep(2 * time.Second) |
| 93 | } |
| 94 | client.SendMessage(ofPortStatus) |
| 95 | } |
| 96 | } |