Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package core |
| 17 | |
| 18 | import ( |
| 19 | "context" |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 20 | "github.com/opencord/voltha-go/db/model" |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 21 | "github.com/opencord/voltha-go/ro_core/config" |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 22 | "github.com/opencord/voltha-lib-go/v2/pkg/db" |
Scott Baker | 807addd | 2019-10-24 15:16:21 -0700 | [diff] [blame] | 23 | "github.com/opencord/voltha-lib-go/v2/pkg/db/kvstore" |
Scott Baker | 807addd | 2019-10-24 15:16:21 -0700 | [diff] [blame] | 24 | 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 Baker | 555307d | 2019-11-04 08:58:01 -0800 | [diff] [blame] | 27 | "github.com/opencord/voltha-protos/v2/go/voltha" |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 28 | "google.golang.org/grpc" |
| 29 | ) |
| 30 | |
| 31 | type 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 | |
| 47 | func init() { |
| 48 | log.AddPackage(log.JSON, log.DebugLevel, nil) |
| 49 | } |
| 50 | |
| 51 | func 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. |
sbarbari | 17d7e22 | 2019-11-05 10:02:29 -0500 | [diff] [blame] | 62 | backend := db.Backend{ |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 63 | 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 Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 71 | core.clusterDataProxy = core.clusterDataRoot.CreateProxy(context.Background(), "/", false) |
| 72 | core.localDataProxy = core.localDataRoot.CreateProxy(context.Background(), "/", false) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 73 | return &core |
| 74 | } |
| 75 | |
| 76 | func (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 | |
| 88 | func (core *Core) Stop(ctx context.Context) { |
| 89 | log.Info("stopping-adaptercore") |
David Bainbridge | f794fc5 | 2019-10-03 22:37:12 +0000 | [diff] [blame] | 90 | if core.exitChannel != nil { |
| 91 | core.exitChannel <- 1 |
| 92 | } |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 93 | // Stop all the started services |
David Bainbridge | f794fc5 | 2019-10-03 22:37:12 +0000 | [diff] [blame] | 94 | 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 Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 103 | log.Info("adaptercore-stopped") |
| 104 | } |
| 105 | |
| 106 | //startGRPCService creates the grpc service handlers, registers it to the grpc server |
| 107 | // and starts the server |
| 108 | func (core *Core) startGRPCService(ctx context.Context) { |
| 109 | // create an insecure gserver server |
Scott Baker | ee6a087 | 2019-10-29 15:59:52 -0700 | [diff] [blame] | 110 | core.grpcServer = grpcserver.NewGrpcServer(core.config.GrpcHost, core.config.GrpcPort, nil, false, nil) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 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 Windlass | dc63dde | 2019-09-30 07:15:13 +0000 | [diff] [blame] | 126 | /* |
| 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 Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 139 | // Start the server |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 140 | log.Info("grpc-server-started") |
Hardik Windlass | dc63dde | 2019-09-30 07:15:13 +0000 | [diff] [blame] | 141 | core.grpcServer.Start(context.Background()) |
| 142 | |
| 143 | probe.UpdateStatusFromContext(ctx, "grpc-service", probe.ServiceStatusStopped) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | func (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 | |
| 155 | func (core *Core) startLogicalDeviceManager(ctx context.Context) { |
| 156 | log.Info("starting-Logical-DeviceManager") |
| 157 | core.logicalDeviceMgr.start(ctx) |
| 158 | log.Info("started-Logical-DeviceManager") |
| 159 | } |