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" |
| 29 | "log" |
| 30 | |
Don Newton | b437c6f | 2019-12-18 11:51:57 -0500 | [diff] [blame^] | 31 | pb "github.com/opencord/voltha-protos/v2/go/voltha" |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 32 | "google.golang.org/grpc" |
| 33 | ) |
| 34 | |
| 35 | var client pb.VolthaServiceClient |
| 36 | var clientMap map[string]*openflow.Client |
| 37 | var ofAddress string |
| 38 | var ofPort uint16 |
Don Newton | b437c6f | 2019-12-18 11:51:57 -0500 | [diff] [blame^] | 39 | var mapLock sync.Mutex |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 40 | |
| 41 | func StartClient(endpointAddress string, endpointPort uint16, openFlowAddress string, openFlowPort uint16) { |
| 42 | ofAddress = openFlowAddress |
| 43 | ofPort = openFlowPort |
| 44 | clientMap = make(map[string]*openflow.Client) |
| 45 | var opts []grpc.DialOption |
| 46 | opts = append(opts, grpc.WithInsecure()) |
| 47 | endpoint := fmt.Sprintf("%s:%d", endpointAddress, endpointPort) |
| 48 | conn, err := grpc.Dial(endpoint, opts...) |
| 49 | defer conn.Close() |
| 50 | if err != nil { |
| 51 | log.Fatalf("fail to dial: %v", err) |
| 52 | } |
| 53 | client = pb.NewVolthaServiceClient(conn) |
| 54 | |
| 55 | go receivePacketIn(client) |
| 56 | go receiveChangeEvent(client) |
| 57 | go streamPacketOut(client) |
| 58 | |
| 59 | openflow.SetGrpcClient(&client) |
| 60 | for { |
| 61 | log.Println("entering device refresh pull") |
| 62 | deviceList, err := client.ListLogicalDevices(context.Background(), &empty.Empty{}) |
| 63 | if err != nil { |
| 64 | log.Printf("ERROR GET DEVICE LIST %v", err) |
| 65 | } |
| 66 | devices := deviceList.GetItems() |
| 67 | refreshDeviceList(devices) |
| 68 | time.Sleep(time.Minute) |
| 69 | log.Println("waking up") |
| 70 | } |
| 71 | } |
| 72 | func refreshDeviceList(devices []*pb.LogicalDevice) { |
| 73 | //first find the new ones |
Don Newton | b437c6f | 2019-12-18 11:51:57 -0500 | [diff] [blame^] | 74 | |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 75 | var toAdd []string |
| 76 | var toDel []string |
| 77 | var deviceIdMap = make(map[string]string) |
| 78 | for i := 0; i < len(devices); i++ { |
| 79 | log.Printf("Device ID %s", devices[i].GetId()) |
| 80 | deviceId := devices[i].GetId() |
| 81 | deviceIdMap[deviceId] = deviceId |
| 82 | if clientMap[deviceId] == nil { |
| 83 | toAdd = append(toAdd, deviceId) |
| 84 | } |
| 85 | } |
| 86 | for key, _ := range clientMap { |
| 87 | if deviceIdMap[key] == "" { |
| 88 | toDel = append(toDel, key) |
| 89 | } |
| 90 | } |
| 91 | for i := 0; i < len(toAdd); i++ { |
| 92 | var client = addClient(toAdd[i]) |
| 93 | go client.Start() |
| 94 | } |
| 95 | for i := 0; i < len(toDel); i++ { |
| 96 | clientMap[toDel[i]].End() |
Don Newton | b437c6f | 2019-12-18 11:51:57 -0500 | [diff] [blame^] | 97 | mapLock.Lock() |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 98 | delete(clientMap, toDel[i]) |
Don Newton | b437c6f | 2019-12-18 11:51:57 -0500 | [diff] [blame^] | 99 | mapLock.Unlock() |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | func addClient(deviceId string) *openflow.Client { |
Don Newton | b437c6f | 2019-12-18 11:51:57 -0500 | [diff] [blame^] | 103 | mapLock.Lock() |
| 104 | var client *openflow.Client |
| 105 | client = clientMap[deviceId] |
| 106 | if client == nil { |
| 107 | client = openflow.NewClient(ofAddress, ofPort, deviceId, true) |
| 108 | clientMap[deviceId] = client |
| 109 | } |
| 110 | mapLock.Unlock() |
Don Newton | 98fd881 | 2019-09-23 15:15:02 -0400 | [diff] [blame] | 111 | return client |
| 112 | } |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 113 | func GetClient(deviceId string) *openflow.Client { |
| 114 | return clientMap[deviceId] |
| 115 | } |