blob: 90aadae36d71278c06a35452d6459c31ba533f8a [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"
22 "github.com/opencord/voltha-go/db/kvstore"
23 "github.com/opencord/voltha-go/db/model"
Stephane Barbariea75791c2019-01-24 10:58:06 -050024 "github.com/opencord/voltha-go/ro_core/config"
Kent Hagerman0ab4cb22019-04-24 13:13:35 -040025 "github.com/opencord/voltha-protos/go/voltha"
Stephane Barbariea75791c2019-01-24 10:58:06 -050026 "google.golang.org/grpc"
27)
28
29type Core struct {
30 instanceId string
31 genericMgr *ModelProxyManager
32 deviceMgr *DeviceManager
33 logicalDeviceMgr *LogicalDeviceManager
34 grpcServer *grpcserver.GrpcServer
35 grpcNBIAPIHandler *APIHandler
36 config *config.ROCoreFlags
37 clusterDataRoot model.Root
38 localDataRoot model.Root
39 clusterDataProxy *model.Proxy
40 localDataProxy *model.Proxy
41 exitChannel chan int
42 kvClient kvstore.Client
43}
44
45func init() {
46 log.AddPackage(log.JSON, log.DebugLevel, nil)
47}
48
49func NewCore(id string, cf *config.ROCoreFlags, kvClient kvstore.Client) *Core {
50 var core Core
51 core.instanceId = id
52 core.exitChannel = make(chan int, 1)
53 core.config = cf
54 core.kvClient = kvClient
55
56 // Setup the KV store
57 // Do not call NewBackend constructor; it creates its own KV client
58 // Commented the backend for now until the issue between the model and the KV store
59 // is resolved.
60 backend := model.Backend{
61 Client: kvClient,
62 StoreType: cf.KVStoreType,
63 Host: cf.KVStoreHost,
64 Port: cf.KVStorePort,
65 Timeout: cf.KVStoreTimeout,
66 PathPrefix: "service/voltha"}
67 core.clusterDataRoot = model.NewRoot(&voltha.Voltha{}, &backend)
68 core.localDataRoot = model.NewRoot(&voltha.CoreInstance{}, &backend)
69 core.clusterDataProxy = core.clusterDataRoot.CreateProxy("/", false)
70 core.localDataProxy = core.localDataRoot.CreateProxy("/", false)
71 return &core
72}
73
74func (core *Core) Start(ctx context.Context) {
75 log.Info("starting-adaptercore", log.Fields{"coreId": core.instanceId})
76 core.genericMgr = newModelProxyManager(core.clusterDataProxy)
77 core.deviceMgr = newDeviceManager(core.clusterDataProxy, core.instanceId)
78 core.logicalDeviceMgr = newLogicalDeviceManager(core.deviceMgr, core.clusterDataProxy)
79 go core.startDeviceManager(ctx)
80 go core.startLogicalDeviceManager(ctx)
81 go core.startGRPCService(ctx)
82
83 log.Info("adaptercore-started")
84}
85
86func (core *Core) Stop(ctx context.Context) {
87 log.Info("stopping-adaptercore")
88 core.exitChannel <- 1
89 // Stop all the started services
90 core.grpcServer.Stop()
91 core.logicalDeviceMgr.stop(ctx)
92 core.deviceMgr.stop(ctx)
93 log.Info("adaptercore-stopped")
94}
95
96//startGRPCService creates the grpc service handlers, registers it to the grpc server
97// and starts the server
98func (core *Core) startGRPCService(ctx context.Context) {
99 // create an insecure gserver server
100 core.grpcServer = grpcserver.NewGrpcServer(core.config.GrpcHost, core.config.GrpcPort, nil, false)
101 log.Info("grpc-server-created")
102
103 core.grpcNBIAPIHandler = NewAPIHandler(core.genericMgr, core.deviceMgr, core.logicalDeviceMgr)
104 core.logicalDeviceMgr.setGrpcNbiHandler(core.grpcNBIAPIHandler)
105 // Create a function to register the core GRPC service with the GRPC server
106 f := func(gs *grpc.Server) {
107 voltha.RegisterVolthaServiceServer(
108 gs,
109 core.grpcNBIAPIHandler,
110 )
111 }
112
113 core.grpcServer.AddService(f)
114 log.Info("grpc-service-added")
115
116 // Start the server
117 core.grpcServer.Start(context.Background())
118 log.Info("grpc-server-started")
119}
120
121func (core *Core) startDeviceManager(ctx context.Context) {
122 // TODO: Interaction between the logicaldevicemanager and devicemanager should mostly occur via
123 // callbacks. For now, until the model is ready, devicemanager will keep a reference to the
124 // logicaldevicemanager to initiate the creation of logical devices
125 log.Info("starting-DeviceManager")
126 core.deviceMgr.start(ctx, core.logicalDeviceMgr)
127 log.Info("started-DeviceManager")
128}
129
130func (core *Core) startLogicalDeviceManager(ctx context.Context) {
131 log.Info("starting-Logical-DeviceManager")
132 core.logicalDeviceMgr.start(ctx)
133 log.Info("started-Logical-DeviceManager")
134}