khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 1 | /* |
| 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 | */ |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 16 | package main |
| 17 | |
| 18 | import ( |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 19 | "context" |
| 20 | "errors" |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 21 | "fmt" |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 22 | grpcserver "github.com/opencord/voltha-go/common/grpc" |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 23 | "github.com/opencord/voltha-go/common/log" |
| 24 | "github.com/opencord/voltha-go/db/kvstore" |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 25 | ca "github.com/opencord/voltha-go/protos/core_adapter" |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 26 | "github.com/opencord/voltha-go/rw_core/config" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 27 | c "github.com/opencord/voltha-go/rw_core/core" |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 28 | "os" |
| 29 | "os/signal" |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 30 | "strconv" |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 31 | "syscall" |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 32 | "time" |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 33 | ) |
| 34 | |
| 35 | type rwCore struct { |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 36 | kvClient kvstore.Client |
| 37 | config *config.RWCoreFlags |
| 38 | halted bool |
| 39 | exitChannel chan int |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 40 | //kmp *kafka.KafkaMessagingProxy |
| 41 | grpcServer *grpcserver.GrpcServer |
| 42 | core *c.Core |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 43 | //For test |
| 44 | receiverChannels []<-chan *ca.InterContainerMessage |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 45 | } |
| 46 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 47 | func init() { |
khenaidoo | 2c6f167 | 2018-09-20 23:14:41 -0400 | [diff] [blame] | 48 | log.AddPackage(log.JSON, log.DebugLevel, nil) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 49 | } |
| 50 | |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 51 | func newKVClient(storeType string, address string, timeout int) (kvstore.Client, error) { |
| 52 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 53 | log.Infow("kv-store-type", log.Fields{"store": storeType}) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 54 | switch storeType { |
| 55 | case "consul": |
| 56 | return kvstore.NewConsulClient(address, timeout) |
| 57 | case "etcd": |
| 58 | return kvstore.NewEtcdClient(address, timeout) |
| 59 | } |
| 60 | return nil, errors.New("unsupported-kv-store") |
| 61 | } |
| 62 | |
| 63 | func newRWCore(cf *config.RWCoreFlags) *rwCore { |
| 64 | var rwCore rwCore |
| 65 | rwCore.config = cf |
| 66 | rwCore.halted = false |
| 67 | rwCore.exitChannel = make(chan int, 1) |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 68 | rwCore.receiverChannels = make([]<-chan *ca.InterContainerMessage, 0) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 69 | return &rwCore |
| 70 | } |
| 71 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 72 | func (rw *rwCore) setKVClient() error { |
| 73 | addr := rw.config.KVStoreHost + ":" + strconv.Itoa(rw.config.KVStorePort) |
| 74 | client, err := newKVClient(rw.config.KVStoreType, addr, rw.config.KVStoreTimeout) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 75 | if err != nil { |
Richard Jankowski | e4d7766 | 2018-10-17 13:53:21 -0400 | [diff] [blame] | 76 | rw.kvClient = nil |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 77 | log.Error(err) |
| 78 | return err |
| 79 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 80 | rw.kvClient = client |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 81 | return nil |
| 82 | } |
| 83 | |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 84 | func toString(value interface{}) (string, error) { |
| 85 | switch t := value.(type) { |
| 86 | case []byte: |
| 87 | return string(value.([]byte)), nil |
| 88 | case string: |
| 89 | return value.(string), nil |
| 90 | default: |
| 91 | return "", fmt.Errorf("unexpected-type-%T", t) |
| 92 | } |
| 93 | } |
| 94 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 95 | //func (rw *rwCore) createGRPCService(context.Context) { |
| 96 | // // create an insecure gserver server |
| 97 | // rw.grpcServer = grpcserver.NewGrpcServer(rw.config.GrpcHost, rw.config.GrpcPort, nil, false) |
| 98 | // log.Info("grpc-server-created") |
| 99 | //} |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 100 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 101 | //func (rw *rwCore) startKafkaMessagingProxy(ctx context.Context) error { |
| 102 | // log.Infow("starting-kafka-messaging-proxy", log.Fields{"host":rw.config.KafkaAdapterHost, |
| 103 | // "port":rw.config.KafkaAdapterPort, "topic":rw.config.CoreTopic}) |
| 104 | // var err error |
| 105 | // if rw.kmp, err = kafka.NewKafkaMessagingProxy( |
| 106 | // kafka.KafkaHost(rw.config.KafkaAdapterHost), |
| 107 | // kafka.KafkaPort(rw.config.KafkaAdapterPort), |
| 108 | // kafka.DefaultTopic(&kafka.Topic{Name: rw.config.CoreTopic})); err != nil { |
| 109 | // log.Errorw("fail-to-create-kafka-proxy", log.Fields{"error": err}) |
| 110 | // return err |
| 111 | // } |
| 112 | // if err = rw.kmp.Start(); err != nil { |
| 113 | // log.Fatalw("error-starting-messaging-proxy", log.Fields{"error": err}) |
| 114 | // return err |
| 115 | // } |
| 116 | // |
| 117 | // requestProxy := &c.RequestHandlerProxy{} |
| 118 | // rw.kmp.SubscribeWithTarget(kafka.Topic{Name: rw.config.CoreTopic}, requestProxy) |
| 119 | // |
| 120 | // log.Info("started-kafka-messaging-proxy") |
| 121 | // return nil |
| 122 | //} |
khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -0400 | [diff] [blame] | 123 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 124 | func (rw *rwCore) start(ctx context.Context) { |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 125 | log.Info("Starting RW Core components") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 126 | |
| 127 | //// Setup GRPC Server |
| 128 | //rw.createGRPCService(ctx) |
| 129 | |
| 130 | //// Setup Kafka messaging services |
| 131 | //if err := rw.startKafkaMessagingProxy(ctx); err != nil { |
| 132 | // log.Fatalw("failed-to-start-kafka-proxy", log.Fields{"err":err}) |
| 133 | //} |
| 134 | |
Richard Jankowski | e4d7766 | 2018-10-17 13:53:21 -0400 | [diff] [blame] | 135 | // Setup KV Client |
| 136 | log.Debugw("create-kv-client", log.Fields{"kvstore": rw.config.KVStoreType}) |
| 137 | err := rw.setKVClient() |
| 138 | if err == nil { |
| 139 | // Setup KV transaction context |
| 140 | c.SetTransactionContext(rw.config.InstanceID, |
| 141 | "service/voltha/transactions/", |
| 142 | rw.kvClient, |
| 143 | rw.config.KVStoreTimeout, |
| 144 | rw.config.KVTxnKeyDelTime) |
| 145 | } |
| 146 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 147 | // Create the core service |
Richard Jankowski | e4d7766 | 2018-10-17 13:53:21 -0400 | [diff] [blame] | 148 | rw.core = c.NewCore(rw.config.InstanceID, rw.config, rw.kvClient) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 149 | |
| 150 | // start the core |
| 151 | rw.core.Start(ctx) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 152 | } |
| 153 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 154 | func (rw *rwCore) stop() { |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 155 | // Stop leadership tracking |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 156 | rw.halted = true |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 157 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 158 | //// Stop the Kafka messaging service |
| 159 | //if rw.kmp != nil { |
| 160 | // rw.kmp.Stop() |
| 161 | //} |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 162 | |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 163 | // send exit signal |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 164 | rw.exitChannel <- 0 |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 165 | |
| 166 | // Cleanup - applies only if we had a kvClient |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 167 | if rw.kvClient != nil { |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 168 | // Release all reservations |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 169 | if err := rw.kvClient.ReleaseAllReservations(); err != nil { |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 170 | log.Infow("fail-to-release-all-reservations", log.Fields{"error": err}) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 171 | } |
| 172 | // Close the DB connection |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 173 | rw.kvClient.Close() |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
| 177 | func waitForExit() int { |
| 178 | signalChannel := make(chan os.Signal, 1) |
| 179 | signal.Notify(signalChannel, |
| 180 | syscall.SIGHUP, |
| 181 | syscall.SIGINT, |
| 182 | syscall.SIGTERM, |
| 183 | syscall.SIGQUIT) |
| 184 | |
| 185 | exitChannel := make(chan int) |
| 186 | |
| 187 | go func() { |
| 188 | s := <-signalChannel |
| 189 | switch s { |
| 190 | case syscall.SIGHUP, |
| 191 | syscall.SIGINT, |
| 192 | syscall.SIGTERM, |
| 193 | syscall.SIGQUIT: |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 194 | log.Infow("closing-signal-received", log.Fields{"signal": s}) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 195 | exitChannel <- 0 |
| 196 | default: |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 197 | log.Infow("unexpected-signal-received", log.Fields{"signal": s}) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 198 | exitChannel <- 1 |
| 199 | } |
| 200 | }() |
| 201 | |
| 202 | code := <-exitChannel |
| 203 | return code |
| 204 | } |
| 205 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 206 | func printBanner() { |
| 207 | fmt.Println(" ") |
| 208 | fmt.Println(" ______ ______ ") |
| 209 | fmt.Println("| _ \\ \\ / / ___|___ _ __ ___ ") |
| 210 | fmt.Println("| |_) \\ \\ /\\ / / | / _ \\| '__/ _ \\ ") |
| 211 | fmt.Println("| _ < \\ V V /| |__| (_) | | | __/ ") |
| 212 | fmt.Println("|_| \\_\\ \\_/\\_/ \\____\\___/|_| \\___| ") |
| 213 | fmt.Println(" ") |
| 214 | } |
| 215 | |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 216 | func main() { |
| 217 | start := time.Now() |
| 218 | |
| 219 | cf := config.NewRWCoreFlags() |
| 220 | cf.ParseCommandArguments() |
| 221 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 222 | //// Setup logging |
| 223 | |
| 224 | //Setup default logger - applies for packages that do not have specific logger set |
| 225 | if _, err := log.SetDefaultLogger(log.JSON, cf.LogLevel, log.Fields{"instanceId": cf.InstanceID}); err != nil { |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 226 | log.With(log.Fields{"error": err}).Fatal("Cannot setup logging") |
| 227 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 228 | |
| 229 | // Update all loggers (provisionned via init) with a common field |
| 230 | if err := log.UpdateAllLoggers(log.Fields{"instanceId": cf.InstanceID}); err != nil { |
| 231 | log.With(log.Fields{"error": err}).Fatal("Cannot setup logging") |
| 232 | } |
| 233 | |
| 234 | log.SetPackageLogLevel("github.com/opencord/voltha-go/rw_core/core", log.DebugLevel) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 235 | log.SetPackageLogLevel("github.com/opencord/voltha-go/kafka", log.WarnLevel) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 236 | |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 237 | defer log.CleanUp() |
| 238 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 239 | // 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() |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 248 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 249 | rw := newRWCore(cf) |
| 250 | go rw.start(ctx) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 251 | |
| 252 | code := waitForExit() |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 253 | log.Infow("received-a-closing-signal", log.Fields{"code": code}) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 254 | |
| 255 | // Cleanup before leaving |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 256 | rw.stop() |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 257 | |
| 258 | elapsed := time.Since(start) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 259 | log.Infow("rw-core-run-time", log.Fields{"core": rw.config.InstanceID, "time": elapsed / time.Second}) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 260 | } |