d6f5e5aab74ab80b04df463c728417cb20a6993f
[aai/sparky-fe.git] / src / app / tierSupport / TierSupportActions.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
27 import {tierSupportActionTypes,
28   TS_BACKEND_SEARCH_SELECTED_NODE_URL} from 'app/tierSupport/TierSupportConstants.js';
29 import {
30   POST,
31   POST_HEADER,
32   ERROR_RETRIEVING_DATA,
33   NO_RESULTS_FOUND
34 } from 'app/networking/NetworkConstants.js';
35 import networkCall from 'app/networking/NetworkCalls.js';
36 import {
37   getSetGlobalMessageEvent,
38   getClearGlobalMessageEvent
39 } from 'app/GlobalInlineMessageBar/GlobalInlineMessageBarActions.js';
40 import {
41   STATUS_CODE_204_NO_CONTENT,
42   STATUS_CODE_3XX_REDIRECTION,
43   STATUS_CODE_5XX_SERVER_ERROR
44 } from 'utils/GlobalConstants.js';
45
46 function createOnNodeDetailsChangeEvent(newDetails) {
47   return {
48     type: tierSupportActionTypes.TS_GRAPH_NODE_SELECTED,
49     data: newDetails
50   };
51 }
52
53 function createSplitPaneResizeEvent(initialLoad) {
54   return {
55     type: tierSupportActionTypes.SPLIT_PANE_RESIZE,
56     data: initialLoad
57   };
58 }
59
60 function createOnNodeMenuSelectEvent(selectedMenu) {
61   return {
62     type: tierSupportActionTypes.TS_GRAPH_NODE_MENU_SELECTED,
63     data: selectedMenu
64   };
65 }
66
67 export function onNodeDetailsChange(newDetails) {
68   return dispatch => {
69     dispatch(createOnNodeDetailsChangeEvent(newDetails));
70   };
71 }
72
73 export function splitPaneResize(initialLoad) {
74   return dispatch => {
75     dispatch(createSplitPaneResizeEvent(initialLoad));
76   };
77 }
78
79 export function onNodeMenuChange(selectedMenu) {
80   return dispatch => {
81     dispatch(createOnNodeMenuSelectEvent(selectedMenu));
82   };
83 }
84
85 function createNodeDetailsFoundEvent(nodeDetails) {
86   return {
87     type: tierSupportActionTypes.TS_NODE_SEARCH_RESULTS,
88     data: nodeDetails
89   };
90 }
91
92 function noNodeDetailsFoundEvent(errorText) {
93   return {
94     type: tierSupportActionTypes.TS_NODE_SEARCH_NO_RESULTS,
95     data: {errorMsg: errorText}
96   };
97 }
98
99 function getInvalidSelectedNodeSearchEvent(errorText) {
100   return {
101     type: tierSupportActionTypes.TIER_SUPPORT_NETWORK_ERROR,
102     data: {value: errorText, errorMsg: ERROR_RETRIEVING_DATA}
103   };
104 }
105
106 export function clearVIData() {
107   return {
108     type: tierSupportActionTypes.TIER_SUPPORT_CLEAR_DATA
109   };
110 }
111
112 export function fetchSelectedNodeElement(fetchRequestCallback) {
113   return dispatch => {
114     return fetchRequestCallback().then(
115       (response) => {
116         if (response.status === STATUS_CODE_204_NO_CONTENT || response.status >= STATUS_CODE_3XX_REDIRECTION) {
117           return Promise.reject(new Error(response.status));
118         } else {
119           // assume 200 status
120           return response.json();
121         }
122       }
123     ).then(
124       (responseJson) => {
125         if (responseJson.nodes.length > 0) {
126           dispatch(createNodeDetailsFoundEvent(responseJson));
127         } else {
128           dispatch(noNodeDetailsFoundEvent(NO_RESULTS_FOUND));
129         }
130       }
131     ).catch(
132       (errorCode) => {
133         if (errorCode.message >= STATUS_CODE_5XX_SERVER_ERROR) {
134           dispatch(getInvalidSelectedNodeSearchEvent(ERROR_RETRIEVING_DATA));
135         } else {
136           // TODO - assuming 204 status, but should include additional
137           // statuses in the future with proper messaging in order to return
138           // better messaging
139           dispatch(noNodeDetailsFoundEvent(NO_RESULTS_FOUND));
140         }
141       }
142     );
143   };
144 }
145
146 export function querySelectedNodeElement(
147   searchHashId, selectedNodeFetchRequest) {
148   let payload = {
149     hashId: searchHashId
150   };
151
152   if (selectedNodeFetchRequest === undefined) {
153     let postBody = JSON.stringify(payload);
154     selectedNodeFetchRequest =
155       () => networkCall.fetchRequestObj(TS_BACKEND_SEARCH_SELECTED_NODE_URL, POST,
156         POST_HEADER, postBody);
157   }
158
159   return dispatch => {
160     dispatch(fetchSelectedNodeElement(selectedNodeFetchRequest));
161   };
162 }
163
164 export function setNotificationText(msgText, msgSeverity) {
165   if (msgText.length > 0) {
166     return dispatch => {
167       dispatch(
168         getSetGlobalMessageEvent(msgText, msgSeverity));
169     };
170   } else {
171     return dispatch => {
172       dispatch(getClearGlobalMessageEvent());
173     };
174   }
175 }