4ca149d6f95016070d0c6e2c5b20fabfafa76d87
[aai/sparky-fe.git] / src / app / vnfSearch / VnfSearchActions.js
1 /*
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 import {
22   vnfActionTypes,
23   VNF_FILTER_AGGREGATION_URL,
24   CHART_PROV_STATUS,
25   CHART_ORCH_STATUS,
26   CHART_NF_TYPE,
27   CHART_NF_ROLE,
28   TOTAL_VNF_COUNT,
29   VNF_FILTER_EMPTY_RESULT
30 } from 'app/vnfSearch/VnfSearchConstants.js';
31 import {
32   POST,
33   POST_HEADER,
34   ERROR_RETRIEVING_DATA
35 } from 'app/networking/NetworkConstants.js';
36 import {
37   getSetGlobalMessageEvent,
38   getClearGlobalMessageEvent
39 } from 'app/globalInlineMessageBar/GlobalInlineMessageBarActions.js';
40 import {MESSAGE_LEVEL_WARNING} from 'utils/GlobalConstants.js';
41
42 let fetch = require('node-fetch');
43 fetch.Promise = require('es6-promise').Promise;
44
45 const itemKeyWord = 'key';
46 const countKeyWord = 'doc_count';
47
48 function getInvalidQueryEvent() {
49   return {
50     type: vnfActionTypes.VNF_NETWORK_ERROR,
51     data: {errorMsg: ERROR_RETRIEVING_DATA}
52   };
53 }
54
55 function processProvData(provDataList) {
56   let dataPoints = [];
57   let newProvStatusChartData = CHART_PROV_STATUS.emptyData;
58   for (let provData of provDataList) {
59     dataPoints.push(
60       {
61         'x': provData[itemKeyWord],
62         'y': provData[countKeyWord]
63       }
64     );
65   }
66
67   if (dataPoints.length > 0) {
68     newProvStatusChartData = {
69       'values': dataPoints
70     };
71   }
72
73   return newProvStatusChartData;
74 }
75
76 function processOrchData(orchDataList) {
77   let dataPoints = [];
78   let newOrchStatusChartData = CHART_ORCH_STATUS.emptyData;
79   for (let orchData of orchDataList) {
80     dataPoints.push(
81       {
82         'x': orchData[itemKeyWord],
83         'y': orchData[countKeyWord]
84       }
85     );
86   }
87
88   if (dataPoints.length > 0) {
89     newOrchStatusChartData = {
90       'values': dataPoints
91     };
92   }
93
94   return newOrchStatusChartData;
95 }
96
97 function processNfTypeData(nfDataList) {
98   let dataPoints = [];
99   let newNfTypeChartData = CHART_NF_TYPE.emptyData;
100   for (let nfData of nfDataList) {
101     dataPoints.push(
102       {
103         'x': nfData[itemKeyWord],
104         'y': nfData[countKeyWord]
105       }
106     );
107   }
108
109   if (dataPoints.length > 0) {
110     newNfTypeChartData = {
111       'values': dataPoints
112     };
113   }
114
115   return newNfTypeChartData;
116 }
117
118 function processNfRoleData(nfDataList) {
119   let dataPoints = [];
120   let newNfRoleChartData = CHART_NF_ROLE.emptyData;
121   for (let nfData of nfDataList) {
122     dataPoints.push(
123       {
124         'x': nfData[itemKeyWord],
125         'y': nfData[countKeyWord]
126       }
127     );
128   }
129
130   if (dataPoints.length > 0) {
131     newNfRoleChartData = {
132       'values': dataPoints
133     };
134   }
135
136   return newNfRoleChartData;
137 }
138
139 function getVnfFilterAggregationQueryString(filterValueMap) {
140   let filterList = [];
141
142   for (let filter in filterValueMap) {
143     if (filterValueMap[filter] !== '') {
144       filterList.push(
145         {
146           'filterId': filter,
147           'filterValue': filterValueMap[filter]
148         }
149       );
150     } else {
151       filterList.push(
152         {
153           'filterId': filter
154         }
155       );
156     }
157   }
158
159   return {
160     'filters': filterList
161   };
162 }
163
164 function getVnfVisualizationsResultsEvent(results) {
165   let count = TOTAL_VNF_COUNT.emptyData;
166   let provData = CHART_PROV_STATUS.emptyData;
167   let orchData = CHART_ORCH_STATUS.emptyData;
168   let netFuncTypeData = CHART_NF_TYPE.emptyData;
169   let netFuncRoleData = CHART_NF_ROLE.emptyData;
170
171   if (results.total) {
172     count = results.total;
173   }
174
175   if (results['aggregations'] && results['aggregations']['prov-status']) {
176     provData = processProvData(results['aggregations']['prov-status']);
177   }
178
179   if (results['aggregations'] &&
180     results['aggregations']['orchestration-status']) {
181     orchData = processOrchData(results['aggregations']['orchestration-status']);
182   }
183
184   if (results['aggregations'] &&
185     results['aggregations']['nf-type']) {
186     netFuncTypeData = processNfTypeData(results['aggregations']['nf-type']);
187   }
188
189   if (results['aggregations'] &&
190     results['aggregations']['nf-role']) {
191     netFuncRoleData = processNfRoleData(results['aggregations']['nf-role']);
192   }
193
194   return {
195     type: vnfActionTypes.VNF_SEARCH_RESULTS_RECEIVED,
196     data: {
197       count: count,
198       provStatusData: provData,
199       orchStatusData: orchData,
200       nfTypeData: netFuncTypeData,
201       nfRoleData: netFuncRoleData
202     }
203   };
204 }
205
206 function setBusyFeedback(){
207   return {
208     type: vnfActionTypes.VNF_ACTIVATE_BUSY_FEEDBACK
209   };
210 }
211
212 function disableBusyFeedback(){
213   return {
214     type: vnfActionTypes.VNF_DISABLE_BUSY_FEEDBACK
215   };
216 }
217
218 export function processVnfVisualizationsOnFilterChange(filterValueMap) {
219   return dispatch => {
220     dispatch(setBusyFeedback());
221     return fetch(VNF_FILTER_AGGREGATION_URL, {
222       method: POST,
223       headers: POST_HEADER,
224       body: JSON.stringify(getVnfFilterAggregationQueryString(filterValueMap))
225     }).then(
226       (response) => response.json()
227     ).then(
228       (responseJson) => {
229         if(responseJson.total === 0) {
230           dispatch(getSetGlobalMessageEvent(VNF_FILTER_EMPTY_RESULT, MESSAGE_LEVEL_WARNING));
231         } else {
232           dispatch(getClearGlobalMessageEvent());
233         }
234         dispatch(getVnfVisualizationsResultsEvent(responseJson));
235       }
236     ).then(
237       () => {
238         dispatch(disableBusyFeedback());
239       }
240     ).catch(
241       () => {
242         dispatch(disableBusyFeedback());
243         dispatch(getInvalidQueryEvent());
244       }
245     );
246   };
247 }
248
249 export function processVnfFilterPanelCollapse(isOpen) {
250   let vnfVisualizationPanelClass = 'collapsible-panel-main-panel';
251
252   if (isOpen) {
253     vnfVisualizationPanelClass += ' vertical-filter-panel-is-open';
254   }
255
256   return {
257     type: vnfActionTypes.VNF_FILTER_PANEL_TOGGLED,
258     data: {
259       vnfVisualizationPanelClass: vnfVisualizationPanelClass
260     }
261   };
262 }
263
264 export function setNotificationText(msgText, msgSeverity) {
265   if (msgText.length > 0) {
266     return dispatch => {
267       dispatch(
268         getSetGlobalMessageEvent(msgText, msgSeverity));
269     };
270   } else {
271     return dispatch => {
272       dispatch(getClearGlobalMessageEvent());
273     };
274   }
275 }
276
277 export function clearVnfSearchData() {
278   return {
279     type: vnfActionTypes.VNF_SEARCH_RESULTS_RECEIVED,
280     data: {
281       count: '',
282       provStatusData: CHART_PROV_STATUS.emptyData,
283       orchStatusData: CHART_ORCH_STATUS.emptyData,
284       nfTypeData: CHART_NF_TYPE.emptyData,
285       nfRoleData: CHART_NF_ROLE.emptyData
286     }
287   };
288 }