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