Seed policysync container code
[dcaegen2/deployments.git] / dcae-services-policy-sync / tests / test_client_v1.py
1 # ============LICENSE_START=======================================================
2 # Copyright (c) 2021 AT&T Intellectual Property. All rights reserved.
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 # ============LICENSE_END=========================================================
16
17 from aiohttp import web, WSMsgType
18 import json, pytest, re
19 from policysync.clients import PolicyClientV1 as PolicyClient 
20
21 DECISION_ENDPOINT = 'policy/pdpx/v1/decision'
22 async def get_decision(request):
23     req_data = await request.json()
24     assert req_data['ONAPName'] == 'DCAE'
25     assert req_data['ONAPComponent'] == 'policy-sync'
26     assert req_data['action'] == 'configure'
27     assert req_data['resource'] == {
28         'policy-id': [
29             'onap.scaleout.tca', 
30             'onap.restart.tca'
31         ]
32     }
33
34
35     j = {
36         "policies": {
37             "onap.scaleout.tca": {
38                 "type": "onap.policies.monitoring.cdap.tca.hi.lo.app",
39                 "version": "1.0.0",
40                 "metadata": {"policy-id": "onap.scaleout.tca"},
41                 "properties": {
42                     "tca_policy": {
43                         "domain": "measurementsForVfScaling",
44                         "metricsPerEventName": [
45                             {
46                                 "eventName": "vLoadBalancer",
47                                 "controlLoopSchemaType": "VNF",
48                                 "policyScope": "type=configuration",
49                                 "policyName": "onap.scaleout.tca",
50                                 "policyVersion": "v0.0.1",
51                                 "thresholds": [
52                                     {
53                                         "closedLoopControlName": "ControlLoop-vDNS-6f37f56d-a87d-4b85-b6a9-cc953cf779b3",
54                                         "closedLoopEventStatus": "ONSET",
55                                         "version": "1.0.2",
56                                         "fieldPath": "$.event.measurementsForVfScalingFields.vNicPerformanceArray[*].receivedBroadcastPacketsAccumulated",
57                                         "thresholdValue": 500,
58                                         "direction": "LESS_OR_EQUAL",
59                                         "severity": "MAJOR",
60                                     },
61                                     {
62                                         "closedLoopControlName": "ControlLoop-vDNS-6f37f56d-a87d-4b85-b6a9-cc953cf779b3",
63                                         "closedLoopEventStatus": "ONSET",
64                                         "version": "1.0.2",
65                                         "fieldPath": "$.event.measurementsForVfScalingFields.vNicPerformanceArray[*].receivedBroadcastPacketsAccumulated",
66                                         "thresholdValue": 5000,
67                                         "direction": "GREATER_OR_EQUAL",
68                                         "severity": "CRITICAL",
69                                     },
70                                 ],
71                             }
72                         ],
73                     }
74                 },
75             },
76             "onap.restart.tca": {
77                 "type": "onap.policies.monitoring.cdap.tca.hi.lo.app",
78                 "version": "1.0.0",
79                 "metadata": {"policy-id": "onap.restart.tca", "policy-version": 1},
80                 "properties": {
81                     "tca_policy": {
82                         "domain": "measurementsForVfScaling",
83                         "metricsPerEventName": [
84                             {
85                                 "eventName": "Measurement_vGMUX",
86                                 "controlLoopSchemaType": "VNF",
87                                 "policyScope": "DCAE",
88                                 "policyName": "DCAE.Config_tca-hi-lo",
89                                 "policyVersion": "v0.0.1",
90                                 "thresholds": [
91                                     {
92                                         "closedLoopControlName": "ControlLoop-vCPE-48f0c2c3-a172-4192-9ae3-052274181b6e",
93                                         "version": "1.0.2",
94                                         "fieldPath": "$.event.measurementsForVfScalingFields.additionalMeasurements[*].arrayOfFields[0].value",
95                                         "thresholdValue": 0,
96                                         "direction": "EQUAL",
97                                         "severity": "MAJOR",
98                                         "closedLoopEventStatus": "ABATED",
99                                     },
100                                     {
101                                         "closedLoopControlName": "ControlLoop-vCPE-48f0c2c3-a172-4192-9ae3-052274181b6e",
102                                         "version": "1.0.2",
103                                         "fieldPath": "$.event.measurementsForVfScalingFields.additionalMeasurements[*].arrayOfFields[0].value",
104                                         "thresholdValue": 0,
105                                         "direction": "GREATER",
106                                         "severity": "CRITICAL",
107                                         "closedLoopEventStatus": "ONSET",
108                                     },
109                                 ],
110                             }
111                         ],
112                     }
113                 },
114             },
115         }
116     }
117
118     return web.json_response(j)
119
120
121 @pytest.fixture
122 def policyclient(aiohttp_client, loop):
123     app = web.Application()
124     app.router.add_route("POST", "/" + DECISION_ENDPOINT, get_decision)
125     fake_client = loop.run_until_complete(aiohttp_client(app))
126     server = "{}://{}:{}".format("http", fake_client.host, fake_client.port)
127     return PolicyClient({}, server)
128
129
130 async def test_getconfig(policyclient):
131     j = await policyclient.get_config(ids=['onap.scaleout.tca', 'onap.restart.tca' ])
132     assert j == [{
133         "type": "onap.policies.monitoring.cdap.tca.hi.lo.app",
134         "version": "1.0.0",
135         "metadata": {
136             "policy-id": "onap.scaleout.tca"
137         },
138         "policyName": "onap.scaleout.tca.1-0-0.xml",
139         "policyVersion": "1.0.0",
140         "config": {
141             "tca_policy": {
142                 "domain": "measurementsForVfScaling",
143                 "metricsPerEventName": [{
144                     "eventName": "vLoadBalancer",
145                     "controlLoopSchemaType": "VNF",
146                     "policyScope": "type=configuration",
147                     "policyName": "onap.scaleout.tca",
148                     "policyVersion": "v0.0.1",
149                     "thresholds": [{
150                             "closedLoopControlName": "ControlLoop-vDNS-6f37f56d-a87d-4b85-b6a9-cc953cf779b3",
151                             "closedLoopEventStatus": "ONSET",
152                             "version": "1.0.2",
153                             "fieldPath": "$.event.measurementsForVfScalingFields.vNicPerformanceArray[*].receivedBroadcastPacketsAccumulated",
154                             "thresholdValue": 500,
155                             "direction": "LESS_OR_EQUAL",
156                             "severity": "MAJOR"
157                         },
158                         {
159                             "closedLoopControlName": "ControlLoop-vDNS-6f37f56d-a87d-4b85-b6a9-cc953cf779b3",
160                             "closedLoopEventStatus": "ONSET",
161                             "version": "1.0.2",
162                             "fieldPath": "$.event.measurementsForVfScalingFields.vNicPerformanceArray[*].receivedBroadcastPacketsAccumulated",
163                             "thresholdValue": 5000,
164                             "direction": "GREATER_OR_EQUAL",
165                             "severity": "CRITICAL"
166                         }
167                     ]
168                 }]
169             }
170         }
171     }, {
172         "type": "onap.policies.monitoring.cdap.tca.hi.lo.app",
173         "version": "1.0.0",
174         "metadata": {
175             "policy-id": "onap.restart.tca",
176             "policy-version": 1
177         },
178         "policyName": "onap.restart.tca.1-0-0.xml",
179         "policyVersion": "1.0.0",
180         "config": {
181             "tca_policy": {
182                 "domain": "measurementsForVfScaling",
183                 "metricsPerEventName": [{
184                     "eventName": "Measurement_vGMUX",
185                     "controlLoopSchemaType": "VNF",
186                     "policyScope": "DCAE",
187                     "policyName": "DCAE.Config_tca-hi-lo",
188                     "policyVersion": "v0.0.1",
189                     "thresholds": [{
190                             "closedLoopControlName": "ControlLoop-vCPE-48f0c2c3-a172-4192-9ae3-052274181b6e",
191                             "version": "1.0.2",
192                             "fieldPath": "$.event.measurementsForVfScalingFields.additionalMeasurements[*].arrayOfFields[0].value",
193                             "thresholdValue": 0,
194                             "direction": "EQUAL",
195                             "severity": "MAJOR",
196                             "closedLoopEventStatus": "ABATED"
197                         },
198                         {
199                             "closedLoopControlName": "ControlLoop-vCPE-48f0c2c3-a172-4192-9ae3-052274181b6e",
200                             "version": "1.0.2",
201                             "fieldPath": "$.event.measurementsForVfScalingFields.additionalMeasurements[*].arrayOfFields[0].value",
202                             "thresholdValue": 0,
203                             "direction": "GREATER",
204                             "severity": "CRITICAL",
205                             "closedLoopEventStatus": "ONSET"
206                         }
207                     ]
208                 }]
209             }
210         }
211     }]
212     await policyclient.close()
213
214
215 async def test_supports_notifications(policyclient):
216     assert not policyclient.supports_notifications()