1 package org.metricshub.ipmi.core.coding.commands.payload;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import org.metricshub.ipmi.core.coding.commands.CommandCodes;
26 import org.metricshub.ipmi.core.coding.commands.IpmiCommandCoder;
27 import org.metricshub.ipmi.core.coding.payload.CompletionCode;
28 import org.metricshub.ipmi.core.coding.payload.IpmiPayload;
29 import org.metricshub.ipmi.core.coding.payload.lan.IPMIException;
30 import org.metricshub.ipmi.core.coding.payload.lan.IpmiLanRequest;
31 import org.metricshub.ipmi.core.coding.payload.lan.IpmiLanResponse;
32 import org.metricshub.ipmi.core.coding.payload.lan.NetworkFunction;
33 import org.metricshub.ipmi.core.coding.protocol.IpmiMessage;
34 import org.metricshub.ipmi.core.coding.protocol.PayloadType;
35 import org.metricshub.ipmi.core.common.MessageComposer;
36 import org.metricshub.ipmi.core.common.TypeConverter;
37
38 import java.security.InvalidKeyException;
39 import java.security.NoSuchAlgorithmException;
40
41
42
43
44 public class DeactivatePayload extends IpmiCommandCoder<DeactivatePayloadResponseData> {
45
46 private static final int REQUEST_DATA_LENGTH = 6;
47
48
49
50
51 private final PayloadType payloadType;
52
53
54
55
56 private final int payloadInstance;
57
58
59
60
61
62
63
64
65
66 public DeactivatePayload(PayloadType payloadType, int payloadInstance) {
67 this.payloadType = payloadType;
68 this.payloadInstance = payloadInstance;
69 }
70
71 @Override
72 public byte getCommandCode() {
73 return CommandCodes.DEACTIVATE_PAYLOAD;
74 }
75
76 @Override
77 public NetworkFunction getNetworkFunction() {
78 return NetworkFunction.ApplicationRequest;
79 }
80
81 @Override
82 protected IpmiPayload preparePayload(int sequenceNumber) throws NoSuchAlgorithmException, InvalidKeyException {
83
84
85 byte[] message = MessageComposer.get(REQUEST_DATA_LENGTH)
86 .appendField(TypeConverter.intToByte(payloadType.getCode()))
87 .appendField(TypeConverter.intToByte(payloadInstance))
88 .getMessage();
89
90 return new IpmiLanRequest(getNetworkFunction(), getCommandCode(), message, TypeConverter.intToByte(sequenceNumber));
91 }
92
93 @Override
94 public DeactivatePayloadResponseData getResponseData(IpmiMessage message)
95 throws IPMIException,
96 NoSuchAlgorithmException,
97 InvalidKeyException {
98 if (!isCommandResponse(message)) {
99 throw new IllegalArgumentException("This is not a response for Deactivate Payload command");
100 }
101
102 if (!(message.getPayload() instanceof IpmiLanResponse)) {
103 throw new IllegalArgumentException("Invalid response payload");
104 }
105
106 IpmiLanResponse response = (IpmiLanResponse) message.getPayload();
107
108 if (response.getCompletionCode() != CompletionCode.Ok) {
109
110 DeactivatePayloadCompletionCode specificCompletionCode = DeactivatePayloadCompletionCode
111 .parseInt(response.getRawCompletionCode());
112
113
114 if (specificCompletionCode != DeactivatePayloadCompletionCode.PAYLOAD_ALREADY_DEACTIVATED) {
115 throw new IPMIException(
116 response.getCompletionCode(),
117 response.getRawCompletionCode(),
118 specificCompletionCode == null ? null : specificCompletionCode.getMessage() + ".");
119 }
120 }
121
122 return new DeactivatePayloadResponseData();
123 }
124
125 @Override
126 public boolean equals(Object o) {
127 if (this == o) {
128 return true;
129 }
130 if (o == null || getClass() != o.getClass()) {
131 return false;
132 }
133
134 DeactivatePayload that = (DeactivatePayload) o;
135
136 if (payloadInstance != that.payloadInstance) {
137 return false;
138 }
139
140 return payloadType == that.payloadType;
141 }
142
143 @Override
144 public int hashCode() {
145 int result = payloadInstance;
146 result = 31 * result + payloadType.getCode();
147 return result;
148 }
149 }