View Javadoc
1   /**
2    * Portions of this software was developed by employees of the National Institute
3    * of Standards and Technology (NIST), an agency of the Federal Government and is
4    * being made available as a public service. Pursuant to title 17 United States
5    * Code Section 105, works of NIST employees are not subject to copyright
6    * protection in the United States. This software may be subject to foreign
7    * copyright. Permission in the United States and in foreign countries, to the
8    * extent that NIST may hold copyright, to use, copy, modify, create derivative
9    * works, and distribute this software and its documentation without fee is hereby
10   * granted on a non-exclusive basis, provided that this notice and disclaimer
11   * of warranty appears in all copies.
12   *
13   * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
14   * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
15   * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
16   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
17   * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
18   * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE.  IN NO EVENT
19   * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
20   * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
21   * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
22   * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
23   * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
24   * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
25   */
26  
27  package gov.nist.secauto.swid.builder.resource.firmware;
28  
29  import gov.nist.secauto.swid.builder.ValidationException;
30  import gov.nist.secauto.swid.builder.resource.AbstractResourceBuilder;
31  import gov.nist.secauto.swid.builder.resource.ResourceCollectionEntryGenerator;
32  import gov.nist.secauto.swid.builder.util.Util;
33  
34  import java.math.BigInteger;
35  import java.time.ZonedDateTime;
36  import java.util.LinkedHashMap;
37  import java.util.LinkedList;
38  import java.util.List;
39  import java.util.Map;
40  import java.util.Objects;
41  import java.util.Random;
42  
43  public class FirmwareBuilder extends AbstractResourceBuilder<FirmwareBuilder> {
44    private FirmwareIdentifier id;
45    private ZonedDateTime creationTimestamp = ZonedDateTime.now();
46    private BigInteger version;
47    private String description;
48    private byte[] nonce;
49    private List<ResourceReference> aliases = new LinkedList<>();
50    private List<ResourceReference> dependencies = new LinkedList<>();
51    private String blockDeviceIdentifier;
52    private DeviceIdentifier targetDeviceIdentifier;
53    private List<FirmwarePayloadBuilder> payloads = new LinkedList<>();
54    private Map<Integer, byte[]> extensions = new LinkedHashMap<>();
55  
56    public FirmwareBuilder() {
57      super();
58      byte[] nonceData = new byte[8];
59      new Random().nextBytes(nonceData);
60      this.nonce = nonceData;
61    }
62  
63    @Override
64    public <T> void accept(T parentContext, ResourceCollectionEntryGenerator<T> creator) {
65      creator.generate(parentContext, this);
66    }
67  
68    /**
69     * Retrieve the identifier of the firmware manifest.
70     * 
71     * @return the id
72     */
73    public FirmwareIdentifier getId() {
74      return id;
75    }
76  
77    /**
78     * Set the id of the firmware manifest.
79     * 
80     * @param id
81     *          the firmware's identifier
82     * @return the same builder instance
83     */
84    public FirmwareBuilder id(FirmwareIdentifier id) {
85      Objects.requireNonNull(id, "id");
86      this.id = id;
87      return this;
88    }
89  
90    /**
91     * Retrieve the identifier of the firmware manifest.
92     * 
93     * @return the creationTimestamp
94     */
95    public ZonedDateTime getCreationTimestamp() {
96      return creationTimestamp;
97    }
98  
99    /**
100    * Set the creation timestamp of the firmware manifest.
101    * 
102    * @param dateTime
103    *          the creationTimestamp to set
104    * @return the same builder instance
105    */
106   public FirmwareBuilder creationTimestamp(ZonedDateTime dateTime) {
107     Objects.requireNonNull(dateTime, "dateTime");
108     this.creationTimestamp = dateTime;
109     return this;
110   }
111 
112   /**
113    * Retrieve the version of the firmware manifest.
114    * 
115    * @return the version
116    */
117   public BigInteger getVersion() {
118     return version;
119   }
120 
121   /**
122    * Set the version of the firmware manifest.
123    * 
124    * @param version
125    *          the firmware's version
126    * @return the same builder instance
127    */
128   public FirmwareBuilder version(BigInteger version) {
129     Objects.requireNonNull(version, "version");
130     this.version = version;
131     return this;
132   }
133 
134   /**
135    * Retrieve the description of the firmware manifest.
136    * 
137    * @return the description
138    */
139   public String getDescription() {
140     return description;
141   }
142 
143   /**
144    * Set the version of the firmware manifest.
145    * 
146    * @param description
147    *          the firmware's description
148    * @return the same builder instance
149    */
150   public FirmwareBuilder description(String description) {
151     Util.requireNonEmpty(description, "description");
152     this.description = description;
153     return this;
154   }
155 
156   /**
157    * Retrieve the nonce of the firmware manifest.
158    * 
159    * @return the nonce
160    */
161   public byte[] getNonce() {
162     return nonce;
163   }
164 
165   /**
166    * Set the nonce of the firmware manifest.
167    * 
168    * @param nonce
169    *          the firmware's nonce
170    * @return the same builder instance
171    */
172   public FirmwareBuilder nonce(byte[] nonce) {
173     Objects.requireNonNull(nonce, "nonce");
174     this.nonce = nonce;
175     return this;
176   }
177 
178   /**
179    * Retrieve the aliases of the firmware manifest.
180    * 
181    * @return the aliases
182    */
183   public List<ResourceReference> getAliases() {
184     return aliases;
185   }
186 
187   /**
188    * Adds a new alias to the list of aliases.
189    * 
190    * @param alias
191    *          the alias to add
192    * @return the same builder instance
193    */
194   public FirmwareBuilder addAlias(ResourceReference alias) {
195     Objects.requireNonNull(alias, "alias");
196     this.aliases.add(alias);
197     return this;
198   }
199 
200   /**
201    * Retrieve the dependencies of the firmware manifest.
202    * 
203    * @return the dependencies
204    */
205   public List<ResourceReference> getDependencies() {
206     return dependencies;
207   }
208 
209   /**
210    * Adds a new dependency to the list of aliases.
211    * 
212    * @param dependency
213    *          the dependency to add
214    * @return the same builder instance
215    */
216   public FirmwareBuilder addDependency(ResourceReference dependency) {
217     Objects.requireNonNull(dependency, "dependency");
218     this.dependencies.add(dependency);
219     return this;
220   }
221 
222   /**
223    * Retrieve the block device identifier for the firmware.
224    * 
225    * @return the blockDeviceIdentifier
226    */
227   public String getBlockDeviceIdentifier() {
228     return blockDeviceIdentifier;
229   }
230 
231   /**
232    * Set the block device identifier.
233    * 
234    * @param blockDeviceIdentifier
235    *          a {@code non-null} block device identifier
236    * @return the same builder instance
237    */
238   public FirmwareBuilder blockDeviceIdentifier(String blockDeviceIdentifier) {
239     Util.requireNonEmpty(blockDeviceIdentifier, "blockDeviceIdentifier");
240     this.blockDeviceIdentifier = blockDeviceIdentifier;
241     return this;
242   }
243 
244   /**
245    * Retrieve the target device identifier for the firmware manifest.
246    * 
247    * @return the targetDeviceIdentifier
248    */
249   public DeviceIdentifier getTargetDeviceIdentifier() {
250     return targetDeviceIdentifier;
251   }
252 
253   /**
254    * Set the target device identifier.
255    * 
256    * @param targetDeviceIdentifier
257    *          a {@code non-null} target device identifier
258    * @return the same builder instance
259    */
260   public FirmwareBuilder targetDeviceIdentifier(DeviceIdentifier targetDeviceIdentifier) {
261     Objects.requireNonNull(targetDeviceIdentifier, "targetDeviceIdentifier");
262     this.targetDeviceIdentifier = targetDeviceIdentifier;
263     return this;
264   }
265 
266   /**
267    * Retrieve the firmware payloads of the firmware manifest.
268    * 
269    * @return the payloads
270    */
271   public List<FirmwarePayloadBuilder> getPayloads() {
272     return payloads;
273   }
274 
275   /**
276    * Adds a new payload to the list of payloads.
277    * 
278    * @param payload
279    *          the payload to add
280    * @return the same builder instance
281    */
282   public FirmwareBuilder addPayload(FirmwarePayloadBuilder payload) {
283     Objects.requireNonNull(payload, "payload");
284     this.payloads.add(payload);
285     return this;
286   }
287 
288   /**
289    * Retrieve the firmware manifest extensions.
290    * 
291    * @return the extensions
292    */
293   public Map<Integer, byte[]> getExtensions() {
294     return extensions;
295   }
296 
297   /**
298    * Adds a new extension object.
299    * 
300    * @param extenstionType
301    *          the extension type identifier
302    * @param extension
303    *          the extension content
304    * @return the same builder instance
305    */
306   public FirmwareBuilder addExtension(int extenstionType, byte[] extension) {
307     this.extensions.put(extenstionType, extension);
308     return this;
309   }
310 
311   @Override
312   public void validate() throws ValidationException {
313     validateNonNull("id", id);
314     validateNonNull("creationTimestamp", creationTimestamp);
315     validateNonNull("version", version);
316     validateNonEmpty("nonce", nonce);
317     validateNonNull("targetDeviceIdentifier", nonce);
318   }
319 
320   public static FirmwareBuilder create() {
321     return new FirmwareBuilder();
322   }
323 
324 }