Add ORAN Python SDK first draft
[it/dep.git] / smo-install / test / pythonsdk / src / oransdk / policy / policy.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 # SPDX-License-Identifier: Apache-2.0
4 """Onap Policy module."""
5
6 from dataclasses import dataclass
7 from typing import Dict
8 from oransdk.configuration import settings
9 from onapsdk.onap_service import OnapService
10
11 @dataclass
12 class PolicyType:
13     """PolicyType dataclass."""
14
15     type: str
16     version: str
17
18
19 class OranPolicy(OnapService):
20     """Onap Policy library."""
21
22     pap_url = settings.POLICY_PAP_URL
23     api_url = settings.POLICY_API_URL
24     header = {"Accept": "application/json", "Content-Type": "application/json"}
25
26     @classmethod
27     def get_components_status(cls,
28                               basic_auth: Dict[str, str]) -> Dict:
29         """
30         Get status of Policy component.
31
32         Args:
33            basic_auth: (Dict[str, str]) for example:{ 'username': 'bob', 'password': 'secret' }
34
35         Returns:
36            the status of the Policy component
37
38         """
39         url = f"{cls.pap_url}/policy/pap/v1/components/healthcheck"
40         status = cls.send_message_json('GET',
41                                        'Get status of Policy component',
42                                        url,
43                                        basic_auth=basic_auth)
44         return status
45
46     @classmethod
47     def get_policy_status(cls,
48                           basic_auth: Dict[str, str]) -> Dict:
49         """
50         Get status of all the policies.
51
52         Returns:
53            the status of all the policies
54
55         """
56         url = f"{cls.pap_url}/policy/pap/v1/policies/status"
57         status = cls.send_message_json('GET',
58                                        'Get status of all the policies',
59                                        url,
60                                        basic_auth=basic_auth)
61         return status
62
63     @classmethod
64     def get_policy(cls,
65                    policy_type: PolicyType,
66                    policy_name,
67                    policy_version,
68                    basic_auth: Dict[str, str]) -> Dict:
69         """
70         Get the policy.
71
72         Args:
73            policy_type: the policy type
74            policy_name: the policy name
75            policy_version: the version of the policy
76            basic_auth: (Dict[str, str]) for example:{ 'username': 'bob', 'password': 'secret' }
77
78         Returns:
79            the policy
80
81         """
82         url = f"{cls.api_url}/policy/api/v1/policytypes/{policy_type.type}/versions/"\
83               + f"{policy_type.version}/policies/{policy_name}/versions/{policy_version}"
84         policy = cls.send_message_json('GET',
85                                        'Get the policy',
86                                        url,
87                                        basic_auth=basic_auth)
88         return policy
89
90     @classmethod
91     def create_policy(cls,
92                       policy_type: PolicyType,
93                       policy_data,
94                       basic_auth: Dict[str, str]) -> None:
95         """
96         Create a policy.
97
98         Args:
99            policy_type: the policy type
100            type_version: the version of the policy type
101            policy_data: the policy to be created, in binary format
102
103         """
104         url = f"{cls.api_url}/policy/api/v1/policytypes/{policy_type.type}/"\
105               + f"versions/{policy_type.version}/policies"
106         cls.send_message('POST',
107                          'Create Policy',
108                          url,
109                          data=policy_data,
110                          headers=cls.header,
111                          basic_auth=basic_auth)
112
113     @classmethod
114     def deploy_policy(cls,
115                       policy_data,
116                       basic_auth: Dict[str, str]) -> None:
117         """
118         Deploy a policy.
119
120         Args:
121            policy_data: the policy to be deployed, in binary format
122
123         """
124         url = f"{cls.pap_url}/policy/pap/v1/pdps/policies"
125         cls.send_message('POST',
126                          'Deploy Policy',
127                          url,
128                          data=policy_data,
129                          headers=cls.header,
130                          basic_auth=basic_auth)