| Jeremy Mordkoff | 6f07e6f | 2016-09-07 18:56:51 -0400 | [diff] [blame] | 1 | AUTOBAHN_DEBUG = true; |
| 2 | var autobahn = require('autobahn'); |
| 3 | var uuid = require('node-uuid'); |
| 4 | var _ = require('lodash'); |
| 5 | |
| 6 | // Our modules |
| 7 | var dm = require('./data_model.js'); |
| 8 | |
| 9 | |
| 10 | var DUMP_RESULTS = false; |
| 11 | |
| 12 | // TODO: make the url be configurable via command line arg |
| 13 | var connection = new autobahn.Connection({ |
| 14 | url: 'ws://localhost:8090/ws', |
| 15 | realm: 'dts_mock' |
| 16 | }); |
| 17 | |
| 18 | // Instance of our data model/data store |
| 19 | var dataModel = new dm.DataModel(); |
| 20 | |
| 21 | var descriptor_module = (function () { |
| 22 | |
| 23 | my = {}; |
| 24 | |
| 25 | /* |
| 26 | * This function sets descriptors in the dataModel |
| 27 | */ |
| 28 | function on_config_descriptor_catalog(args) { |
| 29 | try { |
| 30 | var xpath = args[0]; |
| 31 | var msg = args[1]; |
| 32 | |
| 33 | console.log("\n\n*** Got on_config_descriptor_catalog:\n (xpath: %s)(msg: %j)", xpath, msg); |
| 34 | |
| 35 | var descriptor_type = xpath.match(new RegExp(/(nsd|vnfd|vld)-catalog/))[1]; |
| 36 | |
| 37 | if (descriptor_type in msg) { |
| 38 | msg[descriptor_type].forEach(function(entry) { |
| 39 | console.log('Assigning descriptor "%s" with id %s', |
| 40 | descriptor_type, entry.id); |
| 41 | if (descriptor_type == 'vnfd') { |
| 42 | console.log('-- Adding VNFR data'); |
| 43 | dataModel.addVnfData(entry); |
| 44 | } else { |
| 45 | // Simply assign |
| 46 | dataModel.setDescriptor(descriptor_type, entry); |
| 47 | } |
| 48 | }); |
| 49 | } |
| 50 | } catch(e) { |
| 51 | console.error("Caught exception: %s\n\n%s", e, e.stack); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | my.register = function (session) { |
| 56 | console.log('Registering for descriptor handling'); |
| 57 | session.subscribe('dts.config.nsd-catalog', on_config_descriptor_catalog); |
| 58 | session.subscribe('dts.config.vnfd-catalog', on_config_descriptor_catalog); |
| 59 | session.subscribe('dts.config.vld-catalog', on_config_descriptor_catalog); |
| 60 | }; |
| 61 | |
| 62 | return my; |
| 63 | }()); |
| 64 | |
| 65 | |
| 66 | var instance_module = (function () { |
| 67 | my = {}; |
| 68 | |
| 69 | function on_config_config(args) { |
| 70 | try { |
| 71 | var xpath = args[0]; |
| 72 | var msg = args[1]; |
| 73 | |
| 74 | console.log("\n\n*** Got on_config_config:\n (xpath: %s)(msg: %j)", xpath, msg); |
| 75 | |
| 76 | var record_type = xpath.match(new RegExp(/(ns|vnf|vl)-instance-config/))[1]; |
| 77 | record_type += 'r'; |
| 78 | |
| 79 | console.log('record_type = %s', record_type); |
| 80 | |
| 81 | if (record_type in msg) { |
| 82 | msg[record_type].forEach(function(entry) { |
| 83 | console.log('Assigning record (%s) id=%s, descriptor: id=%s', |
| 84 | record_type, entry.id, entry.nsd_ref); |
| 85 | if (record_type == 'nsr') { |
| 86 | dataModel.setNsInstanceConfig(entry); |
| 87 | } else { |
| 88 | // vnfd, vld, which don't have instance_config records yet |
| 89 | dataModel.setConfigRecord(record_type, entry); |
| 90 | } |
| 91 | }); |
| 92 | } |
| 93 | |
| 94 | } catch (e) { |
| 95 | console.error("Caught exception: %s\n\n%s", e, e.stack); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /* |
| 100 | * Get all nsr opdata records: |
| 101 | * xpath: D,/nsr:ns-instance-opdata/nsr:nsr |
| 102 | * msg: {"nsr":[{"ns_instance_config_ref":""}]} |
| 103 | * |
| 104 | * Get Ping Pong nsr opdata record: |
| 105 | * xpath: D,/nsr:ns-instance-opdata/nsr:nsr[nsr:ns-instance-config-ref='f5f41f36-78f6-11e5-b9ba-6cb3113b406f'] |
| 106 | * msg: {"nsr":[{"ns_instance_config_ref":"f5f41f36-78f6-11e5-b9ba-6cb3113b406f"}]} |
| 107 | * |
| 108 | * Get monitoring param for nsr instance opdata record: |
| 109 | * xpath: D,/nsr:ns-instance-opdata/nsr:nsr[nsr:ns-instance-config-ref='f5f41f36-78f6-11e5-b9ba-6cb3113b406f'] |
| 110 | * msg: { |
| 111 | * "nsr":[{ |
| 112 | * "monitoring_param":[{"id":""}], |
| 113 | * "ns_instance_config_ref":"f5f41f36-78f6-11e5-b9ba-6cb3113b406f" |
| 114 | * }]} |
| 115 | * |
| 116 | * Note that the xpath arg is identical in getting the entire NSR and getting sub-elements in the NSR |
| 117 | * The message tells what values to get |
| 118 | */ |
| 119 | function on_get_opdata(args) { |
| 120 | try { |
| 121 | var xpath = args[0]; |
| 122 | var msg = args[1]; |
| 123 | //console.log("\n\n*** Got on_get_opdata:\n (xpath: %s)(msg: %j)", xpath, msg); |
| 124 | console.log("*** Got on_get_opdata:\n (xpath: %s)(msg: %j)", xpath, msg); |
| 125 | |
| 126 | var record_type = xpath.match(new RegExp(/(ns|vnf|vl)-instance-opdata/))[1]; |
| 127 | record_type += 'r'; |
| 128 | |
| 129 | var gi_type_map = { |
| 130 | "nsr": "RwNsrYang.YangData_Nsr_NsInstanceOpdata", |
| 131 | "vnfr": "VnfrYang.YangData_Vnfr_VnfInstanceOpdata_Vnfr", |
| 132 | "vlr": "VlrYang.YangData_Vlr_VlInstanceOpdata_Vlr" |
| 133 | }; |
| 134 | |
| 135 | if (record_type == 'nsr') { |
| 136 | //console.log("###################\n data model:\n\n"); |
| 137 | //dataModel.prettyPrint(); |
| 138 | var response = { |
| 139 | 'nsr': dataModel.getNsInstanceOpdata() |
| 140 | }; |
| 141 | var respond_xpath = 'D,/nsr:ns-instance-opdata'; |
| 142 | } else { |
| 143 | throw new dm.NotImplementedException( |
| 144 | "record_type '%s' is not yet supported.", record_type); |
| 145 | } |
| 146 | |
| 147 | var result = new autobahn.Result([ |
| 148 | 'RwNsrYang.YangData_Nsr_NsInstanceOpdata', |
| 149 | response |
| 150 | ], {"xpath": respond_xpath}); |
| 151 | |
| 152 | if (DUMP_RESULTS) |
| 153 | console.log("result=\n%s", JSON.stringify(result) ); |
| 154 | |
| 155 | return result; |
| 156 | } catch(e) { |
| 157 | console.error("Caught exception: %s\n\n%s", e, e.stack); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | function on_get_vnfr_catalog(args) { |
| 162 | try { |
| 163 | var xpath = args[0]; |
| 164 | var msg = args[1]; |
| 165 | console.log("*** Got on_vnfr_catalog:\n (xpath: %s)(msg: %j)", xpath, msg); |
| 166 | |
| 167 | var response = { |
| 168 | 'vnfr': dataModel.getVnfrs() |
| 169 | }; |
| 170 | var respond_xpath = 'D,/vnfr:vnfr-catalog'; |
| 171 | |
| 172 | var result = new autobahn.Result([ |
| 173 | 'RwVnfrYang.YangData_Vnfr_VnfrCatalog', |
| 174 | response |
| 175 | ], {"xpath": respond_xpath}); |
| 176 | |
| 177 | if (DUMP_RESULTS) |
| 178 | console.log("result=\n%s", JSON.stringify(result) ); |
| 179 | |
| 180 | return result; |
| 181 | } catch(e) { |
| 182 | console.error("Caught exception: %s\n\n%s", e, e.stack); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | my.register = function (session) { |
| 187 | console.log('Registering for record handling'); |
| 188 | session.register('dts.data.ns-instance-opdata', on_get_opdata); |
| 189 | session.register('dts.data.vnfr-catalog', on_get_vnfr_catalog); |
| 190 | session.subscribe('dts.config.ns-instance-config', on_config_config); |
| 191 | } |
| 192 | |
| 193 | return my; |
| 194 | }()); |
| 195 | |
| 196 | |
| 197 | var action_module = (function() { |
| 198 | my = {}; |
| 199 | |
| 200 | /* |
| 201 | * Set the specified VNFR operating state |
| 202 | * |
| 203 | * (xpath: I,/lpmocklet:start-vnfr) |
| 204 | * (msg: {"id":"f26b90b0-8184-11e5-bc47-2b429643382b"}) |
| 205 | */ |
| 206 | function on_set_opstate(args) { |
| 207 | try { |
| 208 | var xpath = args[0]; |
| 209 | var msg = args[1]; |
| 210 | |
| 211 | console.log("\n\n*** Got on_start_vnfr:\n (xpath: %s)(msg: %j)", |
| 212 | xpath, msg); |
| 213 | var action_match = xpath.match(new RegExp(/lpmocklet:(\w+)-(\w+)/)); |
| 214 | var action = action_match[1]; |
| 215 | var obj_type = action_match[2]; |
| 216 | |
| 217 | var record_id = msg['id']; |
| 218 | console.log('action="%s", obj_type="%s", record_id="%s"', |
| 219 | action, obj_type, record_id); |
| 220 | |
| 221 | if (obj_type == 'vnfr') { |
| 222 | if (action == 'start') { |
| 223 | dataModel.startVnfr(record_id); |
| 224 | } |
| 225 | else if (action == 'stop') { |
| 226 | dataModel.stopVnfr(record_id); |
| 227 | } |
| 228 | else { |
| 229 | console.error('Unsupported opstate action "%s"', action); |
| 230 | } |
| 231 | } else { |
| 232 | console.error('Unsupported opstate action object: "%s"', |
| 233 | obj_type); |
| 234 | } |
| 235 | |
| 236 | console.log('\n\nBuilding response....'); |
| 237 | |
| 238 | var response = { |
| 239 | id: uuid.v1(), |
| 240 | object_type: obj_type, |
| 241 | action: action, |
| 242 | status: 'SUCCESS' |
| 243 | }; |
| 244 | var respond_xpath = 'D,/lpmocklet:lpmocklet-action-status'; |
| 245 | var result = new autobahn.Result([ |
| 246 | 'LpmockletYang.YangData_Lpmocklet_LpmockletActionStatus', |
| 247 | response |
| 248 | ], {"xpath": respond_xpath}); |
| 249 | |
| 250 | console.log('Done running on_set_opdata'); |
| 251 | return result; |
| 252 | |
| 253 | } catch (e) { |
| 254 | console.error("Caught exception: %s\n\n%s", e, e.stack); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | function on_set_control_param(args) { |
| 259 | try { |
| 260 | var xpath = args[0]; |
| 261 | var msg = args[1]; |
| 262 | |
| 263 | console.log("\n\n*** Got on_set_control_param:\n (xpath: %s)(msg: %j)", |
| 264 | xpath, msg); |
| 265 | |
| 266 | // We can ignore xpath. We expect: "I,/lpmocklet:set-control-param" |
| 267 | // msg: {"set":{"id":"f8d63b30-84b3-11e5-891c-61c6a71edd3c","obj_code":"VNFR","control_id":"ping-packet-size-1","value":10}} |
| 268 | |
| 269 | var response_class = 'LpmockletYang.YangData_Lpmocklet_LpmockletActionStatus'; |
| 270 | var status = dataModel.updateControlParam( |
| 271 | msg.obj_code.toLowerCase(), |
| 272 | msg.id, |
| 273 | msg.control_id, |
| 274 | msg.value); |
| 275 | |
| 276 | var response = { |
| 277 | id: uuid.v1(), |
| 278 | object_type: msg.obj_code, |
| 279 | action: msg.control_id, |
| 280 | status: status |
| 281 | }; |
| 282 | |
| 283 | var respond_xpath = 'D,/lpmocklet:lpmocklet-action-status'; |
| 284 | var result = new autobahn.Result([ |
| 285 | 'LpmockletYang.YangData_Lpmocklet_LpmockletActionStatus', |
| 286 | response |
| 287 | ], {"xpath": respond_xpath}); |
| 288 | |
| 289 | console.log('Done running on_set_opdata'); |
| 290 | return result; |
| 291 | } catch (e) { |
| 292 | console.error("Caught exception: %s\n\n%s", e, e.stack); |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | my.register = function(session) { |
| 297 | console.log('Registering for action handling'); |
| 298 | session.register('dts.rpc.start-vnfr', on_set_opstate); |
| 299 | session.register('dts.rpc.stop-vnfr', on_set_opstate); |
| 300 | session.register('dts.rpc.set-control-param', on_set_control_param); |
| 301 | } |
| 302 | |
| 303 | return my; |
| 304 | |
| 305 | }()); |
| 306 | |
| 307 | |
| 308 | connection.onopen = function (session) { |
| 309 | console.log('Connection to wamp server established!'); |
| 310 | descriptor_module.register(session); |
| 311 | instance_module.register(session); |
| 312 | action_module.register(session); |
| 313 | } |
| 314 | |
| 315 | console.log('Opening autobahn connection'); |
| 316 | connection.open(); |
| 317 | |