Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020-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 | |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 17 | //Package main -> this is the entry point of the OpenOnuAdapter |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 18 | package main |
| 19 | |
| 20 | import ( |
| 21 | "context" |
| 22 | "errors" |
| 23 | "fmt" |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 24 | "io/ioutil" |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 25 | "os" |
| 26 | "os/signal" |
| 27 | "strconv" |
Andrea Campanella | 3d7c931 | 2021-01-19 09:20:49 +0100 | [diff] [blame] | 28 | "strings" |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 29 | "syscall" |
| 30 | "time" |
| 31 | |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 32 | "github.com/opencord/voltha-lib-go/v4/pkg/adapters" |
| 33 | "github.com/opencord/voltha-lib-go/v4/pkg/adapters/adapterif" |
| 34 | com "github.com/opencord/voltha-lib-go/v4/pkg/adapters/common" |
| 35 | conf "github.com/opencord/voltha-lib-go/v4/pkg/config" |
| 36 | "github.com/opencord/voltha-lib-go/v4/pkg/db/kvstore" |
Himani Chawla | c07fda0 | 2020-12-09 16:21:21 +0530 | [diff] [blame] | 37 | "github.com/opencord/voltha-lib-go/v4/pkg/events" |
| 38 | "github.com/opencord/voltha-lib-go/v4/pkg/events/eventif" |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 39 | "github.com/opencord/voltha-lib-go/v4/pkg/kafka" |
| 40 | "github.com/opencord/voltha-lib-go/v4/pkg/log" |
| 41 | "github.com/opencord/voltha-lib-go/v4/pkg/probe" |
| 42 | "github.com/opencord/voltha-lib-go/v4/pkg/version" |
| 43 | ic "github.com/opencord/voltha-protos/v4/go/inter_container" |
| 44 | "github.com/opencord/voltha-protos/v4/go/voltha" |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 45 | |
Matteo Scandolo | 761f751 | 2020-11-23 15:52:40 -0800 | [diff] [blame] | 46 | "github.com/opencord/voltha-openonu-adapter-go/internal/pkg/config" |
| 47 | ac "github.com/opencord/voltha-openonu-adapter-go/internal/pkg/onuadaptercore" |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 48 | ) |
| 49 | |
| 50 | type adapter struct { |
Himani Chawla | 4d90833 | 2020-08-31 12:30:20 +0530 | [diff] [blame] | 51 | //defaultAppName string |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 52 | instanceID string |
| 53 | config *config.AdapterFlags |
| 54 | iAdapter adapters.IAdapter // from Voltha interface adapters |
| 55 | kafkaClient kafka.Client |
| 56 | kvClient kvstore.Client |
| 57 | kip kafka.InterContainerProxy |
| 58 | coreProxy adapterif.CoreProxy |
| 59 | adapterProxy adapterif.AdapterProxy |
Himani Chawla | c07fda0 | 2020-12-09 16:21:21 +0530 | [diff] [blame] | 60 | eventProxy eventif.EventProxy |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 61 | halted bool |
| 62 | exitChannel chan int |
| 63 | receiverChannels []<-chan *ic.InterContainerMessage //from inter-container |
| 64 | } |
| 65 | |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 66 | func newAdapter(cf *config.AdapterFlags) *adapter { |
| 67 | var a adapter |
| 68 | a.instanceID = cf.InstanceID |
| 69 | a.config = cf |
| 70 | a.halted = false |
| 71 | a.exitChannel = make(chan int, 1) |
| 72 | a.receiverChannels = make([]<-chan *ic.InterContainerMessage, 0) |
| 73 | return &a |
| 74 | } |
| 75 | |
| 76 | func (a *adapter) start(ctx context.Context) error { |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 77 | logger.Info(ctx, "Starting Core Adapter components") |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 78 | var err error |
| 79 | |
| 80 | var p *probe.Probe |
| 81 | if value := ctx.Value(probe.ProbeContextKey); value != nil { |
| 82 | if _, ok := value.(*probe.Probe); ok { |
| 83 | p = value.(*probe.Probe) |
| 84 | p.RegisterService( |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 85 | ctx, |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 86 | "message-bus", |
| 87 | "kv-store", |
| 88 | "container-proxy", |
| 89 | "core-request-handler", |
| 90 | "register-with-core", |
| 91 | ) |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | // Setup KV Client |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 96 | logger.Debugw(ctx, "create-kv-client", log.Fields{"kvstore": a.config.KVStoreType}) |
| 97 | if err = a.setKVClient(ctx); err != nil { |
| 98 | logger.Fatalw(ctx, "error-setting-kv-client", log.Fields{"error": err}) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | if p != nil { |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 102 | p.UpdateStatus(ctx, "kv-store", probe.ServiceStatusRunning) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 105 | // Setup Log Config |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 106 | /* address config update acc. to [VOL-2736] */ |
| 107 | addr := a.config.KVStoreHost + ":" + strconv.Itoa(a.config.KVStorePort) |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 108 | cm := conf.NewConfigManager(ctx, a.kvClient, a.config.KVStoreType, addr, a.config.KVStoreTimeout) |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 109 | go conf.StartLogLevelConfigProcessing(cm, ctx) |
| 110 | |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 111 | // Setup Kafka Client |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 112 | if a.kafkaClient, err = newKafkaClient(ctx, "sarama", a.config.KafkaAdapterHost, a.config.KafkaAdapterPort); err != nil { |
| 113 | logger.Fatalw(ctx, "Unsupported-common-client", log.Fields{"error": err}) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | if p != nil { |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 117 | p.UpdateStatus(ctx, "message-bus", probe.ServiceStatusRunning) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | // Start the common InterContainer Proxy - retries as per program arguments or indefinitely per default |
| 121 | if a.kip, err = a.startInterContainerProxy(ctx, a.config.KafkaReconnectRetries); err != nil { |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 122 | logger.Fatalw(ctx, "error-starting-inter-container-proxy", log.Fields{"error": err}) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 123 | //aborting the complete processing here (does notmake sense after set Retry number [else use -1 for infinite]) |
| 124 | return err |
| 125 | } |
| 126 | |
| 127 | // Create the core proxy to handle requests to the Core |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 128 | a.coreProxy = com.NewCoreProxy(ctx, a.kip, a.config.Topic, a.config.CoreTopic) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 129 | |
Andrea Campanella | 961734c | 2021-01-18 11:44:47 +0100 | [diff] [blame] | 130 | logger.Debugw(ctx, "create adapter proxy", log.Fields{"CoreTopic": a.config.CoreTopic}) |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 131 | a.adapterProxy = com.NewAdapterProxy(ctx, a.kip, a.config.CoreTopic, cm.Backend) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 132 | |
| 133 | // Create the event proxy to post events to KAFKA |
Himani Chawla | c07fda0 | 2020-12-09 16:21:21 +0530 | [diff] [blame] | 134 | a.eventProxy = events.NewEventProxy(events.MsgClient(a.kafkaClient), events.MsgTopic(kafka.Topic{Name: a.config.EventTopic})) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 135 | |
| 136 | // Create the open ONU interface adapter |
| 137 | if a.iAdapter, err = a.startVolthaInterfaceAdapter(ctx, a.kip, a.coreProxy, a.adapterProxy, a.eventProxy, |
Matteo Scandolo | f1f39a7 | 2020-11-24 12:08:11 -0800 | [diff] [blame] | 138 | a.config, cm); err != nil { |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 139 | logger.Fatalw(ctx, "error-starting-volthaInterfaceAdapter for OpenOnt", log.Fields{"error": err}) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | // Register the core request handler |
| 143 | if err = a.setupRequestHandler(ctx, a.instanceID, a.iAdapter); err != nil { |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 144 | logger.Fatalw(ctx, "error-setting-core-request-handler", log.Fields{"error": err}) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | // Register this adapter to the Core - retries indefinitely |
| 148 | if err = a.registerWithCore(ctx, -1); err != nil { |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 149 | logger.Fatalw(ctx, "error-registering-with-core", log.Fields{"error": err}) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | // check the readiness and liveliness and update the probe status |
| 153 | a.checkServicesReadiness(ctx) |
| 154 | return err |
| 155 | } |
| 156 | |
| 157 | func (a *adapter) stop(ctx context.Context) { |
| 158 | // Stop leadership tracking |
| 159 | a.halted = true |
| 160 | |
| 161 | // send exit signal |
| 162 | a.exitChannel <- 0 |
| 163 | |
| 164 | // Cleanup - applies only if we had a kvClient |
| 165 | if a.kvClient != nil { |
| 166 | // Release all reservations |
| 167 | if err := a.kvClient.ReleaseAllReservations(ctx); err != nil { |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 168 | logger.Infow(ctx, "fail-to-release-all-reservations", log.Fields{"error": err}) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 169 | } |
| 170 | // Close the DB connection |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 171 | a.kvClient.Close(ctx) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 174 | if a.kip != nil { |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 175 | a.kip.Stop(ctx) |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 176 | } |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | // ############################################# |
| 180 | // Adapter Utility methods ##### begin ######### |
| 181 | |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 182 | func newKVClient(ctx context.Context, storeType, address string, timeout time.Duration) (kvstore.Client, error) { |
| 183 | logger.Infow(ctx, "kv-store-type", log.Fields{"store": storeType}) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 184 | switch storeType { |
| 185 | case "consul": |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 186 | return kvstore.NewConsulClient(ctx, address, timeout) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 187 | case "etcd": |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 188 | return kvstore.NewEtcdClient(ctx, address, timeout, log.FatalLevel) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 189 | } |
| 190 | return nil, errors.New("unsupported-kv-store") |
| 191 | } |
| 192 | |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 193 | func newKafkaClient(ctx context.Context, clientType, host string, port int) (kafka.Client, error) { |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 194 | |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 195 | logger.Infow(ctx, "common-client-type", log.Fields{"client": clientType}) |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 196 | /* address config update acc. to [VOL-2736] */ |
| 197 | addr := host + ":" + strconv.Itoa(port) |
| 198 | |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 199 | switch clientType { |
| 200 | case "sarama": |
| 201 | return kafka.NewSaramaClient( |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 202 | kafka.Address(addr), |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 203 | kafka.ProducerReturnOnErrors(true), |
| 204 | kafka.ProducerReturnOnSuccess(true), |
| 205 | kafka.ProducerMaxRetries(6), |
| 206 | kafka.ProducerRetryBackoff(time.Millisecond*30), |
| 207 | kafka.MetadatMaxRetries(15)), nil |
| 208 | } |
| 209 | |
| 210 | return nil, errors.New("unsupported-client-type") |
| 211 | } |
| 212 | |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 213 | func (a *adapter) setKVClient(ctx context.Context) error { |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 214 | addr := a.config.KVStoreHost + ":" + strconv.Itoa(a.config.KVStorePort) |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 215 | client, err := newKVClient(ctx, a.config.KVStoreType, addr, a.config.KVStoreTimeout) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 216 | if err != nil { |
| 217 | a.kvClient = nil |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 218 | logger.Errorw(ctx, "error-starting-KVClient", log.Fields{"error": err}) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 219 | return err |
| 220 | } |
| 221 | a.kvClient = client |
| 222 | return nil |
| 223 | } |
| 224 | |
| 225 | func (a *adapter) startInterContainerProxy(ctx context.Context, retries int) (kafka.InterContainerProxy, error) { |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 226 | logger.Infow(ctx, "starting-intercontainer-messaging-proxy", log.Fields{"host": a.config.KafkaAdapterHost, |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 227 | "port": a.config.KafkaAdapterPort, "topic": a.config.Topic}) |
| 228 | var err error |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 229 | /* address config update acc. to [VOL-2736] */ |
| 230 | addr := a.config.KafkaAdapterHost + ":" + strconv.Itoa(a.config.KafkaAdapterPort) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 231 | kip := kafka.NewInterContainerProxy( |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 232 | kafka.InterContainerAddress(addr), |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 233 | kafka.MsgClient(a.kafkaClient), |
| 234 | kafka.DefaultTopic(&kafka.Topic{Name: a.config.Topic})) |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 235 | count := 0 |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 236 | for { |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 237 | if err = kip.Start(ctx); err != nil { |
| 238 | logger.Warnw(ctx, "error-starting-messaging-proxy", log.Fields{"error": err, "retry": retries, "count": count}) |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 239 | if retries == count { |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 240 | return nil, err |
| 241 | } |
| 242 | count++ |
| 243 | // Take a nap before retrying |
| 244 | time.Sleep(2 * time.Second) |
| 245 | } else { |
| 246 | break |
| 247 | } |
| 248 | } |
| 249 | probe.UpdateStatusFromContext(ctx, "container-proxy", probe.ServiceStatusRunning) |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 250 | logger.Info(ctx, "common-messaging-proxy-created") |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 251 | return kip, nil |
| 252 | } |
| 253 | |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 254 | func (a *adapter) startVolthaInterfaceAdapter(ctx context.Context, kip kafka.InterContainerProxy, |
Himani Chawla | c07fda0 | 2020-12-09 16:21:21 +0530 | [diff] [blame] | 255 | cp adapterif.CoreProxy, ap adapterif.AdapterProxy, ep eventif.EventProxy, |
Matteo Scandolo | f1f39a7 | 2020-11-24 12:08:11 -0800 | [diff] [blame] | 256 | cfg *config.AdapterFlags, cm *conf.ConfigManager) (*ac.OpenONUAC, error) { |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 257 | var err error |
Matteo Scandolo | f1f39a7 | 2020-11-24 12:08:11 -0800 | [diff] [blame] | 258 | sAcONU := ac.NewOpenONUAC(ctx, a.kip, cp, ap, ep, a.kvClient, cfg, cm) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 259 | |
| 260 | if err = sAcONU.Start(ctx); err != nil { |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 261 | logger.Fatalw(ctx, "error-starting-OpenOnuAdapterCore", log.Fields{"error": err}) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 262 | return nil, err |
| 263 | } |
| 264 | |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 265 | logger.Info(ctx, "open-ont-OpenOnuAdapterCore-started") |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 266 | return sAcONU, nil |
| 267 | } |
| 268 | |
| 269 | func (a *adapter) setupRequestHandler(ctx context.Context, coreInstanceID string, iadapter adapters.IAdapter) error { |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 270 | logger.Info(ctx, "setting-request-handler") |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 271 | requestProxy := com.NewRequestHandlerProxy(coreInstanceID, iadapter, a.coreProxy) |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 272 | if err := a.kip.SubscribeWithRequestHandlerInterface(ctx, kafka.Topic{Name: a.config.Topic}, requestProxy); err != nil { |
| 273 | logger.Errorw(ctx, "request-handler-setup-failed", log.Fields{"error": err}) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 274 | return err |
| 275 | |
| 276 | } |
| 277 | probe.UpdateStatusFromContext(ctx, "core-request-handler", probe.ServiceStatusRunning) |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 278 | logger.Info(ctx, "request-handler-setup-done") |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 279 | return nil |
| 280 | } |
| 281 | |
| 282 | func (a *adapter) registerWithCore(ctx context.Context, retries int) error { |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 283 | adapterID := fmt.Sprintf("brcm_openomci_onu_%d", a.config.CurrentReplica) |
Andrea Campanella | 3d7c931 | 2021-01-19 09:20:49 +0100 | [diff] [blame] | 284 | vendorIdsList := strings.Split(a.config.OnuVendorIds, ",") |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 285 | logger.Infow(ctx, "registering-with-core", log.Fields{ |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 286 | "adapterID": adapterID, |
| 287 | "currentReplica": a.config.CurrentReplica, |
| 288 | "totalReplicas": a.config.TotalReplicas, |
Andrea Campanella | 3d7c931 | 2021-01-19 09:20:49 +0100 | [diff] [blame] | 289 | "onuVendorIds": vendorIdsList, |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 290 | }) |
Matteo Scandolo | 2e6f1e3 | 2020-04-15 11:28:45 -0700 | [diff] [blame] | 291 | adapterDescription := &voltha.Adapter{ |
Andrea Campanella | 961734c | 2021-01-18 11:44:47 +0100 | [diff] [blame] | 292 | Id: adapterID, // Unique name for the device type ->exact type required for OLT comm???? |
| 293 | Vendor: "VOLTHA OpenONUGo", |
| 294 | Version: version.VersionInfo.Version, |
Matteo Scandolo | efbec27 | 2020-11-17 10:33:09 -0800 | [diff] [blame] | 295 | Endpoint: a.config.Topic, |
Matteo Scandolo | 2e6f1e3 | 2020-04-15 11:28:45 -0700 | [diff] [blame] | 296 | Type: "brcm_openomci_onu", |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 297 | CurrentReplica: int32(a.config.CurrentReplica), |
| 298 | TotalReplicas: int32(a.config.TotalReplicas), |
Matteo Scandolo | 2e6f1e3 | 2020-04-15 11:28:45 -0700 | [diff] [blame] | 299 | } |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 300 | types := []*voltha.DeviceType{{Id: "brcm_openomci_onu", |
Andrea Campanella | 3d7c931 | 2021-01-19 09:20:49 +0100 | [diff] [blame] | 301 | VendorIds: vendorIdsList, |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 302 | Adapter: "brcm_openomci_onu", // Name of the adapter that handles device type |
| 303 | AcceptsBulkFlowUpdate: false, // Currently openolt adapter does not support bulk flow handling |
| 304 | AcceptsAddRemoveFlowUpdates: true}} |
| 305 | deviceTypes := &voltha.DeviceTypes{Items: types} |
| 306 | count := 0 |
| 307 | for { |
| 308 | if err := a.coreProxy.RegisterAdapter(context.TODO(), adapterDescription, deviceTypes); err != nil { |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 309 | logger.Warnw(ctx, "registering-with-core-failed", log.Fields{"error": err}) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 310 | if retries == count { |
| 311 | return err |
| 312 | } |
| 313 | count++ |
| 314 | // Take a nap before retrying |
| 315 | time.Sleep(2 * time.Second) |
| 316 | } else { |
| 317 | break |
| 318 | } |
| 319 | } |
| 320 | probe.UpdateStatusFromContext(ctx, "register-with-core", probe.ServiceStatusRunning) |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 321 | logger.Info(ctx, "registered-with-core") |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 322 | return nil |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | This function checks the liveliness and readiness of the kakfa and kv-client services |
| 327 | and update the status in the probe. |
| 328 | */ |
| 329 | func (a *adapter) checkServicesReadiness(ctx context.Context) { |
| 330 | // checks the kafka readiness |
| 331 | go a.checkKafkaReadiness(ctx) |
| 332 | |
| 333 | // checks the kv-store readiness |
| 334 | go a.checkKvStoreReadiness(ctx) |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | This function checks the liveliness and readiness of the kv-store service |
| 339 | and update the status in the probe. |
| 340 | */ |
| 341 | func (a *adapter) checkKvStoreReadiness(ctx context.Context) { |
| 342 | // dividing the live probe interval by 2 to get updated status every 30s |
| 343 | timeout := a.config.LiveProbeInterval / 2 |
| 344 | kvStoreChannel := make(chan bool, 1) |
| 345 | |
| 346 | // Default false to check the liveliness. |
| 347 | kvStoreChannel <- false |
| 348 | for { |
| 349 | timeoutTimer := time.NewTimer(timeout) |
| 350 | select { |
| 351 | case liveliness := <-kvStoreChannel: |
| 352 | if !liveliness { |
| 353 | // kv-store not reachable or down, updating the status to not ready state |
| 354 | probe.UpdateStatusFromContext(ctx, "kv-store", probe.ServiceStatusNotReady) |
| 355 | timeout = a.config.NotLiveProbeInterval |
| 356 | } else { |
| 357 | // kv-store is reachable , updating the status to running state |
| 358 | probe.UpdateStatusFromContext(ctx, "kv-store", probe.ServiceStatusRunning) |
| 359 | timeout = a.config.LiveProbeInterval / 2 |
| 360 | } |
| 361 | // Check if the timer has expired or not |
| 362 | if !timeoutTimer.Stop() { |
| 363 | <-timeoutTimer.C |
| 364 | } |
| 365 | case <-timeoutTimer.C: |
| 366 | // Check the status of the kv-store |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 367 | logger.Info(ctx, "kv-store liveliness-recheck") |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 368 | if a.kvClient.IsConnectionUp(ctx) { |
| 369 | kvStoreChannel <- true |
| 370 | } else { |
| 371 | kvStoreChannel <- false |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | This function checks the liveliness and readiness of the kafka service |
| 379 | and update the status in the probe. |
| 380 | */ |
| 381 | func (a *adapter) checkKafkaReadiness(ctx context.Context) { |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 382 | livelinessChannel := a.kafkaClient.EnableLivenessChannel(ctx, true) |
| 383 | healthinessChannel := a.kafkaClient.EnableHealthinessChannel(ctx, true) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 384 | timeout := a.config.LiveProbeInterval |
| 385 | for { |
| 386 | timeoutTimer := time.NewTimer(timeout) |
| 387 | |
| 388 | select { |
| 389 | case healthiness := <-healthinessChannel: |
| 390 | if !healthiness { |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 391 | // logger.Fatal will call os.Exit(1) to terminate |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 392 | logger.Fatal(ctx, "Kafka service has become unhealthy") |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 393 | } |
| 394 | case liveliness := <-livelinessChannel: |
| 395 | if !liveliness { |
| 396 | // kafka not reachable or down, updating the status to not ready state |
| 397 | probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusNotReady) |
| 398 | timeout = a.config.NotLiveProbeInterval |
| 399 | } else { |
| 400 | // kafka is reachable , updating the status to running state |
| 401 | probe.UpdateStatusFromContext(ctx, "message-bus", probe.ServiceStatusRunning) |
| 402 | timeout = a.config.LiveProbeInterval |
| 403 | } |
| 404 | // Check if the timer has expired or not |
| 405 | if !timeoutTimer.Stop() { |
| 406 | <-timeoutTimer.C |
| 407 | } |
| 408 | case <-timeoutTimer.C: |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 409 | logger.Info(ctx, "kafka-proxy-liveness-recheck") |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 410 | // send the liveness probe in a goroutine; we don't want to deadlock ourselves as |
| 411 | // the liveness probe may wait (and block) writing to our channel. |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 412 | err := a.kafkaClient.SendLiveness(ctx) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 413 | if err != nil { |
| 414 | // Catch possible error case if sending liveness after Sarama has been stopped. |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 415 | logger.Warnw(ctx, "error-kafka-send-liveness", log.Fields{"error": err}) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 416 | } |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | // Adapter Utility methods ##### end ######### |
| 422 | // ############################################# |
| 423 | |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 424 | func getVerifiedCodeVersion(ctx context.Context) string { |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 425 | if version.VersionInfo.Version == "unknown-version" { |
| 426 | content, err := ioutil.ReadFile("VERSION") |
| 427 | if err == nil { |
Andrea Campanella | 3d7c931 | 2021-01-19 09:20:49 +0100 | [diff] [blame] | 428 | return string(content) |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 429 | } |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 430 | logger.Error(ctx, "'VERSION'-file not readable") |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 431 | } |
| 432 | return version.VersionInfo.Version |
| 433 | } |
| 434 | |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 435 | func printVersion(appName string) { |
| 436 | fmt.Println(appName) |
| 437 | fmt.Println(version.VersionInfo.String(" ")) |
| 438 | } |
| 439 | |
| 440 | func printBanner() { |
| 441 | fmt.Println(" ____ ____ ___ ___ _ ") |
| 442 | fmt.Println(" / __ \\ / __ \\| \\ \\ | | | | |") |
| 443 | fmt.Println(" | | | |_ __ ___ _ __ | | | | |\\ \\ | | | | | ____ ____") |
| 444 | fmt.Println(" | | | | '_ \\ / _ \\ '_ \\ | | | | | \\ \\ | | | | | / '_ \\ / _' \\") |
| 445 | fmt.Println(" | |__| | |_) | __/| | | || |__| | | \\ \\| | \\__/ || (__) | (__) |") |
| 446 | fmt.Println(" \\___ /| .__/ \\___|_| |_| \\____/|_| \\___|______| \\.___ |\\___./") |
| 447 | fmt.Println(" | | __| |") |
| 448 | fmt.Println(" |_| |____/") |
| 449 | fmt.Println(" ") |
| 450 | } |
| 451 | |
| 452 | func waitForExit(ctx context.Context) int { |
| 453 | signalChannel := make(chan os.Signal, 1) |
| 454 | signal.Notify(signalChannel, |
| 455 | syscall.SIGHUP, |
| 456 | syscall.SIGINT, |
| 457 | syscall.SIGTERM, |
| 458 | syscall.SIGQUIT) |
| 459 | |
| 460 | exitChannel := make(chan int) |
| 461 | |
| 462 | go func() { |
| 463 | select { |
| 464 | case <-ctx.Done(): |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 465 | logger.Infow(ctx, "Adapter run aborted due to internal errors", log.Fields{"context": "done"}) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 466 | exitChannel <- 2 |
| 467 | case s := <-signalChannel: |
| 468 | switch s { |
| 469 | case syscall.SIGHUP, |
| 470 | syscall.SIGINT, |
| 471 | syscall.SIGTERM, |
| 472 | syscall.SIGQUIT: |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 473 | logger.Infow(ctx, "closing-signal-received", log.Fields{"signal": s}) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 474 | exitChannel <- 0 |
| 475 | default: |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 476 | logger.Infow(ctx, "unexpected-signal-received", log.Fields{"signal": s}) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 477 | exitChannel <- 1 |
| 478 | } |
| 479 | } |
| 480 | }() |
| 481 | |
| 482 | code := <-exitChannel |
| 483 | return code |
| 484 | } |
| 485 | |
| 486 | func main() { |
| 487 | start := time.Now() |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 488 | ctx, cancel := context.WithCancel(context.Background()) |
| 489 | defer cancel() |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 490 | |
| 491 | cf := config.NewAdapterFlags() |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 492 | defaultAppName := cf.InstanceID + "_" + getVerifiedCodeVersion(ctx) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 493 | cf.ParseCommandArguments() |
| 494 | |
| 495 | // Setup logging |
| 496 | |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 497 | logLevel, err := log.StringToLogLevel(cf.LogLevel) |
Matteo Scandolo | 2e6f1e3 | 2020-04-15 11:28:45 -0700 | [diff] [blame] | 498 | if err != nil { |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 499 | logger.Fatalf(ctx, "Cannot setup logging, %s", err) |
Matteo Scandolo | 2e6f1e3 | 2020-04-15 11:28:45 -0700 | [diff] [blame] | 500 | } |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 501 | |
| 502 | // Setup default logger - applies for packages that do not have specific logger set |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 503 | if _, err := log.SetDefaultLogger(log.JSON, logLevel, log.Fields{"instanceId": cf.InstanceID}); err != nil { |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 504 | logger.With(log.Fields{"error": err}).Fatal(ctx, "Cannot setup logging") |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 505 | } |
| 506 | |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 507 | // Update all loggers (provisioned via init) with a common field |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 508 | if err := log.UpdateAllLoggers(log.Fields{"instanceId": cf.InstanceID}); err != nil { |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 509 | logger.With(log.Fields{"error": err}).Fatal(ctx, "Cannot setup logging") |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 510 | } |
| 511 | |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 512 | log.SetAllLogLevel(logLevel) |
| 513 | |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 514 | realMain(ctx) //fatal on httpListen(0,6060) ... |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 515 | |
Himani Chawla | 4d90833 | 2020-08-31 12:30:20 +0530 | [diff] [blame] | 516 | defer func() { |
| 517 | _ = log.CleanUp() |
| 518 | }() |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 519 | // Print version / build information and exit |
| 520 | if cf.DisplayVersionOnly { |
| 521 | printVersion(defaultAppName) |
| 522 | return |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 523 | } |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 524 | logger.Infow(ctx, "config", log.Fields{"StartName": defaultAppName}) |
| 525 | logger.Infow(ctx, "config", log.Fields{"BuildVersion": version.VersionInfo.String(" ")}) |
| 526 | logger.Infow(ctx, "config", log.Fields{"Arguments": os.Args[1:]}) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 527 | |
| 528 | // Print banner if specified |
| 529 | if cf.Banner { |
| 530 | printBanner() |
| 531 | } |
| 532 | |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 533 | logger.Infow(ctx, "config", log.Fields{"config": *cf}) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 534 | |
| 535 | ad := newAdapter(cf) |
| 536 | |
| 537 | p := &probe.Probe{} |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 538 | logger.Infow(ctx, "resources", log.Fields{"Context": ctx, "Adapter": ad.instanceID, "ProbeCoreState": p.GetStatus("register-with-core")}) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 539 | |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 540 | go p.ListenAndServe(ctx, fmt.Sprintf("%s:%d", ad.config.ProbeHost, ad.config.ProbePort)) |
| 541 | logger.Infow(ctx, "probeState", log.Fields{"ProbeCoreState": p.GetStatus("register-with-core")}) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 542 | |
| 543 | probeCtx := context.WithValue(ctx, probe.ProbeContextKey, p) |
| 544 | |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 545 | closer, err := log.GetGlobalLFM().InitTracingAndLogCorrelation(cf.TraceEnabled, cf.TraceAgentAddress, cf.LogCorrelationEnabled) |
| 546 | if err != nil { |
| 547 | logger.Warnw(ctx, "unable-to-initialize-tracing-and-log-correlation-module", log.Fields{"error": err}) |
| 548 | } else { |
| 549 | defer log.TerminateTracing(closer) |
| 550 | } |
| 551 | |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 552 | go func() { |
| 553 | err := ad.start(probeCtx) |
| 554 | // If this operation returns an error |
| 555 | // cancel all operations using this context |
| 556 | if err != nil { |
| 557 | cancel() |
| 558 | } |
| 559 | }() |
| 560 | |
| 561 | code := waitForExit(ctx) |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 562 | logger.Infow(ctx, "received-a-closing-signal", log.Fields{"code": code}) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 563 | |
| 564 | // Cleanup before leaving |
| 565 | ad.stop(ctx) |
| 566 | |
| 567 | elapsed := time.Since(start) |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 568 | logger.Infow(ctx, "run-time", log.Fields{"Name": "openadapter", "time": elapsed / time.Microsecond}) |
| 569 | //logger.Infow(ctx,"run-time", log.Fields{"instanceId": ad.config.InstanceID, "time": elapsed / time.Second}) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 570 | } |