View Javadoc
1   package org.metricshub.ipmi.core.coding.commands.payload;
2   
3   /*-
4    * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
5    * IPMI Java Client
6    * ჻჻჻჻჻჻
7    * Copyright 2023 Verax Systems, MetricsHub
8    * ჻჻჻჻჻჻
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   *
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Lesser Public License for more details.
18   *
19   * You should have received a copy of the GNU General Lesser Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
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.commands.IpmiVersion;
28  import org.metricshub.ipmi.core.coding.commands.ResponseData;
29  import org.metricshub.ipmi.core.coding.payload.CompletionCode;
30  import org.metricshub.ipmi.core.coding.payload.IpmiPayload;
31  import org.metricshub.ipmi.core.coding.payload.lan.IPMIException;
32  import org.metricshub.ipmi.core.coding.payload.lan.IpmiLanRequest;
33  import org.metricshub.ipmi.core.coding.payload.lan.IpmiLanResponse;
34  import org.metricshub.ipmi.core.coding.payload.lan.NetworkFunction;
35  import org.metricshub.ipmi.core.coding.protocol.AuthenticationType;
36  import org.metricshub.ipmi.core.coding.protocol.IpmiMessage;
37  import org.metricshub.ipmi.core.coding.protocol.PayloadType;
38  import org.metricshub.ipmi.core.coding.security.CipherSuite;
39  import org.metricshub.ipmi.core.common.MessageComposer;
40  import org.metricshub.ipmi.core.common.MessageReader;
41  import org.metricshub.ipmi.core.common.TypeConverter;
42  
43  import java.security.InvalidKeyException;
44  import java.security.NoSuchAlgorithmException;
45  
46  /**
47   * Wrapper for Get Payload Activation Status Command.
48   */
49  public class GetPayloadActivationStatus extends IpmiCommandCoder {
50  
51      private static final int REQUEST_DATA_LENGTH = 1;
52      private static final int INSTANCE_CAPACITY_FIELD_LENGTH = 1;
53      private static final int AVAILABLE_INSTANCES_FIELD_LENGTH = 2;
54  
55      private final PayloadType payloadType;
56  
57      public GetPayloadActivationStatus(CipherSuite cipherSuite, PayloadType payloadType) {
58          super(IpmiVersion.V20, cipherSuite, AuthenticationType.RMCPPlus);
59          this.payloadType = payloadType;
60      }
61  
62      public GetPayloadActivationStatus(PayloadType payloadType) {
63          this.payloadType = payloadType;
64      }
65  
66      @Override
67      public byte getCommandCode() {
68          return CommandCodes.GET_PAYLOAD_ACTIVATION_STATUS;
69      }
70  
71      @Override
72      public NetworkFunction getNetworkFunction() {
73          return NetworkFunction.ApplicationRequest;
74      }
75  
76      @Override
77      protected IpmiPayload preparePayload(int sequenceNumber) throws NoSuchAlgorithmException, InvalidKeyException {
78          byte[] message = MessageComposer.get(REQUEST_DATA_LENGTH)
79                  .appendField(TypeConverter.intToByte(payloadType.getCode()))
80                  .getMessage();
81  
82          return new IpmiLanRequest(getNetworkFunction(), getCommandCode(), message, TypeConverter.intToByte(sequenceNumber));
83      }
84  
85      @Override
86      public ResponseData getResponseData(IpmiMessage message) throws IPMIException, NoSuchAlgorithmException, InvalidKeyException {
87          if (!isCommandResponse(message)) {
88              throw new IllegalArgumentException("This is not a response for Get Payload Activation Status");
89          }
90  
91          if (!(message.getPayload() instanceof IpmiLanResponse)) {
92              throw new IllegalArgumentException("Invalid response payload");
93          }
94  
95          if (((IpmiLanResponse) message.getPayload()).getCompletionCode() != CompletionCode.Ok) {
96              throw new IPMIException(((IpmiLanResponse) message.getPayload()).getCompletionCode());
97          }
98  
99          MessageReader messageReader = new MessageReader(message.getPayload().getData());
100 
101         GetPayloadActivationStatusResponseData responseData = new GetPayloadActivationStatusResponseData();
102 
103         byte instaceCapacity = messageReader.readNextField(INSTANCE_CAPACITY_FIELD_LENGTH)[0];
104         responseData.setInstanceCapacity(instaceCapacity);
105 
106         byte[] availableInstancesData = messageReader.readNextField(AVAILABLE_INSTANCES_FIELD_LENGTH);
107         responseData.setAvailableInstances(availableInstancesData);
108 
109         return responseData;
110     }
111 
112     @Override
113     public boolean equals(Object o) {
114         if (this == o) return true;
115         if (o == null || getClass() != o.getClass()) return false;
116 
117         GetPayloadActivationStatus that = (GetPayloadActivationStatus) o;
118 
119         if (payloadType != that.payloadType) {
120             return false;
121         }
122 
123         return getCipherSuite().equals(that.getCipherSuite());
124     }
125 
126     @Override
127     public int hashCode() {
128         int result = payloadType.getCode();
129         result = 31 * result + (getCipherSuite() != null ? getCipherSuite().hashCode() : 0);
130         return result;
131     }
132 }