blob: cd27a4296bf031bab84faff542c34ef2fdd43199 [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"
20 grpcserver "github.com/opencord/voltha-go/common/grpc"
21 "github.com/opencord/voltha-go/common/log"
Hardik Windlassdc63dde2019-09-30 07:15:13 +000022 "github.com/opencord/voltha-go/common/probe"
Stephane Barbariea75791c2019-01-24 10:58:06 -050023 "github.com/opencord/voltha-go/db/kvstore"
24 "github.com/opencord/voltha-go/db/model"
Stephane Barbariea75791c2019-01-24 10:58:06 -050025 "github.com/opencord/voltha-go/ro_core/config"
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040026 "github.com/opencord/voltha-protos/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")
89 core.exitChannel <- 1
90 // Stop all the started services
91 core.grpcServer.Stop()
92 core.logicalDeviceMgr.stop(ctx)
93 core.deviceMgr.stop(ctx)
94 log.Info("adaptercore-stopped")
95}
96
97//startGRPCService creates the grpc service handlers, registers it to the grpc server
98// and starts the server
99func (core *Core) startGRPCService(ctx context.Context) {
100 // create an insecure gserver server
101 core.grpcServer = grpcserver.NewGrpcServer(core.config.GrpcHost, core.config.GrpcPort, nil, false)
102 log.Info("grpc-server-created")
103
104 core.grpcNBIAPIHandler = NewAPIHandler(core.genericMgr, core.deviceMgr, core.logicalDeviceMgr)
105 core.logicalDeviceMgr.setGrpcNbiHandler(core.grpcNBIAPIHandler)
106 // Create a function to register the core GRPC service with the GRPC server
107 f := func(gs *grpc.Server) {
108 voltha.RegisterVolthaServiceServer(
109 gs,
110 core.grpcNBIAPIHandler,
111 )
112 }
113
114 core.grpcServer.AddService(f)
115 log.Info("grpc-service-added")
116
Hardik Windlassdc63dde2019-09-30 07:15:13 +0000117 /*
118 * Start the GRPC server
119 *
120 * This is a bit sub-optimal here as the grpcServer.Start call does not return (blocks)
121 * until something fails, but we want to send a "start" status update. As written this
122 * means that we are actually sending the "start" status update before the server is
123 * started, which means it is possible that the status is "running" before it actually is.
124 *
125 * This means that there is a small window in which the core could return its status as
126 * ready, when it really isn't.
127 */
128 probe.UpdateStatusFromContext(ctx, "grpc-service", probe.ServiceStatusRunning)
129
Stephane Barbariea75791c2019-01-24 10:58:06 -0500130 // Start the server
Stephane Barbariea75791c2019-01-24 10:58:06 -0500131 log.Info("grpc-server-started")
Hardik Windlassdc63dde2019-09-30 07:15:13 +0000132 core.grpcServer.Start(context.Background())
133
134 probe.UpdateStatusFromContext(ctx, "grpc-service", probe.ServiceStatusStopped)
Stephane Barbariea75791c2019-01-24 10:58:06 -0500135}
136
137func (core *Core) startDeviceManager(ctx context.Context) {
138 // TODO: Interaction between the logicaldevicemanager and devicemanager should mostly occur via
139 // callbacks. For now, until the model is ready, devicemanager will keep a reference to the
140 // logicaldevicemanager to initiate the creation of logical devices
141 log.Info("starting-DeviceManager")
142 core.deviceMgr.start(ctx, core.logicalDeviceMgr)
143 log.Info("started-DeviceManager")
144}
145
146func (core *Core) startLogicalDeviceManager(ctx context.Context) {
147 log.Info("starting-Logical-DeviceManager")
148 core.logicalDeviceMgr.start(ctx)
149 log.Info("started-Logical-DeviceManager")
150}