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" |
| 20 | grpcserver "github.com/opencord/voltha-go/common/grpc" |
| 21 | "github.com/opencord/voltha-go/common/log" |
Hardik Windlass | dc63dde | 2019-09-30 07:15:13 +0000 | [diff] [blame] | 22 | "github.com/opencord/voltha-go/common/probe" |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 23 | "github.com/opencord/voltha-go/db/kvstore" |
| 24 | "github.com/opencord/voltha-go/db/model" |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 25 | "github.com/opencord/voltha-go/ro_core/config" |
Kent Hagerman | 0ab4cb2 | 2019-04-24 13:13:35 -0400 | [diff] [blame] | 26 | "github.com/opencord/voltha-protos/go/voltha" |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 27 | "google.golang.org/grpc" |
| 28 | ) |
| 29 | |
| 30 | type 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 | |
| 46 | func init() { |
| 47 | log.AddPackage(log.JSON, log.DebugLevel, nil) |
| 48 | } |
| 49 | |
| 50 | func 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 Barbarie | ef6650d | 2019-07-18 12:15:09 -0400 | [diff] [blame] | 70 | core.clusterDataProxy = core.clusterDataRoot.CreateProxy(context.Background(), "/", false) |
| 71 | core.localDataProxy = core.localDataRoot.CreateProxy(context.Background(), "/", false) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 72 | return &core |
| 73 | } |
| 74 | |
| 75 | func (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 | |
| 87 | func (core *Core) Stop(ctx context.Context) { |
| 88 | log.Info("stopping-adaptercore") |
David Bainbridge | f794fc5 | 2019-10-03 22:37:12 +0000 | [diff] [blame] | 89 | if core.exitChannel != nil { |
| 90 | core.exitChannel <- 1 |
| 91 | } |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 92 | // Stop all the started services |
David Bainbridge | f794fc5 | 2019-10-03 22:37:12 +0000 | [diff] [blame] | 93 | 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 Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 102 | log.Info("adaptercore-stopped") |
| 103 | } |
| 104 | |
| 105 | //startGRPCService creates the grpc service handlers, registers it to the grpc server |
| 106 | // and starts the server |
| 107 | func (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 Windlass | dc63dde | 2019-09-30 07:15:13 +0000 | [diff] [blame] | 125 | /* |
| 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 Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 138 | // Start the server |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 139 | log.Info("grpc-server-started") |
Hardik Windlass | dc63dde | 2019-09-30 07:15:13 +0000 | [diff] [blame] | 140 | core.grpcServer.Start(context.Background()) |
| 141 | |
| 142 | probe.UpdateStatusFromContext(ctx, "grpc-service", probe.ServiceStatusStopped) |
Stephane Barbarie | a75791c | 2019-01-24 10:58:06 -0500 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | func (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 | |
| 154 | func (core *Core) startLogicalDeviceManager(ctx context.Context) { |
| 155 | log.Info("starting-Logical-DeviceManager") |
| 156 | core.logicalDeviceMgr.start(ctx) |
| 157 | log.Info("started-Logical-DeviceManager") |
| 158 | } |