blob: 1a22267b299d9db5324efa0f92b3819221f8f13d [file] [log] [blame]
khenaidoob9203542018-09-17 22:56:37 -04001/*
2 * Copyright 2018-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 */
npujar1d86a522019-11-14 17:11:16 +053016
khenaidoob9203542018-09-17 22:56:37 -040017package core
18
19import (
20 "context"
npujar1d86a522019-11-14 17:11:16 +053021 "time"
22
sbarbari17d7e222019-11-05 10:02:29 -050023 "github.com/opencord/voltha-go/db/model"
khenaidoob9203542018-09-17 22:56:37 -040024 "github.com/opencord/voltha-go/rw_core/config"
Kent Hagerman2b216042020-04-03 18:28:56 -040025 "github.com/opencord/voltha-go/rw_core/core/adapter"
26 "github.com/opencord/voltha-go/rw_core/core/api"
27 "github.com/opencord/voltha-go/rw_core/core/device"
Maninderdfadc982020-10-28 14:04:33 +053028 conf "github.com/opencord/voltha-lib-go/v4/pkg/config"
29 grpcserver "github.com/opencord/voltha-lib-go/v4/pkg/grpc"
30 "github.com/opencord/voltha-lib-go/v4/pkg/kafka"
31 "github.com/opencord/voltha-lib-go/v4/pkg/log"
32 "github.com/opencord/voltha-lib-go/v4/pkg/probe"
Salman Siddiqui1cf95042020-11-19 00:42:56 +053033 "github.com/opencord/voltha-protos/v4/go/extension"
Maninderdfadc982020-10-28 14:04:33 +053034 "github.com/opencord/voltha-protos/v4/go/voltha"
khenaidoob9203542018-09-17 22:56:37 -040035 "google.golang.org/grpc"
khenaidoob9203542018-09-17 22:56:37 -040036)
37
npujar1d86a522019-11-14 17:11:16 +053038// Core represent read,write core attributes
khenaidoob9203542018-09-17 22:56:37 -040039type Core struct {
Kent Hagerman2f0d0552020-04-23 17:28:52 -040040 shutdown context.CancelFunc
41 stopped chan struct{}
khenaidoob9203542018-09-17 22:56:37 -040042}
43
npujar1d86a522019-11-14 17:11:16 +053044// NewCore creates instance of rw core
Kent Hagerman2f0d0552020-04-23 17:28:52 -040045func NewCore(ctx context.Context, id string, cf *config.RWCoreFlags) *Core {
David K. Bainbridgeb4a9ab02019-09-20 15:12:16 -070046 // If the context has a probe then fetch it and register our services
Kent Hagerman2f0d0552020-04-23 17:28:52 -040047 if p := probe.GetProbeFromContext(ctx); p != nil {
48 p.RegisterService(
Rohan Agrawal31f21802020-06-12 05:38:46 +000049 ctx,
Kent Hagerman2f0d0552020-04-23 17:28:52 -040050 "message-bus",
51 "kv-store",
52 "adapter-manager",
53 "grpc-service",
khenaidoob9203542018-09-17 22:56:37 -040054 )
55 }
56
Kent Hagerman2f0d0552020-04-23 17:28:52 -040057 // new threads will be given a new cancelable context, so that they can be aborted later when Stop() is called
58 shutdownCtx, cancelCtx := context.WithCancel(ctx)
59
60 core := &Core{shutdown: cancelCtx, stopped: make(chan struct{})}
61 go core.start(shutdownCtx, id, cf)
62 return core
63}
64
65func (core *Core) start(ctx context.Context, id string, cf *config.RWCoreFlags) {
Rohan Agrawal31f21802020-06-12 05:38:46 +000066 logger.Info(ctx, "starting-core-services", log.Fields{"coreId": id})
Kent Hagerman2f0d0552020-04-23 17:28:52 -040067
68 // deferred functions are used to run cleanup
69 // failing partway will stop anything that's been started
70 defer close(core.stopped)
71 defer core.shutdown()
72
Rohan Agrawal31f21802020-06-12 05:38:46 +000073 logger.Info(ctx, "Starting RW Core components")
Kent Hagerman2f0d0552020-04-23 17:28:52 -040074
75 // setup kv client
Rohan Agrawal31f21802020-06-12 05:38:46 +000076 logger.Debugw(ctx, "create-kv-client", log.Fields{"kvstore": cf.KVStoreType})
77 kvClient, err := newKVClient(ctx, cf.KVStoreType, cf.KVStoreAddress, cf.KVStoreTimeout)
Kent Hagerman2f0d0552020-04-23 17:28:52 -040078 if err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +000079 logger.Fatal(ctx, err)
Kent Hagerman2f0d0552020-04-23 17:28:52 -040080 }
Rohan Agrawalcf12f202020-08-03 04:42:01 +000081 defer stopKVClient(log.WithSpanFromContext(context.Background(), ctx), kvClient)
Kent Hagerman2f0d0552020-04-23 17:28:52 -040082
83 // sync logging config with kv store
Rohan Agrawal31f21802020-06-12 05:38:46 +000084 cm := conf.NewConfigManager(ctx, kvClient, cf.KVStoreType, cf.KVStoreAddress, cf.KVStoreTimeout)
Kent Hagerman2f0d0552020-04-23 17:28:52 -040085 go conf.StartLogLevelConfigProcessing(cm, ctx)
Girish Kumarf8d4f8d2020-08-18 11:45:30 +000086 go conf.StartLogFeaturesConfigProcessing(cm, ctx)
Kent Hagerman2f0d0552020-04-23 17:28:52 -040087
serkant.uluderya8ff291d2020-05-20 00:58:00 -070088 backend := cm.Backend
89 backend.LivenessChannelInterval = cf.LiveProbeInterval / 2
Kent Hagerman2f0d0552020-04-23 17:28:52 -040090
91 // wait until connection to KV Store is up
92 if err := waitUntilKVStoreReachableOrMaxTries(ctx, kvClient, cf.MaxConnectionRetries, cf.ConnectionRetryInterval); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +000093 logger.Fatal(ctx, "Unable-to-connect-to-KV-store")
Kent Hagerman2f0d0552020-04-23 17:28:52 -040094 }
95 go monitorKVStoreLiveness(ctx, backend, cf.LiveProbeInterval, cf.NotLiveProbeInterval)
96
97 // create kafka client
98 kafkaClient := kafka.NewSaramaClient(
Neha Sharmad1387da2020-05-07 20:07:28 +000099 kafka.Address(cf.KafkaAdapterAddress),
Kent Hagerman2f0d0552020-04-23 17:28:52 -0400100 kafka.ConsumerType(kafka.GroupCustomer),
101 kafka.ProducerReturnOnErrors(true),
102 kafka.ProducerReturnOnSuccess(true),
103 kafka.ProducerMaxRetries(6),
104 kafka.NumPartitions(3),
105 kafka.ConsumerGroupName(id),
106 kafka.ConsumerGroupPrefix(id),
107 kafka.AutoCreateTopic(true),
108 kafka.ProducerFlushFrequency(5),
109 kafka.ProducerRetryBackoff(time.Millisecond*30),
110 kafka.LivenessChannelInterval(cf.LiveProbeInterval/2),
111 )
112 // defer kafkaClient.Stop()
113
Kent Hagermanf5a67352020-04-30 15:15:26 -0400114 // create kv path
115 dbPath := model.NewDBPath(backend)
Kent Hagerman2f0d0552020-04-23 17:28:52 -0400116
117 // load adapters & device types while other things are starting
Rohan Agrawal31f21802020-06-12 05:38:46 +0000118 adapterMgr := adapter.NewAdapterManager(ctx, dbPath, id, kafkaClient)
Kent Hagerman2f0d0552020-04-23 17:28:52 -0400119 go adapterMgr.Start(ctx)
120
121 // connect to kafka, then wait until reachable and publisher/consumer created
122 // core.kmp must be created before deviceMgr and adapterMgr
David Bainbridge9ae13132020-06-22 17:28:01 -0700123 kmp, err := startKafkInterContainerProxy(ctx, kafkaClient, cf.KafkaAdapterAddress, cf.CoreTopic, cf.ConnectionRetryInterval)
Kent Hagerman2f0d0552020-04-23 17:28:52 -0400124 if err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000125 logger.Warn(ctx, "Failed to setup kafka connection")
Kent Hagerman2f0d0552020-04-23 17:28:52 -0400126 return
127 }
Rohan Agrawal31f21802020-06-12 05:38:46 +0000128 defer kmp.Stop(ctx)
Kent Hagerman2f0d0552020-04-23 17:28:52 -0400129 go monitorKafkaLiveness(ctx, kmp, cf.LiveProbeInterval, cf.NotLiveProbeInterval)
130
131 // create the core of the system, the device managers
132 endpointMgr := kafka.NewEndpointManager(backend)
serkant.uluderya8ff291d2020-05-20 00:58:00 -0700133 deviceMgr, logicalDeviceMgr := device.NewManagers(dbPath, adapterMgr, kmp, endpointMgr, cf.CoreTopic, id, cf.DefaultCoreTimeout)
Kent Hagerman2f0d0552020-04-23 17:28:52 -0400134
135 // register kafka RPC handler
Rohan Agrawal31f21802020-06-12 05:38:46 +0000136 registerAdapterRequestHandlers(ctx, kmp, deviceMgr, adapterMgr, cf.CoreTopic)
Kent Hagerman2f0d0552020-04-23 17:28:52 -0400137
138 // start gRPC handler
Neha Sharmad1387da2020-05-07 20:07:28 +0000139 grpcServer := grpcserver.NewGrpcServer(cf.GrpcAddress, nil, false, probe.GetProbeFromContext(ctx))
Salman Siddiqui1cf95042020-11-19 00:42:56 +0530140
141 //Register the 'Extension' service on this gRPC server
142 addGRPCExtensionService(ctx, grpcServer, device.GetNewExtensionManager(deviceMgr))
143
Kent Hagerman2f0d0552020-04-23 17:28:52 -0400144 go startGRPCService(ctx, grpcServer, api.NewNBIHandler(deviceMgr, logicalDeviceMgr, adapterMgr))
145 defer grpcServer.Stop()
146
147 // wait for core to be stopped, via Stop() or context cancellation, before running deferred functions
148 <-ctx.Done()
149}
150
151// Stop brings down core services
152func (core *Core) Stop() {
153 core.shutdown()
154 <-core.stopped
155}
156
157// startGRPCService creates the grpc service handlers, registers it to the grpc server and starts the server
158func startGRPCService(ctx context.Context, server *grpcserver.GrpcServer, handler voltha.VolthaServiceServer) {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000159 logger.Info(ctx, "grpc-server-created")
Kent Hagerman2f0d0552020-04-23 17:28:52 -0400160
161 server.AddService(func(gs *grpc.Server) { voltha.RegisterVolthaServiceServer(gs, handler) })
Rohan Agrawal31f21802020-06-12 05:38:46 +0000162 logger.Info(ctx, "grpc-service-added")
khenaidoob9203542018-09-17 22:56:37 -0400163
David K. Bainbridgeb4a9ab02019-09-20 15:12:16 -0700164 probe.UpdateStatusFromContext(ctx, "grpc-service", probe.ServiceStatusRunning)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000165 logger.Info(ctx, "grpc-server-started")
Kent Hagerman2f0d0552020-04-23 17:28:52 -0400166 // Note that there is a small window here in which the core could return its status as ready,
167 // when it really isn't. This is unlikely to cause issues, as the delay is incredibly short.
168 server.Start(ctx)
David K. Bainbridgeb4a9ab02019-09-20 15:12:16 -0700169 probe.UpdateStatusFromContext(ctx, "grpc-service", probe.ServiceStatusStopped)
khenaidoob9203542018-09-17 22:56:37 -0400170}
Salman Siddiqui1cf95042020-11-19 00:42:56 +0530171
172func addGRPCExtensionService(ctx context.Context, server *grpcserver.GrpcServer, handler extension.ExtensionServer) {
173 logger.Info(ctx, "extension-grpc-server-created")
174
175 server.AddService(func(server *grpc.Server) {
176 extension.RegisterExtensionServer(server, handler)
177 })
178
179}