Policy Reconfiguration, Component Spec, Help text
[dcaegen2/platform/cli.git] / dcae-cli / dcae_cli / util / policy.py
1 # ============LICENSE_START=======================================================
2 # org.onap.dcae
3 # ================================================================================
4 # Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
5 # ================================================================================
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 #      http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 # ============LICENSE_END=========================================================
18 #
19 # ECOMP is a trademark and service mark of AT&T Intellectual Property.
20
21 """
22 Function for Policy schema validation
23 """
24
25 from jsonschema import validate, ValidationError
26 from dcae_cli.util.logger import get_logger
27 from dcae_cli.util import reraise_with_msg
28
29 logger = get_logger('policy')
30
31 _SCHEMA = {
32   "$schema": "http://json-schema.org/draft-04/schema#",
33   "title": "Schema for policy changes",
34   "type": "object",
35   "properties": {
36       "updated_policies": {"type": "array"},
37       "removed_policies": {"type": "array"},
38       "policies":         {"type": "array"}
39   },
40   "additionalProperties": False
41 }
42
43 _validation_msg = """
44 Is your Policy file a valid json?
45 Does your Policy file follow this format?
46
47 {
48   "updated_policies": [{},{},...],
49   "removed_policies": [{},{},...],
50   "policies":         [{},{},...]
51 }
52 """
53
54
55 def validate_against_policy_schema(policy_file):
56     """Validate the policy file against the schema"""
57
58     try:
59         validate(policy_file, _SCHEMA)
60     except ValidationError as e:
61         logger.error("Policy file validation issue")
62         logger.error(_validation_msg)
63         reraise_with_msg(e, as_dcae=True)
64