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 | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 22 | ofp "github.com/donNewtonAlpha/goloxi/of13" |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 23 | "github.com/golang/protobuf/ptypes/empty" |
| 24 | "github.com/opencord/ofagent-go/openflow" |
Don Newton | b437c6f | 2019-12-18 11:51:57 -0500 | [diff] [blame^] | 25 | pb "github.com/opencord/voltha-protos/v2/go/voltha" |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 26 | "google.golang.org/grpc" |
| 27 | "log" |
| 28 | "net" |
| 29 | "time" |
| 30 | ) |
| 31 | |
| 32 | func receiveChangeEvent(client pb.VolthaServiceClient) { |
| 33 | opt := grpc.EmptyCallOption{} |
| 34 | stream, err := client.ReceiveChangeEvents(context.Background(), &empty.Empty{}, opt) |
| 35 | if err != nil { |
Don Newton | b437c6f | 2019-12-18 11:51:57 -0500 | [diff] [blame^] | 36 | log.Fatalln("Unable to establish Receive Change Event Stream") |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 37 | } |
| 38 | for { |
| 39 | changeEvent, err := stream.Recv() |
| 40 | if err != nil { |
| 41 | log.Printf("Error receiving change event %v", err) |
| 42 | } |
| 43 | js, _ := json.Marshal(changeEvent) |
| 44 | |
| 45 | log.Printf("Received Change Event \n\n\n %s \n\n\n", js) |
| 46 | deviceId := changeEvent.GetId() |
| 47 | portStatus := changeEvent.GetPortStatus() |
| 48 | if portStatus == nil { |
| 49 | jsonMessage, _ := json.Marshal(changeEvent.GetEvent()) |
| 50 | log.Printf("Received change event that was not port status %v", jsonMessage) |
| 51 | break |
| 52 | } |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 53 | ofPortStatus := ofp.NewPortStatus() |
| 54 | ofPortStatus.SetXid(openflow.GetXid()) |
| 55 | ofPortStatus.SetVersion(4) |
| 56 | |
| 57 | ofReason := ofp.PortReason(portStatus.GetReason()) |
| 58 | ofPortStatus.SetReason(ofReason) |
| 59 | ofDesc := ofp.NewPortDesc() |
| 60 | |
| 61 | desc := portStatus.GetDesc() |
| 62 | ofDesc.SetAdvertised(ofp.PortFeatures(desc.GetAdvertised())) |
| 63 | ofDesc.SetConfig(ofp.PortConfig(0)) |
| 64 | ofDesc.SetCurr(ofp.PortFeatures(desc.GetAdvertised())) |
| 65 | ofDesc.SetCurrSpeed(desc.GetCurrSpeed()) |
| 66 | intArray := desc.GetHwAddr() |
| 67 | var octets []byte |
| 68 | for i := 0; i < len(intArray); i++ { |
| 69 | octets = append(octets, byte(intArray[i])) |
| 70 | } |
| 71 | addr := net.HardwareAddr(octets) |
| 72 | ofDesc.SetHwAddr(addr) |
| 73 | ofDesc.SetMaxSpeed(desc.GetMaxSpeed()) |
| 74 | ofDesc.SetName(openflow.PadString(desc.GetName(), 16)) |
| 75 | ofDesc.SetPeer(ofp.PortFeatures(desc.GetPeer())) |
| 76 | ofDesc.SetPortNo(ofp.Port(desc.GetPortNo())) |
| 77 | ofDesc.SetState(ofp.PortState(desc.GetState())) |
| 78 | ofDesc.SetSupported(ofp.PortFeatures(desc.GetSupported())) |
| 79 | ofPortStatus.SetDesc(*ofDesc) |
| 80 | var client = clientMap[deviceId] |
| 81 | if client == nil { |
| 82 | client = addClient(deviceId) |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 83 | time.Sleep(2 * time.Second) |
| 84 | } |
| 85 | client.SendMessage(ofPortStatus) |
| 86 | } |
| 87 | } |