1# Copyright (c) 2017, The MITRE Corporation. All rights reserved.
2# See LICENSE.txt for complete terms.
3
4from __future__ import absolute_import
5
6from mixbox import fields
7
8import stix
9import stix.utils as utils
10import stix.bindings.stix_common as common_binding
11
12from .vocabs import VocabField
13from .structured_text import StructuredTextList
14from .datetimewithprecision import validate_precision
15
16
17class Confidence(stix.Entity):
18    _namespace = 'http://stix.mitre.org/common-1'
19    _binding = common_binding
20    _binding_class = common_binding.ConfidenceType
21
22    value = VocabField("Value")
23    descriptions = fields.TypedField("Description", StructuredTextList)
24    timestamp = fields.DateTimeField("timestamp")
25    timestamp_precision = fields.TypedField("timestamp_precision", preset_hook=validate_precision)
26    source = fields.TypedField("Source", type_="stix.common.InformationSource")
27
28    def __init__(self, value=None, timestamp=None, description=None, source=None):
29        super(Confidence, self).__init__()
30
31        self.timestamp = timestamp or utils.dates.now()
32        self.timestamp_precision = "second"
33        self.value = value
34        self.description = StructuredTextList(description)
35        self.source = source
36
37        # TODO: support confidence_assertion_chain
38        # self.confidence_assertion_chain = None
39
40    @property
41    def description(self):
42        """A single description about the contents or purpose of this object.
43
44        Default Value: ``None``
45
46        Note:
47            If this object has more than one description set, this will return
48            the description with the lowest ordinality value.
49
50        Returns:
51            An instance of :class:`.StructuredText`
52
53        """
54        return next(iter(self.descriptions), None)
55
56    @description.setter
57    def description(self, value):
58        from stix.common.structured_text import StructuredTextList
59        if value is None:
60            self.descriptions = StructuredTextList()
61        else:
62            self.descriptions = StructuredTextList(value)
63
64    def add_description(self, description):
65        """Adds a description to the ``descriptions`` collection.
66
67        This is the same as calling "foo.descriptions.add(bar)".
68
69        """
70        self.descriptions.add(description)
71
72
73# class ConfidenceAssertionChain(stix.Entity):
74#     _namespace = 'http://stix.mitre.org/common-2'
75#     _binding = common_binding
76#
77#     def __init__(self):
78#         self.confidence_assertions = []
79#
80#     def add_confidence_assertion(self, confidence_assertion):
81#         if isinstance(confidence_assertion, Confidence):
82#             self.confidence_assertions.append(confidence_assertion)
83#         else:
84#             tmp_confidence = Confidence(value=confidence_assertion)
85#             self.confidence_assertions.append(tmp_confidence)
86#
87#     def to_obj(self, return_obj=None, ns_info=None):
88#         super(ConfidenceAssertionChain, self).to_obj(return_obj=return_obj, ns_info=ns_info)
89#
90#         if not return_obj:
91#             return_obj = self._binding.ConfidenceAssertionChainType()
92#
93#         return None
94#
95#     @classmethod
96#     def from_obj(cls, obj, return_obj=None):
97#         return None
98#
99#     def to_dict(self):
100#         return {}
101#
102#     @classmethod
103#     def from_dict(cls, dict_repr, return_obj=None):
104#         return None
105