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" |
David Bainbridge | f8ce7d2 | 2020-04-08 12:49:41 -0700 | [diff] [blame] | 22 | "net" |
| 23 | |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 24 | "github.com/golang/protobuf/ptypes/empty" |
Jonathan Hart | 828908c | 2020-04-15 14:23:45 -0700 | [diff] [blame] | 25 | ofp "github.com/opencord/goloxi/of13" |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 26 | "github.com/opencord/ofagent-go/internal/pkg/openflow" |
Andrea Campanella | 18448bc | 2021-07-08 18:47:22 +0200 | [diff] [blame] | 27 | "github.com/opencord/voltha-lib-go/v5/pkg/log" |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 28 | "google.golang.org/grpc" |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 29 | ) |
| 30 | |
| 31 | func (ofa *OFAgent) receiveChangeEvents(ctx context.Context) { |
Girish Kumar | 01e0c63 | 2020-08-10 16:48:56 +0000 | [diff] [blame] | 32 | span, ctx := log.CreateChildSpan(ctx, "receive-change-events") |
| 33 | defer span.Finish() |
| 34 | |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 35 | logger.Debug(ctx, "receive-change-events-started") |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 36 | // If we exit, assume disconnected |
| 37 | defer func() { |
| 38 | ofa.events <- ofaEventVolthaDisconnected |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 39 | logger.Debug(ctx, "receive-change-events-finished") |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 40 | }() |
| 41 | if ofa.volthaClient == nil { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 42 | logger.Error(ctx, "no-voltha-connection") |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 43 | return |
| 44 | } |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 45 | opt := grpc.EmptyCallOption{} |
Girish Kumar | cd40201 | 2020-08-18 12:17:38 +0000 | [diff] [blame] | 46 | streamCtx, streamDone := context.WithCancel(log.WithSpanFromContext(context.Background(), ctx)) |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 47 | defer streamDone() |
David Bainbridge | f8ce7d2 | 2020-04-08 12:49:41 -0700 | [diff] [blame] | 48 | stream, err := ofa.volthaClient.Get().ReceiveChangeEvents(streamCtx, &empty.Empty{}, opt) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 49 | if err != nil { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 50 | logger.Errorw(ctx, "Unable to establish Receive Change Event Stream", |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 51 | log.Fields{"error": err}) |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 52 | return |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 53 | } |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 54 | |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 55 | top: |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 56 | for { |
| 57 | select { |
| 58 | case <-ctx.Done(): |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 59 | break top |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 60 | default: |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 61 | ce, err := stream.Recv() |
| 62 | if err != nil { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 63 | logger.Errorw(ctx, "error receiving change event", |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 64 | log.Fields{"error": err}) |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 65 | break top |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 66 | } |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 67 | ofa.changeEventChannel <- ce |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 68 | logger.Debug(ctx, "receive-change-event-queued") |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | func (ofa *OFAgent) handleChangeEvents(ctx context.Context) { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 74 | logger.Debug(ctx, "handle-change-event-started") |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 75 | |
| 76 | top: |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 77 | for { |
| 78 | select { |
| 79 | case <-ctx.Done(): |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 80 | break top |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 81 | case changeEvent := <-ofa.changeEventChannel: |
| 82 | deviceID := changeEvent.GetId() |
Maninder | e2af7e4 | 2020-12-04 11:46:26 +0530 | [diff] [blame] | 83 | if errMsg := changeEvent.GetError(); errMsg != nil { |
| 84 | logger.Debugw(ctx, "received-change-event", |
| 85 | log.Fields{ |
| 86 | "device-id": deviceID, |
| 87 | "error": errMsg}) |
| 88 | header := errMsg.Header |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 89 | |
Maninder | e2af7e4 | 2020-12-04 11:46:26 +0530 | [diff] [blame] | 90 | ofErrMsg := ofp.NewFlowModFailedErrorMsg() |
| 91 | |
| 92 | ofErrMsg.SetXid(header.Xid) |
Andrea Campanella | 111fc34 | 2021-01-29 09:45:45 +0100 | [diff] [blame] | 93 | ofErrMsg.SetCode(ofp.FlowModFailedCode(errMsg.Code)) |
| 94 | |
Maninder | e2af7e4 | 2020-12-04 11:46:26 +0530 | [diff] [blame] | 95 | if header.Version != 0 { |
| 96 | ofErrMsg.SetVersion(uint8(header.Version)) |
| 97 | } else { |
| 98 | ofErrMsg.SetVersion(4) |
| 99 | } |
Maninder | e2af7e4 | 2020-12-04 11:46:26 +0530 | [diff] [blame] | 100 | ofErrMsg.SetData(errMsg.Data) |
Maninder | e2af7e4 | 2020-12-04 11:46:26 +0530 | [diff] [blame] | 101 | if err := ofa.getOFClient(ctx, deviceID).SendMessage(ctx, ofErrMsg); err != nil { |
| 102 | logger.Errorw(ctx, "handle-change-events-send-message", log.Fields{"error": err}) |
| 103 | } |
| 104 | |
| 105 | } else if portStatus := changeEvent.GetPortStatus(); portStatus != nil { |
| 106 | logger.Debugw(ctx, "received-change-event", |
| 107 | log.Fields{ |
| 108 | "device-id": deviceID, |
| 109 | "port-status": portStatus}) |
| 110 | ofPortStatus := ofp.NewPortStatus() |
| 111 | ofPortStatus.SetXid(openflow.GetXid()) |
| 112 | ofPortStatus.SetVersion(4) |
| 113 | |
| 114 | ofReason := ofp.PortReason(portStatus.GetReason()) |
| 115 | ofPortStatus.SetReason(ofReason) |
| 116 | ofDesc := ofp.NewPortDesc() |
| 117 | |
| 118 | desc := portStatus.GetDesc() |
| 119 | ofDesc.SetAdvertised(ofp.PortFeatures(desc.GetAdvertised())) |
| 120 | ofDesc.SetConfig(ofp.PortConfig(0)) |
| 121 | ofDesc.SetCurr(ofp.PortFeatures(desc.GetAdvertised())) |
| 122 | ofDesc.SetCurrSpeed(desc.GetCurrSpeed()) |
| 123 | intArray := desc.GetHwAddr() |
| 124 | var octets []byte |
| 125 | for _, val := range intArray { |
| 126 | octets = append(octets, byte(val)) |
| 127 | } |
| 128 | addr := net.HardwareAddr(octets) |
| 129 | ofDesc.SetHwAddr(addr) |
| 130 | ofDesc.SetMaxSpeed(desc.GetMaxSpeed()) |
| 131 | ofDesc.SetName(openflow.PadString(desc.GetName(), 16)) |
| 132 | ofDesc.SetPeer(ofp.PortFeatures(desc.GetPeer())) |
| 133 | ofDesc.SetPortNo(ofp.Port(desc.GetPortNo())) |
| 134 | ofDesc.SetState(ofp.PortState(desc.GetState())) |
| 135 | ofDesc.SetSupported(ofp.PortFeatures(desc.GetSupported())) |
| 136 | ofPortStatus.SetDesc(*ofDesc) |
| 137 | if err := ofa.getOFClient(ctx, deviceID).SendMessage(ctx, ofPortStatus); err != nil { |
| 138 | logger.Errorw(ctx, "handle-change-events-send-message", log.Fields{"error": err}) |
| 139 | } |
| 140 | } else { |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 141 | if logger.V(log.WarnLevel) { |
| 142 | js, _ := json.Marshal(changeEvent.GetEvent()) |
Maninder | e2af7e4 | 2020-12-04 11:46:26 +0530 | [diff] [blame] | 143 | logger.Warnw(ctx, "Received change event that was neither port status nor error message.", |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 144 | log.Fields{"ChangeEvent": js}) |
| 145 | } |
| 146 | break |
| 147 | } |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 148 | } |
| 149 | } |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 150 | |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 151 | logger.Debug(ctx, "handle-change-event-finsihed") |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 152 | } |