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" |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 21 | "github.com/golang/protobuf/proto" |
Scott Baker | 807addd | 2019-10-24 15:16:21 -0700 | [diff] [blame] | 22 | "github.com/opencord/voltha-lib-go/v2/pkg/log" |
Scott Baker | 555307d | 2019-11-04 08:58:01 -0800 | [diff] [blame] | 23 | "github.com/opencord/voltha-protos/v2/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{}{ |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 35 | PRE_ADD: {}, |
| 36 | GET: {}, |
| 37 | POST_LISTCHANGE: {}, |
| 38 | PRE_REMOVE: {}, |
| 39 | PRE_UPDATE: {}, |
| 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 { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 59 | log.Debugf("ignoring event - type:%s, data:%+v", eventType, data) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 60 | } |
| 61 | var kind voltha.ConfigEventType_ConfigEventType |
| 62 | switch eventType { |
| 63 | case POST_ADD: |
| 64 | kind = voltha.ConfigEventType_add |
| 65 | case POST_REMOVE: |
| 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 { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 75 | 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] | 76 | } |
| 77 | } else if data[0] != nil { |
| 78 | if msg, err = json.Marshal(data[0]); err != nil { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 79 | 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] | 80 | } |
| 81 | } else { |
Stephane Barbarie | dc5022d | 2018-11-19 15:21:44 -0500 | [diff] [blame] | 82 | log.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 | } |