blob: 5db407890b505a1f2fe3e9d1f20f165e914e25fb [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"
William Kurkiandaa6bb22019-03-07 12:26:28 -050026 ic "github.com/opencord/voltha-protos/go/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
khenaidooca301322019-01-09 23:06:32 -050065func newKafkaClient(clientType string, host string, port int, instanceID string) (kafka.Client, error) {
khenaidoo43c82122018-11-22 18:38:28 -050066
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),
khenaidooca301322019-01-09 23:06:32 -050073 kafka.ConsumerType(kafka.GroupCustomer),
khenaidoo90847922018-12-03 14:47:51 -050074 kafka.ProducerReturnOnErrors(true),
75 kafka.ProducerReturnOnSuccess(true),
76 kafka.ProducerMaxRetries(6),
khenaidooca301322019-01-09 23:06:32 -050077 kafka.NumPartitions(3),
78 kafka.ConsumerGroupName(instanceID),
79 kafka.ConsumerGroupPrefix(instanceID),
khenaidoo54e0ddf2019-02-27 16:21:33 -050080 kafka.AutoCreateTopic(true),
khenaidooca301322019-01-09 23:06:32 -050081 kafka.ProducerFlushFrequency(5),
khenaidoo79232702018-12-04 11:00:41 -050082 kafka.ProducerRetryBackoff(time.Millisecond*30)), nil
khenaidoo43c82122018-11-22 18:38:28 -050083 }
84 return nil, errors.New("unsupported-client-type")
85}
86
khenaidoocfee5f42018-07-19 22:47:38 -040087func newRWCore(cf *config.RWCoreFlags) *rwCore {
88 var rwCore rwCore
89 rwCore.config = cf
90 rwCore.halted = false
91 rwCore.exitChannel = make(chan int, 1)
khenaidoo79232702018-12-04 11:00:41 -050092 rwCore.receiverChannels = make([]<-chan *ic.InterContainerMessage, 0)
khenaidoocfee5f42018-07-19 22:47:38 -040093 return &rwCore
94}
95
khenaidoob9203542018-09-17 22:56:37 -040096func (rw *rwCore) setKVClient() error {
97 addr := rw.config.KVStoreHost + ":" + strconv.Itoa(rw.config.KVStorePort)
98 client, err := newKVClient(rw.config.KVStoreType, addr, rw.config.KVStoreTimeout)
khenaidoocfee5f42018-07-19 22:47:38 -040099 if err != nil {
Richard Jankowskie4d77662018-10-17 13:53:21 -0400100 rw.kvClient = nil
khenaidoocfee5f42018-07-19 22:47:38 -0400101 log.Error(err)
102 return err
103 }
khenaidoob9203542018-09-17 22:56:37 -0400104 rw.kvClient = client
khenaidoocfee5f42018-07-19 22:47:38 -0400105 return nil
106}
107
khenaidoocfee5f42018-07-19 22:47:38 -0400108func toString(value interface{}) (string, error) {
109 switch t := value.(type) {
110 case []byte:
111 return string(value.([]byte)), nil
112 case string:
113 return value.(string), nil
114 default:
115 return "", fmt.Errorf("unexpected-type-%T", t)
116 }
117}
118
khenaidoob9203542018-09-17 22:56:37 -0400119func (rw *rwCore) start(ctx context.Context) {
khenaidoo5c11af72018-07-20 17:21:05 -0400120 log.Info("Starting RW Core components")
khenaidoob9203542018-09-17 22:56:37 -0400121
khenaidoo90847922018-12-03 14:47:51 -0500122 // Setup KV Client
Richard Jankowskie4d77662018-10-17 13:53:21 -0400123 log.Debugw("create-kv-client", log.Fields{"kvstore": rw.config.KVStoreType})
124 err := rw.setKVClient()
125 if err == nil {
126 // Setup KV transaction context
khenaidoo9cdc1a62019-01-24 21:57:40 -0500127 txnPrefix := rw.config.KVStoreDataPrefix + "/transactions/"
128 if err = c.SetTransactionContext(rw.config.InstanceID,
129 txnPrefix,
Richard Jankowskie4d77662018-10-17 13:53:21 -0400130 rw.kvClient,
131 rw.config.KVStoreTimeout,
Richard Jankowski199fd862019-03-18 14:49:51 -0400132 rw.config.KVTxnKeyDelTime,
khenaidoo1ce37ad2019-03-24 22:07:24 -0400133 1); err != nil {
khenaidoo9cdc1a62019-01-24 21:57:40 -0500134 log.Fatal("creating-transaction-context-failed")
135 }
Richard Jankowskie4d77662018-10-17 13:53:21 -0400136 }
137
khenaidoo90847922018-12-03 14:47:51 -0500138 // Setup Kafka Client
khenaidooca301322019-01-09 23:06:32 -0500139 if rw.kafkaClient, err = newKafkaClient("sarama", rw.config.KafkaAdapterHost, rw.config.KafkaAdapterPort, rw.config.InstanceID); err != nil {
khenaidoo43c82122018-11-22 18:38:28 -0500140 log.Fatal("Unsupported-kafka-client")
141 }
142
khenaidoob9203542018-09-17 22:56:37 -0400143 // Create the core service
khenaidoo43c82122018-11-22 18:38:28 -0500144 rw.core = c.NewCore(rw.config.InstanceID, rw.config, rw.kvClient, rw.kafkaClient)
khenaidoob9203542018-09-17 22:56:37 -0400145
146 // start the core
147 rw.core.Start(ctx)
khenaidoocfee5f42018-07-19 22:47:38 -0400148}
149
khenaidoob9203542018-09-17 22:56:37 -0400150func (rw *rwCore) stop() {
khenaidoocfee5f42018-07-19 22:47:38 -0400151 // Stop leadership tracking
khenaidoob9203542018-09-17 22:56:37 -0400152 rw.halted = true
khenaidoocfee5f42018-07-19 22:47:38 -0400153
154 // send exit signal
khenaidoob9203542018-09-17 22:56:37 -0400155 rw.exitChannel <- 0
khenaidoocfee5f42018-07-19 22:47:38 -0400156
157 // Cleanup - applies only if we had a kvClient
khenaidoob9203542018-09-17 22:56:37 -0400158 if rw.kvClient != nil {
khenaidoocfee5f42018-07-19 22:47:38 -0400159 // Release all reservations
khenaidoob9203542018-09-17 22:56:37 -0400160 if err := rw.kvClient.ReleaseAllReservations(); err != nil {
khenaidoo5c11af72018-07-20 17:21:05 -0400161 log.Infow("fail-to-release-all-reservations", log.Fields{"error": err})
khenaidoocfee5f42018-07-19 22:47:38 -0400162 }
163 // Close the DB connection
khenaidoob9203542018-09-17 22:56:37 -0400164 rw.kvClient.Close()
khenaidoocfee5f42018-07-19 22:47:38 -0400165 }
khenaidoo43c82122018-11-22 18:38:28 -0500166
167 rw.core.Stop(nil)
168
169 //if rw.kafkaClient != nil {
170 // rw.kafkaClient.Stop()
171 //}
khenaidoocfee5f42018-07-19 22:47:38 -0400172}
173
174func waitForExit() int {
175 signalChannel := make(chan os.Signal, 1)
176 signal.Notify(signalChannel,
177 syscall.SIGHUP,
178 syscall.SIGINT,
179 syscall.SIGTERM,
180 syscall.SIGQUIT)
181
182 exitChannel := make(chan int)
183
184 go func() {
185 s := <-signalChannel
186 switch s {
187 case syscall.SIGHUP,
188 syscall.SIGINT,
189 syscall.SIGTERM,
190 syscall.SIGQUIT:
khenaidoo5c11af72018-07-20 17:21:05 -0400191 log.Infow("closing-signal-received", log.Fields{"signal": s})
khenaidoocfee5f42018-07-19 22:47:38 -0400192 exitChannel <- 0
193 default:
khenaidoo5c11af72018-07-20 17:21:05 -0400194 log.Infow("unexpected-signal-received", log.Fields{"signal": s})
khenaidoocfee5f42018-07-19 22:47:38 -0400195 exitChannel <- 1
196 }
197 }()
198
199 code := <-exitChannel
200 return code
201}
202
khenaidoo5c11af72018-07-20 17:21:05 -0400203func printBanner() {
204 fmt.Println(" ")
205 fmt.Println(" ______ ______ ")
206 fmt.Println("| _ \\ \\ / / ___|___ _ __ ___ ")
207 fmt.Println("| |_) \\ \\ /\\ / / | / _ \\| '__/ _ \\ ")
208 fmt.Println("| _ < \\ V V /| |__| (_) | | | __/ ")
209 fmt.Println("|_| \\_\\ \\_/\\_/ \\____\\___/|_| \\___| ")
210 fmt.Println(" ")
211}
212
khenaidoocfee5f42018-07-19 22:47:38 -0400213func main() {
214 start := time.Now()
215
216 cf := config.NewRWCoreFlags()
217 cf.ParseCommandArguments()
218
khenaidoob9203542018-09-17 22:56:37 -0400219 //// Setup logging
220
221 //Setup default logger - applies for packages that do not have specific logger set
222 if _, err := log.SetDefaultLogger(log.JSON, cf.LogLevel, log.Fields{"instanceId": cf.InstanceID}); err != nil {
khenaidoocfee5f42018-07-19 22:47:38 -0400223 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
224 }
khenaidoob9203542018-09-17 22:56:37 -0400225
226 // Update all loggers (provisionned via init) with a common field
227 if err := log.UpdateAllLoggers(log.Fields{"instanceId": cf.InstanceID}); err != nil {
228 log.With(log.Fields{"error": err}).Fatal("Cannot setup logging")
229 }
230
231 log.SetPackageLogLevel("github.com/opencord/voltha-go/rw_core/core", log.DebugLevel)
khenaidoo1ce37ad2019-03-24 22:07:24 -0400232 //log.SetPackageLogLevel("github.com/opencord/voltha-go/rw_core/flow_decomposition", log.DebugLevel)
233 //log.SetPackageLogLevel("github.com/opencord/voltha-go/rw_core/graph", log.DebugLevel)
234 //log.SetPackageLogLevel("github.com/opencord/voltha-go/kafka", log.DebugLevel)
235 //log.SetPackageLogLevel("github.com/opencord/voltha-go/db/model", log.DebugLevel)
khenaidoob9203542018-09-17 22:56:37 -0400236
khenaidoocfee5f42018-07-19 22:47:38 -0400237 defer log.CleanUp()
238
khenaidoo5c11af72018-07-20 17:21:05 -0400239 // Print banner if specified
240 if cf.Banner {
241 printBanner()
242 }
243
244 log.Infow("rw-core-config", log.Fields{"config": *cf})
245
246 ctx, cancel := context.WithCancel(context.Background())
247 defer cancel()
khenaidoocfee5f42018-07-19 22:47:38 -0400248
khenaidoob9203542018-09-17 22:56:37 -0400249 rw := newRWCore(cf)
250 go rw.start(ctx)
khenaidoocfee5f42018-07-19 22:47:38 -0400251
252 code := waitForExit()
khenaidoo5c11af72018-07-20 17:21:05 -0400253 log.Infow("received-a-closing-signal", log.Fields{"code": code})
khenaidoocfee5f42018-07-19 22:47:38 -0400254
255 // Cleanup before leaving
khenaidoob9203542018-09-17 22:56:37 -0400256 rw.stop()
khenaidoocfee5f42018-07-19 22:47:38 -0400257
258 elapsed := time.Since(start)
khenaidoob9203542018-09-17 22:56:37 -0400259 log.Infow("rw-core-run-time", log.Fields{"core": rw.config.InstanceID, "time": elapsed / time.Second})
khenaidoocfee5f42018-07-19 22:47:38 -0400260}