blob: e8e13d0465b79eccbe8a4cc08b83eca8ae5f198b [file] [log] [blame]
Radek Krejci30ce1592018-03-01 14:44:14 +01001import {Component, OnInit} from '@angular/core';
Jakub Man59c4ea12018-06-13 15:21:32 +02002import { Router } from '@angular/router';
3import { Observable } from 'rxjs/Observable';
Radek Krejci95bd14c2017-09-21 14:24:13 +02004
Radek Krejci30ce1592018-03-01 14:44:14 +01005import {TreeService} from './tree.service';
Radek Krejci9b41f5b2018-01-31 14:17:50 +01006import {ModificationsService} from './modifications.service';
Radek Krejci95bd14c2017-09-21 14:24:13 +02007import {SessionsService} from './sessions.service';
Radek Krejciae758392017-10-20 10:53:26 +02008import {Session} from './session';
Radek Krejcid23f0df2017-08-31 16:34:49 +02009
10@Component({
Radek Krejcib4794962017-09-21 14:16:28 +020011 selector: 'netopeer-config',
12 templateUrl: './config.component.html',
Radek Krejci5a69fa32018-02-01 11:03:04 +010013 styleUrls: ['./config.component.scss'],
Radek Krejci30ce1592018-03-01 14:44:14 +010014 providers: [ModificationsService]
Radek Krejcid23f0df2017-08-31 16:34:49 +020015})
16
Radek Krejci77f77202017-11-03 15:33:50 +010017export class ConfigComponent implements OnInit {
Radek Krejci95bd14c2017-09-21 14:24:13 +020018 title = 'Configuration';
Radek Krejciae758392017-10-20 10:53:26 +020019 activeSession: Session;
Radek Krejci95bd14c2017-09-21 14:24:13 +020020 err_msg = "";
Radek Krejcifc908f32018-02-08 14:53:31 +010021 commit_error = [];
Radek Krejci95bd14c2017-09-21 14:24:13 +020022
Radek Krejci4a4c9ed2018-03-21 13:22:04 +010023 constructor(public sessionsService: SessionsService,
24 public modsService: ModificationsService,
25 public treeService: TreeService,
Radek Krejci9b41f5b2018-01-31 14:17:50 +010026 private router: Router) {}
Radek Krejci95bd14c2017-09-21 14:24:13 +020027
28 addSession() {
29 this.router.navigateByUrl('/netopeer/inventory/devices');
30 }
31
Radek Krejcia1339602017-11-02 13:52:38 +010032 reloadData() {
Radek Krejcid0fac3b2018-03-13 16:55:47 +010033 switch (this.activeSession.dataPresence) {
34 case 'root':
35 this.activeSession.data = null;
Radek Krejci30ce1592018-03-01 14:44:14 +010036 this.sessionsService.rpcGet(this.activeSession, false);
Radek Krejcid0fac3b2018-03-13 16:55:47 +010037 break;
38 case 'all':
39 this.activeSession.data = null;
Radek Krejci30ce1592018-03-01 14:44:14 +010040 this.sessionsService.rpcGet(this.activeSession, true);
Radek Krejcid0fac3b2018-03-13 16:55:47 +010041 break;
42 case 'mixed':
43 this.sessionsService.rpcGetSubtree(this.activeSession.key, false).subscribe(result => {
44 let root = this.activeSession.data;
45 if (result['success']) {
46 for (let newRoot of result['data']) {
47 let matchIndex = -1;
48 for (let i in root['children']) {
49 if (newRoot['path'] == root['children'][i]['path']) {
50 matchIndex = Number(i);
51 break;
52 }
53 }
54 if (matchIndex == -1) {
55 /* add new subtree */
56 root['children'].push(newRoot);
57 } else {
58 let subtree = root['children'][matchIndex];
59 let filterIndex = this.activeSession.treeFilters.indexOf(subtree['path']);
60 if (filterIndex != -1) {
61 /* reloading currently present but not visible subtrees is postponed to the point they are displayed */
62 subtree['subtreeRoot'] = true;
63 delete subtree['children'];
64 this.activeSession.treeFilters.splice(filterIndex, 1);
65 this.activeSession.dataPresence = 'root';
66 for (let root of this.activeSession.data['children']) {
67 if (!('subtreeRoot' in root)) {
68 this.activeSession.dataPresence = 'mixed';
69 break;
70 }
71 }
72 } else if (!('subtreeRoot' in subtree)) {
73 /* reload currently present and visible subtrees */
74 subtree['loading'] = true;
75 this.sessionsService.rpcGetSubtree(this.activeSession.key, true, subtree['path']).subscribe(result => {
76 subtree['loading'] = false;
77 if (result['success']) {
78 for (let iter of result['data']['children']) {
79 this.treeService.setDirty(this.activeSession, iter);
80 }
81 subtree['children'] = result['data']['children'];
82 this.treeService.updateHiddenFlags(this.activeSession);
83 this.sessionsService.storeSessions();
84 }
85 });
86 }
87 }
88 }
89 }
90 this.sessionsService.storeSessions();
91 });
Radek Krejcia1339602017-11-02 13:52:38 +010092 }
Radek Krejciae758392017-10-20 10:53:26 +020093 }
94
Radek Krejci95bd14c2017-09-21 14:24:13 +020095 disconnect(key: string) {
96 this.sessionsService.close(key).subscribe(result => {
97 if (result['success']) {
98 if (!this.sessionsService.activeSession) {
99 this.router.navigateByUrl('/netopeer/inventory/devices');
100 }
Radek Krejci482629d2018-03-13 14:56:00 +0100101 this.activeSession = this.sessionsService.getSession();
Radek Krejci95bd14c2017-09-21 14:24:13 +0200102 } else {
103 this.err_msg = result['error-msg'];
104 }
105 });
106 }
107
Radek Krejci77f77202017-11-03 15:33:50 +0100108 setCpbltsVisibility(value: boolean) {
109 this.activeSession.cpbltsVisibility = value;
Radek Krejci482629d2018-03-13 14:56:00 +0100110 this.sessionsService.storeSessions();
Radek Krejci77f77202017-11-03 15:33:50 +0100111 }
112
Radek Krejci77f77202017-11-03 15:33:50 +0100113 invertStatus() {
114 this.activeSession.statusVisibility = !this.activeSession.statusVisibility;
Radek Krejci482629d2018-03-13 14:56:00 +0100115 this.sessionsService.storeSessions();
Radek Krejci77f77202017-11-03 15:33:50 +0100116 }
117
Radek Krejci95bd14c2017-09-21 14:24:13 +0200118 getCapabilities(key: string) {
Radek Krejciae758392017-10-20 10:53:26 +0200119 if (this.activeSession.cpblts) {
Radek Krejci77f77202017-11-03 15:33:50 +0100120 this.activeSession.cpbltsVisibility = true;
Radek Krejci482629d2018-03-13 14:56:00 +0100121 this.sessionsService.storeSessions();
Radek Krejciae758392017-10-20 10:53:26 +0200122 return;
123 }
Radek Krejci95bd14c2017-09-21 14:24:13 +0200124 this.sessionsService.getCpblts(key).subscribe(result => {
125 if (result['success']) {
Radek Krejcia1339602017-11-02 13:52:38 +0100126 this.activeSession.cpblts = result['capabilities'];
Radek Krejci77f77202017-11-03 15:33:50 +0100127 this.activeSession.cpbltsVisibility = true;
Radek Krejci95bd14c2017-09-21 14:24:13 +0200128 } else {
Radek Krejcia1339602017-11-02 13:52:38 +0100129 this.activeSession.cpbltsVisibility = false;
130 this.err_msg = result['error-msg'];
Radek Krejci95bd14c2017-09-21 14:24:13 +0200131 }
Radek Krejci482629d2018-03-13 14:56:00 +0100132 this.sessionsService.storeSessions();
Radek Krejci95bd14c2017-09-21 14:24:13 +0200133 });
134 }
135
Radek Krejciae758392017-10-20 10:53:26 +0200136 parseCapabilityName(cpblt: string): string {
137 let name = cpblt;
138 let pos = cpblt.search('module=');
139 if (pos != -1) {
140 /* schema */
141 pos += 7;
142 name = cpblt.slice(pos);
143 let end = name.search('&');
144 if (end != -1) {
145 name = name.slice(0, end);
146 }
147 } else {
148 /* capability */
149 pos = 0;
150 if (cpblt.match('urn:ietf:params:netconf:capability:*')) {
151 pos = 34;
152 } else if (cpblt.match('urn:ietf:params:netconf:*')) {
153 pos = 23;
154 }
155 name = cpblt.slice(pos);
156
157 let end = name.search('\\?');
158 if (end != -1) {
159 name = name.slice(0, end);
160 }
161 pos = name.lastIndexOf(':')
162 name = name.slice(0, pos);
163 }
164 return name;
165 }
166
167 parseCapabilityRevision(cpblt: string): string {
168 let version = "";
169 let pos = cpblt.search('revision=');
170 if (pos != -1) {
171 pos += 9;
172 version = cpblt.slice(pos);
173 let end = version.search('&');
174 if (end != -1) {
175 version = version.slice(0, end);
176 }
177 return version;
178 } else if (cpblt.match('urn:ietf:params:netconf:*')) {
179 let end = cpblt.search('\\?');
180 if (end != -1) {
181 cpblt = cpblt.slice(0, end);
182 }
183 pos = cpblt.lastIndexOf(':')
184 version = cpblt.slice(pos + 1);
185 }
186 return version;
187 }
188
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100189 cancelChanges() {
Radek Krejci2ca11ba2018-01-26 11:25:42 +0100190 //console.log(JSON.stringify(this.activeSession.modifications))
Radek Krejci9b41f5b2018-01-31 14:17:50 +0100191 this.modsService.cancelModification(this.activeSession);
Radek Krejcifc908f32018-02-08 14:53:31 +0100192 this.commit_error = [];
Radek Krejci482629d2018-03-13 14:56:00 +0100193 this.sessionsService.storeSessions();
Radek Krejci2ca11ba2018-01-26 11:25:42 +0100194 //console.log(JSON.stringify(this.activeSession.modifications))
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100195 }
196
197 applyChanges() {
Radek Krejci30ce1592018-03-01 14:44:14 +0100198 //console.log(JSON.stringify(this.activeSession.modifications))
Jakub Man59c4ea12018-06-13 15:21:32 +0200199 this.modsService.applyModification(this.activeSession).subscribe(result => {
Radek Krejci62fec6a2018-02-06 10:24:09 +0100200 if (result['success']) {
201 this.reloadData();
Radek Krejcifc908f32018-02-08 14:53:31 +0100202 this.commit_error = [];
Jakub Man59c4ea12018-06-13 15:21:32 +0200203 }
204 else {
Radek Krejcifc908f32018-02-08 14:53:31 +0100205 this.commit_error = result['error'];
Radek Krejci62fec6a2018-02-06 10:24:09 +0100206 }
Jakub Man59c4ea12018-06-13 15:21:32 +0200207 });
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100208 }
Radek Krejci6e772b22018-01-25 13:28:57 +0100209
Radek Krejci95bd14c2017-09-21 14:24:13 +0200210 ngOnInit(): void {
211 this.sessionsService.checkSessions();
Radek Krejci482629d2018-03-13 14:56:00 +0100212 this.activeSession = this.sessionsService.getSession();
Radek Krejci6be087d2018-02-14 08:53:20 +0100213 if (this.activeSession && !this.activeSession.data) {
Radek Krejci30ce1592018-03-01 14:44:14 +0100214 this.sessionsService.rpcGet(this.activeSession, false);
Radek Krejcid0fac3b2018-03-13 16:55:47 +0100215 this.activeSession.dataPresence = 'root';
Radek Krejci5a69fa32018-02-01 11:03:04 +0100216 }
Radek Krejciae758392017-10-20 10:53:26 +0200217 }
Radek Krejcid23f0df2017-08-31 16:34:49 +0200218}