Add new code new version
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / softwareProduct / components / monitoring / SoftwareProductComponentsMonitoringActionHelper.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 import i18n from 'nfvo-utils/i18n/i18n.js';
22 import RestAPIUtil from 'nfvo-utils/RestAPIUtil.js';
23 import NotificationConstants from 'nfvo-components/notifications/NotificationConstants.js';
24 import Configuration from 'sdc-app/config/Configuration.js';
25 import SoftwareProductComponentsMonitoringConstants, {actionTypes} from './SoftwareProductComponentsMonitoringConstants.js';
26
27 const UPLOAD = true;
28
29 function baseUrl(vspId, componentId) {
30         const restPrefix = Configuration.get('restPrefix');
31         return `${restPrefix}/v1.0/vendor-software-products/${vspId}/components/${componentId}/monitors`;
32 }
33
34 function snmpTrapUrl(vspId, componentId, isUpload) {
35         return `${baseUrl(vspId, componentId)}/snmp-trap${isUpload ? '/upload' : ''}`;
36 }
37
38 function snmpPollUrl(vspId, componentId, isUpload) {
39         return `${baseUrl(vspId, componentId)}/snmp${isUpload ? '/upload' : ''}`;
40 }
41
42 let onInvalidFileSizeUpload = (dispatch) => dispatch({
43         type: NotificationConstants.NOTIFY_ERROR,
44         data: {
45                 title: i18n('Upload Failed'),
46                 msg: i18n('no zip file was uploaded or zip file doesn\'t exist')
47         }
48 });
49
50 let uploadSnmpTrapFile = (dispatch, {softwareProductId, componentId, formData}) => {
51         RestAPIUtil.create(snmpTrapUrl(softwareProductId, componentId, UPLOAD), formData).then(()=> dispatch({
52                 type: actionTypes.SNMP_TRAP_UPLOADED, data: {filename: formData.get('upload').name}
53         }));
54 };
55
56 let uploadSnmpPollFile = (dispatch, {softwareProductId, componentId, formData}) => {
57         RestAPIUtil.create(snmpPollUrl(softwareProductId, componentId, UPLOAD), formData).then(()=> dispatch({
58                 type: actionTypes.SNMP_POLL_UPLOADED, data: {filename: formData.get('upload').name}
59         }));
60 };
61
62 let deleteSnmpTrapFile = (dispatch, {softwareProductId, componentId}) => {
63         RestAPIUtil.destroy(snmpTrapUrl(softwareProductId, componentId, !UPLOAD)).then(()=> dispatch({
64                 type: actionTypes.SNMP_TRAP_DELETED
65         }));
66 };
67
68 let deleteSnmpPollFile = (dispatch, {softwareProductId, componentId}) => {
69         RestAPIUtil.destroy(snmpPollUrl(softwareProductId, componentId, !UPLOAD)).then(()=> dispatch({
70                 type: actionTypes.SNMP_POLL_DELETED
71         }));
72 };
73
74 const SoftwareProductComponentsMonitoringAction = {
75
76         fetchExistingFiles(dispatch, {softwareProductId, componentId}){
77                 RestAPIUtil.fetch(`${baseUrl(softwareProductId, componentId)}/snmp`).then(response =>
78                         dispatch({
79                                 type: actionTypes.SNMP_FILES_DATA_CHANGE,
80                                 data: {trapFilename: response.snmpTrap, pollFilename: response.snmpPoll}
81                         })
82                 );
83         },
84
85         uploadSnmpFile(dispatch, {softwareProductId, componentId, formData, type}){
86                 if (formData.get('upload').size) {
87                         if (type === SoftwareProductComponentsMonitoringConstants.SNMP_TRAP) {
88                                 uploadSnmpTrapFile(dispatch, {softwareProductId, componentId, formData});
89                         }
90                         else {
91                                 uploadSnmpPollFile(dispatch, {softwareProductId, componentId, formData});
92                         }
93                 }
94                 else {
95                         onInvalidFileSizeUpload(dispatch);
96                 }
97         },
98
99         deleteSnmpFile(dispatch, {softwareProductId, componentId, type}){
100                 if (type === SoftwareProductComponentsMonitoringConstants.SNMP_TRAP) {
101                         deleteSnmpTrapFile(dispatch, {softwareProductId, componentId});
102                 }
103                 else {
104                         deleteSnmpPollFile(dispatch, {softwareProductId, componentId});
105                 }
106         }
107
108 };
109
110 export default SoftwareProductComponentsMonitoringAction;