Sean Condon | d6f95bf | 2020-01-21 10:10:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Sean Condon | 8649b09 | 2020-01-22 10:39:35 +0000 | [diff] [blame] | 17 | import {Component, OnInit, OnDestroy, Inject} from '@angular/core'; |
Sean Condon | 3dd062f | 2020-04-14 09:25:00 +0100 | [diff] [blame] | 18 | import {WebSocketService, LogService, FnService, SortDir, TableBaseImpl} from 'org_onosproject_onos/web/gui2-fw-lib/public_api'; |
Sean Condon | 8649b09 | 2020-01-22 10:39:35 +0000 | [diff] [blame] | 19 | import { HttpClient } from '@angular/common/http'; |
Sean Condon | d6f95bf | 2020-01-21 10:10:23 +0000 | [diff] [blame] | 20 | |
| 21 | /** |
| 22 | * Model of the response from the WebSocket |
| 23 | */ |
| 24 | export interface YangModel { |
| 25 | id: string; |
| 26 | revision: string; |
| 27 | modelId: string; |
| 28 | } |
| 29 | |
| 30 | export interface YangModelDataResponse { |
| 31 | yangModels: YangModel[]; |
| 32 | } |
| 33 | |
Sean Condon | 8649b09 | 2020-01-22 10:39:35 +0000 | [diff] [blame] | 34 | /** Prefix to access the REST service for applications */ |
| 35 | const YANGURLPREFIX = '../yang/models/'; |
| 36 | const YANGMODELID = '?modelId='; |
| 37 | const DRAGDROPMSG1 = 'Drag and drop one file at a time'; |
| 38 | const DRAGDROPMSGEXT = 'Only files ending in .yang, .zip or .jar can be loaded'; |
| 39 | |
Sean Condon | d6f95bf | 2020-01-21 10:10:23 +0000 | [diff] [blame] | 40 | @Component({ |
| 41 | selector: 'onos-yangtable', |
| 42 | templateUrl: './yangtable.component.html', |
| 43 | styleUrls: [ |
| 44 | './yangtable.component.scss', 'yangtable.theme.scss', |
| 45 | '../../../../../web/gui2-fw-lib/lib/widget/table.css', '../../../../../web/gui2-fw-lib/lib/widget/table.theme.css' |
| 46 | ] |
| 47 | }) |
| 48 | export class YangTableComponent extends TableBaseImpl implements OnInit, OnDestroy { |
| 49 | selectedModel: YangModel = undefined; |
Sean Condon | 8649b09 | 2020-01-22 10:39:35 +0000 | [diff] [blame] | 50 | alertMsg: string; |
Sean Condon | d6f95bf | 2020-01-21 10:10:23 +0000 | [diff] [blame] | 51 | |
| 52 | constructor( |
| 53 | protected log: LogService, |
| 54 | protected fs: FnService, |
| 55 | protected wss: WebSocketService, |
Sean Condon | 8649b09 | 2020-01-22 10:39:35 +0000 | [diff] [blame] | 56 | protected httpClient: HttpClient, |
Sean Condon | d6f95bf | 2020-01-21 10:10:23 +0000 | [diff] [blame] | 57 | ) { |
| 58 | super(fs, log, wss, 'yangModel'); |
| 59 | this.log.debug('YangTableComponent constructed'); |
| 60 | this.parentSelCb = this.rowSelectionParent; |
| 61 | |
| 62 | this.sortParams = { |
| 63 | firstCol: 'id', |
| 64 | firstDir: SortDir.desc, |
| 65 | secondCol: 'revision', |
| 66 | secondDir: SortDir.asc, |
| 67 | }; |
| 68 | } |
| 69 | |
| 70 | ngOnInit() { |
| 71 | this.init(); |
| 72 | this.log.debug('YangTableComponent initialized'); |
| 73 | } |
| 74 | |
| 75 | ngOnDestroy() { |
| 76 | this.destroy(); |
| 77 | this.log.debug('YangTableComponent destroyed'); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * When the upload button is clicked pass this on to the file input (hidden) |
| 82 | */ |
| 83 | triggerForm() { |
Sean Condon | 8649b09 | 2020-01-22 10:39:35 +0000 | [diff] [blame] | 84 | document.getElementById('uploadYangFile') |
Sean Condon | d6f95bf | 2020-01-21 10:10:23 +0000 | [diff] [blame] | 85 | .dispatchEvent(new MouseEvent('click')); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * called when a row is selected - sets the state of control icons |
| 90 | */ |
| 91 | rowSelectionParent(event: any, selRow: any) { |
| 92 | this.selectedModel = selRow; |
| 93 | } |
Sean Condon | 8649b09 | 2020-01-22 10:39:35 +0000 | [diff] [blame] | 94 | |
| 95 | onDrop(event: DragEvent) { |
| 96 | event.preventDefault(); |
| 97 | event.stopPropagation(); |
| 98 | |
| 99 | const dt = event.dataTransfer; |
| 100 | const droppedFiles = dt.files; |
| 101 | |
| 102 | this.log.debug(droppedFiles.length, 'File(s) dropped'); |
| 103 | if (droppedFiles.length !== 1) { |
| 104 | this.log.error(DRAGDROPMSG1, droppedFiles.length, 'were dropped'); |
| 105 | this.alertMsg = DRAGDROPMSG1; |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | const fileEvent = { |
| 110 | target: { |
| 111 | files: droppedFiles |
| 112 | } |
| 113 | }; |
| 114 | this.fileEvent(fileEvent); |
| 115 | } |
| 116 | |
| 117 | onDragOver(evt) { |
| 118 | evt.preventDefault(); |
| 119 | evt.stopPropagation(); |
| 120 | } |
| 121 | |
| 122 | onDragLeave(evt) { |
| 123 | evt.preventDefault(); |
| 124 | evt.stopPropagation(); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * When the file is selected this fires |
| 129 | * It passes the file on to the server through a POST request |
| 130 | * If there is an error its logged and raised to the user through Flash Component |
| 131 | */ |
| 132 | fileEvent(event: any): void { |
| 133 | const file: File = event.target.files[0]; |
| 134 | const suffix: string = file.name.slice(file.name.lastIndexOf('.')); |
| 135 | this.log.debug('File event for', file.name, file.type, file.size, suffix); |
| 136 | if (suffix !== '.yang' && suffix !== '.jar' && suffix !== '.zip') { |
| 137 | this.log.error(DRAGDROPMSGEXT, file.name, 'rejected'); |
| 138 | this.alertMsg = DRAGDROPMSGEXT; |
| 139 | return; |
| 140 | } |
| 141 | |
| 142 | const formData = new FormData(); |
| 143 | formData.append('file', file); |
| 144 | |
| 145 | this.httpClient |
| 146 | .post<any>(YANGURLPREFIX + YANGMODELID + file.name, formData) |
| 147 | .subscribe( |
| 148 | data => this.log.debug(data), |
| 149 | err => { |
| 150 | this.log.warn(err.error.message, err.status); |
| 151 | this.alertMsg = err.error.message; // This will activate flash msg |
| 152 | } |
| 153 | ); |
| 154 | } |
Sean Condon | d6f95bf | 2020-01-21 10:10:23 +0000 | [diff] [blame] | 155 | } |