/
CollectCommand.ts
145 lines (129 loc) · 6.53 KB
/
CollectCommand.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*
*/
import { AbstractSession, Logger, TextUtils } from "@zowe/imperative";
import { ZosmfRestClient } from "@zowe/core-for-zowe-sdk";
import { isNullOrUndefined } from "util";
import { ConsoleValidator } from "./ConsoleValidator";
import { IZosmfCollectResponse } from "./doc/zosmf/IZosmfCollectResponse";
import { collectProcessingDetails, ConsoleConstants, decreaseCounter, resetCounter } from "./ConsoleConstants";
import { ICollectParms } from "./doc/ICollectParms";
import { IConsoleResponse } from "./doc/IConsoleResponse";
import { ConsoleResponseService } from "./ConsoleResponseService";
/**
* Get the response to a command that was issued asynchronously with the Issue Command service
* @export
* @class CollectCommand
*/
export class CollectCommand {
/**
* Collect any messages related to the synchronous command response key provided
* @static
* @param {AbstractSession} session representing connection to this api
* @param {string} consoleName name of the EMCS console that is used to issue the command
* @param {string} commandResponseKey command response key from the Issue Command request
* @returns {Promise<IZosmfIssueResponse>} command response on resolve, @see {IZosmfIssueResponse}
* @memberof CollectCommand
*/
public static collectCommon(session: AbstractSession, consoleName: string, commandResponseKey: string) {
ConsoleValidator.validateCollectCommonParms(session, consoleName, commandResponseKey);
return ZosmfRestClient.getExpectJSON<IZosmfCollectResponse>(session,
CollectCommand.getResource(consoleName, commandResponseKey));
}
/**
* Collect any messages (from default console) related to the synchronous command response key provided
* @static
* @param {AbstractSession} session representing connection to this api
* @param {string} commandResponseKey command response key from the Issue Command request
* @return {Promise<IZosmfCollectResponse>} command response on resolve, @see {IZosmfCollectResponse}
* @memberof CollectCommand
*/
public static collectDefConsoleCommon(session: AbstractSession, commandResponseKey: string) {
return CollectCommand.collectCommon(session, ConsoleConstants.RES_DEF_CN, commandResponseKey);
}
/**
* Collect any messages related to the synchronous command response key provided and collect them into IConsoleResponse
*
* To control additional collection and other behaviors, populate the ICollectParms object according
* to your needs (see ICollectParms for details).
* @static
* @param {AbstractSession} session representing connection to this api
* @param {ICollectParms} parms console collect parameters, @see {ICollectParms}
* @param {IConsoleResponse} response response from previous command (if present), @see {IConsoleResponse}
* @return {Promise<IConsoleResponse>} command response on resolve, @see {IConsoleResponse}
* @memberof CollectCommand
*/
public static async collect(session: AbstractSession, parms: ICollectParms, response?: IConsoleResponse) {
ConsoleValidator.validateCollectParms(session, parms);
if (isNullOrUndefined(response)) {
response = ConsoleResponseService.getEmptyConsoleResponse();
}
const consoleName: string = isNullOrUndefined(parms.consoleName) ? ConsoleConstants.RES_DEF_CN : parms.consoleName;
const maxFollowUpAttempts: number = CollectCommand.getFollowUpAttempts(parms);
const timeout = CollectCommand.getTimeout(parms);
let collectResponse: IZosmfCollectResponse;
let followUpCounter = maxFollowUpAttempts;
do {
Logger.getImperativeLogger().info(TextUtils.formatMessage(collectProcessingDetails.message,
{
timer: timeout,
counter: followUpCounter
}));
try {
if (timeout > 0) {
await new Promise((doNothing) => setTimeout(doNothing, timeout));
}
collectResponse = await CollectCommand.collectCommon(session, consoleName, parms.commandResponseKey);
response = ConsoleResponseService.populate(collectResponse, response, parms.processResponses);
if (ConsoleResponseService.isLastZosmfResponseEmpty(response)) {
followUpCounter--;
Logger.getImperativeLogger().info(decreaseCounter.message);
} else {
followUpCounter = maxFollowUpAttempts;
Logger.getImperativeLogger().info(resetCounter.message);
}
} catch (error) {
response = ConsoleResponseService.populateError(error, response);
followUpCounter = 0;
}
} while (followUpCounter > 0 || response.keywordDetected);
return response;
}
/**
* Get resource path for collect command
* @param {string} consoleName name of the EMCS console that is used to issue the command
* @param {string} commandResponseKey command response key from the Issue Command request
* @return {string} resource path
* @memberof CollectCommand
*/
public static getResource(consoleName: string, commandResponseKey: string): string {
return ConsoleConstants.RESOURCE + "/" + consoleName + ConsoleConstants.SOL_MSGS + "/" + commandResponseKey;
}
private static readonly TO_SECONDS: number = 1000;
/**
*
* @param {ICollectParms} parms parameters for collect command
* @return {number}
*/
private static getFollowUpAttempts(parms: ICollectParms): number {
return isNullOrUndefined(parms) || isNullOrUndefined(parms.followUpAttempts) ? ConsoleConstants.DEFAULT_FOLLOWUP_ATTEMPTS
: parms.followUpAttempts;
}
/**
* @static
* @param {ICollectParms} parms console collect parameters, @see {ICollectParms}s
* @return {number} timeout in milliseconds or default value
* @memberof CollectCommand
*/
private static getTimeout(parms: ICollectParms): number {
return isNullOrUndefined(parms) || isNullOrUndefined(parms.waitToCollect) ? ConsoleConstants.DEFAULT_TIMEOUT
: parms.waitToCollect * CollectCommand.TO_SECONDS;
}
}