blob: 411646109c0ea91d22ec10e4d69d1097e01a05dc [file] [log] [blame]
Don Newton98fd8812019-09-23 15:15:02 -04001/*
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
17package grpc
18
19import (
20 "context"
Don Newtonb437c6f2019-12-18 11:51:57 -050021 "sync"
Don Newton98fd8812019-09-23 15:15:02 -040022 "time"
23
24 "github.com/golang/protobuf/ptypes/empty"
25
26 "github.com/opencord/ofagent-go/openflow"
27
28 "fmt"
29 "log"
30
Don Newtonb437c6f2019-12-18 11:51:57 -050031 pb "github.com/opencord/voltha-protos/v2/go/voltha"
Don Newton98fd8812019-09-23 15:15:02 -040032 "google.golang.org/grpc"
33)
34
35var client pb.VolthaServiceClient
36var clientMap map[string]*openflow.Client
37var ofAddress string
38var ofPort uint16
Don Newtonb437c6f2019-12-18 11:51:57 -050039var mapLock sync.Mutex
Don Newton98fd8812019-09-23 15:15:02 -040040
41func 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}
72func refreshDeviceList(devices []*pb.LogicalDevice) {
73 //first find the new ones
Don Newtonb437c6f2019-12-18 11:51:57 -050074
Don Newton98fd8812019-09-23 15:15:02 -040075 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 Newtonb437c6f2019-12-18 11:51:57 -050097 mapLock.Lock()
Don Newton98fd8812019-09-23 15:15:02 -040098 delete(clientMap, toDel[i])
Don Newtonb437c6f2019-12-18 11:51:57 -050099 mapLock.Unlock()
Don Newton98fd8812019-09-23 15:15:02 -0400100 }
101}
102func addClient(deviceId string) *openflow.Client {
Don Newtonb437c6f2019-12-18 11:51:57 -0500103 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 Newton98fd8812019-09-23 15:15:02 -0400111 return client
112}
Don Newtone0d34a82019-11-14 10:58:06 -0500113func GetClient(deviceId string) *openflow.Client {
114 return clientMap[deviceId]
115}