[AAI-92 Amsterdam] Update license
[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_RESULT_URL
26 } from 'app/vnfSearch/VnfSearchConstants.js';
27
28 import {
29   getVnfProvStatusQueryString,
30   getVnfOrchStatusQueryString,
31   getVnfCountQueryString
32 } from 'app/networking/NetworkUtil.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
43 let fetch = require('node-fetch');
44 fetch.Promise = require('es6-promise').Promise;
45
46 const itemKeyWord = 'key';
47 const countKeyWord = 'doc_count';
48
49 function getInvalidQueryEvent() {
50   return {
51     type: vnfActionTypes.VNF_NETWORK_ERROR,
52     data: {errorMsg: ERROR_RETRIEVING_DATA}
53   };
54 }
55 /*it is a vertical bar chart then y and x are switched */
56 function getProvStatusEvent(responseJson) {
57   if (responseJson && responseJson.groupby_aggregation &&
58     responseJson.groupby_aggregation.buckets &&
59     responseJson.groupby_aggregation.buckets.length > 0) {
60     let groupByProvStatusBucket;
61     let dataPoints = [];
62     for (groupByProvStatusBucket of
63       responseJson.groupby_aggregation.buckets) {
64       dataPoints.push({
65         'x': groupByProvStatusBucket[itemKeyWord].split('=', 1)[0],
66         'y': groupByProvStatusBucket[countKeyWord]
67       });
68     }
69
70     let newProvStatusChartData = [
71       {
72         'values': dataPoints
73       }
74     ];
75
76     let provStatusCountChartData = {
77       chartData: newProvStatusChartData
78     };
79     return {
80       type: vnfActionTypes.COUNT_BY_PROV_STATUS_RECEIVED,
81       data: {provStatusCountChartData}
82     };
83   }
84   else {
85     return {
86       type: vnfActionTypes.ERROR_NO_DATA_FOR_PROV_STATUS_IN_SEARCH_RANGE_RECEIVED
87     };
88   }
89 }
90
91 function getOrchStatusEvent(responseJson) {
92   if (responseJson && responseJson.groupby_aggregation &&
93     responseJson.groupby_aggregation.buckets &&
94     responseJson.groupby_aggregation.buckets.length > 0) {
95     let groupByOrchStatusBucket;
96     let dataPoints = [];
97     for (groupByOrchStatusBucket of
98       responseJson.groupby_aggregation.buckets) {
99       dataPoints.push({
100         'x': groupByOrchStatusBucket[itemKeyWord].split('=', 1)[0],
101         'y': groupByOrchStatusBucket[countKeyWord]
102       });
103     }
104
105     let newOrchStatusChartData = [
106       {
107         'values': dataPoints
108       }
109     ];
110
111     let orchStatusCountChartData = {
112       chartData: newOrchStatusChartData
113     };
114     return {
115       type: vnfActionTypes.COUNT_BY_ORCH_STATUS_RECEIVED,
116       data: {orchStatusCountChartData}
117     };
118   }
119   else {
120     return {
121       type: vnfActionTypes.ERROR_NO_DATA_FOR_ORCH_STATUS_IN_SEARCH_RANGE_RECEIVED
122     };
123   }
124 }
125
126 function getTotalVnfEvent(responseJson) {
127   if (responseJson && responseJson.count && responseJson.count > 0) {
128     return {
129       type: vnfActionTypes.TOTAL_VNF_COUNT_RECEIVED,
130       data: {count: responseJson.count}
131     };
132   }
133   else {
134     return {
135       type: vnfActionTypes.ERROR_NO_COUNT_RECEIVED
136     };
137   }
138 }
139
140 export function processProvStatusVisualizationOnSearchChange(requestObject) {
141   return dispatch => {
142     return fetch(VNF_RESULT_URL, {
143       method: POST,
144       headers: POST_HEADER,
145       body: JSON.stringify(getVnfProvStatusQueryString(requestObject))
146     }).then(
147       (response) => response.json()
148     ).then(
149       (responseJson) => {
150         dispatch(getProvStatusEvent(responseJson));
151       }
152     ).catch(
153       () => {
154         dispatch(getInvalidQueryEvent());
155       }
156     );
157   };
158 }
159
160 export function processOrchStatusVisualizationOnSearchChange(requestObject) {
161   return dispatch => {
162     return fetch(VNF_RESULT_URL, {
163       method: POST,
164       headers: POST_HEADER,
165       body: JSON.stringify(getVnfOrchStatusQueryString(requestObject))
166     }).then(
167       (response) => response.json()
168     ).then(
169       (responseJson) => {
170         dispatch(getOrchStatusEvent(responseJson));
171       }
172     ).catch(
173       () => {
174         dispatch(getInvalidQueryEvent());
175       }
176     );
177   };
178 }
179
180 export function processTotalVnfVisualizationOnSearchChange(requestObject) {
181   return dispatch => {
182     return fetch(VNF_RESULT_URL + '/count', {
183       method: POST,
184       headers: POST_HEADER,
185       body: JSON.stringify(
186         getVnfCountQueryString(requestObject))
187     }).then(
188       (response) => response.json()
189     ).then(
190       (responseJson) => {
191         dispatch(getTotalVnfEvent(responseJson));
192       }
193     ).catch(
194       () => {
195         dispatch(getInvalidQueryEvent());
196       }
197     );
198   };
199 }
200
201 export function setNotificationText(msgText, msgSeverity) {
202   if (msgText.length > 0) {
203     return dispatch => {
204       dispatch(
205         getSetGlobalMessageEvent(msgText, msgSeverity));
206     };
207   } else {
208     return dispatch => {
209       dispatch(getClearGlobalMessageEvent());
210     };
211   }
212 }