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.payload.IpmiPayload;
28  import org.metricshub.ipmi.core.coding.payload.lan.IPMIException;
29  import org.metricshub.ipmi.core.coding.payload.lan.IpmiLanRequest;
30  import org.metricshub.ipmi.core.coding.payload.lan.NetworkFunction;
31  import org.metricshub.ipmi.core.coding.protocol.IpmiMessage;
32  import org.metricshub.ipmi.core.coding.protocol.PayloadType;
33  import org.metricshub.ipmi.core.common.MessageComposer;
34  import org.metricshub.ipmi.core.common.MessageReader;
35  import org.metricshub.ipmi.core.common.TypeConverter;
36  
37  import java.security.InvalidKeyException;
38  import java.security.NoSuchAlgorithmException;
39  
40  /**
41   * Wrapper for Get Payload Activation Status Command.
42   */
43  public class GetPayloadActivationStatus extends IpmiCommandCoder<GetPayloadActivationStatusResponseData> {
44  
45  	private static final int REQUEST_DATA_LENGTH = 1;
46  	private static final int INSTANCE_CAPACITY_FIELD_LENGTH = 1;
47  	private static final int AVAILABLE_INSTANCES_FIELD_LENGTH = 2;
48  
49  	private final PayloadType payloadType;
50  
51  	/**
52  	 * Creates the command.
53  	 *
54  	 * @param payloadType the payload type
55  	 */
56  	public GetPayloadActivationStatus(PayloadType payloadType) {
57  		this.payloadType = payloadType;
58  	}
59  
60  	@Override
61  	public byte getCommandCode() {
62  		return CommandCodes.GET_PAYLOAD_ACTIVATION_STATUS;
63  	}
64  
65  	@Override
66  	public NetworkFunction getNetworkFunction() {
67  		return NetworkFunction.ApplicationRequest;
68  	}
69  
70  	@Override
71  	protected IpmiPayload preparePayload(int sequenceNumber) throws NoSuchAlgorithmException, InvalidKeyException {
72  		byte[] message = MessageComposer.get(REQUEST_DATA_LENGTH)
73  				.appendField(TypeConverter.intToByte(payloadType.getCode()))
74  				.getMessage();
75  
76  		return new IpmiLanRequest(getNetworkFunction(), getCommandCode(), message, TypeConverter.intToByte(sequenceNumber));
77  	}
78  
79  	@Override
80  	public GetPayloadActivationStatusResponseData getResponseData(IpmiMessage message)
81  			throws IPMIException,
82  			NoSuchAlgorithmException,
83  			InvalidKeyException {
84  
85  		MessageReader messageReader = new MessageReader(validateResponse(message));
86  
87  		GetPayloadActivationStatusResponseData responseData = new GetPayloadActivationStatusResponseData();
88  
89  		byte instaceCapacity = messageReader.readNextField(INSTANCE_CAPACITY_FIELD_LENGTH)[0];
90  		responseData.setInstanceCapacity(instaceCapacity);
91  
92  		byte[] availableInstancesData = messageReader.readNextField(AVAILABLE_INSTANCES_FIELD_LENGTH);
93  		responseData.setAvailableInstances(availableInstancesData);
94  
95  		return responseData;
96  	}
97  
98  	@Override
99  	public boolean equals(Object o) {
100 		if (this == o) {
101 			return true;
102 		}
103 		if (o == null || getClass() != o.getClass()) {
104 			return false;
105 		}
106 
107 		GetPayloadActivationStatus that = (GetPayloadActivationStatus) o;
108 
109 		return payloadType == that.payloadType;
110 	}
111 
112 	@Override
113 	public int hashCode() {
114 		int result = payloadType.getCode();
115 		return result;
116 	}
117 }