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