blob: 1f0b379bd996454d8c353d1d5fea0e3179d4d2e4 [file] [log] [blame]
Scott Bakere702d122019-10-22 11:54:12 -07001/*
2 * Copyright 2018-present Open Networking Foundation
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 afrouterd
18
19import (
20 "fmt"
Scott Bakerf579f132019-10-24 14:31:41 -070021 "github.com/opencord/voltha-lib-go/v2/pkg/log"
divyadesaif117fc22019-11-04 06:32:01 +000022 "github.com/opencord/voltha-lib-go/v2/pkg/probe"
Scott Bakerb6de7a52019-11-04 09:13:37 -080023 pb "github.com/opencord/voltha-protos/v2/go/afrouter"
Scott Bakere702d122019-10-22 11:54:12 -070024 "golang.org/x/net/context"
25 "google.golang.org/grpc"
26 "google.golang.org/grpc/connectivity"
27 "google.golang.org/grpc/keepalive"
28 "math"
29 "os"
30 "strconv"
31 "time"
32)
33
34type volthaPod struct {
35 name string
36 ipAddr string
37 node string
38 devIds map[string]struct{}
39 backend string
40}
41
42// TODO: These variables should be passed in from main() rather than
43// declared here.
44
45var (
46 // if k8s variables are undefined, will attempt to use in-cluster config
47 k8sApiServer = GetStrEnv("K8S_API_SERVER", "")
48 k8sKubeConfigPath = GetStrEnv("K8S_KUBE_CONFIG_PATH", "")
49
50 podNamespace = GetStrEnv("POD_NAMESPACE", "voltha")
51 podLabelSelector = GetStrEnv("POD_LABEL_SELECTOR", "app=rw-core")
52 podAffinityGroupLabel = GetStrEnv("POD_AFFINITY_GROUP_LABEL", "affinity-group")
53
54 podGrpcPort = uint64(GetIntEnv("POD_GRPC_PORT", 0, math.MaxUint16, 50057))
55
56 afrouterRouterName = GetStrEnv("AFROUTER_ROUTER_NAME", "vcore")
57 afrouterRouteName = GetStrEnv("AFROUTER_ROUTE_NAME", "dev_manager")
58 afrouterRWClusterName = GetStrEnv("AFROUTER_RW_CLUSTER_NAME", "vcore")
59
60 kafkaTopic = GetStrEnv("KAFKA_TOPIC", "AffinityRouter")
61 kafkaClientType = GetStrEnv("KAFKA_CLIENT_TYPE", "sarama")
62 kafkaHost = GetStrEnv("KAFKA_HOST", "kafka")
63 kafkaPort = GetIntEnv("KAFKA_PORT", 0, math.MaxUint16, 9092)
64 kafkaInstanceID = GetStrEnv("KAFKA_INSTANCE_ID", "arouterd")
65)
66
67func GetIntEnv(key string, min, max, defaultValue int) int {
68 if val, have := os.LookupEnv(key); have {
69 num, err := strconv.Atoi(val)
70 if err != nil || !(min <= num && num <= max) {
71 panic(fmt.Errorf("%s must be a number in the range [%d, %d]; default: %d", key, min, max, defaultValue))
72 }
73 return num
74 }
75 return defaultValue
76}
77
78func GetStrEnv(key, defaultValue string) string {
79 if val, have := os.LookupEnv(key); have {
80 return val
81 }
82 return defaultValue
83}
84
85func Connect(ctx context.Context, addr string) (*grpc.ClientConn, error) {
86 log.Debugf("Trying to connect to %s", addr)
87 conn, err := grpc.DialContext(ctx, addr,
88 grpc.WithInsecure(),
89 grpc.WithBlock(),
90 grpc.WithBackoffMaxDelay(time.Second*5),
91 grpc.WithKeepaliveParams(keepalive.ClientParameters{Time: time.Second * 10, Timeout: time.Second * 5}))
92 if err == nil {
93 log.Debugf("Connection succeeded")
94 }
95 return conn, err
96}
97
98func setAffinity(ctx context.Context, client pb.ConfigurationClient, deviceId string, backend string) {
99 log.Debugf("Configuring backend %s with device id %s \n", backend, deviceId)
100 if res, err := client.SetAffinity(ctx, &pb.Affinity{
101 Router: afrouterRouterName,
102 Route: afrouterRouteName,
103 Cluster: afrouterRWClusterName,
104 Backend: backend,
105 Id: deviceId,
106 }); err != nil {
107 log.Debugf("failed affinity RPC call: %s\n", err)
108 } else {
109 log.Debugf("Result: %v\n", res)
110 }
111}
112
113// endOnClose cancels the context when the connection closes
divyadesaif117fc22019-11-04 06:32:01 +0000114func ConnectionActiveContext(conn *grpc.ClientConn, p *probe.Probe) context.Context {
Scott Bakere702d122019-10-22 11:54:12 -0700115 ctx, disconnected := context.WithCancel(context.Background())
116 go func() {
117 for state := conn.GetState(); state != connectivity.TransientFailure && state != connectivity.Shutdown; state = conn.GetState() {
118 if !conn.WaitForStateChange(context.Background(), state) {
119 break
120 }
121 }
122 log.Infof("Connection to afrouter lost")
divyadesaif117fc22019-11-04 06:32:01 +0000123 p.UpdateStatus("affinity-router", probe.ServiceStatusStopped)
Scott Bakere702d122019-10-22 11:54:12 -0700124 disconnected()
125 }()
126 return ctx
127}