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