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