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 | |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 31 | "github.com/golang/protobuf/ptypes/empty" |
| 32 | conf "github.com/opencord/voltha-lib-go/v7/pkg/config" |
| 33 | "github.com/opencord/voltha-lib-go/v7/pkg/db/kvstore" |
| 34 | "github.com/opencord/voltha-lib-go/v7/pkg/events" |
| 35 | "github.com/opencord/voltha-lib-go/v7/pkg/events/eventif" |
| 36 | vgrpc "github.com/opencord/voltha-lib-go/v7/pkg/grpc" |
| 37 | "github.com/opencord/voltha-lib-go/v7/pkg/kafka" |
| 38 | "github.com/opencord/voltha-lib-go/v7/pkg/log" |
| 39 | "github.com/opencord/voltha-lib-go/v7/pkg/probe" |
| 40 | "github.com/opencord/voltha-lib-go/v7/pkg/version" |
| 41 | "github.com/opencord/voltha-protos/v5/go/adapter_services" |
| 42 | "github.com/opencord/voltha-protos/v5/go/core" |
| 43 | "github.com/opencord/voltha-protos/v5/go/voltha" |
| 44 | "google.golang.org/grpc" |
| 45 | |
| 46 | ic "github.com/opencord/voltha-protos/v5/go/inter_container" |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 47 | |
Matteo Scandolo | 761f751 | 2020-11-23 15:52:40 -0800 | [diff] [blame] | 48 | "github.com/opencord/voltha-openonu-adapter-go/internal/pkg/config" |
Holger Hildebrandt | 4b5e73f | 2021-08-19 06:51:21 +0000 | [diff] [blame] | 49 | ac "github.com/opencord/voltha-openonu-adapter-go/internal/pkg/core" |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 50 | ) |
| 51 | |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 52 | const ( |
| 53 | clusterMessagingService = "cluster-message-service" |
| 54 | onuAdapterService = "onu-adapter-service" |
| 55 | kvService = "kv-service" |
| 56 | coreService = "core-service" |
| 57 | ) |
| 58 | |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 59 | type adapter struct { |
Himani Chawla | 4d90833 | 2020-08-31 12:30:20 +0530 | [diff] [blame] | 60 | //defaultAppName string |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 61 | instanceID string |
| 62 | config *config.AdapterFlags |
| 63 | kafkaClient kafka.Client |
| 64 | kvClient kvstore.Client |
| 65 | eventProxy eventif.EventProxy |
| 66 | grpcServer *vgrpc.GrpcServer |
| 67 | onuAdapter *ac.OpenONUAC |
| 68 | coreClient *vgrpc.Client |
| 69 | halted bool |
| 70 | exitChannel chan int |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 73 | func newAdapter(cf *config.AdapterFlags) *adapter { |
| 74 | var a adapter |
| 75 | a.instanceID = cf.InstanceID |
| 76 | a.config = cf |
| 77 | a.halted = false |
| 78 | a.exitChannel = make(chan int, 1) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 79 | return &a |
| 80 | } |
| 81 | |
| 82 | func (a *adapter) start(ctx context.Context) error { |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 83 | logger.Info(ctx, "Starting Core Adapter components") |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 84 | var err error |
| 85 | |
| 86 | var p *probe.Probe |
| 87 | if value := ctx.Value(probe.ProbeContextKey); value != nil { |
| 88 | if _, ok := value.(*probe.Probe); ok { |
| 89 | p = value.(*probe.Probe) |
| 90 | p.RegisterService( |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 91 | ctx, |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 92 | clusterMessagingService, |
| 93 | kvService, |
| 94 | onuAdapterService, |
| 95 | coreService, |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 96 | ) |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | // Setup KV Client |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 101 | logger.Debugw(ctx, "create-kv-client", log.Fields{"kvstore": a.config.KVStoreType}) |
| 102 | if err = a.setKVClient(ctx); err != nil { |
| 103 | logger.Fatalw(ctx, "error-setting-kv-client", log.Fields{"error": err}) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 106 | // Setup Log Config |
Matteo Scandolo | 127c59d | 2021-01-28 11:31:18 -0800 | [diff] [blame] | 107 | 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] | 108 | go conf.StartLogLevelConfigProcessing(cm, ctx) |
| 109 | |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 110 | // Setup Kafka Client |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 111 | if a.kafkaClient, err = newKafkaClient(ctx, "sarama", a.config.KafkaClusterAddress); err != nil { |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 112 | logger.Fatalw(ctx, "Unsupported-common-client", log.Fields{"error": err}) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 113 | } |
| 114 | |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 115 | // Start kafka communication with the broker |
| 116 | if err := kafka.StartAndWaitUntilKafkaConnectionIsUp(ctx, a.kafkaClient, a.config.HeartbeatCheckInterval, clusterMessagingService); err != nil { |
| 117 | logger.Fatal(ctx, "unable-to-connect-to-kafka") |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 118 | } |
| 119 | |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 120 | // Wait until connection to KV store is established |
| 121 | if err := WaitUntilKvStoreConnectionIsUp(ctx, a.kvClient, a.config.KVStoreTimeout, kvService); err != nil { |
| 122 | logger.Fatal(ctx, "unable-to-connect-to-kv-store") |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 125 | // Create the event proxy to post events to KAFKA |
Himani Chawla | c07fda0 | 2020-12-09 16:21:21 +0530 | [diff] [blame] | 126 | a.eventProxy = events.NewEventProxy(events.MsgClient(a.kafkaClient), events.MsgTopic(kafka.Topic{Name: a.config.EventTopic})) |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 127 | go func() { |
| 128 | if err := a.eventProxy.Start(); err != nil { |
| 129 | logger.Fatalw(ctx, "event-proxy-cannot-start", log.Fields{"error": err}) |
| 130 | } |
| 131 | }() |
| 132 | |
| 133 | // Create the Core client to handle requests to the Core. Note that the coreClient is an interface and needs to be |
| 134 | // cast to the appropriate grpc client by invoking GetCoreGrpcClient on the a.coreClient |
| 135 | if a.coreClient, err = vgrpc.NewClient(a.config.CoreEndpoint, |
| 136 | a.coreRestarted, |
| 137 | vgrpc.ActivityCheck(true)); err != nil { |
| 138 | logger.Fatal(ctx, "grpc-client-not-created") |
| 139 | } |
| 140 | // Start the core grpc client |
| 141 | go a.coreClient.Start(ctx, setAndTestCoreServiceHandler) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 142 | |
| 143 | // Create the open ONU interface adapter |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 144 | if a.onuAdapter, err = a.startVolthaInterfaceAdapter(ctx, a.coreClient, a.eventProxy, a.config, cm); err != nil { |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 145 | logger.Fatalw(ctx, "error-starting-volthaInterfaceAdapter for OpenOnt", log.Fields{"error": err}) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 146 | } |
| 147 | |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 148 | // Create and start the grpc server |
| 149 | a.grpcServer = vgrpc.NewGrpcServer(a.config.GrpcAddress, nil, false, p) |
| 150 | |
| 151 | //Register the adapter service |
| 152 | a.addAdapterService(ctx, a.grpcServer, a.onuAdapter) |
| 153 | |
| 154 | //Register the onu inter adapter service |
| 155 | a.addOnuInterAdapterService(ctx, a.grpcServer, a.onuAdapter) |
| 156 | |
| 157 | go a.startGRPCService(ctx, a.grpcServer, onuAdapterService) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 158 | |
| 159 | // Register this adapter to the Core - retries indefinitely |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 160 | if err = a.registerWithCore(ctx, coreService, -1); err != nil { |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 161 | logger.Fatalw(ctx, "error-registering-with-core", log.Fields{"error": err}) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 162 | } |
| 163 | |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 164 | // Start the readiness and liveliness check and update the probe status |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 165 | a.checkServicesReadiness(ctx) |
| 166 | return err |
| 167 | } |
| 168 | |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 169 | // TODO: Any action the adapter needs to do following a Core restart? |
| 170 | func (a *adapter) coreRestarted(ctx context.Context, endPoint string) error { |
| 171 | logger.Errorw(ctx, "core-restarted", log.Fields{"endpoint": endPoint}) |
| 172 | return nil |
| 173 | } |
| 174 | |
| 175 | // setAndTestCoreServiceHandler is used to test whether the remote gRPC service is up |
| 176 | func setAndTestCoreServiceHandler(ctx context.Context, conn *grpc.ClientConn) interface{} { |
| 177 | svc := core.NewCoreServiceClient(conn) |
| 178 | if h, err := svc.GetHealthStatus(ctx, &empty.Empty{}); err != nil || h.State != voltha.HealthStatus_HEALTHY { |
| 179 | return nil |
| 180 | } |
| 181 | return svc |
| 182 | } |
| 183 | |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 184 | func (a *adapter) stop(ctx context.Context) { |
| 185 | // Stop leadership tracking |
| 186 | a.halted = true |
| 187 | |
| 188 | // send exit signal |
| 189 | a.exitChannel <- 0 |
| 190 | |
| 191 | // Cleanup - applies only if we had a kvClient |
| 192 | if a.kvClient != nil { |
| 193 | // Release all reservations |
| 194 | if err := a.kvClient.ReleaseAllReservations(ctx); err != nil { |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 195 | logger.Infow(ctx, "fail-to-release-all-reservations", log.Fields{"error": err}) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 196 | } |
| 197 | // Close the DB connection |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 198 | a.kvClient.Close(ctx) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 199 | } |
| 200 | |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 201 | if a.eventProxy != nil { |
| 202 | a.eventProxy.Stop() |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 203 | } |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 204 | |
| 205 | if a.kafkaClient != nil { |
| 206 | a.kafkaClient.Stop(ctx) |
| 207 | } |
| 208 | |
| 209 | // Stop core client |
| 210 | if a.coreClient != nil { |
| 211 | a.coreClient.Stop(ctx) |
| 212 | } |
| 213 | |
| 214 | // TODO: More cleanup |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | // ############################################# |
| 218 | // Adapter Utility methods ##### begin ######### |
| 219 | |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 220 | func newKVClient(ctx context.Context, storeType, address string, timeout time.Duration) (kvstore.Client, error) { |
| 221 | logger.Infow(ctx, "kv-store-type", log.Fields{"store": storeType}) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 222 | switch storeType { |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 223 | case "etcd": |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 224 | return kvstore.NewEtcdClient(ctx, address, timeout, log.FatalLevel) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 225 | } |
| 226 | return nil, errors.New("unsupported-kv-store") |
| 227 | } |
| 228 | |
Matteo Scandolo | 127c59d | 2021-01-28 11:31:18 -0800 | [diff] [blame] | 229 | func newKafkaClient(ctx context.Context, clientType, addr string) (kafka.Client, error) { |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 230 | |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 231 | logger.Infow(ctx, "common-client-type", log.Fields{"client": clientType}) |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 232 | |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 233 | switch clientType { |
| 234 | case "sarama": |
| 235 | return kafka.NewSaramaClient( |
mpagenko | af80163 | 2020-07-03 10:00:42 +0000 | [diff] [blame] | 236 | kafka.Address(addr), |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 237 | kafka.ProducerReturnOnErrors(true), |
| 238 | kafka.ProducerReturnOnSuccess(true), |
| 239 | kafka.ProducerMaxRetries(6), |
| 240 | kafka.ProducerRetryBackoff(time.Millisecond*30), |
| 241 | kafka.MetadatMaxRetries(15)), nil |
| 242 | } |
| 243 | |
| 244 | return nil, errors.New("unsupported-client-type") |
| 245 | } |
| 246 | |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 247 | func (a *adapter) setKVClient(ctx context.Context) error { |
Matteo Scandolo | 127c59d | 2021-01-28 11:31:18 -0800 | [diff] [blame] | 248 | 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] | 249 | if err != nil { |
| 250 | a.kvClient = nil |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 251 | logger.Errorw(ctx, "error-starting-KVClient", log.Fields{"error": err}) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 252 | return err |
| 253 | } |
| 254 | a.kvClient = client |
| 255 | return nil |
| 256 | } |
| 257 | |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 258 | func (a *adapter) startVolthaInterfaceAdapter(ctx context.Context, cc *vgrpc.Client, ep eventif.EventProxy, |
Matteo Scandolo | f1f39a7 | 2020-11-24 12:08:11 -0800 | [diff] [blame] | 259 | cfg *config.AdapterFlags, cm *conf.ConfigManager) (*ac.OpenONUAC, error) { |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 260 | var err error |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 261 | sAcONU := ac.NewOpenONUAC(ctx, cc, ep, a.kvClient, cfg, cm) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 262 | |
| 263 | if err = sAcONU.Start(ctx); err != nil { |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 264 | logger.Fatalw(ctx, "error-starting-OpenOnuAdapterCore", log.Fields{"error": err}) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 265 | return nil, err |
| 266 | } |
| 267 | |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 268 | logger.Info(ctx, "open-ont-OpenOnuAdapterCore-started") |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 269 | return sAcONU, nil |
| 270 | } |
| 271 | |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 272 | func (a *adapter) registerWithCore(ctx context.Context, serviceName string, 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, |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 285 | Endpoint: a.config.AdapterEndpoint, |
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, |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 292 | AdapterType: "brcm_openomci_onu", // Type of adapter that handles this device type |
| 293 | Adapter: "brcm_openomci_onu", // Deprecated attribute |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 294 | AcceptsBulkFlowUpdate: false, // Currently openolt adapter does not support bulk flow handling |
| 295 | AcceptsAddRemoveFlowUpdates: true}} |
| 296 | deviceTypes := &voltha.DeviceTypes{Items: types} |
| 297 | count := 0 |
| 298 | for { |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 299 | gClient, err := a.coreClient.GetCoreServiceClient() |
| 300 | if gClient != nil { |
| 301 | if gClient != nil { |
| 302 | if _, err = gClient.RegisterAdapter(log.WithSpanFromContext(context.TODO(), ctx), &ic.AdapterRegistration{ |
| 303 | Adapter: adapterDescription, |
| 304 | DTypes: deviceTypes}); err == nil { |
| 305 | break |
| 306 | } |
| 307 | } |
| 308 | logger.Warnw(ctx, "registering-with-core-failed", log.Fields{"endpoint": a.config.CoreEndpoint, "error": err, "count": count, "gclient": gClient}) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 309 | if retries == count { |
| 310 | return err |
| 311 | } |
| 312 | count++ |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 313 | // Take a power nap before retrying |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 314 | time.Sleep(2 * time.Second) |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 315 | |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 316 | } |
| 317 | } |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 318 | probe.UpdateStatusFromContext(ctx, serviceName, probe.ServiceStatusRunning) |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 319 | logger.Info(ctx, "registered-with-core") |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 320 | return nil |
| 321 | } |
| 322 | |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 323 | // startGRPCService creates the grpc service handlers, registers it to the grpc server and starts the server |
| 324 | func (a *adapter) startGRPCService(ctx context.Context, server *vgrpc.GrpcServer, serviceName string) { |
| 325 | logger.Infow(ctx, "service-created", log.Fields{"service": serviceName}) |
| 326 | |
| 327 | probe.UpdateStatusFromContext(ctx, serviceName, probe.ServiceStatusRunning) |
| 328 | logger.Infow(ctx, "service-started", log.Fields{"service": serviceName}) |
| 329 | |
| 330 | server.Start(ctx) |
| 331 | probe.UpdateStatusFromContext(ctx, serviceName, probe.ServiceStatusStopped) |
| 332 | } |
| 333 | |
| 334 | func (a *adapter) addAdapterService(ctx context.Context, server *vgrpc.GrpcServer, handler adapter_services.AdapterServiceServer) { |
| 335 | logger.Info(ctx, "adding-adapter-service") |
| 336 | |
| 337 | server.AddService(func(gs *grpc.Server) { |
| 338 | adapter_services.RegisterAdapterServiceServer(gs, handler) |
| 339 | }) |
| 340 | } |
| 341 | |
| 342 | func (a *adapter) addOnuInterAdapterService(ctx context.Context, server *vgrpc.GrpcServer, handler adapter_services.OnuInterAdapterServiceServer) { |
| 343 | logger.Info(ctx, "adding-onu-inter-adapter-service") |
| 344 | |
| 345 | server.AddService(func(gs *grpc.Server) { |
| 346 | adapter_services.RegisterOnuInterAdapterServiceServer(gs, handler) |
| 347 | }) |
| 348 | } |
| 349 | |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 350 | /** |
| 351 | This function checks the liveliness and readiness of the kakfa and kv-client services |
| 352 | and update the status in the probe. |
| 353 | */ |
| 354 | func (a *adapter) checkServicesReadiness(ctx context.Context) { |
| 355 | // checks the kafka readiness |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 356 | go kafka.MonitorKafkaReadiness(ctx, a.kafkaClient, a.config.LiveProbeInterval, a.config.NotLiveProbeInterval, clusterMessagingService) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 357 | |
| 358 | // checks the kv-store readiness |
| 359 | go a.checkKvStoreReadiness(ctx) |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | This function checks the liveliness and readiness of the kv-store service |
| 364 | and update the status in the probe. |
| 365 | */ |
| 366 | func (a *adapter) checkKvStoreReadiness(ctx context.Context) { |
| 367 | // dividing the live probe interval by 2 to get updated status every 30s |
| 368 | timeout := a.config.LiveProbeInterval / 2 |
| 369 | kvStoreChannel := make(chan bool, 1) |
| 370 | |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 371 | // Default true - we are here only after we already had a KV store connection |
| 372 | kvStoreChannel <- true |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 373 | for { |
| 374 | timeoutTimer := time.NewTimer(timeout) |
| 375 | select { |
| 376 | case liveliness := <-kvStoreChannel: |
| 377 | if !liveliness { |
| 378 | // kv-store not reachable or down, updating the status to not ready state |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 379 | probe.UpdateStatusFromContext(ctx, kvService, probe.ServiceStatusNotReady) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 380 | timeout = a.config.NotLiveProbeInterval |
| 381 | } else { |
| 382 | // kv-store is reachable , updating the status to running state |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 383 | probe.UpdateStatusFromContext(ctx, kvService, probe.ServiceStatusRunning) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 384 | timeout = a.config.LiveProbeInterval / 2 |
| 385 | } |
| 386 | // Check if the timer has expired or not |
| 387 | if !timeoutTimer.Stop() { |
| 388 | <-timeoutTimer.C |
| 389 | } |
| 390 | case <-timeoutTimer.C: |
| 391 | // Check the status of the kv-store |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 392 | logger.Info(ctx, "kv-store liveliness-recheck") |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 393 | if a.kvClient.IsConnectionUp(ctx) { |
| 394 | kvStoreChannel <- true |
| 395 | } else { |
| 396 | kvStoreChannel <- false |
| 397 | } |
| 398 | } |
| 399 | } |
| 400 | } |
| 401 | |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 402 | // WaitUntilKvStoreConnectionIsUp waits until the KV client can establish a connection to the KV server or until the |
| 403 | // context times out. |
| 404 | func WaitUntilKvStoreConnectionIsUp(ctx context.Context, kvClient kvstore.Client, connectionRetryInterval time.Duration, serviceName string) error { |
| 405 | if kvClient == nil { |
| 406 | return errors.New("kvclient-is-nil") |
| 407 | } |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 408 | for { |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 409 | if !kvClient.IsConnectionUp(ctx) { |
| 410 | probe.UpdateStatusFromContext(ctx, serviceName, probe.ServiceStatusNotReady) |
| 411 | logger.Warnw(ctx, "kvconnection-down", log.Fields{"service-name": serviceName, "connect-retry-interval": connectionRetryInterval}) |
| 412 | select { |
| 413 | case <-time.After(connectionRetryInterval): |
| 414 | continue |
| 415 | case <-ctx.Done(): |
| 416 | return ctx.Err() |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 417 | } |
| 418 | } |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 419 | probe.UpdateStatusFromContext(ctx, serviceName, probe.ServiceStatusRunning) |
| 420 | logger.Info(ctx, "kv-connection-up") |
| 421 | break |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 422 | } |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 423 | return nil |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | // Adapter Utility methods ##### end ######### |
| 427 | // ############################################# |
| 428 | |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 429 | func getVerifiedCodeVersion(ctx context.Context) string { |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 430 | if version.VersionInfo.Version == "unknown-version" { |
| 431 | content, err := ioutil.ReadFile("VERSION") |
| 432 | if err == nil { |
Andrea Campanella | 3d7c931 | 2021-01-19 09:20:49 +0100 | [diff] [blame] | 433 | return string(content) |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 434 | } |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 435 | logger.Error(ctx, "'VERSION'-file not readable") |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 436 | } |
| 437 | return version.VersionInfo.Version |
| 438 | } |
| 439 | |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 440 | func printVersion(appName string) { |
| 441 | fmt.Println(appName) |
| 442 | fmt.Println(version.VersionInfo.String(" ")) |
| 443 | } |
| 444 | |
| 445 | func printBanner() { |
| 446 | fmt.Println(" ____ ____ ___ ___ _ ") |
| 447 | fmt.Println(" / __ \\ / __ \\| \\ \\ | | | | |") |
| 448 | fmt.Println(" | | | |_ __ ___ _ __ | | | | |\\ \\ | | | | | ____ ____") |
| 449 | fmt.Println(" | | | | '_ \\ / _ \\ '_ \\ | | | | | \\ \\ | | | | | / '_ \\ / _' \\") |
| 450 | fmt.Println(" | |__| | |_) | __/| | | || |__| | | \\ \\| | \\__/ || (__) | (__) |") |
| 451 | fmt.Println(" \\___ /| .__/ \\___|_| |_| \\____/|_| \\___|______| \\.___ |\\___./") |
| 452 | fmt.Println(" | | __| |") |
| 453 | fmt.Println(" |_| |____/") |
| 454 | fmt.Println(" ") |
| 455 | } |
| 456 | |
| 457 | func waitForExit(ctx context.Context) int { |
| 458 | signalChannel := make(chan os.Signal, 1) |
| 459 | signal.Notify(signalChannel, |
| 460 | syscall.SIGHUP, |
| 461 | syscall.SIGINT, |
| 462 | syscall.SIGTERM, |
| 463 | syscall.SIGQUIT) |
| 464 | |
| 465 | exitChannel := make(chan int) |
| 466 | |
| 467 | go func() { |
| 468 | select { |
| 469 | case <-ctx.Done(): |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 470 | 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] | 471 | exitChannel <- 2 |
| 472 | case s := <-signalChannel: |
| 473 | switch s { |
| 474 | case syscall.SIGHUP, |
| 475 | syscall.SIGINT, |
| 476 | syscall.SIGTERM, |
| 477 | syscall.SIGQUIT: |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 478 | logger.Infow(ctx, "closing-signal-received", log.Fields{"signal": s}) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 479 | exitChannel <- 0 |
| 480 | default: |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 481 | logger.Infow(ctx, "unexpected-signal-received", log.Fields{"signal": s}) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 482 | exitChannel <- 1 |
| 483 | } |
| 484 | } |
| 485 | }() |
| 486 | |
| 487 | code := <-exitChannel |
| 488 | return code |
| 489 | } |
| 490 | |
| 491 | func main() { |
| 492 | start := time.Now() |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 493 | ctx, cancel := context.WithCancel(context.Background()) |
| 494 | defer cancel() |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 495 | |
khenaidoo | 7d3c558 | 2021-08-11 18:09:44 -0400 | [diff] [blame] | 496 | cf := &config.AdapterFlags{} |
| 497 | cf.ParseCommandArguments(os.Args[1:]) |
| 498 | |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 499 | defaultAppName := cf.InstanceID + "_" + getVerifiedCodeVersion(ctx) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 500 | |
| 501 | // Setup logging |
| 502 | |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 503 | logLevel, err := log.StringToLogLevel(cf.LogLevel) |
Matteo Scandolo | 2e6f1e3 | 2020-04-15 11:28:45 -0700 | [diff] [blame] | 504 | if err != nil { |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 505 | logger.Fatalf(ctx, "Cannot setup logging, %s", err) |
Matteo Scandolo | 2e6f1e3 | 2020-04-15 11:28:45 -0700 | [diff] [blame] | 506 | } |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 507 | |
| 508 | // 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] | 509 | 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] | 510 | logger.With(log.Fields{"error": err}).Fatal(ctx, "Cannot setup logging") |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 511 | } |
| 512 | |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 513 | // Update all loggers (provisioned via init) with a common field |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 514 | if err := log.UpdateAllLoggers(log.Fields{"instanceId": cf.InstanceID}); err != nil { |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 515 | logger.With(log.Fields{"error": err}).Fatal(ctx, "Cannot setup logging") |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 516 | } |
| 517 | |
Holger Hildebrandt | 0f9b88d | 2020-04-20 13:33:25 +0000 | [diff] [blame] | 518 | log.SetAllLogLevel(logLevel) |
| 519 | |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 520 | realMain(ctx) //fatal on httpListen(0,6060) ... |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 521 | |
Himani Chawla | 4d90833 | 2020-08-31 12:30:20 +0530 | [diff] [blame] | 522 | defer func() { |
| 523 | _ = log.CleanUp() |
| 524 | }() |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 525 | // Print version / build information and exit |
| 526 | if cf.DisplayVersionOnly { |
| 527 | printVersion(defaultAppName) |
| 528 | return |
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 | logger.Infow(ctx, "config", log.Fields{"StartName": defaultAppName}) |
| 531 | logger.Infow(ctx, "config", log.Fields{"BuildVersion": version.VersionInfo.String(" ")}) |
| 532 | logger.Infow(ctx, "config", log.Fields{"Arguments": os.Args[1:]}) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 533 | |
| 534 | // Print banner if specified |
| 535 | if cf.Banner { |
| 536 | printBanner() |
| 537 | } |
| 538 | |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 539 | logger.Infow(ctx, "config", log.Fields{"config": *cf}) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 540 | |
| 541 | ad := newAdapter(cf) |
| 542 | |
| 543 | p := &probe.Probe{} |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 544 | 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] | 545 | |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 546 | go p.ListenAndServe(ctx, fmt.Sprintf("%s:%d", ad.config.ProbeHost, ad.config.ProbePort)) |
| 547 | logger.Infow(ctx, "probeState", log.Fields{"ProbeCoreState": p.GetStatus("register-with-core")}) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 548 | |
| 549 | probeCtx := context.WithValue(ctx, probe.ProbeContextKey, p) |
| 550 | |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 551 | closer, err := log.GetGlobalLFM().InitTracingAndLogCorrelation(cf.TraceEnabled, cf.TraceAgentAddress, cf.LogCorrelationEnabled) |
| 552 | if err != nil { |
| 553 | logger.Warnw(ctx, "unable-to-initialize-tracing-and-log-correlation-module", log.Fields{"error": err}) |
| 554 | } else { |
| 555 | defer log.TerminateTracing(closer) |
| 556 | } |
| 557 | |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 558 | go func() { |
| 559 | err := ad.start(probeCtx) |
| 560 | // If this operation returns an error |
| 561 | // cancel all operations using this context |
| 562 | if err != nil { |
| 563 | cancel() |
| 564 | } |
| 565 | }() |
| 566 | |
| 567 | code := waitForExit(ctx) |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 568 | logger.Infow(ctx, "received-a-closing-signal", log.Fields{"code": code}) |
Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 569 | |
| 570 | // Cleanup before leaving |
| 571 | ad.stop(ctx) |
| 572 | |
| 573 | elapsed := time.Since(start) |
dbainbri | 4d3a0dc | 2020-12-02 00:33:42 +0000 | [diff] [blame] | 574 | logger.Infow(ctx, "run-time", log.Fields{"Name": "openadapter", "time": elapsed / time.Microsecond}) |
| 575 | //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] | 576 | } |