blob: 4295c2d3058c5365aa325c741a5abc04eb00c569 [file] [log] [blame]
khenaidoobf6e7bb2018-08-14 22:27:29 -04001/*
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 */
Stephane Barbariedc5022d2018-11-19 15:21:44 -050016
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040017package model
18
19import (
20 "encoding/json"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080021
Stephane Barbarie126101e2018-10-11 16:18:48 -040022 "github.com/golang/protobuf/proto"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080023 "github.com/opencord/voltha-lib-go/v3/pkg/log"
24 "github.com/opencord/voltha-protos/v3/go/voltha"
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040025)
26
Stephane Barbariedc5022d2018-11-19 15:21:44 -050027// EventBus contains the details required to communicate with the event bus mechanism
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040028type EventBus struct {
29 client *EventBusClient
30 topic string
31}
32
Stephane Barbariedc5022d2018-11-19 15:21:44 -050033// ignoredCallbacks keeps a list of callbacks that should not be advertised on the event bus
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040034var (
Stephane Barbariedc5022d2018-11-19 15:21:44 -050035 ignoredCallbacks = map[CallbackType]struct{}{
npujar9a30c702019-11-14 17:06:39 +053036 PreAdd: {},
37 Get: {},
38 PostListchange: {},
39 PreRemove: {},
40 PreUpdate: {},
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040041 }
42)
43
Stephane Barbariedc5022d2018-11-19 15:21:44 -050044// NewEventBus creates a new instance of the EventBus structure
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040045func NewEventBus() *EventBus {
46 bus := &EventBus{
47 client: NewEventBusClient(),
48 topic: "model-change-events",
49 }
50 return bus
51}
52
Stephane Barbariedc5022d2018-11-19 15:21:44 -050053// Advertise will publish the provided information to the event bus
Stephane Barbarie694e2b92018-09-07 12:17:36 -040054func (bus *EventBus) Advertise(args ...interface{}) interface{} {
55 eventType := args[0].(CallbackType)
Stephane Barbarie126101e2018-10-11 16:18:48 -040056 hash := args[1].(string)
57 data := args[2:]
Stephane Barbarie694e2b92018-09-07 12:17:36 -040058
Stephane Barbariedc5022d2018-11-19 15:21:44 -050059 if _, ok := ignoredCallbacks[eventType]; ok {
Stephane Barbarie126101e2018-10-11 16:18:48 -040060 log.Debugf("ignoring event - type:%s, data:%+v", eventType, data)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040061 }
serkant.uluderya2ae470f2020-01-21 11:13:09 -080062 var kind voltha.ConfigEventType_Types
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040063 switch eventType {
npujar9a30c702019-11-14 17:06:39 +053064 case PostAdd:
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040065 kind = voltha.ConfigEventType_add
npujar9a30c702019-11-14 17:06:39 +053066 case PostRemove:
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040067 kind = voltha.ConfigEventType_remove
68 default:
69 kind = voltha.ConfigEventType_update
70 }
71
72 var msg []byte
73 var err error
74 if IsProtoMessage(data) {
Stephane Barbarie126101e2018-10-11 16:18:48 -040075 if msg, err = proto.Marshal(data[0].(proto.Message)); err != nil {
Stephane Barbariedc5022d2018-11-19 15:21:44 -050076 log.Debugf("problem marshalling proto data: %+v, err:%s", data[0], err.Error())
Stephane Barbarie126101e2018-10-11 16:18:48 -040077 }
78 } else if data[0] != nil {
79 if msg, err = json.Marshal(data[0]); err != nil {
Stephane Barbariedc5022d2018-11-19 15:21:44 -050080 log.Debugf("problem marshalling json data: %+v, err:%s", data[0], err.Error())
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040081 }
82 } else {
Stephane Barbariedc5022d2018-11-19 15:21:44 -050083 log.Debugf("no data to advertise : %+v", data[0])
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040084 }
85
86 event := voltha.ConfigEvent{
87 Type: kind,
88 Hash: hash,
89 Data: string(msg),
90 }
91
92 bus.client.Publish(bus.topic, event)
Stephane Barbarie694e2b92018-09-07 12:17:36 -040093
94 return nil
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040095}