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