66e93393c757b5adcfb7223c19377969871fc592
[aai/sparky-fe.git] / src / app / inventory / InventoryActions.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   GET,
23   POST_HEADER,
24   ERROR_RETRIEVING_DATA,
25   SAME_ORIGIN,
26   BACKEND_IP_ADDRESS
27 } from 'app/networking/NetworkConstants.js';
28
29 import {
30   INVENTORY_GEO_VISUALIZATION_SEARCH_URL,
31   GEO_VISUALIZATION_QUERY_STRING_PARAMETERS,
32   INVENTORY_COUNT_BY_TYPE_SEARCH_URL,
33   INVENTORY_COUNT_BY_DATE_SEARCH_URL,
34   InventoryActionTypes
35 } from 'app/inventory/InventoryConstants.js';
36
37 import {MESSAGE_LEVEL_DANGER} from 'utils/GlobalConstants.js';
38
39 function getSuccessfulTopographicVisualizationQueryEvent(responseJson) {
40   return {
41     type: InventoryActionTypes.TOPOGRAPHIC_QUERY_SUCCESS,
42     data: {
43       plotPoints: responseJson.plotPoints
44     }
45   };
46 }
47
48 function getFailedTopographicVisualizationQueryEvent() {
49   return {
50     type: InventoryActionTypes.TOPOGRAPHIC_QUERY_FAILED,
51     data: {
52       message: ERROR_RETRIEVING_DATA,
53       severity: MESSAGE_LEVEL_DANGER
54     }
55   };
56 }
57
58 function getSuccessfulCountByTypeQueryEvent(responseJson) {
59   return {
60     type: InventoryActionTypes.COUNT_BY_ENTITY_SUCCESS,
61     data: {
62       countByType: responseJson.result
63     }
64   };
65 }
66
67 function getFailedCountByTypeQueryEvent() {
68   return {
69     type: InventoryActionTypes.COUNT_BY_ENTITY_FAILED,
70     data: {
71       message: ERROR_RETRIEVING_DATA,
72       severity: MESSAGE_LEVEL_DANGER
73     }
74   };
75 }
76
77 function getSuccessfulCountsByDateQueryEvent(responseJson) {
78   return {
79     type: InventoryActionTypes.COUNT_BY_DATE_SUCCESS,
80     data: {
81       countByDate: responseJson.result
82     }
83   };
84 }
85
86 function getFailedCountByDateQueryEvent() {
87   return {
88     type: InventoryActionTypes.COUNT_BY_DATE_FAILED,
89     data: {
90       message: ERROR_RETRIEVING_DATA,
91       severity: MESSAGE_LEVEL_DANGER
92     }
93   };
94 }
95
96 function getDynamicTopographicQueryURL(entityType) {
97   return INVENTORY_GEO_VISUALIZATION_SEARCH_URL.replace(BACKEND_IP_ADDRESS,
98       BACKEND_IP_ADDRESS) +
99     GEO_VISUALIZATION_QUERY_STRING_PARAMETERS +
100     entityType;
101 }
102
103 function getCountByTypeQueryURL() {
104   return INVENTORY_COUNT_BY_TYPE_SEARCH_URL.replace(BACKEND_IP_ADDRESS,
105     BACKEND_IP_ADDRESS);
106 }
107
108 function getCountByDateQueryURL() {
109   return INVENTORY_COUNT_BY_DATE_SEARCH_URL.replace(BACKEND_IP_ADDRESS,
110     BACKEND_IP_ADDRESS);
111 }
112
113 export function onLoadTotalCountByDate() {
114   return function (dispatch) {
115     fetch(getCountByDateQueryURL(), {
116       credentials: SAME_ORIGIN,
117       method: GET,
118       headers: POST_HEADER
119     }).then(
120       (response) => response.json()
121     ).then(
122       (responseJson) => dispatch(
123         getSuccessfulCountsByDateQueryEvent(responseJson))
124     ).catch(
125       () => dispatch(getFailedCountByDateQueryEvent())
126     );
127   };
128 }
129
130 export function onCountByTypeLoad() {
131   return function (dispatch) {
132     fetch(getCountByTypeQueryURL(), {
133       credentials: SAME_ORIGIN,
134       method: GET,
135       headers: POST_HEADER
136     }).then(
137       (response) => response.json()
138     ).then(
139       (responseJson) => dispatch(
140         getSuccessfulCountByTypeQueryEvent(responseJson))
141     ).catch(
142       () => dispatch(getFailedCountByTypeQueryEvent())
143     );
144   };
145 }
146
147 export function onTopographicMapMounted(requestObject) {
148   return function (dispatch) {
149     fetch(getDynamicTopographicQueryURL(requestObject.entityType), {
150       credentials: SAME_ORIGIN,
151       method: GET,
152       headers: POST_HEADER
153     }).then(
154       (response) => response.json()
155     ).then(
156       (responseJson) => dispatch(
157         getSuccessfulTopographicVisualizationQueryEvent(responseJson))
158     ).catch(
159       () => {
160         dispatch(getFailedTopographicVisualizationQueryEvent());
161       }
162     );
163   };
164 }
165