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