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