refactor: factor out JSON schema validation function getter
[e-mobility-charging-stations-simulator.git] / src / charging-station / ocpp / OCPPResponseService.ts
1 import Ajv, { type JSONSchemaType, type ValidateFunction } from 'ajv';
2 import ajvFormats from 'ajv-formats';
3
4 import { OCPPServiceUtils } from './OCPPServiceUtils';
5 import type { ChargingStation } from '../../charging-station';
6 import { OCPPError } from '../../exception';
7 import type { IncomingRequestCommand, JsonType, OCPPVersion, RequestCommand } from '../../types';
8 import { logger } from '../../utils';
9
10 const moduleName = 'OCPPResponseService';
11
12 export abstract class OCPPResponseService {
13 private static instance: OCPPResponseService | null = null;
14
15 public jsonIncomingRequestResponseValidateFunctions: Map<
16 IncomingRequestCommand,
17 ValidateFunction<JsonType>
18 >;
19
20 private readonly version: OCPPVersion;
21 private readonly ajv: Ajv;
22 private jsonRequestValidateFunctions: Map<RequestCommand, ValidateFunction<JsonType>>;
23
24 public abstract jsonIncomingRequestResponseSchemas: Map<
25 IncomingRequestCommand,
26 JSONSchemaType<JsonType>
27 >;
28
29 protected constructor(version: OCPPVersion) {
30 this.version = version;
31 this.ajv = new Ajv({
32 keywords: ['javaType'],
33 multipleOfPrecision: 2,
34 });
35 ajvFormats(this.ajv);
36 this.jsonRequestValidateFunctions = new Map<RequestCommand, ValidateFunction<JsonType>>();
37 this.jsonIncomingRequestResponseValidateFunctions = new Map<
38 IncomingRequestCommand,
39 ValidateFunction<JsonType>
40 >();
41 this.responseHandler = this.responseHandler.bind(this) as <
42 ReqType extends JsonType,
43 ResType extends JsonType,
44 >(
45 chargingStation: ChargingStation,
46 commandName: RequestCommand,
47 payload: ResType,
48 requestPayload: ReqType,
49 ) => Promise<void>;
50 this.validateResponsePayload = this.validateResponsePayload.bind(this) as <T extends JsonType>(
51 chargingStation: ChargingStation,
52 commandName: RequestCommand,
53 schema: JSONSchemaType<T>,
54 payload: T,
55 ) => boolean;
56 }
57
58 public static getInstance<T extends OCPPResponseService>(this: new () => T): T {
59 if (OCPPResponseService.instance === null) {
60 OCPPResponseService.instance = new this();
61 }
62 return OCPPResponseService.instance as T;
63 }
64
65 protected validateResponsePayload<T extends JsonType>(
66 chargingStation: ChargingStation,
67 commandName: RequestCommand,
68 schema: JSONSchemaType<T>,
69 payload: T,
70 ): boolean {
71 if (chargingStation.stationInfo?.ocppStrictCompliance === false) {
72 return true;
73 }
74 const validate = this.getJsonRequestValidateFunction<T>(commandName, schema);
75 if (validate(payload)) {
76 return true;
77 }
78 logger.error(
79 `${chargingStation.logPrefix()} ${moduleName}.validateResponsePayload: Command '${commandName}' response PDU is invalid: %j`,
80 validate.errors,
81 );
82 throw new OCPPError(
83 OCPPServiceUtils.ajvErrorsToErrorType(validate.errors),
84 'Response PDU is invalid',
85 commandName,
86 JSON.stringify(validate.errors, undefined, 2),
87 );
88 }
89
90 protected emptyResponseHandler() {
91 /* This is intentional */
92 }
93
94 private getJsonRequestValidateFunction<T extends JsonType>(
95 commandName: RequestCommand,
96 schema: JSONSchemaType<T>,
97 ) {
98 if (this.jsonRequestValidateFunctions.has(commandName) === false) {
99 this.jsonRequestValidateFunctions.set(commandName, this.ajv.compile<T>(schema).bind(this));
100 }
101 return this.jsonRequestValidateFunctions.get(commandName)!;
102 }
103
104 public abstract responseHandler<ReqType extends JsonType, ResType extends JsonType>(
105 chargingStation: ChargingStation,
106 commandName: RequestCommand,
107 payload: ResType,
108 requestPayload: ReqType,
109 ): Promise<void>;
110 }