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