khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 4 | "context" |
| 5 | "errors" |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 6 | "fmt" |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 7 | "github.com/opencord/voltha-go/common/log" |
| 8 | "github.com/opencord/voltha-go/db/kvstore" |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 9 | "github.com/opencord/voltha-go/kafka" |
| 10 | ca "github.com/opencord/voltha-go/protos/core_adapter" |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 11 | "github.com/opencord/voltha-go/rw_core/config" |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 12 | "os" |
| 13 | "os/signal" |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 14 | "strconv" |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 15 | "syscall" |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 16 | "time" |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 17 | ) |
| 18 | |
| 19 | type rwCore struct { |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 20 | kvClient kvstore.Client |
| 21 | config *config.RWCoreFlags |
| 22 | halted bool |
| 23 | exitChannel chan int |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 24 | kmp *kafka.KafkaMessagingProxy |
| 25 | //For test |
| 26 | receiverChannels []<-chan *ca.InterContainerMessage |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | func newKVClient(storeType string, address string, timeout int) (kvstore.Client, error) { |
| 30 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 31 | log.Infow("kv-store-type", log.Fields{"store": storeType}) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 32 | switch storeType { |
| 33 | case "consul": |
| 34 | return kvstore.NewConsulClient(address, timeout) |
| 35 | case "etcd": |
| 36 | return kvstore.NewEtcdClient(address, timeout) |
| 37 | } |
| 38 | return nil, errors.New("unsupported-kv-store") |
| 39 | } |
| 40 | |
| 41 | func newRWCore(cf *config.RWCoreFlags) *rwCore { |
| 42 | var rwCore rwCore |
| 43 | rwCore.config = cf |
| 44 | rwCore.halted = false |
| 45 | rwCore.exitChannel = make(chan int, 1) |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 46 | rwCore.receiverChannels = make([]<-chan *ca.InterContainerMessage, 0) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 47 | return &rwCore |
| 48 | } |
| 49 | |
| 50 | func (core *rwCore) setKVClient() error { |
| 51 | addr := core.config.KVStoreHost + ":" + strconv.Itoa(core.config.KVStorePort) |
| 52 | client, err := newKVClient(core.config.KVStoreType, addr, core.config.KVStoreTimeout) |
| 53 | if err != nil { |
| 54 | log.Error(err) |
| 55 | return err |
| 56 | } |
| 57 | core.kvClient = client |
| 58 | return nil |
| 59 | } |
| 60 | |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 61 | func toString(value interface{}) (string, error) { |
| 62 | switch t := value.(type) { |
| 63 | case []byte: |
| 64 | return string(value.([]byte)), nil |
| 65 | case string: |
| 66 | return value.(string), nil |
| 67 | default: |
| 68 | return "", fmt.Errorf("unexpected-type-%T", t) |
| 69 | } |
| 70 | } |
| 71 | |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 72 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 73 | func (core *rwCore) start(ctx context.Context) { |
| 74 | log.Info("Starting RW Core components") |
| 75 | // Setup GRPC Server |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 76 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 77 | // Setup KV Client |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 78 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 79 | // Setup Kafka messaging services |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 80 | var err error |
| 81 | if core.kmp, err = kafka.NewKafkaMessagingProxy( |
| 82 | kafka.KafkaHost("10.100.198.220"), |
| 83 | kafka.KafkaPort(9092), |
| 84 | kafka.DefaultTopic(&kafka.Topic{Name: "Adapter"})); err != nil { |
| 85 | log.Errorw("fail-to-create-kafka-proxy", log.Fields{"error": err}) |
| 86 | return |
| 87 | } |
| 88 | // Start the kafka messaging service - synchronous call to ensure |
| 89 | if err = core.kmp.Start(); err != nil { |
| 90 | log.Fatalw("error-starting-messaging-proxy", log.Fields{"error": err}) |
| 91 | } |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 92 | } |
| 93 | |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 94 | func (core *rwCore) stop() { |
| 95 | // Stop leadership tracking |
| 96 | core.halted = true |
| 97 | |
khenaidoo | abad44c | 2018-08-03 16:58:35 -0400 | [diff] [blame] | 98 | // Stop the Kafka messaging service |
| 99 | if core.kmp != nil { |
| 100 | core.kmp.Stop() |
| 101 | } |
| 102 | |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 103 | // send exit signal |
| 104 | core.exitChannel <- 0 |
| 105 | |
| 106 | // Cleanup - applies only if we had a kvClient |
| 107 | if core.kvClient != nil { |
| 108 | // Release all reservations |
| 109 | if err := core.kvClient.ReleaseAllReservations(); err != nil { |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 110 | log.Infow("fail-to-release-all-reservations", log.Fields{"error": err}) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 111 | } |
| 112 | // Close the DB connection |
| 113 | core.kvClient.Close() |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | func waitForExit() int { |
| 118 | signalChannel := make(chan os.Signal, 1) |
| 119 | signal.Notify(signalChannel, |
| 120 | syscall.SIGHUP, |
| 121 | syscall.SIGINT, |
| 122 | syscall.SIGTERM, |
| 123 | syscall.SIGQUIT) |
| 124 | |
| 125 | exitChannel := make(chan int) |
| 126 | |
| 127 | go func() { |
| 128 | s := <-signalChannel |
| 129 | switch s { |
| 130 | case syscall.SIGHUP, |
| 131 | syscall.SIGINT, |
| 132 | syscall.SIGTERM, |
| 133 | syscall.SIGQUIT: |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 134 | log.Infow("closing-signal-received", log.Fields{"signal": s}) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 135 | exitChannel <- 0 |
| 136 | default: |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 137 | log.Infow("unexpected-signal-received", log.Fields{"signal": s}) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 138 | exitChannel <- 1 |
| 139 | } |
| 140 | }() |
| 141 | |
| 142 | code := <-exitChannel |
| 143 | return code |
| 144 | } |
| 145 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 146 | func printBanner() { |
| 147 | fmt.Println(" ") |
| 148 | fmt.Println(" ______ ______ ") |
| 149 | fmt.Println("| _ \\ \\ / / ___|___ _ __ ___ ") |
| 150 | fmt.Println("| |_) \\ \\ /\\ / / | / _ \\| '__/ _ \\ ") |
| 151 | fmt.Println("| _ < \\ V V /| |__| (_) | | | __/ ") |
| 152 | fmt.Println("|_| \\_\\ \\_/\\_/ \\____\\___/|_| \\___| ") |
| 153 | fmt.Println(" ") |
| 154 | } |
| 155 | |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 156 | func main() { |
| 157 | start := time.Now() |
| 158 | |
| 159 | cf := config.NewRWCoreFlags() |
| 160 | cf.ParseCommandArguments() |
| 161 | |
| 162 | // Setup logging |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 163 | if _, err := log.SetLogger(log.JSON, cf.LogLevel, log.Fields{"instanceId": cf.InstanceID}); err != nil { |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 164 | log.With(log.Fields{"error": err}).Fatal("Cannot setup logging") |
| 165 | } |
| 166 | defer log.CleanUp() |
| 167 | |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 168 | // Print banner if specified |
| 169 | if cf.Banner { |
| 170 | printBanner() |
| 171 | } |
| 172 | |
| 173 | log.Infow("rw-core-config", log.Fields{"config": *cf}) |
| 174 | |
| 175 | ctx, cancel := context.WithCancel(context.Background()) |
| 176 | defer cancel() |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 177 | |
| 178 | core := newRWCore(cf) |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 179 | go core.start(ctx) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 180 | |
| 181 | code := waitForExit() |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 182 | log.Infow("received-a-closing-signal", log.Fields{"code": code}) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 183 | |
| 184 | // Cleanup before leaving |
| 185 | core.stop() |
| 186 | |
| 187 | elapsed := time.Since(start) |
khenaidoo | 5c11af7 | 2018-07-20 17:21:05 -0400 | [diff] [blame] | 188 | log.Infow("rw-core-run-time", log.Fields{"core": core.config.InstanceID, "time": elapsed / time.Second}) |
khenaidoo | cfee5f4 | 2018-07-19 22:47:38 -0400 | [diff] [blame] | 189 | } |