blob: 0b39ca2f2afc12e3c1716a2459852acbd1798dd5 [file] [log] [blame]
Sean Condond6f95bf2020-01-21 10:10:23 +00001/*
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 Condon8649b092020-01-22 10:39:35 +000017import {Component, OnInit, OnDestroy, Inject} from '@angular/core';
Sean Condon3dd062f2020-04-14 09:25:00 +010018import {WebSocketService, LogService, FnService, SortDir, TableBaseImpl} from 'org_onosproject_onos/web/gui2-fw-lib/public_api';
Sean Condon8649b092020-01-22 10:39:35 +000019import { HttpClient } from '@angular/common/http';
Sean Condond6f95bf2020-01-21 10:10:23 +000020
21/**
22 * Model of the response from the WebSocket
23 */
24export interface YangModel {
25 id: string;
26 revision: string;
27 modelId: string;
28}
29
30export interface YangModelDataResponse {
31 yangModels: YangModel[];
32}
33
Sean Condon8649b092020-01-22 10:39:35 +000034/** Prefix to access the REST service for applications */
35const YANGURLPREFIX = '../yang/models/';
36const YANGMODELID = '?modelId=';
37const DRAGDROPMSG1 = 'Drag and drop one file at a time';
38const DRAGDROPMSGEXT = 'Only files ending in .yang, .zip or .jar can be loaded';
39
Sean Condond6f95bf2020-01-21 10:10:23 +000040@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})
48export class YangTableComponent extends TableBaseImpl implements OnInit, OnDestroy {
49 selectedModel: YangModel = undefined;
Sean Condon8649b092020-01-22 10:39:35 +000050 alertMsg: string;
Sean Condond6f95bf2020-01-21 10:10:23 +000051
52 constructor(
53 protected log: LogService,
54 protected fs: FnService,
55 protected wss: WebSocketService,
Sean Condon8649b092020-01-22 10:39:35 +000056 protected httpClient: HttpClient,
Sean Condond6f95bf2020-01-21 10:10:23 +000057 ) {
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 Condon8649b092020-01-22 10:39:35 +000084 document.getElementById('uploadYangFile')
Sean Condond6f95bf2020-01-21 10:10:23 +000085 .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 Condon8649b092020-01-22 10:39:35 +000094
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 Condond6f95bf2020-01-21 10:10:23 +0000155}