blob: dbb82b0f120f8afefe261a81e388d0a877dfbcbe [file] [log] [blame]
khenaidoobf6e7bb2018-08-14 22:27:29 -04001/*
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 */
khenaidoocfee5f42018-07-19 22:47:38 -040016package main
17
18import (
khenaidoo5c11af72018-07-20 17:21:05 -040019 "context"
20 "errors"
khenaidoocfee5f42018-07-19 22:47:38 -040021 "fmt"
khenaidoobf6e7bb2018-08-14 22:27:29 -040022 grpcserver "github.com/opencord/voltha-go/common/grpc"
khenaidoo5c11af72018-07-20 17:21:05 -040023 "github.com/opencord/voltha-go/common/log"
24 "github.com/opencord/voltha-go/db/kvstore"
khenaidoo43c82122018-11-22 18:38:28 -050025 "github.com/opencord/voltha-go/kafka"
khenaidoo79232702018-12-04 11:00:41 -050026 ic "github.com/opencord/voltha-go/protos/inter_container"
khenaidoo5c11af72018-07-20 17:21:05 -040027 "github.com/opencord/voltha-go/rw_core/config"
khenaidoob9203542018-09-17 22:56:37 -040028 c "github.com/opencord/voltha-go/rw_core/core"
khenaidoocfee5f42018-07-19 22:47:38 -040029 "os"
30 "os/signal"
khenaidoocfee5f42018-07-19 22:47:38 -040031 "strconv"
khenaidoocfee5f42018-07-19 22:47:38 -040032 "syscall"
khenaidoo5c11af72018-07-20 17:21:05 -040033 "time"
khenaidoocfee5f42018-07-19 22:47:38 -040034)
35
36type rwCore struct {
khenaidoo5c11af72018-07-20 17:21:05 -040037 kvClient kvstore.Client
38 config *config.RWCoreFlags
39 halted bool
40 exitChannel chan int
khenaidoob9203542018-09-17 22:56:37 -040041 //kmp *kafka.KafkaMessagingProxy
khenaidoo43c82122018-11-22 18:38:28 -050042 grpcServer *grpcserver.GrpcServer
43 kafkaClient kafka.Client
44 core *c.Core
khenaidooabad44c2018-08-03 16:58:35 -040045 //For test
khenaidoo79232702018-12-04 11:00:41 -050046 receiverChannels []<-chan *ic.InterContainerMessage
khenaidoocfee5f42018-07-19 22:47:38 -040047}
48
khenaidoob9203542018-09-17 22:56:37 -040049func init() {
khenaidoo2c6f1672018-09-20 23:14:41 -040050 log.AddPackage(log.JSON, log.DebugLevel, nil)
khenaidoob9203542018-09-17 22:56:37 -040051}
52
khenaidoocfee5f42018-07-19 22:47:38 -040053func newKVClient(storeType string, address string, timeout int) (kvstore.Client, error) {
54
khenaidoo5c11af72018-07-20 17:21:05 -040055 log.Infow("kv-store-type", log.Fields{"store": storeType})
khenaidoocfee5f42018-07-19 22:47:38 -040056 switch storeType {
57 case "consul":
58 return kvstore.NewConsulClient(address, timeout)
59 case "etcd":
60 return kvstore.NewEtcdClient(address, timeout)
61 }
62 return nil, errors.New("unsupported-kv-store")
63}
64
khenaidoo43c82122018-11-22 18:38:28 -050065func newKafkaClient(clientType string, host string, port int) (kafka.Client, error) {
66
67 log.Infow("kafka-client-type", log.Fields{"client": clientType})
68 switch clientType {
69 case "sarama":
70 return kafka.NewSaramaClient(
71 kafka.Host(host),
khenaidoo90847922018-12-03 14:47:51 -050072 kafka.Port(port),
73 kafka.ProducerReturnOnErrors(true),
74 kafka.ProducerReturnOnSuccess(true),
75 kafka.ProducerMaxRetries(6),
khenaidoo79232702018-12-04 11:00:41 -050076 kafka.ProducerRetryBackoff(time.Millisecond*30)), nil
khenaidoo43c82122018-11-22 18:38:28 -050077 }
78 return nil, errors.New("unsupported-client-type")
79}
80
khenaidoocfee5f42018-07-19 22:47:38 -040081func newRWCore(cf *config.RWCoreFlags) *rwCore {
82 var rwCore rwCore
83 rwCore.config = cf
84 rwCore.halted = false
85 rwCore.exitChannel = make(chan int, 1)
khenaidoo79232702018-12-04 11:00:41 -050086 rwCore.receiverChannels = make([]<-chan *ic.InterContainerMessage, 0)
khenaidoocfee5f42018-07-19 22:47:38 -040087 return &rwCore
88}
89
khenaidoob9203542018-09-17 22:56:37 -040090func (rw *rwCore) setKVClient() error {
91 addr := rw.config.KVStoreHost + ":" + strconv.Itoa(rw.config.KVStorePort)
92 client, err := newKVClient(rw.config.KVStoreType, addr, rw.config.KVStoreTimeout)
khenaidoocfee5f42018-07-19 22:47:38 -040093 if err != nil {
Richard Jankowskie4d77662018-10-17 13:53:21 -040094 rw.kvClient = nil
khenaidoocfee5f42018-07-19 22:47:38 -040095 log.Error(err)
96 return err
97 }
khenaidoob9203542018-09-17 22:56:37 -040098 rw.kvClient = client
khenaidoocfee5f42018-07-19 22:47:38 -040099 return nil
100}
101
khenaidoocfee5f42018-07-19 22:47:38 -0400102func toString(value interface{}) (string, error) {
103 switch t := value.(type) {
104 case []byte:
105 return string(value.([]byte)), nil
106 case string:
107 return value.(string), nil
108 default:
109 return "", fmt.Errorf("unexpected-type-%T", t)
110 }
111}
112
khenaidoob9203542018-09-17 22:56:37 -0400113func (rw *rwCore) start(ctx context.Context) {
khenaidoo5c11af72018-07-20 17:21:05 -0400114 log.Info("Starting RW Core components")
khenaidoob9203542018-09-17 22:56:37 -0400115
khenaidoo90847922018-12-03 14:47:51 -0500116 // Setup KV Client
Richard Jankowskie4d77662018-10-17 13:53:21 -0400117 log.Debugw("create-kv-client", log.Fields{"kvstore": rw.config.KVStoreType})
118 err := rw.setKVClient()
119 if err == nil {
120 // Setup KV transaction context
121 c.SetTransactionContext(rw.config.InstanceID,
122 "service/voltha/transactions/",
123 rw.kvClient,
124 rw.config.KVStoreTimeout,
125 rw.config.KVTxnKeyDelTime)
126 }
127
khenaidoo90847922018-12-03 14:47:51 -0500128 // Setup Kafka Client
khenaidoo43c82122018-11-22 18:38:28 -0500129 if rw.kafkaClient, err = newKafkaClient("sarama", rw.config.KafkaAdapterHost, rw.config.KafkaAdapterPort); err != nil {
130 log.Fatal("Unsupported-kafka-client")
131 }
132
khenaidoob9203542018-09-17 22:56:37 -0400133 // Create the core service
khenaidoo43c82122018-11-22 18:38:28 -0500134 rw.core = c.NewCore(rw.config.InstanceID, rw.config, rw.kvClient, rw.kafkaClient)
khenaidoob9203542018-09-17 22:56:37 -0400135
136 // start the core
137 rw.core.Start(ctx)
khenaidoocfee5f42018-07-19 22:47:38 -0400138}
139
khenaidoob9203542018-09-17 22:56:37 -0400140func (rw *rwCore) stop() {
khenaidoocfee5f42018-07-19 22:47:38 -0400141 // Stop leadership tracking
khenaidoob9203542018-09-17 22:56:37 -0400142 rw.halted = true
khenaidoocfee5f42018-07-19 22:47:38 -0400143
144 // send exit signal
khenaidoob9203542018-09-17 22:56:37 -0400145 rw.exitChannel <- 0
khenaidoocfee5f42018-07-19 22:47:38 -0400146
147 // Cleanup - applies only if we had a kvClient
khenaidoob9203542018-09-17 22:56:37 -0400148 if rw.kvClient != nil {
khenaidoocfee5f42018-07-19 22:47:38 -0400149 // Release all reservations
khenaidoob9203542018-09-17 22:56:37 -0400150 if err := rw.kvClient.ReleaseAllReservations(); err != nil {
khenaidoo5c11af72018-07-20 17:21:05 -0400151 log.Infow("fail-to-release-all-reservations", log.Fields{"error": err})
khenaidoocfee5f42018-07-19 22:47:38 -0400152 }
153 // Close the DB connection
khenaidoob9203542018-09-17 22:56:37 -0400154 rw.kvClient.Close()
khenaidoocfee5f42018-07-19 22:47:38 -0400155 }
khenaidoo43c82122018-11-22 18:38:28 -0500156
157 rw.core.Stop(nil)
158
159 //if rw.kafkaClient != nil {
160 // rw.kafkaClient.Stop()
161 //}
khenaidoocfee5f42018-07-19 22:47:38 -0400162}
163
164func waitForExit() int {
165 signalChannel := make(chan os.Signal, 1)
166 signal.Notify(signalChannel,
167 syscall.SIGHUP,
168 syscall.SIGINT,
169 syscall.SIGTERM,
170 syscall.SIGQUIT)
171
172 exitChannel := make(chan int)
173
174 go func() {
175 s := <-signalChannel
176 switch s {
177 case syscall.SIGHUP,
178 syscall.SIGINT,
179 syscall.SIGTERM,
180 syscall.SIGQUIT:
khenaidoo5c11af72018-07-20 17:21:05 -0400181 log.Infow("closing-signal-received", log.Fields{"signal": s})
khenaidoocfee5f42018-07-19 22:47:38 -0400182 exitChannel <- 0
183 default:
khenaidoo5c11af72018-07-20 17:21:05 -0400184 log.Infow("unexpected-signal-received", log.Fields{"signal": s})
khenaidoocfee5f42018-07-19 22:47:38 -0400185 exitChannel <- 1
186 }
187 }()
188
189 code := <-exitChannel
190 return code
191}
192
khenaidoo5c11af72018-07-20 17:21:05 -0400193func printBanner() {
194 fmt.Println(" ")
195 fmt.Println(" ______ ______ ")
196 fmt.Println("| _ \\ \\ / / ___|___ _ __ ___ ")
197 fmt.Println("| |_) \\ \\ /\\ / / | / _ \\| '__/ _ \\ ")
198 fmt.Println("| _ < \\ V V /| |__| (_) | | | __/ ")
199 fmt.Println("|_| \\_\\ \\_/\\_/ \\____\\___/|_| \\___| ")
200 fmt.Println(" ")
201}
202
khenaidoocfee5f42018-07-19 22:47:38 -0400203func main() {
204 start := time.Now()
205
206 cf := config.NewRWCoreFlags()
207 cf.ParseCommandArguments()
208
khenaidoob9203542018-09-17 22:56:37 -0400209 //// Setup logging
210
211 //Setup default logger - applies for packages that do not have specific logger set
212 if _, err := log.SetDefaultLogger(log.JSON, cf.LogLevel, log.Fields{"instanceId": cf.InstanceID}); err != nil {
khenaidoocfee5f42018-07-19 22:47:38 -0400213 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
214 }
khenaidoob9203542018-09-17 22:56:37 -0400215
216 // Update all loggers (provisionned via init) with a common field
217 if err := log.UpdateAllLoggers(log.Fields{"instanceId": cf.InstanceID}); err != nil {
218 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
219 }
220
221 log.SetPackageLogLevel("github.com/opencord/voltha-go/rw_core/core", log.DebugLevel)
khenaidoo90847922018-12-03 14:47:51 -0500222 log.SetPackageLogLevel("github.com/opencord/voltha-go/kafka", log.DebugLevel)
khenaidoob9203542018-09-17 22:56:37 -0400223
khenaidoocfee5f42018-07-19 22:47:38 -0400224 defer log.CleanUp()
225
khenaidoo5c11af72018-07-20 17:21:05 -0400226 // Print banner if specified
227 if cf.Banner {
228 printBanner()
229 }
230
231 log.Infow("rw-core-config", log.Fields{"config": *cf})
232
233 ctx, cancel := context.WithCancel(context.Background())
234 defer cancel()
khenaidoocfee5f42018-07-19 22:47:38 -0400235
khenaidoob9203542018-09-17 22:56:37 -0400236 rw := newRWCore(cf)
237 go rw.start(ctx)
khenaidoocfee5f42018-07-19 22:47:38 -0400238
239 code := waitForExit()
khenaidoo5c11af72018-07-20 17:21:05 -0400240 log.Infow("received-a-closing-signal", log.Fields{"code": code})
khenaidoocfee5f42018-07-19 22:47:38 -0400241
242 // Cleanup before leaving
khenaidoob9203542018-09-17 22:56:37 -0400243 rw.stop()
khenaidoocfee5f42018-07-19 22:47:38 -0400244
245 elapsed := time.Since(start)
khenaidoob9203542018-09-17 22:56:37 -0400246 log.Infow("rw-core-run-time", log.Fields{"core": rw.config.InstanceID, "time": elapsed / time.Second})
khenaidoocfee5f42018-07-19 22:47:38 -0400247}