blob: 0a76ca2eaa8bb570ce99a4a40dfcb21479981d5e [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"
Kent Hagerman2f0d0552020-04-23 17:28:52 -040021 "strconv"
npujar1d86a522019-11-14 17:11:16 +053022 "time"
23
sbarbari17d7e222019-11-05 10:02:29 -050024 "github.com/opencord/voltha-go/db/model"
khenaidoob9203542018-09-17 22:56:37 -040025 "github.com/opencord/voltha-go/rw_core/config"
Kent Hagerman2b216042020-04-03 18:28:56 -040026 "github.com/opencord/voltha-go/rw_core/core/adapter"
27 "github.com/opencord/voltha-go/rw_core/core/api"
28 "github.com/opencord/voltha-go/rw_core/core/device"
Kent Hagerman2f0d0552020-04-23 17:28:52 -040029 conf "github.com/opencord/voltha-lib-go/v3/pkg/config"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080030 grpcserver "github.com/opencord/voltha-lib-go/v3/pkg/grpc"
31 "github.com/opencord/voltha-lib-go/v3/pkg/kafka"
32 "github.com/opencord/voltha-lib-go/v3/pkg/log"
33 "github.com/opencord/voltha-lib-go/v3/pkg/probe"
34 "github.com/opencord/voltha-protos/v3/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(
49 "message-bus",
50 "kv-store",
51 "adapter-manager",
52 "grpc-service",
khenaidoob9203542018-09-17 22:56:37 -040053 )
54 }
55
Kent Hagerman2f0d0552020-04-23 17:28:52 -040056 // new threads will be given a new cancelable context, so that they can be aborted later when Stop() is called
57 shutdownCtx, cancelCtx := context.WithCancel(ctx)
58
59 core := &Core{shutdown: cancelCtx, stopped: make(chan struct{})}
60 go core.start(shutdownCtx, id, cf)
61 return core
62}
63
64func (core *Core) start(ctx context.Context, id string, cf *config.RWCoreFlags) {
65 logger.Info("starting-core-services", log.Fields{"coreId": id})
66
67 // deferred functions are used to run cleanup
68 // failing partway will stop anything that's been started
69 defer close(core.stopped)
70 defer core.shutdown()
71
72 logger.Info("Starting RW Core components")
73
74 // setup kv client
75 logger.Debugw("create-kv-client", log.Fields{"kvstore": cf.KVStoreType})
76 kvClient, err := newKVClient(cf.KVStoreType, cf.KVStoreHost+":"+strconv.Itoa(cf.KVStorePort), cf.KVStoreTimeout)
77 if err != nil {
78 logger.Fatal(err)
79 }
80 defer stopKVClient(context.Background(), kvClient)
81
82 // sync logging config with kv store
83 cm := conf.NewConfigManager(kvClient, cf.KVStoreType, cf.KVStoreHost, cf.KVStorePort, cf.KVStoreTimeout)
84 go conf.StartLogLevelConfigProcessing(cm, ctx)
85
serkant.uluderya8ff291d2020-05-20 00:58:00 -070086 backend := cm.Backend
87 backend.LivenessChannelInterval = cf.LiveProbeInterval / 2
Kent Hagerman2f0d0552020-04-23 17:28:52 -040088
89 // wait until connection to KV Store is up
90 if err := waitUntilKVStoreReachableOrMaxTries(ctx, kvClient, cf.MaxConnectionRetries, cf.ConnectionRetryInterval); err != nil {
91 logger.Fatal("Unable-to-connect-to-KV-store")
92 }
93 go monitorKVStoreLiveness(ctx, backend, cf.LiveProbeInterval, cf.NotLiveProbeInterval)
94
95 // create kafka client
96 kafkaClient := kafka.NewSaramaClient(
97 kafka.Host(cf.KafkaAdapterHost),
98 kafka.Port(cf.KafkaAdapterPort),
99 kafka.ConsumerType(kafka.GroupCustomer),
100 kafka.ProducerReturnOnErrors(true),
101 kafka.ProducerReturnOnSuccess(true),
102 kafka.ProducerMaxRetries(6),
103 kafka.NumPartitions(3),
104 kafka.ConsumerGroupName(id),
105 kafka.ConsumerGroupPrefix(id),
106 kafka.AutoCreateTopic(true),
107 kafka.ProducerFlushFrequency(5),
108 kafka.ProducerRetryBackoff(time.Millisecond*30),
109 kafka.LivenessChannelInterval(cf.LiveProbeInterval/2),
110 )
111 // defer kafkaClient.Stop()
112
Kent Hagermanf5a67352020-04-30 15:15:26 -0400113 // create kv path
114 dbPath := model.NewDBPath(backend)
Kent Hagerman2f0d0552020-04-23 17:28:52 -0400115
116 // load adapters & device types while other things are starting
Kent Hagermanf5a67352020-04-30 15:15:26 -0400117 adapterMgr := adapter.NewAdapterManager(dbPath, id, kafkaClient)
Kent Hagerman2f0d0552020-04-23 17:28:52 -0400118 go adapterMgr.Start(ctx)
119
120 // connect to kafka, then wait until reachable and publisher/consumer created
121 // core.kmp must be created before deviceMgr and adapterMgr
122 kmp, err := startKafkInterContainerProxy(ctx, kafkaClient, cf.KafkaAdapterHost, cf.KafkaAdapterPort, cf.CoreTopic, cf.AffinityRouterTopic, cf.ConnectionRetryInterval)
123 if err != nil {
124 logger.Warn("Failed to setup kafka connection")
125 return
126 }
127 defer kmp.Stop()
128 go monitorKafkaLiveness(ctx, kmp, cf.LiveProbeInterval, cf.NotLiveProbeInterval)
129
130 // create the core of the system, the device managers
131 endpointMgr := kafka.NewEndpointManager(backend)
serkant.uluderya8ff291d2020-05-20 00:58:00 -0700132 deviceMgr, logicalDeviceMgr := device.NewManagers(dbPath, adapterMgr, kmp, endpointMgr, cf.CoreTopic, id, cf.DefaultCoreTimeout)
Kent Hagerman2f0d0552020-04-23 17:28:52 -0400133
134 // register kafka RPC handler
serkant.uluderya8ff291d2020-05-20 00:58:00 -0700135 registerAdapterRequestHandlers(kmp, deviceMgr, adapterMgr, cf.CoreTopic)
Kent Hagerman2f0d0552020-04-23 17:28:52 -0400136
137 // start gRPC handler
138 grpcServer := grpcserver.NewGrpcServer(cf.GrpcHost, cf.GrpcPort, nil, false, probe.GetProbeFromContext(ctx))
139 go startGRPCService(ctx, grpcServer, api.NewNBIHandler(deviceMgr, logicalDeviceMgr, adapterMgr))
140 defer grpcServer.Stop()
141
142 // wait for core to be stopped, via Stop() or context cancellation, before running deferred functions
143 <-ctx.Done()
144}
145
146// Stop brings down core services
147func (core *Core) Stop() {
148 core.shutdown()
149 <-core.stopped
150}
151
152// startGRPCService creates the grpc service handlers, registers it to the grpc server and starts the server
153func startGRPCService(ctx context.Context, server *grpcserver.GrpcServer, handler voltha.VolthaServiceServer) {
154 logger.Info("grpc-server-created")
155
156 server.AddService(func(gs *grpc.Server) { voltha.RegisterVolthaServiceServer(gs, handler) })
Girish Kumarf56a4682020-03-20 20:07:46 +0000157 logger.Info("grpc-service-added")
khenaidoob9203542018-09-17 22:56:37 -0400158
David K. Bainbridgeb4a9ab02019-09-20 15:12:16 -0700159 probe.UpdateStatusFromContext(ctx, "grpc-service", probe.ServiceStatusRunning)
Girish Kumarf56a4682020-03-20 20:07:46 +0000160 logger.Info("grpc-server-started")
Kent Hagerman2f0d0552020-04-23 17:28:52 -0400161 // Note that there is a small window here in which the core could return its status as ready,
162 // when it really isn't. This is unlikely to cause issues, as the delay is incredibly short.
163 server.Start(ctx)
David K. Bainbridgeb4a9ab02019-09-20 15:12:16 -0700164 probe.UpdateStatusFromContext(ctx, "grpc-service", probe.ServiceStatusStopped)
khenaidoob9203542018-09-17 22:56:37 -0400165}