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") |
| 32 | opt := grpc.EmptyCallOption{} |
| 33 | streamCtx, streamDone := context.WithCancel(context.Background()) |
| 34 | stream, err := ofa.volthaClient.ReceiveChangeEvents(streamCtx, &empty.Empty{}, opt) |
| 35 | if err != nil { |
| 36 | logger.Errorw("Unable to establish Receive Change Event Stream", |
| 37 | log.Fields{"error": err}) |
| 38 | ofa.events <- ofaEventVolthaDisconnected |
| 39 | } |
| 40 | defer streamDone() |
| 41 | |
| 42 | for { |
| 43 | select { |
| 44 | case <-ctx.Done(): |
| 45 | return |
| 46 | default: |
| 47 | if ce, err := stream.Recv(); err != nil { |
| 48 | logger.Errorw("error receiving change event", |
| 49 | log.Fields{"error": err}) |
| 50 | ofa.events <- ofaEventVolthaDisconnected |
| 51 | } else { |
| 52 | ofa.changeEventChannel <- ce |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | func (ofa *OFAgent) handleChangeEvents(ctx context.Context) { |
| 59 | logger.Debugln("handle-change-event-started") |
| 60 | for { |
| 61 | select { |
| 62 | case <-ctx.Done(): |
| 63 | return |
| 64 | case changeEvent := <-ofa.changeEventChannel: |
| 65 | deviceID := changeEvent.GetId() |
| 66 | portStatus := changeEvent.GetPortStatus() |
| 67 | logger.Debugw("received-change-event", |
| 68 | log.Fields{ |
| 69 | "device-id": deviceID, |
| 70 | "port-status": portStatus}) |
| 71 | |
| 72 | if portStatus == nil { |
| 73 | if logger.V(log.WarnLevel) { |
| 74 | js, _ := json.Marshal(changeEvent.GetEvent()) |
| 75 | logger.Warnw("Received change event that was not port status", |
| 76 | log.Fields{"ChangeEvent": js}) |
| 77 | } |
| 78 | break |
| 79 | } |
| 80 | ofPortStatus := ofp.NewPortStatus() |
| 81 | ofPortStatus.SetXid(openflow.GetXid()) |
| 82 | ofPortStatus.SetVersion(4) |
| 83 | |
| 84 | ofReason := ofp.PortReason(portStatus.GetReason()) |
| 85 | ofPortStatus.SetReason(ofReason) |
| 86 | ofDesc := ofp.NewPortDesc() |
| 87 | |
| 88 | desc := portStatus.GetDesc() |
| 89 | ofDesc.SetAdvertised(ofp.PortFeatures(desc.GetAdvertised())) |
| 90 | ofDesc.SetConfig(ofp.PortConfig(0)) |
| 91 | ofDesc.SetCurr(ofp.PortFeatures(desc.GetAdvertised())) |
| 92 | ofDesc.SetCurrSpeed(desc.GetCurrSpeed()) |
| 93 | intArray := desc.GetHwAddr() |
| 94 | var octets []byte |
| 95 | for _, val := range intArray { |
| 96 | octets = append(octets, byte(val)) |
| 97 | } |
| 98 | addr := net.HardwareAddr(octets) |
| 99 | ofDesc.SetHwAddr(addr) |
| 100 | ofDesc.SetMaxSpeed(desc.GetMaxSpeed()) |
| 101 | ofDesc.SetName(openflow.PadString(desc.GetName(), 16)) |
| 102 | ofDesc.SetPeer(ofp.PortFeatures(desc.GetPeer())) |
| 103 | ofDesc.SetPortNo(ofp.Port(desc.GetPortNo())) |
| 104 | ofDesc.SetState(ofp.PortState(desc.GetState())) |
| 105 | ofDesc.SetSupported(ofp.PortFeatures(desc.GetSupported())) |
| 106 | ofPortStatus.SetDesc(*ofDesc) |
| 107 | ofa.getOFClient(deviceID).SendMessage(ofPortStatus) |
| 108 | } |
| 109 | } |
| 110 | } |