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 | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 16 | package model |
| 17 | |
| 18 | import ( |
| 19 | "encoding/json" |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 20 | "github.com/golang/protobuf/proto" |
Stephane Barbarie | 8c48b5c | 2018-10-02 09:45:17 -0400 | [diff] [blame] | 21 | "github.com/opencord/voltha-go/common/log" |
Stephane Barbarie | ec0919b | 2018-09-05 14:14:29 -0400 | [diff] [blame] | 22 | "github.com/opencord/voltha-go/protos/voltha" |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 23 | ) |
| 24 | |
| 25 | type EventBus struct { |
| 26 | client *EventBusClient |
| 27 | topic string |
| 28 | } |
| 29 | |
| 30 | var ( |
| 31 | IGNORED_CALLBACKS = map[CallbackType]struct{}{ |
| 32 | PRE_ADD: {}, |
| 33 | GET: {}, |
| 34 | POST_LISTCHANGE: {}, |
| 35 | PRE_REMOVE: {}, |
| 36 | PRE_UPDATE: {}, |
| 37 | } |
| 38 | ) |
| 39 | |
| 40 | func NewEventBus() *EventBus { |
| 41 | bus := &EventBus{ |
| 42 | client: NewEventBusClient(), |
| 43 | topic: "model-change-events", |
| 44 | } |
| 45 | return bus |
| 46 | } |
| 47 | |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 48 | //func (bus *EventBus) Advertise(eventType CallbackType, data interface{}, hash string) { |
| 49 | func (bus *EventBus) Advertise(args ...interface{}) interface{} { |
| 50 | eventType := args[0].(CallbackType) |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 51 | hash := args[1].(string) |
| 52 | data := args[2:] |
Stephane Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 53 | |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 54 | if _, ok := IGNORED_CALLBACKS[eventType]; ok { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 55 | log.Debugf("ignoring event - type:%s, data:%+v", eventType, data) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 56 | } |
| 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 Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 70 | 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 Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 76 | } |
| 77 | } else { |
Stephane Barbarie | 126101e | 2018-10-11 16:18:48 -0400 | [diff] [blame] | 78 | log.Errorf("no data to advertise : %+v", data[0]) |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 79 | } |
| 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 Barbarie | 694e2b9 | 2018-09-07 12:17:36 -0400 | [diff] [blame] | 88 | |
| 89 | return nil |
Stephane Barbarie | 4a2564d | 2018-07-26 11:02:58 -0400 | [diff] [blame] | 90 | } |