David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package ofagent |
| 18 | |
| 19 | import ( |
| 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. Bainbridge | aea73cd | 2020-01-27 10:44:50 -0800 | [diff] [blame] | 25 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 26 | "google.golang.org/grpc" |
| 27 | "net" |
| 28 | ) |
| 29 | |
| 30 | func (ofa *OFAgent) receiveChangeEvents(ctx context.Context) { |
| 31 | logger.Debug("receive-change-events-started") |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 32 | // 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. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 41 | opt := grpc.EmptyCallOption{} |
| 42 | streamCtx, streamDone := context.WithCancel(context.Background()) |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 43 | defer streamDone() |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 44 | 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. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 48 | return |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 49 | } |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 50 | |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 51 | top: |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 52 | for { |
| 53 | select { |
| 54 | case <-ctx.Done(): |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 55 | break top |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 56 | default: |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 57 | ce, err := stream.Recv() |
| 58 | if err != nil { |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 59 | logger.Errorw("error receiving change event", |
| 60 | log.Fields{"error": err}) |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 61 | break top |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 62 | } |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 63 | ofa.changeEventChannel <- ce |
| 64 | logger.Debug("receive-change-event-queued") |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | func (ofa *OFAgent) handleChangeEvents(ctx context.Context) { |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 70 | logger.Debug("handle-change-event-started") |
| 71 | |
| 72 | top: |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 73 | for { |
| 74 | select { |
| 75 | case <-ctx.Done(): |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 76 | break top |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 77 | 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. Bainbridge | cac73ac | 2020-02-19 07:00:12 -0800 | [diff] [blame] | 120 | if err := ofa.getOFClient(deviceID).SendMessage(ofPortStatus); err != nil { |
divyadesai | 81bb7ba | 2020-03-11 11:45:23 +0000 | [diff] [blame] | 121 | logger.Errorw("handle-change-events-send-message", log.Fields{"error": err}) |
David K. Bainbridge | cac73ac | 2020-02-19 07:00:12 -0800 | [diff] [blame] | 122 | } |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 123 | } |
| 124 | } |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 125 | |
Girish Kumar | e9d7617 | 2020-03-20 20:26:04 +0000 | [diff] [blame^] | 126 | logger.Debug("handle-change-event-finsihed") |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 127 | } |