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