blob: f3dfcb231f75d1b873e07ba09f9da022ccc7f12a [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-protos/v3/go/voltha"
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040024)
25
Stephane Barbariedc5022d2018-11-19 15:21:44 -050026// EventBus contains the details required to communicate with the event bus mechanism
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040027type EventBus struct {
28 client *EventBusClient
29 topic string
30}
31
Stephane Barbariedc5022d2018-11-19 15:21:44 -050032// ignoredCallbacks keeps a list of callbacks that should not be advertised on the event bus
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040033var (
Stephane Barbariedc5022d2018-11-19 15:21:44 -050034 ignoredCallbacks = map[CallbackType]struct{}{
npujar9a30c702019-11-14 17:06:39 +053035 PreAdd: {},
36 Get: {},
37 PostListchange: {},
38 PreRemove: {},
39 PreUpdate: {},
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040040 }
41)
42
Stephane Barbariedc5022d2018-11-19 15:21:44 -050043// NewEventBus creates a new instance of the EventBus structure
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040044func NewEventBus() *EventBus {
45 bus := &EventBus{
46 client: NewEventBusClient(),
47 topic: "model-change-events",
48 }
49 return bus
50}
51
Stephane Barbariedc5022d2018-11-19 15:21:44 -050052// Advertise will publish the provided information to the event bus
Stephane Barbarie694e2b92018-09-07 12:17:36 -040053func (bus *EventBus) Advertise(args ...interface{}) interface{} {
54 eventType := args[0].(CallbackType)
Stephane Barbarie126101e2018-10-11 16:18:48 -040055 hash := args[1].(string)
56 data := args[2:]
Stephane Barbarie694e2b92018-09-07 12:17:36 -040057
Stephane Barbariedc5022d2018-11-19 15:21:44 -050058 if _, ok := ignoredCallbacks[eventType]; ok {
Girish Kumarf56a4682020-03-20 20:07:46 +000059 logger.Debugf("ignoring event - type:%s, data:%+v", eventType, data)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040060 }
serkant.uluderya2ae470f2020-01-21 11:13:09 -080061 var kind voltha.ConfigEventType_Types
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040062 switch eventType {
npujar9a30c702019-11-14 17:06:39 +053063 case PostAdd:
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040064 kind = voltha.ConfigEventType_add
npujar9a30c702019-11-14 17:06:39 +053065 case PostRemove:
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040066 kind = voltha.ConfigEventType_remove
67 default:
68 kind = voltha.ConfigEventType_update
69 }
70
71 var msg []byte
72 var err error
73 if IsProtoMessage(data) {
Stephane Barbarie126101e2018-10-11 16:18:48 -040074 if msg, err = proto.Marshal(data[0].(proto.Message)); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +000075 logger.Debugf("problem marshalling proto data: %+v, err:%s", data[0], err.Error())
Stephane Barbarie126101e2018-10-11 16:18:48 -040076 }
77 } else if data[0] != nil {
78 if msg, err = json.Marshal(data[0]); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +000079 logger.Debugf("problem marshalling json data: %+v, err:%s", data[0], err.Error())
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040080 }
81 } else {
Girish Kumarf56a4682020-03-20 20:07:46 +000082 logger.Debugf("no data to advertise : %+v", data[0])
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040083 }
84
85 event := voltha.ConfigEvent{
86 Type: kind,
87 Hash: hash,
88 Data: string(msg),
89 }
90
91 bus.client.Publish(bus.topic, event)
Stephane Barbarie694e2b92018-09-07 12:17:36 -040092
93 return nil
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040094}