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" |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 20 | "errors" |
| 21 | "fmt" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 22 | grpcserver "github.com/opencord/voltha-go/common/grpc" |
| 23 | "github.com/opencord/voltha-go/common/log" |
Richard Jankowski | e4d7766 | 2018-10-17 13:53:21 -0400 | [diff] [blame] | 24 | "github.com/opencord/voltha-go/db/kvstore" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 25 | "github.com/opencord/voltha-go/db/model" |
| 26 | "github.com/opencord/voltha-go/kafka" |
| 27 | "github.com/opencord/voltha-go/protos/voltha" |
| 28 | "github.com/opencord/voltha-go/rw_core/config" |
| 29 | "google.golang.org/grpc" |
khenaidoo | 6417b6c | 2019-03-01 18:18:01 -0500 | [diff] [blame] | 30 | "google.golang.org/grpc/codes" |
| 31 | "google.golang.org/grpc/status" |
| 32 | "sync" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 33 | ) |
| 34 | |
| 35 | type Core struct { |
| 36 | instanceId string |
| 37 | deviceMgr *DeviceManager |
| 38 | logicalDeviceMgr *LogicalDeviceManager |
| 39 | grpcServer *grpcserver.GrpcServer |
Richard Jankowski | dbab94a | 2018-12-06 16:20:25 -0500 | [diff] [blame] | 40 | grpcNBIAPIHandler *APIHandler |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 41 | adapterMgr *AdapterManager |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 42 | config *config.RWCoreFlags |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 43 | kmp *kafka.InterContainerProxy |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 44 | clusterDataRoot model.Root |
| 45 | localDataRoot model.Root |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 46 | clusterDataProxy *model.Proxy |
| 47 | localDataProxy *model.Proxy |
| 48 | exitChannel chan int |
Richard Jankowski | e4d7766 | 2018-10-17 13:53:21 -0400 | [diff] [blame] | 49 | kvClient kvstore.Client |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 50 | kafkaClient kafka.Client |
khenaidoo | 6417b6c | 2019-03-01 18:18:01 -0500 | [diff] [blame] | 51 | coreMembership *voltha.Membership |
| 52 | membershipLock *sync.RWMutex |
Richard Jankowski | 199fd86 | 2019-03-18 14:49:51 -0400 | [diff] [blame] | 53 | deviceOwnership *DeviceOwnership |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | func init() { |
| 57 | log.AddPackage(log.JSON, log.WarnLevel, nil) |
| 58 | } |
| 59 | |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 60 | 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] | 61 | var core Core |
| 62 | core.instanceId = id |
| 63 | core.exitChannel = make(chan int, 1) |
| 64 | core.config = cf |
Richard Jankowski | e4d7766 | 2018-10-17 13:53:21 -0400 | [diff] [blame] | 65 | core.kvClient = kvClient |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 66 | core.kafkaClient = kafkaClient |
Richard Jankowski | e4d7766 | 2018-10-17 13:53:21 -0400 | [diff] [blame] | 67 | |
| 68 | // Setup the KV store |
| 69 | // Do not call NewBackend constructor; it creates its own KV client |
khenaidoo | 91ecfd6 | 2018-11-04 17:13:42 -0500 | [diff] [blame] | 70 | // Commented the backend for now until the issue between the model and the KV store |
| 71 | // is resolved. |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame] | 72 | backend := model.Backend{ |
| 73 | Client: kvClient, |
| 74 | StoreType: cf.KVStoreType, |
| 75 | Host: cf.KVStoreHost, |
| 76 | Port: cf.KVStorePort, |
| 77 | Timeout: cf.KVStoreTimeout, |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 78 | PathPrefix: cf.KVStoreDataPrefix} |
khenaidoo | 7ccedd5 | 2018-12-14 16:48:54 -0500 | [diff] [blame] | 79 | core.clusterDataRoot = model.NewRoot(&voltha.Voltha{}, &backend) |
| 80 | core.localDataRoot = model.NewRoot(&voltha.CoreInstance{}, &backend) |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 81 | core.clusterDataProxy = core.clusterDataRoot.CreateProxy("/", false) |
| 82 | core.localDataProxy = core.localDataRoot.CreateProxy("/", false) |
khenaidoo | 6417b6c | 2019-03-01 18:18:01 -0500 | [diff] [blame] | 83 | core.membershipLock = &sync.RWMutex{} |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 84 | return &core |
| 85 | } |
| 86 | |
| 87 | func (core *Core) Start(ctx context.Context) { |
khenaidoo | 1937407 | 2018-12-11 11:05:15 -0500 | [diff] [blame] | 88 | log.Info("starting-adaptercore", log.Fields{"coreId": core.instanceId}) |
khenaidoo | 9cdc1a6 | 2019-01-24 21:57:40 -0500 | [diff] [blame] | 89 | if err := core.startKafkaMessagingProxy(ctx); err != nil { |
| 90 | log.Fatal("Failure-starting-kafkaMessagingProxy") |
| 91 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 92 | log.Info("values", log.Fields{"kmp": core.kmp}) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 93 | core.adapterMgr = newAdapterManager(core.clusterDataProxy, core.instanceId) |
Richard Jankowski | 199fd86 | 2019-03-18 14:49:51 -0400 | [diff] [blame] | 94 | core.deviceMgr = newDeviceManager(core) |
| 95 | core.logicalDeviceMgr = newLogicalDeviceManager(core, core.deviceMgr, core.kmp, core.clusterDataProxy) |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 96 | |
| 97 | if err := core.registerAdapterRequestHandlers(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] | 98 | log.Fatal("Failure-registering-adapterRequestHandler") |
| 99 | } |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 100 | go core.startDeviceManager(ctx) |
| 101 | go core.startLogicalDeviceManager(ctx) |
| 102 | go core.startGRPCService(ctx) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 103 | go core.startAdapterManager(ctx) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 104 | |
khenaidoo | 1ce37ad | 2019-03-24 22:07:24 -0400 | [diff] [blame] | 105 | // Setup device ownership context |
| 106 | core.deviceOwnership = NewDeviceOwnership(core.instanceId, core.kvClient, core.deviceMgr, core.logicalDeviceMgr, |
| 107 | "service/voltha/owns_device", 10) |
| 108 | |
khenaidoo | 1937407 | 2018-12-11 11:05:15 -0500 | [diff] [blame] | 109 | log.Info("adaptercore-started") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | func (core *Core) Stop(ctx context.Context) { |
khenaidoo | 1937407 | 2018-12-11 11:05:15 -0500 | [diff] [blame] | 113 | log.Info("stopping-adaptercore") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 114 | core.exitChannel <- 1 |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 115 | // Stop all the started services |
| 116 | core.grpcServer.Stop() |
| 117 | core.logicalDeviceMgr.stop(ctx) |
| 118 | core.deviceMgr.stop(ctx) |
| 119 | core.kmp.Stop() |
khenaidoo | 1937407 | 2018-12-11 11:05:15 -0500 | [diff] [blame] | 120 | log.Info("adaptercore-stopped") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 121 | } |
| 122 | |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 123 | //startGRPCService creates the grpc service handlers, registers it to the grpc server |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 124 | // and starts the server |
| 125 | func (core *Core) startGRPCService(ctx context.Context) { |
| 126 | // create an insecure gserver server |
| 127 | core.grpcServer = grpcserver.NewGrpcServer(core.config.GrpcHost, core.config.GrpcPort, nil, false) |
| 128 | log.Info("grpc-server-created") |
| 129 | |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 130 | //core.grpcNBIAPIHandler = NewAPIHandler(core.deviceMgr, core.logicalDeviceMgr, core.adapterMgr, core.config.InCompetingMode, core.config.LongRunningRequestTimeout, core.config.DefaultRequestTimeout) |
| 131 | core.grpcNBIAPIHandler = NewAPIHandler(core) |
Richard Jankowski | 46464e9 | 2019-03-05 11:53:55 -0500 | [diff] [blame] | 132 | log.Infow("grpc-handler", log.Fields{"core_binding_key": core.config.CoreBindingKey}) |
Richard Jankowski | dbab94a | 2018-12-06 16:20:25 -0500 | [diff] [blame] | 133 | core.logicalDeviceMgr.setGrpcNbiHandler(core.grpcNBIAPIHandler) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 134 | // Create a function to register the core GRPC service with the GRPC server |
| 135 | f := func(gs *grpc.Server) { |
| 136 | voltha.RegisterVolthaServiceServer( |
| 137 | gs, |
Richard Jankowski | dbab94a | 2018-12-06 16:20:25 -0500 | [diff] [blame] | 138 | core.grpcNBIAPIHandler, |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 139 | ) |
| 140 | } |
| 141 | |
| 142 | core.grpcServer.AddService(f) |
| 143 | log.Info("grpc-service-added") |
| 144 | |
| 145 | // Start the server |
| 146 | core.grpcServer.Start(context.Background()) |
| 147 | log.Info("grpc-server-started") |
| 148 | } |
| 149 | |
| 150 | func (core *Core) startKafkaMessagingProxy(ctx context.Context) error { |
| 151 | log.Infow("starting-kafka-messaging-proxy", log.Fields{"host": core.config.KafkaAdapterHost, |
| 152 | "port": core.config.KafkaAdapterPort, "topic": core.config.CoreTopic}) |
| 153 | var err error |
khenaidoo | 43c8212 | 2018-11-22 18:38:28 -0500 | [diff] [blame] | 154 | if core.kmp, err = kafka.NewInterContainerProxy( |
| 155 | kafka.InterContainerHost(core.config.KafkaAdapterHost), |
| 156 | kafka.InterContainerPort(core.config.KafkaAdapterPort), |
| 157 | kafka.MsgClient(core.kafkaClient), |
khenaidoo | 7923270 | 2018-12-04 11:00:41 -0500 | [diff] [blame] | 158 | kafka.DefaultTopic(&kafka.Topic{Name: core.config.CoreTopic}), |
| 159 | kafka.DeviceDiscoveryTopic(&kafka.Topic{Name: core.config.AffinityRouterTopic})); err != nil { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 160 | log.Errorw("fail-to-create-kafka-proxy", log.Fields{"error": err}) |
| 161 | return err |
| 162 | } |
| 163 | |
| 164 | if err = core.kmp.Start(); err != nil { |
| 165 | log.Fatalw("error-starting-messaging-proxy", log.Fields{"error": err}) |
| 166 | return err |
| 167 | } |
| 168 | |
| 169 | log.Info("kafka-messaging-proxy-created") |
| 170 | return nil |
| 171 | } |
| 172 | |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 173 | //func (core *Core) registerAdapterRequestHandler(ctx context.Context, coreInstanceId string, dMgr *DeviceManager, |
| 174 | // ldMgr *LogicalDeviceManager, aMgr *AdapterManager, cdProxy *model.Proxy, ldProxy *model.Proxy, |
| 175 | // ) error { |
| 176 | // requestProxy := NewAdapterRequestHandlerProxy(coreInstanceId, dMgr, ldMgr, aMgr, cdProxy, ldProxy, |
| 177 | // core.config.InCompetingMode, core.config.LongRunningRequestTimeout, core.config.DefaultRequestTimeout) |
| 178 | // core.kmp.SubscribeWithRequestHandlerInterface(kafka.Topic{Name: core.config.CoreTopic}, requestProxy) |
| 179 | // |
| 180 | // log.Info("request-handlers") |
| 181 | // return nil |
| 182 | //} |
| 183 | |
| 184 | func (core *Core) registerAdapterRequestHandlers(ctx context.Context, coreInstanceId string, dMgr *DeviceManager, |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 185 | ldMgr *LogicalDeviceManager, aMgr *AdapterManager, cdProxy *model.Proxy, ldProxy *model.Proxy, |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 186 | ) error { |
Richard Jankowski | 199fd86 | 2019-03-18 14:49:51 -0400 | [diff] [blame] | 187 | requestProxy := NewAdapterRequestHandlerProxy(core, coreInstanceId, dMgr, ldMgr, aMgr, cdProxy, ldProxy, |
khenaidoo | 297cd25 | 2019-02-07 22:10:23 -0500 | [diff] [blame] | 188 | core.config.InCompetingMode, core.config.LongRunningRequestTimeout, core.config.DefaultRequestTimeout) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 189 | |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 190 | // Register the broadcast topic to handle any core-bound broadcast requests |
| 191 | if err := core.kmp.SubscribeWithRequestHandlerInterface(kafka.Topic{Name: core.config.CoreTopic}, requestProxy); err != nil { |
| 192 | log.Fatalw("Failed-registering-broadcast-handler", log.Fields{"topic": core.config.CoreTopic}) |
| 193 | return err |
| 194 | } |
| 195 | |
| 196 | log.Info("request-handler-registered") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 197 | return nil |
| 198 | } |
| 199 | |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 200 | |
khenaidoo | 6417b6c | 2019-03-01 18:18:01 -0500 | [diff] [blame] | 201 | func (core *Core) isMembershipRegistrationComplete() bool { |
| 202 | core.membershipLock.RLock() |
| 203 | defer core.membershipLock.RUnlock() |
| 204 | return core.coreMembership != nil |
| 205 | } |
| 206 | |
| 207 | func (core *Core) updateCoreMembership(ctx context.Context, membership *voltha.Membership) error { |
| 208 | core.membershipLock.Lock() |
| 209 | defer core.membershipLock.Unlock() |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 210 | // Sanity check - can only register once |
khenaidoo | 6417b6c | 2019-03-01 18:18:01 -0500 | [diff] [blame] | 211 | if core.coreMembership != nil { |
| 212 | log.Warnw("core-reregistration-not-allowed", log.Fields{"membership": membership}) |
| 213 | return status.Errorf(codes.AlreadyExists, "%s", membership.GroupName) |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 214 | } |
| 215 | |
khenaidoo | 6417b6c | 2019-03-01 18:18:01 -0500 | [diff] [blame] | 216 | if membership == nil || membership.GroupName == "" { |
| 217 | return errors.New("invalid-membership-info") |
| 218 | } |
| 219 | |
| 220 | core.coreMembership = membership |
| 221 | |
| 222 | |
| 223 | // Use the group name to register a specific kafka topic for this container |
| 224 | go func(groupName string) { |
| 225 | // Register the core-pair topic to handle core-bound requests destined to the core pair |
| 226 | pTopic := kafka.Topic{Name: fmt.Sprintf("%s_%s", core.config.CoreTopic, groupName)} |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 227 | // TODO: Set a retry here to ensure this subscription happens |
| 228 | if err := core.kmp.SubscribeWithDefaultRequestHandler(pTopic, kafka.OffsetNewest); err != nil { |
| 229 | log.Fatalw("Failed-registering-pair-handler", log.Fields{"topic": core.config.CoreTopic}) |
| 230 | } |
khenaidoo | 6417b6c | 2019-03-01 18:18:01 -0500 | [diff] [blame] | 231 | // Update the CoreTopic to be used by the adapter proxy |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 232 | core.deviceMgr.adapterProxy.updateCoreTopic(&pTopic) |
khenaidoo | 6417b6c | 2019-03-01 18:18:01 -0500 | [diff] [blame] | 233 | }(membership.GroupName) |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 234 | |
khenaidoo | 6417b6c | 2019-03-01 18:18:01 -0500 | [diff] [blame] | 235 | log.Info("core-membership-registered") |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 236 | return nil |
| 237 | } |
| 238 | |
khenaidoo | 6417b6c | 2019-03-01 18:18:01 -0500 | [diff] [blame] | 239 | func (core *Core) getCoreMembership(ctx context.Context) *voltha.Membership { |
| 240 | core.membershipLock.RLock() |
| 241 | defer core.membershipLock.RUnlock() |
| 242 | return core.coreMembership |
| 243 | } |
| 244 | |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 245 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 246 | func (core *Core) startDeviceManager(ctx context.Context) { |
| 247 | // TODO: Interaction between the logicaldevicemanager and devicemanager should mostly occur via |
| 248 | // callbacks. For now, until the model is ready, devicemanager will keep a reference to the |
| 249 | // logicaldevicemanager to initiate the creation of logical devices |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 250 | log.Info("DeviceManager-Starting...") |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 251 | core.deviceMgr.start(ctx, core.logicalDeviceMgr) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 252 | log.Info("DeviceManager-Started") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | func (core *Core) startLogicalDeviceManager(ctx context.Context) { |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 256 | log.Info("Logical-DeviceManager-Starting...") |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 257 | core.logicalDeviceMgr.start(ctx) |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 258 | log.Info("Logical-DeviceManager-Started") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 259 | } |
khenaidoo | 21d5115 | 2019-02-01 13:48:37 -0500 | [diff] [blame] | 260 | |
| 261 | func (core *Core) startAdapterManager(ctx context.Context) { |
| 262 | log.Info("Adapter-Manager-Starting...") |
| 263 | core.adapterMgr.start(ctx) |
| 264 | log.Info("Adapter-Manager-Started") |
| 265 | } |