khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [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" |
Richard Jankowski | e4d7766 | 2018-10-17 13:53:21 -0400 | [diff] [blame] | 22 | "github.com/opencord/voltha-go/db/kvstore" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 23 | "github.com/opencord/voltha-go/db/model" |
| 24 | "github.com/opencord/voltha-go/kafka" |
| 25 | "github.com/opencord/voltha-go/protos/voltha" |
| 26 | "github.com/opencord/voltha-go/rw_core/config" |
| 27 | "google.golang.org/grpc" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 28 | ) |
| 29 | |
| 30 | type Core struct { |
| 31 | instanceId string |
| 32 | deviceMgr *DeviceManager |
| 33 | logicalDeviceMgr *LogicalDeviceManager |
| 34 | grpcServer *grpcserver.GrpcServer |
Richard Jankowski | dbab94a | 2018-12-06 16:20:25 -0500 | [diff] [blame] | 35 | grpcNBIAPIHandler *APIHandler |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 36 | adapterMgr *AdapterManager |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 37 | config *config.RWCoreFlags |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 38 | kmp *kafka.InterContainerProxy |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 39 | clusterDataRoot model.Root |
| 40 | localDataRoot model.Root |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 41 | clusterDataProxy *model.Proxy |
| 42 | localDataProxy *model.Proxy |
| 43 | exitChannel chan int |
Richard Jankowski | e4d7766 | 2018-10-17 13:53:21 -0400 | [diff] [blame] | 44 | kvClient kvstore.Client |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 45 | kafkaClient kafka.Client |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | func init() { |
| 49 | log.AddPackage(log.JSON, log.WarnLevel, nil) |
| 50 | } |
| 51 | |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 52 | func NewCore(id string, cf *config.RWCoreFlags, kvClient kvstore.Client, kafkaClient kafka.Client) *Core { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 53 | var core Core |
| 54 | core.instanceId = id |
| 55 | core.exitChannel = make(chan int, 1) |
| 56 | core.config = cf |
Richard Jankowski | e4d7766 | 2018-10-17 13:53:21 -0400 | [diff] [blame] | 57 | core.kvClient = kvClient |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 58 | core.kafkaClient = kafkaClient |
Richard Jankowski | e4d7766 | 2018-10-17 13:53:21 -0400 | [diff] [blame] | 59 | |
| 60 | // Setup the KV store |
| 61 | // Do not call NewBackend constructor; it creates its own KV client |
khenaidoo | 91ecfd6 | 2018-11-04 17:13:42 -0500 | [diff] [blame] | 62 | // Commented the backend for now until the issue between the model and the KV store |
| 63 | // is resolved. |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame] | 64 | backend := model.Backend{ |
| 65 | Client: kvClient, |
| 66 | StoreType: cf.KVStoreType, |
| 67 | Host: cf.KVStoreHost, |
| 68 | Port: cf.KVStorePort, |
| 69 | Timeout: cf.KVStoreTimeout, |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 70 | PathPrefix: cf.KVStoreDataPrefix} |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame] | 71 | core.clusterDataRoot = model.NewRoot(&voltha.Voltha{}, &backend) |
| 72 | core.localDataRoot = model.NewRoot(&voltha.CoreInstance{}, &backend) |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 73 | core.clusterDataProxy = core.clusterDataRoot.CreateProxy("/", false) |
| 74 | core.localDataProxy = core.localDataRoot.CreateProxy("/", false) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 75 | return &core |
| 76 | } |
| 77 | |
| 78 | func (core *Core) Start(ctx context.Context) { |
khenaidoo | 1937407 | 2018-12-11 11:05:15 -0500 | [diff] [blame] | 79 | log.Info("starting-adaptercore", log.Fields{"coreId": core.instanceId}) |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 80 | if err := core.startKafkaMessagingProxy(ctx); err != nil { |
| 81 | log.Fatal("Failure-starting-kafkaMessagingProxy") |
| 82 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 83 | log.Info("values", log.Fields{"kmp": core.kmp}) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 84 | core.adapterMgr = newAdapterManager(core.clusterDataProxy, core.instanceId) |
| 85 | core.deviceMgr = newDeviceManager(core.kmp, core.clusterDataProxy, core.adapterMgr, core.instanceId) |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 86 | core.logicalDeviceMgr = newLogicalDeviceManager(core.deviceMgr, core.kmp, core.clusterDataProxy) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 87 | if err := core.registerAdapterRequestHandler(ctx, core.instanceId, core.deviceMgr, core.logicalDeviceMgr, core.adapterMgr, core.clusterDataProxy, core.localDataProxy); err != nil { |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 88 | log.Fatal("Failure-registering-adapterRequestHandler") |
| 89 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 90 | go core.startDeviceManager(ctx) |
| 91 | go core.startLogicalDeviceManager(ctx) |
| 92 | go core.startGRPCService(ctx) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 93 | go core.startAdapterManager(ctx) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 94 | |
khenaidoo | 1937407 | 2018-12-11 11:05:15 -0500 | [diff] [blame] | 95 | log.Info("adaptercore-started") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | func (core *Core) Stop(ctx context.Context) { |
khenaidoo | 1937407 | 2018-12-11 11:05:15 -0500 | [diff] [blame] | 99 | log.Info("stopping-adaptercore") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 100 | core.exitChannel <- 1 |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 101 | // Stop all the started services |
| 102 | core.grpcServer.Stop() |
| 103 | core.logicalDeviceMgr.stop(ctx) |
| 104 | core.deviceMgr.stop(ctx) |
| 105 | core.kmp.Stop() |
khenaidoo | 1937407 | 2018-12-11 11:05:15 -0500 | [diff] [blame] | 106 | log.Info("adaptercore-stopped") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 107 | } |
| 108 | |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 109 | //startGRPCService creates the grpc service handlers, registers it to the grpc server |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 110 | // and starts the server |
| 111 | func (core *Core) startGRPCService(ctx context.Context) { |
| 112 | // create an insecure gserver server |
| 113 | core.grpcServer = grpcserver.NewGrpcServer(core.config.GrpcHost, core.config.GrpcPort, nil, false) |
| 114 | log.Info("grpc-server-created") |
| 115 | |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 116 | core.grpcNBIAPIHandler = NewAPIHandler(core.deviceMgr, core.logicalDeviceMgr, core.adapterMgr, core.config.InCompetingMode, core.config.LongRunningRequestTimeout, core.config.DefaultRequestTimeout) |
Richard Jankowski | dbab94a | 2018-12-06 16:20:25 -0500 | [diff] [blame] | 117 | core.logicalDeviceMgr.setGrpcNbiHandler(core.grpcNBIAPIHandler) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 118 | // Create a function to register the core GRPC service with the GRPC server |
| 119 | f := func(gs *grpc.Server) { |
| 120 | voltha.RegisterVolthaServiceServer( |
| 121 | gs, |
Richard Jankowski | dbab94a | 2018-12-06 16:20:25 -0500 | [diff] [blame] | 122 | core.grpcNBIAPIHandler, |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 123 | ) |
| 124 | } |
| 125 | |
| 126 | core.grpcServer.AddService(f) |
| 127 | log.Info("grpc-service-added") |
| 128 | |
| 129 | // Start the server |
| 130 | core.grpcServer.Start(context.Background()) |
| 131 | log.Info("grpc-server-started") |
| 132 | } |
| 133 | |
| 134 | func (core *Core) startKafkaMessagingProxy(ctx context.Context) error { |
| 135 | log.Infow("starting-kafka-messaging-proxy", log.Fields{"host": core.config.KafkaAdapterHost, |
| 136 | "port": core.config.KafkaAdapterPort, "topic": core.config.CoreTopic}) |
| 137 | var err error |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 138 | if core.kmp, err = kafka.NewInterContainerProxy( |
| 139 | kafka.InterContainerHost(core.config.KafkaAdapterHost), |
| 140 | kafka.InterContainerPort(core.config.KafkaAdapterPort), |
| 141 | kafka.MsgClient(core.kafkaClient), |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 142 | kafka.DefaultTopic(&kafka.Topic{Name: core.config.CoreTopic}), |
| 143 | kafka.DeviceDiscoveryTopic(&kafka.Topic{Name: core.config.AffinityRouterTopic})); err != nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 144 | log.Errorw("fail-to-create-kafka-proxy", log.Fields{"error": err}) |
| 145 | return err |
| 146 | } |
| 147 | |
| 148 | if err = core.kmp.Start(); err != nil { |
| 149 | log.Fatalw("error-starting-messaging-proxy", log.Fields{"error": err}) |
| 150 | return err |
| 151 | } |
| 152 | |
| 153 | log.Info("kafka-messaging-proxy-created") |
| 154 | return nil |
| 155 | } |
| 156 | |
khenaidoo | 91ecfd6 | 2018-11-04 17:13:42 -0500 | [diff] [blame] | 157 | func (core *Core) registerAdapterRequestHandler(ctx context.Context, coreInstanceId string, dMgr *DeviceManager, ldMgr *LogicalDeviceManager, |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 158 | aMgr *AdapterManager, cdProxy *model.Proxy, ldProxy *model.Proxy) error { |
| 159 | requestProxy := NewAdapterRequestHandlerProxy(coreInstanceId, dMgr, ldMgr, aMgr, cdProxy, ldProxy) |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 160 | core.kmp.SubscribeWithRequestHandlerInterface(kafka.Topic{Name: core.config.CoreTopic}, requestProxy) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 161 | |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 162 | log.Info("request-handlers") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 163 | return nil |
| 164 | } |
| 165 | |
| 166 | func (core *Core) startDeviceManager(ctx context.Context) { |
| 167 | // TODO: Interaction between the logicaldevicemanager and devicemanager should mostly occur via |
| 168 | // callbacks. For now, until the model is ready, devicemanager will keep a reference to the |
| 169 | // logicaldevicemanager to initiate the creation of logical devices |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 170 | log.Info("DeviceManager-Starting...") |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 171 | core.deviceMgr.start(ctx, core.logicalDeviceMgr) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 172 | log.Info("DeviceManager-Started") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | func (core *Core) startLogicalDeviceManager(ctx context.Context) { |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 176 | log.Info("Logical-DeviceManager-Starting...") |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 177 | core.logicalDeviceMgr.start(ctx) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 178 | log.Info("Logical-DeviceManager-Started") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 179 | } |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 180 | |
| 181 | func (core *Core) startAdapterManager(ctx context.Context) { |
| 182 | log.Info("Adapter-Manager-Starting...") |
| 183 | core.adapterMgr.start(ctx) |
| 184 | log.Info("Adapter-Manager-Started") |
| 185 | } |