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