Illyoung Choi | 16c6d4f | 2019-07-24 18:09:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-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 | */ |
| 16 | |
| 17 | |
| 18 | (function () { |
| 19 | 'use strict'; |
| 20 | |
| 21 | const logger = require('../config/logger.js'); |
| 22 | |
| 23 | let serviceEvents = { |
| 24 | // probe -> controller -> probe |
| 25 | EVENT_EMIT: 'cord.workflow.ctlsvc.event.emit' |
| 26 | }; |
| 27 | |
| 28 | // WebSocket interface for emitting an event |
| 29 | // Message format: |
| 30 | // { |
| 31 | // topic: 'cord.workflow.ctlsvc.event.emit', |
| 32 | // message: { |
| 33 | // req_id: <req_id>, // optional |
| 34 | // topic: <topic>, |
| 35 | // message: <message> |
| 36 | // } |
| 37 | // } |
| 38 | const emitEvent = (topic, message, cb) => { |
| 39 | const eventrouter = require('./eventrouter.js'); |
| 40 | |
| 41 | let errorMessage; |
| 42 | if(!message) { |
| 43 | // error |
| 44 | errorMessage = `Message body for topic ${topic} is null or empty`; |
| 45 | logger.log('warn', `Return error - ${errorMessage}`); |
| 46 | cb(errorMessage, false); |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | if(!('topic' in message)) { |
| 51 | // error |
| 52 | errorMessage = `field 'topic' does not exist in message body - ${JSON.stringify(message)}`; |
| 53 | logger.log('warn', `Return error - ${errorMessage}`); |
| 54 | cb(errorMessage, false); |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | if(!('message' in message)) { |
| 59 | // error |
| 60 | errorMessage = `field 'message' does not exist in message body - ${JSON.stringify(message)}`; |
| 61 | logger.log('warn', `Return error - ${errorMessage}`); |
| 62 | cb(errorMessage, false); |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | let result = eventrouter.emitEvent( |
| 67 | message.topic, |
| 68 | message.message |
| 69 | ); |
| 70 | if(!result) { |
| 71 | errorMessage = `failed to emit event ${message.topic} - ${message.message}`; |
| 72 | cb(errorMessage, false); |
| 73 | } |
| 74 | else { |
| 75 | cb(null, true); |
| 76 | } |
| 77 | return; |
| 78 | }; |
| 79 | |
| 80 | const getRouter = () => { |
| 81 | return { |
| 82 | emitEvent: { |
| 83 | topic: serviceEvents.EVENT_EMIT, |
| 84 | handler: emitEvent |
| 85 | } |
| 86 | }; |
| 87 | }; |
| 88 | |
| 89 | module.exports = { |
| 90 | serviceEvents: serviceEvents, |
| 91 | getRouter: getRouter |
| 92 | }; |
| 93 | })(); |