UNPKG

@mdf.js/openc2

Version:

MMS - API - Observability

157 lines 5.6 kB
"use strict"; /** * Copyright 2024 Mytra Control S.L. All rights reserved. * * Use of this source code is governed by an MIT-style license that can be found in the LICENSE file * or at https://opensource.org/licenses/MIT. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.Adapter = void 0; const crash_1 = require("@mdf.js/crash"); const openc2_core_1 = require("@mdf.js/openc2-core"); const stream_1 = require("stream"); const uuid_1 = require("uuid"); const types_1 = require("../types"); class Adapter extends stream_1.EventEmitter { /** * Create a new OpenC2 adapter for Redis * @param adapterOptions - Adapter configuration options * @param type - component type */ constructor(adapterOptions, type) { var _a; super(); this.adapterOptions = adapterOptions; this.type = type; /** Component identification */ this.componentId = (0, uuid_1.v4)(); this.separator = (_a = adapterOptions.separator) !== null && _a !== void 0 ? _a : types_1.OC2_TOPIC_SEPARATOR; this.subscriptions = this.defineSubscriptions(this.type, adapterOptions.id, adapterOptions.actuators); } /** Component name */ get name() { return this.adapterOptions.id; } /** * Manage the error in the adapter interface * @param error - error to be processed */ onErrorHandler(error) { const crash = crash_1.Crash.from(error); if (this.listenerCount('error') > 0) { this.emit('error', crash); } } /** * Returns an array of topics through which the message will be sent * @param message - message to be sent * @returns */ defineTopics(message) { if (message.msg_type === 'response') { return this.defineTopicsForResponse(message.to); } else { return this.defineTopicsForCommands(message.to, openc2_core_1.Accessors.getActuatorsFromCommandMessage(message)); } } /** * Returns an array of topics through which the adapter will listen for commands/responses * @param type - component type * @param id - instance identification * @param actuators - actuators */ defineSubscriptions(type, id, actuators = []) { const subscriptions = []; if (type === 'consumer') { subscriptions.push(this.getGeneralCommandTopic()); subscriptions.push(this.getDeviceCommandTopic(id)); for (const actuator of actuators) { subscriptions.push(this.getActuatorCommandTopic(actuator)); } } else if (type === 'producer') { subscriptions.push(this.getGeneralResponseTopic()); subscriptions.push(this.getProducerResponseTopic(id)); } return subscriptions; } /** * Returns an array of topics through which the message will be sent * @param to - Desired destinations of the message * @returns */ defineTopicsForResponse(to) { const topics = []; if (to.includes('*')) { topics.push(this.getGeneralResponseTopic()); } else { for (const producer of to) { topics.push(this.getProducerResponseTopic(producer)); } } return topics; } /** * Returns an array of topics through which the message will be sent * @param to - Desired destinations of the message * @param actuators - Actuators * @returns */ defineTopicsForCommands(to, actuators) { const topics = []; if (to.includes('*') && actuators.length === 0) { topics.push(this.getGeneralCommandTopic()); } else { for (const actuator of actuators) { topics.push(this.getActuatorCommandTopic(actuator)); } for (const device of to) { topics.push(this.getDeviceCommandTopic(device)); } } return topics; } /** * Returns the OpenC2 topic used to send responses for all nodes * @returns */ getGeneralResponseTopic() { return `${types_1.OC2_TOPIC_PREFIX}${this.separator}${types_1.OC2_TOPIC_RESPONSE_PREFIX}`; } /** * Returns the OpenC2 topic used to send responses to a concrete producer * @param producer - producer * @returns */ getProducerResponseTopic(producer) { return `${types_1.OC2_TOPIC_PREFIX}${this.separator}${types_1.OC2_TOPIC_RESPONSE_PREFIX}${this.separator}${producer}`; } /** * Returns the OpenC2 topic used to receive commands for all nodes * @returns */ getGeneralCommandTopic() { return `${types_1.OC2_TOPIC_PREFIX}${this.separator}${types_1.OC2_TOPIC_COMMAND_PREFIX}${this.separator}all`; } /** * Returns the OpenC2 topic used to receive commands for a concrete actuator * @param actuator - Actuator * @returns */ getActuatorCommandTopic(actuator) { return `${types_1.OC2_TOPIC_PREFIX}${this.separator}${types_1.OC2_TOPIC_COMMAND_PREFIX}${this.separator}ap${this.separator}${actuator}`; } /** * Returns the OpenC2 topic used to receive commands for a concrete device * @param actuator - Actuator * @returns */ getDeviceCommandTopic(device) { return `${types_1.OC2_TOPIC_PREFIX}${this.separator}${types_1.OC2_TOPIC_COMMAND_PREFIX}${this.separator}device${this.separator}${device}`; } } exports.Adapter = Adapter; //# sourceMappingURL=Adapter.js.map