blob: aaeb7ac81f3a74280389a94f11203d8a36d10370 [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 Barbarie4a2564d2018-07-26 11:02:58 -040016package model
17
18import (
19 "encoding/json"
Stephane Barbarie126101e2018-10-11 16:18:48 -040020 "github.com/golang/protobuf/proto"
Stephane Barbarie8c48b5c2018-10-02 09:45:17 -040021 "github.com/opencord/voltha-go/common/log"
Stephane Barbarieec0919b2018-09-05 14:14:29 -040022 "github.com/opencord/voltha-go/protos/voltha"
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040023)
24
25type EventBus struct {
26 client *EventBusClient
27 topic string
28}
29
30var (
31 IGNORED_CALLBACKS = map[CallbackType]struct{}{
32 PRE_ADD: {},
33 GET: {},
34 POST_LISTCHANGE: {},
35 PRE_REMOVE: {},
36 PRE_UPDATE: {},
37 }
38)
39
40func NewEventBus() *EventBus {
41 bus := &EventBus{
42 client: NewEventBusClient(),
43 topic: "model-change-events",
44 }
45 return bus
46}
47
Stephane Barbarie694e2b92018-09-07 12:17:36 -040048//func (bus *EventBus) Advertise(eventType CallbackType, data interface{}, hash string) {
49func (bus *EventBus) Advertise(args ...interface{}) interface{} {
50 eventType := args[0].(CallbackType)
Stephane Barbarie126101e2018-10-11 16:18:48 -040051 hash := args[1].(string)
52 data := args[2:]
Stephane Barbarie694e2b92018-09-07 12:17:36 -040053
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040054 if _, ok := IGNORED_CALLBACKS[eventType]; ok {
Stephane Barbarie126101e2018-10-11 16:18:48 -040055 log.Debugf("ignoring event - type:%s, data:%+v", eventType, data)
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040056 }
57 var kind voltha.ConfigEventType_ConfigEventType
58 switch eventType {
59 case POST_ADD:
60 kind = voltha.ConfigEventType_add
61 case POST_REMOVE:
62 kind = voltha.ConfigEventType_remove
63 default:
64 kind = voltha.ConfigEventType_update
65 }
66
67 var msg []byte
68 var err error
69 if IsProtoMessage(data) {
Stephane Barbarie126101e2018-10-11 16:18:48 -040070 if msg, err = proto.Marshal(data[0].(proto.Message)); err != nil {
71 log.Errorf("problem marshalling proto data: %+v, err:%s", data[0], err.Error())
72 }
73 } else if data[0] != nil {
74 if msg, err = json.Marshal(data[0]); err != nil {
75 log.Errorf("problem marshalling json data: %+v, err:%s", data[0], err.Error())
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040076 }
77 } else {
Stephane Barbarie126101e2018-10-11 16:18:48 -040078 log.Errorf("no data to advertise : %+v", data[0])
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040079 }
80
81 event := voltha.ConfigEvent{
82 Type: kind,
83 Hash: hash,
84 Data: string(msg),
85 }
86
87 bus.client.Publish(bus.topic, event)
Stephane Barbarie694e2b92018-09-07 12:17:36 -040088
89 return nil
Stephane Barbarie4a2564d2018-07-26 11:02:58 -040090}