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 |
| 35 | grpcNBIAPIHanfler *APIHandler |
| 36 | config *config.RWCoreFlags |
| 37 | kmp *kafka.KafkaMessagingProxy |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 38 | clusterDataRoot model.Root |
| 39 | localDataRoot model.Root |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 40 | clusterDataProxy *model.Proxy |
| 41 | localDataProxy *model.Proxy |
| 42 | exitChannel chan int |
Richard Jankowski | e4d7766 | 2018-10-17 13:53:21 -0400 | [diff] [blame] | 43 | kvClient kvstore.Client |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | func init() { |
| 47 | log.AddPackage(log.JSON, log.WarnLevel, nil) |
| 48 | } |
| 49 | |
Richard Jankowski | e4d7766 | 2018-10-17 13:53:21 -0400 | [diff] [blame] | 50 | func NewCore(id string, cf *config.RWCoreFlags, kvClient kvstore.Client) *Core { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 51 | var core Core |
| 52 | core.instanceId = id |
| 53 | core.exitChannel = make(chan int, 1) |
| 54 | core.config = cf |
Richard Jankowski | e4d7766 | 2018-10-17 13:53:21 -0400 | [diff] [blame] | 55 | core.kvClient = kvClient |
| 56 | |
| 57 | // Setup the KV store |
| 58 | // Do not call NewBackend constructor; it creates its own KV client |
khenaidoo | 91ecfd6 | 2018-11-04 17:13:42 -0500 | [diff] [blame^] | 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{}, nil) |
| 69 | core.localDataRoot = model.NewRoot(&voltha.CoreInstance{}, nil) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 70 | core.clusterDataProxy = core.clusterDataRoot.GetProxy("/", false) |
| 71 | core.localDataProxy = core.localDataRoot.GetProxy("/", false) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 72 | return &core |
| 73 | } |
| 74 | |
| 75 | func (core *Core) Start(ctx context.Context) { |
khenaidoo | 91ecfd6 | 2018-11-04 17:13:42 -0500 | [diff] [blame^] | 76 | log.Info("starting-core", log.Fields{"coreId": core.instanceId}) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 77 | core.startKafkaMessagingProxy(ctx) |
| 78 | log.Info("values", log.Fields{"kmp": core.kmp}) |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 79 | core.deviceMgr = newDeviceManager(core.kmp, core.clusterDataProxy) |
| 80 | core.logicalDeviceMgr = newLogicalDeviceManager(core.deviceMgr, core.kmp, core.clusterDataProxy) |
khenaidoo | 91ecfd6 | 2018-11-04 17:13:42 -0500 | [diff] [blame^] | 81 | core.registerAdapterRequestHandler(ctx, core.instanceId, core.deviceMgr, core.logicalDeviceMgr, core.localDataProxy, core.clusterDataProxy) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 82 | go core.startDeviceManager(ctx) |
| 83 | go core.startLogicalDeviceManager(ctx) |
| 84 | go core.startGRPCService(ctx) |
| 85 | |
| 86 | log.Info("core-started") |
| 87 | } |
| 88 | |
| 89 | func (core *Core) Stop(ctx context.Context) { |
| 90 | log.Info("stopping-core") |
| 91 | core.exitChannel <- 1 |
| 92 | log.Info("core-stopped") |
| 93 | } |
| 94 | |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 95 | //startGRPCService creates the grpc service handlers, registers it to the grpc server |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 96 | // and starts the server |
| 97 | func (core *Core) startGRPCService(ctx context.Context) { |
| 98 | // create an insecure gserver server |
| 99 | core.grpcServer = grpcserver.NewGrpcServer(core.config.GrpcHost, core.config.GrpcPort, nil, false) |
| 100 | log.Info("grpc-server-created") |
| 101 | |
khenaidoo | 9a46896 | 2018-09-19 15:33:13 -0400 | [diff] [blame] | 102 | core.grpcNBIAPIHanfler = NewAPIHandler(core.deviceMgr, core.logicalDeviceMgr) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 103 | // Create a function to register the core GRPC service with the GRPC server |
| 104 | f := func(gs *grpc.Server) { |
| 105 | voltha.RegisterVolthaServiceServer( |
| 106 | gs, |
| 107 | core.grpcNBIAPIHanfler, |
| 108 | ) |
| 109 | } |
| 110 | |
| 111 | core.grpcServer.AddService(f) |
| 112 | log.Info("grpc-service-added") |
| 113 | |
| 114 | // Start the server |
| 115 | core.grpcServer.Start(context.Background()) |
| 116 | log.Info("grpc-server-started") |
| 117 | } |
| 118 | |
| 119 | func (core *Core) startKafkaMessagingProxy(ctx context.Context) error { |
| 120 | log.Infow("starting-kafka-messaging-proxy", log.Fields{"host": core.config.KafkaAdapterHost, |
| 121 | "port": core.config.KafkaAdapterPort, "topic": core.config.CoreTopic}) |
| 122 | var err error |
| 123 | if core.kmp, err = kafka.NewKafkaMessagingProxy( |
| 124 | kafka.KafkaHost(core.config.KafkaAdapterHost), |
| 125 | kafka.KafkaPort(core.config.KafkaAdapterPort), |
| 126 | kafka.DefaultTopic(&kafka.Topic{Name: core.config.CoreTopic})); err != nil { |
| 127 | log.Errorw("fail-to-create-kafka-proxy", log.Fields{"error": err}) |
| 128 | return err |
| 129 | } |
| 130 | |
| 131 | if err = core.kmp.Start(); err != nil { |
| 132 | log.Fatalw("error-starting-messaging-proxy", log.Fields{"error": err}) |
| 133 | return err |
| 134 | } |
| 135 | |
| 136 | log.Info("kafka-messaging-proxy-created") |
| 137 | return nil |
| 138 | } |
| 139 | |
khenaidoo | 91ecfd6 | 2018-11-04 17:13:42 -0500 | [diff] [blame^] | 140 | func (core *Core) registerAdapterRequestHandler(ctx context.Context, coreInstanceId string, dMgr *DeviceManager, ldMgr *LogicalDeviceManager, |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 141 | cdProxy *model.Proxy, ldProxy *model.Proxy) error { |
khenaidoo | 91ecfd6 | 2018-11-04 17:13:42 -0500 | [diff] [blame^] | 142 | requestProxy := NewAdapterRequestHandlerProxy(coreInstanceId, dMgr, ldMgr, cdProxy, ldProxy) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 143 | core.kmp.SubscribeWithTarget(kafka.Topic{Name: core.config.CoreTopic}, requestProxy) |
| 144 | |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 145 | log.Info("request-handlers") |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 146 | return nil |
| 147 | } |
| 148 | |
| 149 | func (core *Core) startDeviceManager(ctx context.Context) { |
| 150 | // TODO: Interaction between the logicaldevicemanager and devicemanager should mostly occur via |
| 151 | // callbacks. For now, until the model is ready, devicemanager will keep a reference to the |
| 152 | // logicaldevicemanager to initiate the creation of logical devices |
| 153 | log.Info("starting-DeviceManager") |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 154 | core.deviceMgr.start(ctx, core.logicalDeviceMgr) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 155 | log.Info("started-DeviceManager") |
| 156 | } |
| 157 | |
| 158 | func (core *Core) startLogicalDeviceManager(ctx context.Context) { |
| 159 | log.Info("starting-Logical-DeviceManager") |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 160 | core.logicalDeviceMgr.start(ctx) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 161 | log.Info("started-Logical-DeviceManager") |
| 162 | } |