blob: 45bbde29007b3a2c4e1e5334ad547e6e8723b994 [file] [log] [blame]
Stephane Barbariea75791c2019-01-24 10:58:06 -05001/*
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"
sbarbari17d7e222019-11-05 10:02:29 -050020 "github.com/opencord/voltha-go/db/model"
Stephane Barbariea75791c2019-01-24 10:58:06 -050021 "github.com/opencord/voltha-go/ro_core/config"
sbarbari17d7e222019-11-05 10:02:29 -050022 "github.com/opencord/voltha-lib-go/v2/pkg/db"
Scott Baker807addd2019-10-24 15:16:21 -070023 "github.com/opencord/voltha-lib-go/v2/pkg/db/kvstore"
Scott Baker807addd2019-10-24 15:16:21 -070024 grpcserver "github.com/opencord/voltha-lib-go/v2/pkg/grpc"
25 "github.com/opencord/voltha-lib-go/v2/pkg/log"
26 "github.com/opencord/voltha-lib-go/v2/pkg/probe"
Scott Baker555307d2019-11-04 08:58:01 -080027 "github.com/opencord/voltha-protos/v2/go/voltha"
Stephane Barbariea75791c2019-01-24 10:58:06 -050028 "google.golang.org/grpc"
29)
30
31type Core struct {
32 instanceId string
33 genericMgr *ModelProxyManager
34 deviceMgr *DeviceManager
35 logicalDeviceMgr *LogicalDeviceManager
36 grpcServer *grpcserver.GrpcServer
37 grpcNBIAPIHandler *APIHandler
38 config *config.ROCoreFlags
39 clusterDataRoot model.Root
40 localDataRoot model.Root
41 clusterDataProxy *model.Proxy
42 localDataProxy *model.Proxy
43 exitChannel chan int
44 kvClient kvstore.Client
45}
46
47func init() {
48 log.AddPackage(log.JSON, log.DebugLevel, nil)
49}
50
51func NewCore(id string, cf *config.ROCoreFlags, kvClient kvstore.Client) *Core {
52 var core Core
53 core.instanceId = id
54 core.exitChannel = make(chan int, 1)
55 core.config = cf
56 core.kvClient = kvClient
57
58 // Setup the KV store
59 // Do not call NewBackend constructor; it creates its own KV client
60 // Commented the backend for now until the issue between the model and the KV store
61 // is resolved.
sbarbari17d7e222019-11-05 10:02:29 -050062 backend := db.Backend{
Stephane Barbariea75791c2019-01-24 10:58:06 -050063 Client: kvClient,
64 StoreType: cf.KVStoreType,
65 Host: cf.KVStoreHost,
66 Port: cf.KVStorePort,
67 Timeout: cf.KVStoreTimeout,
68 PathPrefix: "service/voltha"}
69 core.clusterDataRoot = model.NewRoot(&voltha.Voltha{}, &backend)
70 core.localDataRoot = model.NewRoot(&voltha.CoreInstance{}, &backend)
Stephane Barbarieef6650d2019-07-18 12:15:09 -040071 core.clusterDataProxy = core.clusterDataRoot.CreateProxy(context.Background(), "/", false)
72 core.localDataProxy = core.localDataRoot.CreateProxy(context.Background(), "/", false)
Stephane Barbariea75791c2019-01-24 10:58:06 -050073 return &core
74}
75
76func (core *Core) Start(ctx context.Context) {
77 log.Info("starting-adaptercore", log.Fields{"coreId": core.instanceId})
78 core.genericMgr = newModelProxyManager(core.clusterDataProxy)
79 core.deviceMgr = newDeviceManager(core.clusterDataProxy, core.instanceId)
80 core.logicalDeviceMgr = newLogicalDeviceManager(core.deviceMgr, core.clusterDataProxy)
81 go core.startDeviceManager(ctx)
82 go core.startLogicalDeviceManager(ctx)
83 go core.startGRPCService(ctx)
84
85 log.Info("adaptercore-started")
86}
87
88func (core *Core) Stop(ctx context.Context) {
89 log.Info("stopping-adaptercore")
David Bainbridgef794fc52019-10-03 22:37:12 +000090 if core.exitChannel != nil {
91 core.exitChannel <- 1
92 }
Stephane Barbariea75791c2019-01-24 10:58:06 -050093 // Stop all the started services
David Bainbridgef794fc52019-10-03 22:37:12 +000094 if core.grpcServer != nil {
95 core.grpcServer.Stop()
96 }
97 if core.logicalDeviceMgr != nil {
98 core.logicalDeviceMgr.stop(ctx)
99 }
100 if core.deviceMgr != nil {
101 core.deviceMgr.stop(ctx)
102 }
Stephane Barbariea75791c2019-01-24 10:58:06 -0500103 log.Info("adaptercore-stopped")
104}
105
106//startGRPCService creates the grpc service handlers, registers it to the grpc server
107// 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
113 core.grpcNBIAPIHandler = NewAPIHandler(core.genericMgr, core.deviceMgr, core.logicalDeviceMgr)
114 core.logicalDeviceMgr.setGrpcNbiHandler(core.grpcNBIAPIHandler)
115 // Create a function to register the core GRPC service with the GRPC server
116 f := func(gs *grpc.Server) {
117 voltha.RegisterVolthaServiceServer(
118 gs,
119 core.grpcNBIAPIHandler,
120 )
121 }
122
123 core.grpcServer.AddService(f)
124 log.Info("grpc-service-added")
125
Hardik Windlassdc63dde2019-09-30 07:15:13 +0000126 /*
127 * Start the GRPC server
128 *
129 * This is a bit sub-optimal here as the grpcServer.Start call does not return (blocks)
130 * until something fails, but we want to send a "start" status update. As written this
131 * means that we are actually sending the "start" status update before the server is
132 * started, which means it is possible that the status is "running" before it actually is.
133 *
134 * This means that there is a small window in which the core could return its status as
135 * ready, when it really isn't.
136 */
137 probe.UpdateStatusFromContext(ctx, "grpc-service", probe.ServiceStatusRunning)
138
Stephane Barbariea75791c2019-01-24 10:58:06 -0500139 // Start the server
Stephane Barbariea75791c2019-01-24 10:58:06 -0500140 log.Info("grpc-server-started")
Hardik Windlassdc63dde2019-09-30 07:15:13 +0000141 core.grpcServer.Start(context.Background())
142
143 probe.UpdateStatusFromContext(ctx, "grpc-service", probe.ServiceStatusStopped)
Stephane Barbariea75791c2019-01-24 10:58:06 -0500144}
145
146func (core *Core) startDeviceManager(ctx context.Context) {
147 // TODO: Interaction between the logicaldevicemanager and devicemanager should mostly occur via
148 // callbacks. For now, until the model is ready, devicemanager will keep a reference to the
149 // logicaldevicemanager to initiate the creation of logical devices
150 log.Info("starting-DeviceManager")
151 core.deviceMgr.start(ctx, core.logicalDeviceMgr)
152 log.Info("started-DeviceManager")
153}
154
155func (core *Core) startLogicalDeviceManager(ctx context.Context) {
156 log.Info("starting-Logical-DeviceManager")
157 core.logicalDeviceMgr.start(ctx)
158 log.Info("started-Logical-DeviceManager")
159}