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