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