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