blob: 1496200ee25aa57e9b2807921b9d304202f5e660 [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 */
16package core
17
18import (
19 "context"
20 grpcserver "github.com/opencord/voltha-go/common/grpc"
21 "github.com/opencord/voltha-go/common/log"
Richard Jankowskie4d77662018-10-17 13:53:21 -040022 "github.com/opencord/voltha-go/db/kvstore"
khenaidoob9203542018-09-17 22:56:37 -040023 "github.com/opencord/voltha-go/db/model"
24 "github.com/opencord/voltha-go/kafka"
25 "github.com/opencord/voltha-go/protos/voltha"
26 "github.com/opencord/voltha-go/rw_core/config"
27 "google.golang.org/grpc"
khenaidoob9203542018-09-17 22:56:37 -040028)
29
30type Core struct {
31 instanceId string
32 deviceMgr *DeviceManager
33 logicalDeviceMgr *LogicalDeviceManager
34 grpcServer *grpcserver.GrpcServer
Richard Jankowskidbab94a2018-12-06 16:20:25 -050035 grpcNBIAPIHandler *APIHandler
khenaidoob9203542018-09-17 22:56:37 -040036 config *config.RWCoreFlags
khenaidoo43c82122018-11-22 18:38:28 -050037 kmp *kafka.InterContainerProxy
khenaidoo92e62c52018-10-03 14:02:54 -040038 clusterDataRoot model.Root
39 localDataRoot model.Root
khenaidoob9203542018-09-17 22:56:37 -040040 clusterDataProxy *model.Proxy
41 localDataProxy *model.Proxy
42 exitChannel chan int
Richard Jankowskie4d77662018-10-17 13:53:21 -040043 kvClient kvstore.Client
khenaidoo43c82122018-11-22 18:38:28 -050044 kafkaClient kafka.Client
khenaidoob9203542018-09-17 22:56:37 -040045}
46
47func init() {
48 log.AddPackage(log.JSON, log.WarnLevel, nil)
49}
50
khenaidoo43c82122018-11-22 18:38:28 -050051func NewCore(id string, cf *config.RWCoreFlags, kvClient kvstore.Client, kafkaClient kafka.Client) *Core {
khenaidoob9203542018-09-17 22:56:37 -040052 var core Core
53 core.instanceId = id
54 core.exitChannel = make(chan int, 1)
55 core.config = cf
Richard Jankowskie4d77662018-10-17 13:53:21 -040056 core.kvClient = kvClient
khenaidoo43c82122018-11-22 18:38:28 -050057 core.kafkaClient = kafkaClient
Richard Jankowskie4d77662018-10-17 13:53:21 -040058
59 // Setup the KV store
60 // Do not call NewBackend constructor; it creates its own KV client
khenaidoo91ecfd62018-11-04 17:13:42 -050061 // Commented the backend for now until the issue between the model and the KV store
62 // is resolved.
khenaidoo7ccedd52018-12-14 16:48:54 -050063 backend := model.Backend{
64 Client: kvClient,
65 StoreType: cf.KVStoreType,
66 Host: cf.KVStoreHost,
67 Port: cf.KVStorePort,
68 Timeout: cf.KVStoreTimeout,
khenaidoo9cdc1a62019-01-24 21:57:40 -050069 PathPrefix: cf.KVStoreDataPrefix}
khenaidoo7ccedd52018-12-14 16:48:54 -050070 core.clusterDataRoot = model.NewRoot(&voltha.Voltha{}, &backend)
71 core.localDataRoot = model.NewRoot(&voltha.CoreInstance{}, &backend)
khenaidoo43c82122018-11-22 18:38:28 -050072 core.clusterDataProxy = core.clusterDataRoot.CreateProxy("/", false)
73 core.localDataProxy = core.localDataRoot.CreateProxy("/", false)
khenaidoob9203542018-09-17 22:56:37 -040074 return &core
75}
76
77func (core *Core) Start(ctx context.Context) {
khenaidoo19374072018-12-11 11:05:15 -050078 log.Info("starting-adaptercore", log.Fields{"coreId": core.instanceId})
khenaidoo9cdc1a62019-01-24 21:57:40 -050079 if err := core.startKafkaMessagingProxy(ctx); err != nil {
80 log.Fatal("Failure-starting-kafkaMessagingProxy")
81 }
khenaidoob9203542018-09-17 22:56:37 -040082 log.Info("values", log.Fields{"kmp": core.kmp})
khenaidoo19374072018-12-11 11:05:15 -050083 core.deviceMgr = newDeviceManager(core.kmp, core.clusterDataProxy, core.instanceId)
khenaidoo4d4802d2018-10-04 21:59:49 -040084 core.logicalDeviceMgr = newLogicalDeviceManager(core.deviceMgr, core.kmp, core.clusterDataProxy)
khenaidoo9cdc1a62019-01-24 21:57:40 -050085 if err := core.registerAdapterRequestHandler(ctx, core.instanceId, core.deviceMgr, core.logicalDeviceMgr, core.clusterDataProxy, core.localDataProxy); err != nil {
86 log.Fatal("Failure-registering-adapterRequestHandler")
87 }
khenaidoob9203542018-09-17 22:56:37 -040088 go core.startDeviceManager(ctx)
89 go core.startLogicalDeviceManager(ctx)
90 go core.startGRPCService(ctx)
91
khenaidoo19374072018-12-11 11:05:15 -050092 log.Info("adaptercore-started")
khenaidoob9203542018-09-17 22:56:37 -040093}
94
95func (core *Core) Stop(ctx context.Context) {
khenaidoo19374072018-12-11 11:05:15 -050096 log.Info("stopping-adaptercore")
khenaidoob9203542018-09-17 22:56:37 -040097 core.exitChannel <- 1
khenaidoo43c82122018-11-22 18:38:28 -050098 // Stop all the started services
99 core.grpcServer.Stop()
100 core.logicalDeviceMgr.stop(ctx)
101 core.deviceMgr.stop(ctx)
102 core.kmp.Stop()
khenaidoo19374072018-12-11 11:05:15 -0500103 log.Info("adaptercore-stopped")
khenaidoob9203542018-09-17 22:56:37 -0400104}
105
khenaidoo92e62c52018-10-03 14:02:54 -0400106//startGRPCService creates the grpc service handlers, registers it to the grpc server
khenaidoob9203542018-09-17 22:56:37 -0400107// and starts the server
108func (core *Core) startGRPCService(ctx context.Context) {
109 // create an insecure gserver server
110 core.grpcServer = grpcserver.NewGrpcServer(core.config.GrpcHost, core.config.GrpcPort, nil, false)
111 log.Info("grpc-server-created")
112
khenaidoob6080322019-01-29 21:47:38 -0500113 core.grpcNBIAPIHandler = NewAPIHandler(core.deviceMgr, core.logicalDeviceMgr, core.config.InCompetingMode, core.config.LongRunningRequestTimeout, core.config.DefaultRequestTimeout)
Richard Jankowskidbab94a2018-12-06 16:20:25 -0500114 core.logicalDeviceMgr.setGrpcNbiHandler(core.grpcNBIAPIHandler)
khenaidoob9203542018-09-17 22:56:37 -0400115 // Create a function to register the core GRPC service with the GRPC server
116 f := func(gs *grpc.Server) {
117 voltha.RegisterVolthaServiceServer(
118 gs,
Richard Jankowskidbab94a2018-12-06 16:20:25 -0500119 core.grpcNBIAPIHandler,
khenaidoob9203542018-09-17 22:56:37 -0400120 )
121 }
122
123 core.grpcServer.AddService(f)
124 log.Info("grpc-service-added")
125
126 // Start the server
127 core.grpcServer.Start(context.Background())
128 log.Info("grpc-server-started")
129}
130
131func (core *Core) startKafkaMessagingProxy(ctx context.Context) error {
132 log.Infow("starting-kafka-messaging-proxy", log.Fields{"host": core.config.KafkaAdapterHost,
133 "port": core.config.KafkaAdapterPort, "topic": core.config.CoreTopic})
134 var err error
khenaidoo43c82122018-11-22 18:38:28 -0500135 if core.kmp, err = kafka.NewInterContainerProxy(
136 kafka.InterContainerHost(core.config.KafkaAdapterHost),
137 kafka.InterContainerPort(core.config.KafkaAdapterPort),
138 kafka.MsgClient(core.kafkaClient),
khenaidoo79232702018-12-04 11:00:41 -0500139 kafka.DefaultTopic(&kafka.Topic{Name: core.config.CoreTopic}),
140 kafka.DeviceDiscoveryTopic(&kafka.Topic{Name: core.config.AffinityRouterTopic})); err != nil {
khenaidoob9203542018-09-17 22:56:37 -0400141 log.Errorw("fail-to-create-kafka-proxy", log.Fields{"error": err})
142 return err
143 }
144
145 if err = core.kmp.Start(); err != nil {
146 log.Fatalw("error-starting-messaging-proxy", log.Fields{"error": err})
147 return err
148 }
149
150 log.Info("kafka-messaging-proxy-created")
151 return nil
152}
153
khenaidoo91ecfd62018-11-04 17:13:42 -0500154func (core *Core) registerAdapterRequestHandler(ctx context.Context, coreInstanceId string, dMgr *DeviceManager, ldMgr *LogicalDeviceManager,
khenaidoob9203542018-09-17 22:56:37 -0400155 cdProxy *model.Proxy, ldProxy *model.Proxy) error {
khenaidoo91ecfd62018-11-04 17:13:42 -0500156 requestProxy := NewAdapterRequestHandlerProxy(coreInstanceId, dMgr, ldMgr, cdProxy, ldProxy)
khenaidoo43c82122018-11-22 18:38:28 -0500157 core.kmp.SubscribeWithRequestHandlerInterface(kafka.Topic{Name: core.config.CoreTopic}, requestProxy)
khenaidoob9203542018-09-17 22:56:37 -0400158
khenaidoo92e62c52018-10-03 14:02:54 -0400159 log.Info("request-handlers")
khenaidoob9203542018-09-17 22:56:37 -0400160 return nil
161}
162
163func (core *Core) startDeviceManager(ctx context.Context) {
164 // TODO: Interaction between the logicaldevicemanager and devicemanager should mostly occur via
165 // callbacks. For now, until the model is ready, devicemanager will keep a reference to the
166 // logicaldevicemanager to initiate the creation of logical devices
167 log.Info("starting-DeviceManager")
khenaidoo4d4802d2018-10-04 21:59:49 -0400168 core.deviceMgr.start(ctx, core.logicalDeviceMgr)
khenaidoob9203542018-09-17 22:56:37 -0400169 log.Info("started-DeviceManager")
170}
171
172func (core *Core) startLogicalDeviceManager(ctx context.Context) {
173 log.Info("starting-Logical-DeviceManager")
khenaidoo4d4802d2018-10-04 21:59:49 -0400174 core.logicalDeviceMgr.start(ctx)
khenaidoob9203542018-09-17 22:56:37 -0400175 log.Info("started-Logical-DeviceManager")
176}