Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package grpc |
| 18 | |
| 19 | import ( |
| 20 | "context" |
Don Newton | b437c6f | 2019-12-18 11:51:57 -0500 | [diff] [blame] | 21 | "sync" |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 22 | "time" |
| 23 | |
| 24 | "github.com/golang/protobuf/ptypes/empty" |
| 25 | |
| 26 | "github.com/opencord/ofagent-go/openflow" |
| 27 | |
| 28 | "fmt" |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame] | 29 | |
| 30 | "github.com/opencord/ofagent-go/settings" |
| 31 | l "github.com/opencord/voltha-lib-go/v2/pkg/log" |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 32 | |
Don Newton | b437c6f | 2019-12-18 11:51:57 -0500 | [diff] [blame] | 33 | pb "github.com/opencord/voltha-protos/v2/go/voltha" |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 34 | "google.golang.org/grpc" |
| 35 | ) |
| 36 | |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame] | 37 | var grpcDeviceID = "GRPC_CLIENT" |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 38 | var client pb.VolthaServiceClient |
| 39 | var clientMap map[string]*openflow.Client |
| 40 | var ofAddress string |
| 41 | var ofPort uint16 |
Don Newton | b437c6f | 2019-12-18 11:51:57 -0500 | [diff] [blame] | 42 | var mapLock sync.Mutex |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame] | 43 | var logger, _ = l.AddPackage(l.JSON, l.DebugLevel, nil) |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 44 | |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame] | 45 | //StartClient - make the inital connection to voltha and kicks off io streams |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 46 | func StartClient(endpointAddress string, endpointPort uint16, openFlowAddress string, openFlowPort uint16) { |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame] | 47 | |
| 48 | if settings.GetDebug(grpcDeviceID) { |
| 49 | logger.Debugw("Starting GRPC - VOLTHA client", l.Fields{"EndPointAddr": endpointAddress, |
| 50 | "EndPointPort": endpointPort, "OpenFlowAddress": openFlowAddress, "OpenFlowPort": openFlowPort}) |
| 51 | } |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 52 | ofAddress = openFlowAddress |
| 53 | ofPort = openFlowPort |
| 54 | clientMap = make(map[string]*openflow.Client) |
| 55 | var opts []grpc.DialOption |
| 56 | opts = append(opts, grpc.WithInsecure()) |
| 57 | endpoint := fmt.Sprintf("%s:%d", endpointAddress, endpointPort) |
| 58 | conn, err := grpc.Dial(endpoint, opts...) |
| 59 | defer conn.Close() |
| 60 | if err != nil { |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame] | 61 | l.Fatalw("StartClient failed opening GRPC connecton", l.Fields{"EndPoint": endpoint, "Error": err}) |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 62 | } |
| 63 | client = pb.NewVolthaServiceClient(conn) |
| 64 | |
| 65 | go receivePacketIn(client) |
| 66 | go receiveChangeEvent(client) |
| 67 | go streamPacketOut(client) |
| 68 | |
| 69 | openflow.SetGrpcClient(&client) |
| 70 | for { |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame] | 71 | if settings.GetDebug(grpcDeviceID) { |
| 72 | logger.Debugln("GrpcClient entering device refresh pull") |
| 73 | } |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 74 | deviceList, err := client.ListLogicalDevices(context.Background(), &empty.Empty{}) |
| 75 | if err != nil { |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame] | 76 | logger.Errorw("GrpcClient getDeviceList failed", l.Fields{"Error": err}) |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 77 | } |
| 78 | devices := deviceList.GetItems() |
| 79 | refreshDeviceList(devices) |
| 80 | time.Sleep(time.Minute) |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | func refreshDeviceList(devices []*pb.LogicalDevice) { |
| 84 | //first find the new ones |
Don Newton | b437c6f | 2019-12-18 11:51:57 -0500 | [diff] [blame] | 85 | |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 86 | var toAdd []string |
| 87 | var toDel []string |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame] | 88 | var deviceIDMap = make(map[string]string) |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 89 | for i := 0; i < len(devices); i++ { |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame] | 90 | deviceID := devices[i].GetId() |
| 91 | deviceIDMap[deviceID] = deviceID |
| 92 | if clientMap[deviceID] == nil { |
| 93 | toAdd = append(toAdd, deviceID) |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 94 | } |
| 95 | } |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame] | 96 | for key := range clientMap { |
| 97 | if deviceIDMap[key] == "" { |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 98 | toDel = append(toDel, key) |
| 99 | } |
| 100 | } |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame] | 101 | if settings.GetDebug(grpcDeviceID) { |
| 102 | logger.Debugw("GrpcClient refreshDeviceList", l.Fields{"ToAdd": toAdd, "ToDel": toDel}) |
| 103 | } |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 104 | for i := 0; i < len(toAdd); i++ { |
| 105 | var client = addClient(toAdd[i]) |
| 106 | go client.Start() |
| 107 | } |
| 108 | for i := 0; i < len(toDel); i++ { |
| 109 | clientMap[toDel[i]].End() |
Don Newton | b437c6f | 2019-12-18 11:51:57 -0500 | [diff] [blame] | 110 | mapLock.Lock() |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 111 | delete(clientMap, toDel[i]) |
Don Newton | b437c6f | 2019-12-18 11:51:57 -0500 | [diff] [blame] | 112 | mapLock.Unlock() |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 113 | } |
| 114 | } |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame] | 115 | func addClient(deviceID string) *openflow.Client { |
| 116 | if settings.GetDebug(grpcDeviceID) { |
| 117 | logger.Debugw("GrpcClient addClient called ", l.Fields{"DeviceID": deviceID}) |
| 118 | } |
Don Newton | b437c6f | 2019-12-18 11:51:57 -0500 | [diff] [blame] | 119 | mapLock.Lock() |
| 120 | var client *openflow.Client |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame] | 121 | client = clientMap[deviceID] |
Don Newton | b437c6f | 2019-12-18 11:51:57 -0500 | [diff] [blame] | 122 | if client == nil { |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame] | 123 | client = openflow.NewClient(ofAddress, ofPort, deviceID, true) |
| 124 | go client.Start() |
| 125 | clientMap[deviceID] = client |
Don Newton | b437c6f | 2019-12-18 11:51:57 -0500 | [diff] [blame] | 126 | } |
| 127 | mapLock.Unlock() |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame] | 128 | logger.Debugw("Finished with addClient", l.Fields{"deviceID": deviceID}) |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 129 | return client |
| 130 | } |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame] | 131 | |
| 132 | //GetClient Returns a pointer to the OpenFlow client |
| 133 | func GetClient(deviceID string) *openflow.Client { |
| 134 | client := clientMap[deviceID] |
| 135 | if client == nil { |
| 136 | client = addClient(deviceID) |
| 137 | } |
| 138 | return client |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 139 | } |