blob: 76beca7118472d983fad8f603ce0f732e600327a [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';
31import TemplateMenuService from '../../../api/TemplateMenuService';
32
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
40 this.handleUploadToscaPolicyModel = this.handleUploadToscaPolicyModel.bind(this);
41 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;
58 let reader = new FileReader();
59 this.setState({policyModelType: '', policyModelTosca: '' });
60 reader.onload = function(e) {
61 var lines = reader.result.split('\n');
62 for (var line = 0; line < lines.length; line++) {
63 if(lines[line].trim().slice(0, 24) === 'onap.policies.monitoring') {
64 var microsvc = lines[line].trim().slice(0, -1);
65 scope.setState({ policyModelType: microsvc, policyModelTosca: reader.result});
66 }
67 }
68 };
69 console.log("Filename is", event.target.files[0]);
70 reader.readAsText(event.target.files[0]);
71 }
72 this.setState({selectedFile: event.target.files[0]});
73 };
74
75 handleClose() {
76 this.setState({ show: false });
77 this.props.history.push('/');
78 }
79
80 handleUploadToscaPolicyModel(e) {
81 e.preventDefault();
82 console.log("Policy Model Type is", this.state.policyModelType);
83 if(this.state.policyModelType && this.state.policyModelTosca) {
84 TemplateMenuService.uploadToscaPolicyModal(this.state.policyModelType, this.state.policyModelTosca).then(resp => {
85 if(resp.status === 200) {
86 this.setState({apiResponseStatus: resp.status, apiResponseMessage: resp.message, upldBtnClicked: true});
87 } else {
88 this.setState({apiResponseStatus: 500, apiResponseMessage: resp, upldBtnClicked: true});
89 }
90 });
91 } else {
92 this.setState({apiResponse: 500, apiResponseMessage: 'Parameters are missing', upldBtnClicked: true});
93 }
94}
95
96 handlePolicyModelType = event => {
97 this.setState({
98 policyModelType: event.target.value
99 })
100 }
101
102 render() {
103 return (
104 <ModalStyled size="lg" show={this.state.show} onHide={this.handleClose}>
105 <Modal.Header closeButton>
106 <Modal.Title>Upload Tosca Modal</Modal.Title>
107 </Modal.Header>
108 <Modal.Body>
109 <Form.Group as={Row} controlId="formPlaintextEmail">
110 <Col sm="10">
111 <input style={{display: 'none'}} type="file" name="file" accept=".yaml" onChange={this.fileSelectedHandler}
112 ref={fileInput => this.fileInput = fileInput}/>
113 <button onClick={() => this.fileInput.click()}>Pick Tosca File</button>
114 <Alert variant="secondary">
115 <p>{this.state.selectedFile.name}</p>
116 </Alert>
117 <Form.Label column sm="2">Micro Service Name:</Form.Label>
118 <input type="text" style={{width: '50%'}}
119 value={this.state.policyModelType}
120 onChange={this.handlePolicyModelType}
121 />
122 </Col>
123 </Form.Group>
124 </Modal.Body>
125 <Modal.Footer>
126 {!this.state.apiResponseStatus?<Button variant="secondary" type="null" onClick={this.handleClose}>Cancel</Button>:""}
127 {!this.state.apiResponseStatus?<Button disabled={!this.state.selectedFile.name || this.state.upldBtnClicked} variant="primary" type="submit" onClick={this.handleUploadToscaPolicyModel.bind(this)}>Upload</Button>:""}
128 {this.state.apiResponseStatus?<Alert variant={this.state.apiResponseStatus === 200?"success":"danger"}>
129 <p>{this.state.apiResponseMessage}</p>
130 <Button onClick={this.handleClose} variant={this.state.apiResponseStatus === 200?"outline-success":"danger"}>
131 Exit
132 </Button>
133 </Alert>:""}
134 </Modal.Footer>
135 </ModalStyled>
136 );
137 }
138}