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