View Javadoc
1   /*
2    * Copyright 2020-2022 Foreseeti AB <https://foreseeti.com>
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.mal_lang.langspec;
18  
19  import static java.util.Objects.requireNonNull;
20  
21  import jakarta.json.Json;
22  import jakarta.json.JsonObject;
23  
24  /**
25   * Immutable class representing the risk of an attack step in a MAL language.
26   *
27   * @since 1.0.0
28   */
29  public final class Risk {
30    private final boolean isConfidentiality;
31    private final boolean isIntegrity;
32    private final boolean isAvailability;
33  
34    /**
35     * Constructs a new {@code Risk} object.
36     *
37     * @param isConfidentiality whether this {@code Risk} object is confidentiality
38     * @param isIntegrity whether this {@code Risk} object is integrity
39     * @param isAvailability whether this {@code Risk} object is availability
40     * @since 1.0.0
41     */
42    public Risk(boolean isConfidentiality, boolean isIntegrity, boolean isAvailability) {
43      this.isConfidentiality = isConfidentiality;
44      this.isIntegrity = isIntegrity;
45      this.isAvailability = isAvailability;
46    }
47  
48    /**
49     * Returns whether this {@code Risk} object is confidentiality.
50     *
51     * @return whether this {@code Risk} object is confidentiality
52     * @since 1.0.0
53     */
54    public boolean isConfidentiality() {
55      return this.isConfidentiality;
56    }
57  
58    /**
59     * Returns whether this {@code Risk} object is integrity.
60     *
61     * @return whether this {@code Risk} object is integrity
62     * @since 1.0.0
63     */
64    public boolean isIntegrity() {
65      return this.isIntegrity;
66    }
67  
68    /**
69     * Returns whether this {@code Risk} object is availability.
70     *
71     * @return whether this {@code Risk} object is availability
72     * @since 1.0.0
73     */
74    public boolean isAvailability() {
75      return this.isAvailability;
76    }
77  
78    JsonObject toJson() {
79      return Json.createObjectBuilder()
80          .add("isConfidentiality", this.isConfidentiality)
81          .add("isIntegrity", this.isIntegrity)
82          .add("isAvailability", this.isAvailability)
83          .build();
84    }
85  
86    /**
87     * Creates a new {@code Risk} from a {@link jakarta.json.JsonObject}.
88     *
89     * @param jsonRisk the {@link jakarta.json.JsonObject}
90     * @return a new {@code Risk}
91     * @throws java.lang.NullPointerException if {@code jsonRisk} is {@code null}
92     * @since 1.0.0
93     */
94    public static Risk fromJson(JsonObject jsonRisk) {
95      requireNonNull(jsonRisk);
96      return new Risk(
97          jsonRisk.getBoolean("isConfidentiality"),
98          jsonRisk.getBoolean("isIntegrity"),
99          jsonRisk.getBoolean("isAvailability"));
100   }
101 }