khenaidoo | bf6e7bb | 2018-08-14 22:27:29 -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 | */ |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 16 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 17 | package model |
| 18 | |
| 19 | import ( |
| 20 | "encoding/json" |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 21 | |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 22 | "github.com/golang/protobuf/proto" |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 23 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
| 24 | "github.com/opencord/voltha-protos/v3/go/voltha" |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 25 | ) |
| 26 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 27 | // EventBus contains the details required to communicate with the event bus mechanism |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 28 | type EventBus struct { |
| 29 | client *EventBusClient |
| 30 | topic string |
| 31 | } |
| 32 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 33 | // ignoredCallbacks keeps a list of callbacks that should not be advertised on the event bus |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 34 | var ( |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 35 | ignoredCallbacks = map[CallbackType]struct{}{ |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 36 | PreAdd: {}, |
| 37 | Get: {}, |
| 38 | PostListchange: {}, |
| 39 | PreRemove: {}, |
| 40 | PreUpdate: {}, |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 41 | } |
| 42 | ) |
| 43 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 44 | // NewEventBus creates a new instance of the EventBus structure |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 45 | func NewEventBus() *EventBus { |
| 46 | bus := &EventBus{ |
| 47 | client: NewEventBusClient(), |
| 48 | topic: "model-change-events", |
| 49 | } |
| 50 | return bus |
| 51 | } |
| 52 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 53 | // Advertise will publish the provided information to the event bus |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 54 | func (bus *EventBus) Advertise(args ...interface{}) interface{} { |
| 55 | eventType := args[0].(CallbackType) |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 56 | hash := args[1].(string) |
| 57 | data := args[2:] |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 58 | |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 59 | if _, ok := ignoredCallbacks[eventType]; ok { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 60 | log.Debugf("ignoring event - type:%s, data:%+v", eventType, data) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 61 | } |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 62 | var kind voltha.ConfigEventType_Types |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 63 | switch eventType { |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 64 | case PostAdd: |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 65 | kind = voltha.ConfigEventType_add |
npujar | 9a30c70 | 2019-11-14 17:06:39 +0530 | [diff] [blame] | 66 | case PostRemove: |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 67 | 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 Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 75 | if msg, err = proto.Marshal(data[0].(proto.Message)); err != nil { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 76 | log.Debugf("problem marshalling proto data: %+v, err:%s", data[0], err.Error()) |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 77 | } |
| 78 | } else if data[0] != nil { |
| 79 | if msg, err = json.Marshal(data[0]); err != nil { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 80 | log.Debugf("problem marshalling json data: %+v, err:%s", data[0], err.Error()) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 81 | } |
| 82 | } else { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 83 | log.Debugf("no data to advertise : %+v", data[0]) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 84 | } |
| 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 Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 93 | |
| 94 | return nil |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 95 | } |