blob: acb2304eda6dfc22ea54d1b8c96598a064d5247e [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"
21 "time"
22
23 "github.com/golang/protobuf/ptypes/empty"
24
25 "github.com/opencord/ofagent-go/openflow"
26
27 "fmt"
28 "log"
29
30 pb "github.com/opencord/voltha-protos/go/voltha"
31 "google.golang.org/grpc"
32)
33
34var client pb.VolthaServiceClient
35var clientMap map[string]*openflow.Client
36var ofAddress string
37var ofPort uint16
38
39func StartClient(endpointAddress string, endpointPort uint16, openFlowAddress string, openFlowPort uint16) {
40 ofAddress = openFlowAddress
41 ofPort = openFlowPort
42 clientMap = make(map[string]*openflow.Client)
43 var opts []grpc.DialOption
44 opts = append(opts, grpc.WithInsecure())
45 endpoint := fmt.Sprintf("%s:%d", endpointAddress, endpointPort)
46 conn, err := grpc.Dial(endpoint, opts...)
47 defer conn.Close()
48 if err != nil {
49 log.Fatalf("fail to dial: %v", err)
50 }
51 client = pb.NewVolthaServiceClient(conn)
52
53 go receivePacketIn(client)
54 go receiveChangeEvent(client)
55 go streamPacketOut(client)
56
57 openflow.SetGrpcClient(&client)
58 for {
59 log.Println("entering device refresh pull")
60 deviceList, err := client.ListLogicalDevices(context.Background(), &empty.Empty{})
61 if err != nil {
62 log.Printf("ERROR GET DEVICE LIST %v", err)
63 }
64 devices := deviceList.GetItems()
65 refreshDeviceList(devices)
66 time.Sleep(time.Minute)
67 log.Println("waking up")
68 }
69}
70func refreshDeviceList(devices []*pb.LogicalDevice) {
71 //first find the new ones
72 var toAdd []string
73 var toDel []string
74 var deviceIdMap = make(map[string]string)
75 for i := 0; i < len(devices); i++ {
76 log.Printf("Device ID %s", devices[i].GetId())
77 deviceId := devices[i].GetId()
78 deviceIdMap[deviceId] = deviceId
79 if clientMap[deviceId] == nil {
80 toAdd = append(toAdd, deviceId)
81 }
82 }
83 for key, _ := range clientMap {
84 if deviceIdMap[key] == "" {
85 toDel = append(toDel, key)
86 }
87 }
88 for i := 0; i < len(toAdd); i++ {
89 var client = addClient(toAdd[i])
90 go client.Start()
91 }
92 for i := 0; i < len(toDel); i++ {
93 clientMap[toDel[i]].End()
94 delete(clientMap, toDel[i])
95 }
96}
97func addClient(deviceId string) *openflow.Client {
98 client := openflow.NewClient(ofAddress, ofPort, deviceId, true)
99 clientMap[deviceId] = client
100 return client
101}