Timoney, Daniel (dt5972) | 324ee36 | 2017-02-15 10:37:53 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright 2013, 2014 IBM Corp. |
| 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 | var registry = require("./registry"); |
| 17 | var credentials = require("./credentials"); |
| 18 | var flows = require("./flows"); |
| 19 | var Node = require("./Node"); |
| 20 | |
| 21 | /** |
| 22 | * Registers a node constructor |
| 23 | * @param type - the string type name |
| 24 | * @param constructor - the constructor function for this node type |
| 25 | * @param opts - optional additional options for the node |
| 26 | */ |
| 27 | function registerType(type,constructor,opts) { |
| 28 | if (opts && opts.credentials) { |
| 29 | credentials.register(type,opts.credentials); |
| 30 | } |
| 31 | registry.registerType(type,constructor); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Called from a Node's constructor function, invokes the super-class |
| 36 | * constructor and attaches any credentials to the node. |
| 37 | * @param node the node object being created |
| 38 | * @param def the instance definition for the node |
| 39 | */ |
| 40 | function createNode(node,def) { |
| 41 | Node.call(node,def); |
| 42 | var creds = credentials.get(node.id); |
| 43 | if (creds) { |
| 44 | node.credentials = creds; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | function init(_settings,storage) { |
| 49 | credentials.init(storage); |
| 50 | flows.init(storage); |
| 51 | registry.init(_settings); |
| 52 | } |
| 53 | |
| 54 | function checkTypeInUse(id) { |
| 55 | var nodeInfo = registry.getNodeInfo(id); |
| 56 | if (!nodeInfo) { |
| 57 | throw new Error("Unrecognised id: "+info); |
| 58 | } |
| 59 | var inUse = {}; |
| 60 | flows.each(function(n) { |
| 61 | inUse[n.type] = (inUse[n.type]||0)+1; |
| 62 | }); |
| 63 | var nodesInUse = []; |
| 64 | nodeInfo.types.forEach(function(t) { |
| 65 | if (inUse[t]) { |
| 66 | nodesInUse.push(t); |
| 67 | } |
| 68 | }); |
| 69 | if (nodesInUse.length > 0) { |
| 70 | var msg = nodesInUse.join(", "); |
| 71 | throw new Error("Type in use: "+msg); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | function removeNode(id) { |
| 76 | checkTypeInUse(id); |
| 77 | return registry.removeNode(id); |
| 78 | } |
| 79 | |
| 80 | function removeModule(module) { |
| 81 | var info = registry.getNodeModuleInfo(module); |
| 82 | for (var i=0;i<info.nodes.length;i++) { |
| 83 | checkTypeInUse(info.nodes[i]); |
| 84 | } |
| 85 | return registry.removeModule(module); |
| 86 | } |
| 87 | |
| 88 | |
| 89 | function disableNode(id) { |
| 90 | checkTypeInUse(id); |
| 91 | return registry.disableNode(id); |
| 92 | } |
| 93 | |
| 94 | module.exports = { |
| 95 | // Lifecycle |
| 96 | init: init, |
| 97 | load: registry.load, |
| 98 | |
| 99 | // Node registry |
| 100 | createNode: createNode, |
| 101 | getNode: flows.get, |
| 102 | |
| 103 | addNode: registry.addNode, |
| 104 | removeNode: removeNode, |
| 105 | |
| 106 | addModule: registry.addModule, |
| 107 | removeModule: removeModule, |
| 108 | |
| 109 | enableNode: registry.enableNode, |
| 110 | disableNode: disableNode, |
| 111 | |
| 112 | // Node type registry |
| 113 | registerType: registerType, |
| 114 | getType: registry.get, |
| 115 | getNodeInfo: registry.getNodeInfo, |
| 116 | getNodeModuleInfo: registry.getNodeModuleInfo, |
| 117 | getNodeList: registry.getNodeList, |
| 118 | getNodeConfigs: registry.getNodeConfigs, |
| 119 | getNodeConfig: registry.getNodeConfig, |
| 120 | clearRegistry: registry.clear, |
| 121 | cleanNodeList: registry.cleanNodeList, |
| 122 | |
| 123 | // Flow handling |
| 124 | loadFlows: flows.load, |
| 125 | stopFlows: flows.stopFlows, |
| 126 | setFlows: flows.setFlows, |
| 127 | getFlows: flows.getFlows, |
| 128 | |
| 129 | // Credentials |
| 130 | addCredentials: credentials.add, |
| 131 | getCredentials: credentials.get, |
| 132 | deleteCredentials: credentials.delete |
| 133 | } |
| 134 | |