blob: 2e0460f61b68a708f0f928ba55442cb8fde1120c [file] [log] [blame]
ash742683a83e2a2020-01-31 15:40:15 +00001/*-
2 * ============LICENSE_START=======================================================
3 * ONAP CLAMP
4 * ================================================================================
5 * Copyright (C) 2020 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 */
22
23import React from 'react'
24import Button from 'react-bootstrap/Button';
25import Modal from 'react-bootstrap/Modal';
26import Form from 'react-bootstrap/Form';
27import Row from 'react-bootstrap/Row';
28import Col from 'react-bootstrap/Col';
29import styled from 'styled-components';
30import Alert from 'react-bootstrap/Alert';
sebdetaa486be2020-02-18 02:00:11 -080031import PolicyToscaService from '../../../api/PolicyToscaService';
ash742683a83e2a2020-01-31 15:40:15 +000032
33const ModalStyled = styled(Modal)`
34 background-color: transparent;
35`
36export default class UploadToscaPolicyModal extends React.Component {
37 constructor(props, context) {
38 super(props, context);
39
sebdetaa486be2020-02-18 02:00:11 -080040 this.handleCreateFromToscaPolicyModel = this.handleCreateFromToscaPolicyModel.bind(this);
ash742683a83e2a2020-01-31 15:40:15 +000041 this.handleClose = this.handleClose.bind(this);
42 this.handlePolicyModelType = this.handlePolicyModelType.bind(this);
43 this.fileSelectedHandler = this.fileSelectedHandler.bind(this);
44 this.state = {
45 show: true,
46 selectedFile: '',
47 policyModelType: '',
48 policyModelTosca: [],
49 apiResponseStatus: '',
50 apiResponseMessage: '',
51 upldBtnClicked: false
52 };
53 }
54
55 fileSelectedHandler = (event) => {
56 if (event.target.files && event.target.files[0]) {
57 const scope = this;
sebdetaa486be2020-02-18 02:00:11 -080058 let reader = new FileReader();
ash742683a83e2a2020-01-31 15:40:15 +000059 this.setState({policyModelType: '', policyModelTosca: '' });
60 reader.onload = function(e) {
sebdetaa486be2020-02-18 02:00:11 -080061 scope.setState({ policyModelTosca: reader.result});
ash742683a83e2a2020-01-31 15:40:15 +000062 };
63 console.log("Filename is", event.target.files[0]);
64 reader.readAsText(event.target.files[0]);
65 }
66 this.setState({selectedFile: event.target.files[0]});
67 };
68
69 handleClose() {
70 this.setState({ show: false });
71 this.props.history.push('/');
72 }
73
sebdetaa486be2020-02-18 02:00:11 -080074 handleCreateFromToscaPolicyModel(e) {
75 e.preventDefault();
ash742683a83e2a2020-01-31 15:40:15 +000076 console.log("Policy Model Type is", this.state.policyModelType);
77 if(this.state.policyModelType && this.state.policyModelTosca) {
sebdetaa486be2020-02-18 02:00:11 -080078 PolicyToscaService.createPolicyModelFromToscaModel(this.state.policyModelType, this.state.policyModelTosca).then(resp => {
ash742683a83e2a2020-01-31 15:40:15 +000079 if(resp.status === 200) {
80 this.setState({apiResponseStatus: resp.status, apiResponseMessage: resp.message, upldBtnClicked: true});
81 } else {
82 this.setState({apiResponseStatus: 500, apiResponseMessage: resp, upldBtnClicked: true});
83 }
84 });
85 } else {
86 this.setState({apiResponse: 500, apiResponseMessage: 'Parameters are missing', upldBtnClicked: true});
87 }
88}
89
90 handlePolicyModelType = event => {
91 this.setState({
92 policyModelType: event.target.value
93 })
94 }
95
96 render() {
97 return (
98 <ModalStyled size="lg" show={this.state.show} onHide={this.handleClose}>
99 <Modal.Header closeButton>
sebdetaa486be2020-02-18 02:00:11 -0800100 <Modal.Title>Upload Tosca Model</Modal.Title>
ash742683a83e2a2020-01-31 15:40:15 +0000101 </Modal.Header>
102 <Modal.Body>
103 <Form.Group as={Row} controlId="formPlaintextEmail">
104 <Col sm="10">
105 <input style={{display: 'none'}} type="file" name="file" accept=".yaml" onChange={this.fileSelectedHandler}
106 ref={fileInput => this.fileInput = fileInput}/>
107 <button onClick={() => this.fileInput.click()}>Pick Tosca File</button>
108 <Alert variant="secondary">
109 <p>{this.state.selectedFile.name}</p>
110 </Alert>
sebdetaa486be2020-02-18 02:00:11 -0800111 <Form.Label column sm="2">Policy Model Type:</Form.Label>
ash742683a83e2a2020-01-31 15:40:15 +0000112 <input type="text" style={{width: '50%'}}
113 value={this.state.policyModelType}
114 onChange={this.handlePolicyModelType}
115 />
116 </Col>
117 </Form.Group>
118 </Modal.Body>
119 <Modal.Footer>
120 {!this.state.apiResponseStatus?<Button variant="secondary" type="null" onClick={this.handleClose}>Cancel</Button>:""}
sebdetaa486be2020-02-18 02:00:11 -0800121 {!this.state.apiResponseStatus?<Button disabled={!this.state.selectedFile.name || this.state.upldBtnClicked} variant="primary" type="submit" onClick={this.handleCreateFromToscaPolicyModel.bind(this)}>Create</Button>:""}
ash742683a83e2a2020-01-31 15:40:15 +0000122 {this.state.apiResponseStatus?<Alert variant={this.state.apiResponseStatus === 200?"success":"danger"}>
123 <p>{this.state.apiResponseMessage}</p>
124 <Button onClick={this.handleClose} variant={this.state.apiResponseStatus === 200?"outline-success":"danger"}>
125 Exit
126 </Button>
127 </Alert>:""}
128 </Modal.Footer>
129 </ModalStyled>
130 );
131 }
132}