2 var autobahn
= require('autobahn');
3 var uuid
= require('node-uuid');
4 var _
= require('lodash');
7 var dm
= require('./data_model.js');
10 var DUMP_RESULTS
= false;
12 // TODO: make the url be configurable via command line arg
13 var connection
= new autobahn
.Connection({
14 url
: 'ws://localhost:8090/ws',
18 // Instance of our data model/data store
19 var dataModel
= new dm
.DataModel();
21 var descriptor_module
= (function () {
26 * This function sets descriptors in the dataModel
28 function on_config_descriptor_catalog(args
) {
33 console
.log("\n\n*** Got on_config_descriptor_catalog:\n (xpath: %s)(msg: %j)", xpath
, msg
);
35 var descriptor_type
= xpath
.match(new RegExp(/(nsd|vnfd|vld)-catalog/))[1];
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
);
46 dataModel
.setDescriptor(descriptor_type
, entry
);
51 console
.error("Caught exception: %s\n\n%s", e
, e
.stack
);
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
);
66 var instance_module
= (function () {
69 function on_config_config(args
) {
74 console
.log("\n\n*** Got on_config_config:\n (xpath: %s)(msg: %j)", xpath
, msg
);
76 var record_type
= xpath
.match(new RegExp(/(ns|vnf|vl)-instance-config/))[1];
79 console
.log('record_type = %s', record_type
);
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
);
88 // vnfd, vld, which don't have instance_config records yet
89 dataModel
.setConfigRecord(record_type
, entry
);
95 console
.error("Caught exception: %s\n\n%s", e
, e
.stack
);
100 * Get all nsr opdata records:
101 * xpath: D,/nsr:ns-instance-opdata/nsr:nsr
102 * msg: {"nsr":[{"ns_instance_config_ref":""}]}
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"}]}
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']
112 * "monitoring_param":[{"id":""}],
113 * "ns_instance_config_ref":"f5f41f36-78f6-11e5-b9ba-6cb3113b406f"
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
119 function on_get_opdata(args
) {
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
);
126 var record_type
= xpath
.match(new RegExp(/(ns|vnf|vl)-instance-opdata/))[1];
130 "nsr": "RwNsrYang.YangData_Nsr_NsInstanceOpdata",
131 "vnfr": "VnfrYang.YangData_Vnfr_VnfInstanceOpdata_Vnfr",
132 "vlr": "VlrYang.YangData_Vlr_VlInstanceOpdata_Vlr"
135 if (record_type
== 'nsr') {
136 //console.log("###################\n data model:\n\n");
137 //dataModel.prettyPrint();
139 'nsr': dataModel
.getNsInstanceOpdata()
141 var respond_xpath
= 'D,/nsr:ns-instance-opdata';
143 throw new dm
.NotImplementedException(
144 "record_type '%s' is not yet supported.", record_type
);
147 var result
= new autobahn
.Result([
148 'RwNsrYang.YangData_Nsr_NsInstanceOpdata',
150 ], {"xpath": respond_xpath
});
153 console
.log("result=\n%s", JSON
.stringify(result
) );
157 console
.error("Caught exception: %s\n\n%s", e
, e
.stack
);
161 function on_get_vnfr_catalog(args
) {
165 console
.log("*** Got on_vnfr_catalog:\n (xpath: %s)(msg: %j)", xpath
, msg
);
168 'vnfr': dataModel
.getVnfrs()
170 var respond_xpath
= 'D,/vnfr:vnfr-catalog';
172 var result
= new autobahn
.Result([
173 'RwVnfrYang.YangData_Vnfr_VnfrCatalog',
175 ], {"xpath": respond_xpath
});
178 console
.log("result=\n%s", JSON
.stringify(result
) );
182 console
.error("Caught exception: %s\n\n%s", e
, e
.stack
);
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
);
197 var action_module
= (function() {
201 * Set the specified VNFR operating state
203 * (xpath: I,/lpmocklet:start-vnfr)
204 * (msg: {"id":"f26b90b0-8184-11e5-bc47-2b429643382b"})
206 function on_set_opstate(args
) {
211 console
.log("\n\n*** Got on_start_vnfr:\n (xpath: %s)(msg: %j)",
213 var action_match
= xpath
.match(new RegExp(/lpmocklet:(\w+)-(\w+)/));
214 var action
= action_match
[1];
215 var obj_type
= action_match
[2];
217 var record_id
= msg
['id'];
218 console
.log('action="%s", obj_type="%s", record_id="%s"',
219 action
, obj_type
, record_id
);
221 if (obj_type
== 'vnfr') {
222 if (action
== 'start') {
223 dataModel
.startVnfr(record_id
);
225 else if (action
== 'stop') {
226 dataModel
.stopVnfr(record_id
);
229 console
.error('Unsupported opstate action "%s"', action
);
232 console
.error('Unsupported opstate action object: "%s"',
236 console
.log('\n\nBuilding response....');
240 object_type
: obj_type
,
244 var respond_xpath
= 'D,/lpmocklet:lpmocklet-action-status';
245 var result
= new autobahn
.Result([
246 'LpmockletYang.YangData_Lpmocklet_LpmockletActionStatus',
248 ], {"xpath": respond_xpath
});
250 console
.log('Done running on_set_opdata');
254 console
.error("Caught exception: %s\n\n%s", e
, e
.stack
);
258 function on_set_control_param(args
) {
263 console
.log("\n\n*** Got on_set_control_param:\n (xpath: %s)(msg: %j)",
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}}
269 var response_class
= 'LpmockletYang.YangData_Lpmocklet_LpmockletActionStatus';
270 var status
= dataModel
.updateControlParam(
271 msg
.obj_code
.toLowerCase(),
278 object_type
: msg
.obj_code
,
279 action
: msg
.control_id
,
283 var respond_xpath
= 'D,/lpmocklet:lpmocklet-action-status';
284 var result
= new autobahn
.Result([
285 'LpmockletYang.YangData_Lpmocklet_LpmockletActionStatus',
287 ], {"xpath": respond_xpath
});
289 console
.log('Done running on_set_opdata');
292 console
.error("Caught exception: %s\n\n%s", e
, e
.stack
);
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
);
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
);
315 console
.log('Opening autobahn connection');