/* Copyright 2018 Intel Corporation Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ /** * @description Intel(r) AMT WSMAN Stack * @author Ylian Saint-Hilaire * @version v0.2.0 */ // Construct a MeshServer object function WsmanStackCreateService(/*CreateWsmanComm, host, port, user, pass, tls, extra*/) { var obj = {_ObjectID: 'WSMAN'}; //obj.onDebugMessage = null; // Set to a function if you want to get debug messages. obj.NextMessageId = 1; // Next message number, used to label WSMAN calls. obj.Address = '/wsman'; obj.xmlParser = require('amt-xml'); if (arguments.length == 1 && typeof (arguments[0] == 'object')) { var CreateWsmanComm = arguments[0].transport; if (CreateWsmanComm) { obj.comm = new CreateWsmanComm(arguments[0]); } } else { var CreateWsmanComm = arguments[0]; if (CreateWsmanComm) { obj.comm = new CreateWsmanComm(arguments[1], arguments[2], arguments[3], arguments[4], arguments[5], arguments[6]); } } obj.PerformAjax = function PerformAjax(postdata, callback, tag, pri, namespaces) { if (namespaces == null) namespaces = ''; obj.comm.PerformAjax('
' + postdata, function (data, status, tag) { if (status != 200) { callback(obj, null, { Header: { HttpError: status } }, status, tag); return; } var wsresponse = obj.xmlParser.ParseWsman(data); if (!wsresponse || wsresponse == null) { callback(obj, null, { Header: { HttpError: status } }, 601, tag); } else { callback(obj, wsresponse.Header["ResourceURI"], wsresponse, 200, tag); } }, tag, pri); } // Private method //obj.Debug = function (msg) { /*console.log(msg);*/ } // Cancel all pending queries with given status obj.CancelAllQueries = function CancelAllQueries(s) { obj.comm.CancelAllQueries(s); } // Get the last element of a URI string obj.GetNameFromUrl = function (resuri) { var x = resuri.lastIndexOf("/"); return (x == -1)?resuri:resuri.substring(x + 1); } // Perform a WSMAN Subscribe operation obj.ExecSubscribe = function ExecSubscribe(resuri, delivery, url, callback, tag, pri, selectors, opaque, user, pass) { var digest = "", digest2 = "", opaque = ""; if (user != null && pass != null) { digest = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#UsernameToken' + user + '' + pass + ''; digest2 = ''; } if (opaque != null) { opaque = '' + opaque + ''; } if (delivery == 'PushWithAck') { delivery = 'dmtf.org/wbem/wsman/1/wsman/PushWithAck'; } else if (delivery == 'Push') { delivery = 'xmlsoap.org/ws/2004/08/eventing/DeliveryModes/Push'; } var data = "http://schemas.xmlsoap.org/ws/2004/08/eventing/Subscribe" + obj.Address + "" + resuri + "" + (obj.NextMessageId++) + "http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous" + _PutObjToSelectorsXml(selectors) + digest + '
' + url + '' + opaque + '' + digest2 + ''; obj.PerformAjax(data + "
", callback, tag, pri, 'xmlns:e="http://schemas.xmlsoap.org/ws/2004/08/eventing" xmlns:m="http://x.com"'); } // Perform a WSMAN UnSubscribe operation obj.ExecUnSubscribe = function ExecUnSubscribe(resuri, callback, tag, pri, selectors) { var data = "http://schemas.xmlsoap.org/ws/2004/08/eventing/Unsubscribe" + obj.Address + "" + resuri + "" + (obj.NextMessageId++) + "http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous" + _PutObjToSelectorsXml(selectors) + ''; obj.PerformAjax(data + "", callback, tag, pri, 'xmlns:e="http://schemas.xmlsoap.org/ws/2004/08/eventing"'); } // Perform a WSMAN PUT operation obj.ExecPut = function ExecPut(resuri, putobj, callback, tag, pri, selectors) { var data = "http://schemas.xmlsoap.org/ws/2004/09/transfer/Put" + obj.Address + "" + resuri + "" + (obj.NextMessageId++) + "http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymousPT60.000S" + _PutObjToSelectorsXml(selectors) + '' + _PutObjToBodyXml(resuri, putobj); obj.PerformAjax(data + "", callback, tag, pri); } // Perform a WSMAN CREATE operation obj.ExecCreate = function ExecCreate(resuri, putobj, callback, tag, pri, selectors) { var objname = obj.GetNameFromUrl(resuri); var data = "http://schemas.xmlsoap.org/ws/2004/09/transfer/Create" + obj.Address + "" + resuri + "" + (obj.NextMessageId++) + "http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymousPT60S" + _PutObjToSelectorsXml(selectors) + ""; for (var n in putobj) { data += "" + putobj[n] + "" } obj.PerformAjax(data + "", callback, tag, pri); } // Perform a WSMAN DELETE operation obj.ExecDelete = function ExecDelete(resuri, putobj, callback, tag, pri) { var data = "http://schemas.xmlsoap.org/ws/2004/09/transfer/Delete" + obj.Address + "" + resuri + "" + (obj.NextMessageId++) + "http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymousPT60S" + _PutObjToSelectorsXml(putobj) + ""; obj.PerformAjax(data, callback, tag, pri); } // Perform a WSMAN GET operation obj.ExecGet = function ExecGet(resuri, callback, tag, pri) { obj.PerformAjax("http://schemas.xmlsoap.org/ws/2004/09/transfer/Get" + obj.Address + "" + resuri + "" + (obj.NextMessageId++) + "http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymousPT60S", callback, tag, pri); } // Perform a WSMAN method call operation obj.ExecMethod = function ExecMethod(resuri, method, args, callback, tag, pri, selectors) { var argsxml = ""; for (var i in args) { if (args[i] != null) { if (Array.isArray(args[i])) { for (var x in args[i]) { argsxml += "" + args[i][x] + ""; } } else { argsxml += "" + args[i] + ""; } } } obj.ExecMethodXml(resuri, method, argsxml, callback, tag, pri, selectors); } // Perform a WSMAN method call operation. The arguments are already formatted in XML. obj.ExecMethodXml = function ExecMethodXml(resuri, method, argsxml, callback, tag, pri, selectors) { obj.PerformAjax(resuri + "/" + method + "" + obj.Address + "" + resuri + "" + (obj.NextMessageId++) + "http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymousPT60S" + _PutObjToSelectorsXml(selectors) + "" + argsxml + "", callback, tag, pri); } // Perform a WSMAN ENUM operation obj.ExecEnum = function ExecEnum(resuri, callback, tag, pri) { obj.PerformAjax("http://schemas.xmlsoap.org/ws/2004/09/enumeration/Enumerate" + obj.Address + "" + resuri + "" + (obj.NextMessageId++) + "http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymousPT60S", callback, tag, pri); } // Perform a WSMAN PULL operation obj.ExecPull = function ExecPull(resuri, enumctx, callback, tag, pri) { obj.PerformAjax("http://schemas.xmlsoap.org/ws/2004/09/enumeration/Pull" + obj.Address + "" + resuri + "" + (obj.NextMessageId++) + "http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymousPT60S" + enumctx + "99999999", callback, tag, pri); } function _PutObjToBodyXml(resuri, putObj) { if (!resuri || putObj == null) return ''; var objname = obj.GetNameFromUrl(resuri); var result = ''; for (var prop in putObj) { if (!putObj.hasOwnProperty(prop) || prop.indexOf('__') === 0 || prop.indexOf('@') === 0) continue; if (putObj[prop] == null || typeof putObj[prop] === 'function') continue; if (typeof putObj[prop] === 'object' && putObj[prop]['ReferenceParameters']) { result += '' + putObj[prop].Address + '' + putObj[prop]['ReferenceParameters']["ResourceURI"] + ''; var selectorArray = putObj[prop]['ReferenceParameters']['SelectorSet']['Selector']; if (Array.isArray(selectorArray)) { for (var i=0; i< selectorArray.length; i++) { result += '' + selectorArray[i]['Value'] + ''; } } else { result += '' + selectorArray['Value'] + ''; } result += ''; } else { if (Array.isArray(putObj[prop])) { for (var i = 0; i < putObj[prop].length; i++) { result += '' + putObj[prop][i].toString() + ''; } } else { result += '' + putObj[prop].toString() + ''; } } } result += ''; return result; } /* convert { @Name: 'InstanceID', @AttrName: 'Attribute Value'} into ' Name="InstanceID" AttrName="Attribute Value" ' */ function _ObjectToXmlAttributes(objWithAttributes) { if(!objWithAttributes) return ''; var result = ' '; for (var propName in objWithAttributes) { if (!objWithAttributes.hasOwnProperty(propName) || propName.indexOf('@') !== 0) continue; result += propName.substring(1) + '="' + objWithAttributes[propName] + '" '; } return result; } function _PutObjToSelectorsXml(selectorSet) { if (!selectorSet) return ''; if (typeof selectorSet == 'string') return selectorSet; if (selectorSet['InstanceID']) return "" + selectorSet['InstanceID'] + ""; var result = ''; for(var propName in selectorSet) { if (!selectorSet.hasOwnProperty(propName)) continue; result += ''; if (selectorSet[propName]['ReferenceParameters']) { result += ''; result += '' + selectorSet[propName]['Address'] + '' + selectorSet[propName]['ReferenceParameters']['ResourceURI'] + ''; var selectorArray = selectorSet[propName]['ReferenceParameters']['SelectorSet']['Selector']; if (Array.isArray(selectorArray)) { for (var i = 0; i < selectorArray.length; i++) { result += '' + selectorArray[i]['Value'] + ''; } } else { result += '' + selectorArray['Value'] + ''; } result += ''; } else { result += selectorSet[propName]; } result += ''; } result += ''; return result; } return obj; } module.exports = WsmanStackCreateService;