View Javadoc
1   package org.metricshub.ipmi.core.coding;
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.protocol.decoder.Protocolv15Decoder;
26  import org.metricshub.ipmi.core.coding.protocol.decoder.Protocolv20Decoder;
27  import org.metricshub.ipmi.core.coding.protocol.encoder.IpmiEncoder;
28  import org.metricshub.ipmi.core.coding.rmcp.RmcpEncoder;
29  import org.metricshub.ipmi.core.coding.rmcp.RmcpIpmiMessage;
30  
31  import java.security.InvalidKeyException;
32  import java.security.NoSuchAlgorithmException;
33  
34  /**
35   * Creates RMCP packet containing encrypted IPMI command from IPMICommand
36   * wrapper class or raw byte data.
37   */
38  public final class Encoder {
39  
40      /**
41       * Encodes IPMI command specified by payloadCoder into byte array raw data.
42       *
43       * @param protcolEncoder
44       *            - instance of {@link IpmiEncoder} class for encoding of the
45       *            IPMI session header. {@link Protocolv15Decoder} or
46       *            {@link Protocolv20Decoder} should be used (depending on IPMI
47       *            protocol version used).
48       * @param payloadCoder
49       *            - instance of {@link PayloadCoder} class used for building
50       *            IPMI message payload.
51       * @param messageSequenceNumber
52       *            - A generated sequence number used for matching request and
53       *            response. If IPMI message is sent in a session, it is used as
54       *            a Session Sequence Number. For all IPMI messages,
55       *            messageSequenceNumber is used as a IPMI LAN Message sequence
56       *            number and as an IPMI payload message tag.
57       * @param sessionId
58       *            - ID of the managed system's session message is being sent in.
59       *            For sessionless commands should be set to 0.
60       * @return encoded IPMI command
61       * @throws NoSuchAlgorithmException
62       *             - when authentication, confidentiality or integrity algorithm
63       *             fails.
64       * @throws InvalidKeyException
65       *             - when creating of the algorithm key fails
66       */
67      public static byte[] encode(IpmiEncoder protcolEncoder, PayloadCoder payloadCoder,
68                                  int messageSequenceNumber, int sessionSequenceNumber, int sessionId)
69              throws NoSuchAlgorithmException, InvalidKeyException {
70          return RmcpEncoder
71                  .encode(new RmcpIpmiMessage(protcolEncoder.encode(payloadCoder
72                          .encodePayload(messageSequenceNumber, sessionSequenceNumber, sessionId))));
73      }
74  
75      private Encoder() {
76      }
77  }