summaryrefslogtreecommitdiff
path: root/src/components/policy/src/policy/policy_table/table_struct/validation.cc
blob: be39b8022e252ec66a295402708fabe8bd66b636 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include <algorithm>
#include "./types.h"
#include "utils/logger.h"

namespace {
bool IsTypeInvalid(rpc::Enum<rpc::policy_table_interface_base::RequestType> request) { return !request.is_valid(); }
}

namespace rpc {
namespace policy_table_interface_base {

CREATE_LOGGERPTR_GLOBAL(logger_, "PolicyTableValidation")

void RemoveInvalidTypes(RequestTypes& types) {
 types.erase(
       std::remove_if(types.begin(), types.end(), &IsTypeInvalid),
       types.end());
}

bool PolicyBase::Validate() const {
  return true;
}

bool ApplicationPoliciesSection::Validate() const {
  ApplicationPolicies::iterator it_default_policy =
      apps.find(kDefaultApp);
  ApplicationPolicies::iterator it_pre_data_policy =
      apps.find(kPreDataConsentApp);

  // Default and PreData policies are mandatory
  if (apps.end() == it_default_policy || apps.end() == it_pre_data_policy) {
    LOG4CXX_ERROR(logger_, "Default or preData policy is not present.");
    return false;
  }

  // Device policy is mandatory
  if (!device.is_initialized()) {
    LOG4CXX_ERROR(logger_, "Device policy is not present.");
    return false;
  }

  PolicyTableType pt_type = GetPolicyTableType();
  if (PT_PRELOADED != pt_type && PT_UPDATE != pt_type) {
    return true;
  }

  if (!it_default_policy->second.RequestType.is_valid()) {
    LOG4CXX_WARN(logger_,
                 "Default policy RequestTypes are not valid. Will be cleaned.");
    RemoveInvalidTypes(*it_default_policy->second.RequestType);
    // If preloaded does not have valid default types - validation fails
    // Otherwise default will be empty, i.e. all types allowed
    if (PT_PRELOADED == pt_type) {
      if (it_default_policy->second.RequestType->empty()) {
        LOG4CXX_ERROR(
              logger_,
              "Default policy RequestTypes empty after clean-up. Exiting.");
        return false;
      }
    }
  }

  ApplicationPolicies::iterator iter = apps.begin();
  ApplicationPolicies::iterator end_iter = apps.end();


  while(iter != end_iter) {
    ApplicationParams& app_params = (*iter).second;
    bool is_request_type_ommited = !app_params.RequestType.is_initialized();
    bool is_request_type_valid = app_params.RequestType.is_valid();
    bool is_request_type_empty = app_params.RequestType->empty();

    if (PT_PRELOADED == pt_type) {
      if (!is_request_type_valid) {
        LOG4CXX_WARN(
              logger_,
              "App policy RequestTypes are not valid. Will be cleaned.");
        RemoveInvalidTypes(*app_params.RequestType);
        if (app_params.RequestType->empty()) {
          LOG4CXX_ERROR(
                logger_,
                "App policy RequestTypes empty after clean-up. Exiting.");
          return false;
        }
      }
    } else {
      if (is_request_type_ommited) {
        LOG4CXX_WARN(logger_, "App policy RequestTypes ommited."
                              " Will be replaced with default.");
        app_params.RequestType = apps[kDefaultApp].RequestType;
        ++iter;
        continue;
      }
      if (!is_request_type_valid) {
        LOG4CXX_WARN(
              logger_,
              "App policy RequestTypes are invalid. Will be cleaned.");
        RemoveInvalidTypes(*app_params.RequestType);
        if (app_params.RequestType->empty()) {
          LOG4CXX_WARN(logger_, "App policy RequestTypes empty after clean-up."
                                " Will be replaced with default.");
          app_params.RequestType = apps[kDefaultApp].RequestType;
          ++iter;
          continue;
        }
      }
      if (is_request_type_empty) {
        LOG4CXX_WARN(logger_, "App policy RequestTypes empty.");
      }
    }
    ++iter;
  }

  return true;
}

bool ApplicationParams::Validate() const {
  return true;
}
bool RpcParameters::Validate() const {
  return true;
}
bool Rpcs::Validate() const {
  return true;
}

bool ModuleConfig::Validate() const {

  if (PT_PRELOADED == GetPolicyTableType()) {

    if (vehicle_make.is_initialized()) {
      return false;
    }
    if (vehicle_year.is_initialized()) {
      return false;
    }
    if (vehicle_model.is_initialized()) {
      return false;
    }
  }
  return true;
}

bool MessageString::Validate() const {
  return true;
}

bool MessageLanguages::Validate() const {
  if (PT_SNAPSHOT == GetPolicyTableType()) {
    return false;
  }
  return true;
}

bool ConsumerFriendlyMessages::Validate() const {
  if (PT_SNAPSHOT == GetPolicyTableType()) {
    return false;
  }
  return true;
}

bool ModuleMeta::Validate() const {
  return true;
}

bool AppLevel::Validate() const {
  if (PT_PRELOADED == GetPolicyTableType() ||
      PT_UPDATE == GetPolicyTableType()) {
    return false;
  }
  return true;
}

bool UsageAndErrorCounts::Validate() const {
  if (PT_PRELOADED == GetPolicyTableType() ||
      PT_UPDATE == GetPolicyTableType()) {
   return false;
  }
  return true;
}
bool DeviceParams::Validate() const {
  return true;
}
bool PolicyTable::Validate() const {
  if (PT_PRELOADED == GetPolicyTableType() ||
      PT_UPDATE == GetPolicyTableType()) {
    if (device_data.is_initialized()) {
      return false;
    }
  }
  return true;
}
bool Table::Validate() const {
  return true;
}
}  // namespace policy_table_interface_base
}  // namespace rpc