0f31e098131f796e01a13ffdc04e6ca62bcb8fbe
[aai/sparky-fe.git] / src / app / vnfSearch / VnfSearchActions.js
1 /*
2  * ============LICENSE_START===================================================
3  * SPARKY (AAI UI service)
4  * ============================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
8  * ============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=====================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25
26 import {
27   vnfActionTypes,
28   VNF_RESULT_URL
29 } from 'app/vnfSearch/VnfSearchConstants.js';
30
31 import {
32   getVnfProvStatusQueryString,
33   getVnfOrchStatusQueryString,
34   getVnfCountQueryString
35 } from 'app/networking/NetworkUtil.js';
36 import {
37   POST,
38   POST_HEADER,
39   ERROR_RETRIEVING_DATA
40 } from 'app/networking/NetworkConstants.js';
41 import {
42   getSetGlobalMessageEvent,
43   getClearGlobalMessageEvent
44 } from 'app/GlobalInlineMessageBar/GlobalInlineMessageBarActions.js';
45
46 let fetch = require('node-fetch');
47 fetch.Promise = require('es6-promise').Promise;
48
49 const itemKeyWord = 'key';
50 const countKeyWord = 'doc_count';
51
52 function getInvalidQueryEvent() {
53   return {
54     type: vnfActionTypes.VNF_NETWORK_ERROR,
55     data: {errorMsg: ERROR_RETRIEVING_DATA}
56   };
57 }
58 /*it is a vertical bar chart then y and x are switched */
59 function getProvStatusEvent(responseJson) {
60   if (responseJson && responseJson.groupby_aggregation &&
61     responseJson.groupby_aggregation.buckets &&
62     responseJson.groupby_aggregation.buckets.length > 0) {
63     let groupByProvStatusBucket;
64     let dataPoints = [];
65     for (groupByProvStatusBucket of
66       responseJson.groupby_aggregation.buckets) {
67       dataPoints.push({
68         'x': groupByProvStatusBucket[itemKeyWord].split('=', 1)[0],
69         'y': groupByProvStatusBucket[countKeyWord]
70       });
71     }
72
73     let newProvStatusChartData = [
74       {
75         'values': dataPoints
76       }
77     ];
78
79     let provStatusCountChartData = {
80       chartData: newProvStatusChartData
81     };
82     return {
83       type: vnfActionTypes.COUNT_BY_PROV_STATUS_RECEIVED,
84       data: {provStatusCountChartData}
85     };
86   }
87   else {
88     return {
89       type: vnfActionTypes.ERROR_NO_DATA_FOR_PROV_STATUS_IN_SEARCH_RANGE_RECEIVED
90     };
91   }
92 }
93
94 function getOrchStatusEvent(responseJson) {
95   if (responseJson && responseJson.groupby_aggregation &&
96     responseJson.groupby_aggregation.buckets &&
97     responseJson.groupby_aggregation.buckets.length > 0) {
98     let groupByOrchStatusBucket;
99     let dataPoints = [];
100     for (groupByOrchStatusBucket of
101       responseJson.groupby_aggregation.buckets) {
102       dataPoints.push({
103         'x': groupByOrchStatusBucket[itemKeyWord].split('=', 1)[0],
104         'y': groupByOrchStatusBucket[countKeyWord]
105       });
106     }
107
108     let newOrchStatusChartData = [
109       {
110         'values': dataPoints
111       }
112     ];
113
114     let orchStatusCountChartData = {
115       chartData: newOrchStatusChartData
116     };
117     return {
118       type: vnfActionTypes.COUNT_BY_ORCH_STATUS_RECEIVED,
119       data: {orchStatusCountChartData}
120     };
121   }
122   else {
123     return {
124       type: vnfActionTypes.ERROR_NO_DATA_FOR_ORCH_STATUS_IN_SEARCH_RANGE_RECEIVED
125     };
126   }
127 }
128
129 function getTotalVnfEvent(responseJson) {
130   if (responseJson && responseJson.count && responseJson.count > 0) {
131     return {
132       type: vnfActionTypes.TOTAL_VNF_COUNT_RECEIVED,
133       data: {count: responseJson.count}
134     };
135   }
136   else {
137     return {
138       type: vnfActionTypes.ERROR_NO_COUNT_RECEIVED
139     };
140   }
141 }
142
143 export function processProvStatusVisualizationOnSearchChange(requestObject) {
144   return dispatch => {
145     return fetch(VNF_RESULT_URL, {
146       method: POST,
147       headers: POST_HEADER,
148       body: JSON.stringify(getVnfProvStatusQueryString(requestObject))
149     }).then(
150       (response) => response.json()
151     ).then(
152       (responseJson) => {
153         dispatch(getProvStatusEvent(responseJson));
154       }
155     ).catch(
156       () => {
157         dispatch(getInvalidQueryEvent());
158       }
159     );
160   };
161 }
162
163 export function processOrchStatusVisualizationOnSearchChange(requestObject) {
164   return dispatch => {
165     return fetch(VNF_RESULT_URL, {
166       method: POST,
167       headers: POST_HEADER,
168       body: JSON.stringify(getVnfOrchStatusQueryString(requestObject))
169     }).then(
170       (response) => response.json()
171     ).then(
172       (responseJson) => {
173         dispatch(getOrchStatusEvent(responseJson));
174       }
175     ).catch(
176       () => {
177         dispatch(getInvalidQueryEvent());
178       }
179     );
180   };
181 }
182
183 export function processTotalVnfVisualizationOnSearchChange(requestObject) {
184   return dispatch => {
185     return fetch(VNF_RESULT_URL + '/count', {
186       method: POST,
187       headers: POST_HEADER,
188       body: JSON.stringify(
189         getVnfCountQueryString(requestObject))
190     }).then(
191       (response) => response.json()
192     ).then(
193       (responseJson) => {
194         dispatch(getTotalVnfEvent(responseJson));
195       }
196     ).catch(
197       () => {
198         dispatch(getInvalidQueryEvent());
199       }
200     );
201   };
202 }
203
204 export function setNotificationText(msgText, msgSeverity) {
205   if (msgText.length > 0) {
206     return dispatch => {
207       dispatch(
208         getSetGlobalMessageEvent(msgText, msgSeverity));
209     };
210   } else {
211     return dispatch => {
212       dispatch(getClearGlobalMessageEvent());
213     };
214   }
215 }