blob: c6f63762977b456b6a54dcb92e56247720bfa1f3 [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 Newtone0d34a82019-11-14 10:58:06 -050022 ofp "github.com/donNewtonAlpha/goloxi/of13"
Don Newton98fd8812019-09-23 15:15:02 -040023 "github.com/golang/protobuf/ptypes/empty"
24 "github.com/opencord/ofagent-go/openflow"
Don Newtonb437c6f2019-12-18 11:51:57 -050025 pb "github.com/opencord/voltha-protos/v2/go/voltha"
Don Newton98fd8812019-09-23 15:15:02 -040026 "google.golang.org/grpc"
27 "log"
28 "net"
29 "time"
30)
31
32func receiveChangeEvent(client pb.VolthaServiceClient) {
33 opt := grpc.EmptyCallOption{}
34 stream, err := client.ReceiveChangeEvents(context.Background(), &empty.Empty{}, opt)
35 if err != nil {
Don Newtonb437c6f2019-12-18 11:51:57 -050036 log.Fatalln("Unable to establish Receive Change Event Stream")
Don Newton98fd8812019-09-23 15:15:02 -040037 }
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 Newton98fd8812019-09-23 15:15:02 -040053 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 Newton98fd8812019-09-23 15:15:02 -040083 time.Sleep(2 * time.Second)
84 }
85 client.SendMessage(ofPortStatus)
86 }
87}