blob: 89205b5396578c88b64a23d6bc68ba1c7d7a33f8 [file] [log] [blame]
Radek Krejci5a69fa32018-02-01 11:03:04 +01001import {Component, Injectable, OnInit} from '@angular/core';
Radek Krejci95bd14c2017-09-21 14:24:13 +02002import {Router} from '@angular/router';
3
Radek Krejci9b41f5b2018-01-31 14:17:50 +01004import {ModificationsService} from './modifications.service';
Radek Krejci95bd14c2017-09-21 14:24:13 +02005import {SessionsService} from './sessions.service';
Radek Krejciae758392017-10-20 10:53:26 +02006import {Session} from './session';
Radek Krejcid23f0df2017-08-31 16:34:49 +02007
Radek Krejci5a69fa32018-02-01 11:03:04 +01008@Injectable()
9export class TreeService {
10 loading = false;
11
12 constructor(private sessionsService: SessionsService, private modsService: ModificationsService) {}
13
14 rpcGet(activeSession, all: boolean) {
15 if (activeSession.data) {
16 if ((all && activeSession.dataVisibility == 'all') ||
17 (!all && activeSession.dataVisibility == 'root')) {
18 return;
19 }
20 }
21 this.loading = true;
22 delete activeSession.data;
23 this.sessionsService.rpcGetSubtree(activeSession.key, all).subscribe(result => {
24 if (result['success']) {
25 for (let iter of result['data']) {
26 this.modsService.setDirty(activeSession, iter);
27 }
28 activeSession.data = {};
29 activeSession.data['path'] = '/';
30 activeSession.data['info'] = {};
31 activeSession.data['info']['path'] = '/';
32 activeSession.data['children'] = result['data'];
33 if (all) {
34 activeSession.dataVisibility = 'all';
35 } else {
36 activeSession.dataVisibility = 'root';
37 }
38 console.log(activeSession.data);
39 }
40 this.sessionsService.storeData();
41 this.loading = false;
42 });
43 }
44
45 expandable(node): boolean {
46 if (node['info']['type'] == 1 || /* container */
47 node['info']['type'] == 16) { /* list */
48 return true;
49 }
50 return false;
51 }
52
53 hasHiddenChild(node, clean=false): boolean {
54 if (!clean && 'hasHiddenChild' in node) {
55 return node['hasHiddenChild'];
56 }
57 node['hasHiddenChild'] = false;
58 if (!this.expandable(node)) {
59 /* terminal node (leaf or leaf-list) */
60 return node['hasHiddenChild'];
61 } else if (!('children' in node)) {
62 /* internal node without children */
63 node['hasHiddenChild'] = true;
64 } else {
65 /* go recursively */
66 for (let child of node['children']) {
67 if (this.hasHiddenChild(child, clean)) {
68 node['hasHiddenChild'] = true;
69 break;
70 }
71 }
72 }
73 return node['hasHiddenChild'];
74 }
75
76 updateHiddenFlags(activeSession) {
77 let mixed = false;
78 let rootsonly = true;
79 for (let root of activeSession.data['children']) {
80 if (this.hasHiddenChild(root, true)) {
81 mixed = true;
82 } else {
83 rootsonly = false;
84 }
85 }
86 if (mixed) {
87 if (rootsonly) {
88 activeSession.dataVisibility = 'root';
89 } else {
90 activeSession.dataVisibility = 'mixed';
91 }
92 }
93 }
94
95 collapse(activeSession, node = null) {
96 if (node) {
97 delete node['children'];
98 activeSession.dataVisibility = 'mixed';
99 } else {
100 for (let root of activeSession.data['children']) {
101 delete root['children'];
102 }
103 activeSession.dataVisibility = 'root';
104 }
105 this.updateHiddenFlags(activeSession);
106 this.sessionsService.storeData();
107 }
108
109 expand(activeSession, node, all: boolean) {
110 node['loading'] = true;
111 this.sessionsService.rpcGetSubtree(activeSession.key, all, node['path']).subscribe(result => {
112 if (result['success']) {
113 for (let iter of result['data']['children']) {
114 this.modsService.setDirty(activeSession, iter);
115 }
116 node['children'] = result['data']['children'];
117 this.updateHiddenFlags(activeSession);
118 delete node['loading'];
119 this.sessionsService.storeData();
120 }
121 });
122 }
123}
124
Radek Krejcid23f0df2017-08-31 16:34:49 +0200125@Component({
Radek Krejcib4794962017-09-21 14:16:28 +0200126 selector: 'netopeer-config',
127 templateUrl: './config.component.html',
Radek Krejci5a69fa32018-02-01 11:03:04 +0100128 styleUrls: ['./config.component.scss'],
129 providers: [ModificationsService, TreeService]
Radek Krejcid23f0df2017-08-31 16:34:49 +0200130})
131
Radek Krejci77f77202017-11-03 15:33:50 +0100132export class ConfigComponent implements OnInit {
Radek Krejci95bd14c2017-09-21 14:24:13 +0200133 title = 'Configuration';
Radek Krejciae758392017-10-20 10:53:26 +0200134 activeSession: Session;
Radek Krejci95bd14c2017-09-21 14:24:13 +0200135 err_msg = "";
136
Radek Krejci9b41f5b2018-01-31 14:17:50 +0100137 constructor(private sessionsService: SessionsService,
138 private modsService: ModificationsService,
Radek Krejci5a69fa32018-02-01 11:03:04 +0100139 private treeService: TreeService,
Radek Krejci9b41f5b2018-01-31 14:17:50 +0100140 private router: Router) {}
Radek Krejci95bd14c2017-09-21 14:24:13 +0200141
142 addSession() {
143 this.router.navigateByUrl('/netopeer/inventory/devices');
144 }
145
Radek Krejcia1339602017-11-02 13:52:38 +0100146 reloadData() {
Radek Krejciae758392017-10-20 10:53:26 +0200147 this.activeSession.data = null;
Radek Krejci5a69fa32018-02-01 11:03:04 +0100148 if (this.activeSession.dataVisibility == 'root') {
149 this.treeService.rpcGet(this.activeSession, false);
150 } else {
151 this.treeService.rpcGet(this.activeSession, true);
Radek Krejcia1339602017-11-02 13:52:38 +0100152 }
Radek Krejciae758392017-10-20 10:53:26 +0200153 }
154
Radek Krejci95bd14c2017-09-21 14:24:13 +0200155 disconnect(key: string) {
156 this.sessionsService.close(key).subscribe(result => {
157 if (result['success']) {
158 if (!this.sessionsService.activeSession) {
159 this.router.navigateByUrl('/netopeer/inventory/devices');
160 }
Radek Krejcifb0c0462018-01-26 10:01:12 +0100161 this.activeSession = this.sessionsService.getActiveSession();
Radek Krejci95bd14c2017-09-21 14:24:13 +0200162 } else {
163 this.err_msg = result['error-msg'];
164 }
165 });
166 }
167
Radek Krejci77f77202017-11-03 15:33:50 +0100168 setCpbltsVisibility(value: boolean) {
169 this.activeSession.cpbltsVisibility = value;
170 this.sessionsService.storeData();
171 }
172
Radek Krejci77f77202017-11-03 15:33:50 +0100173 invertStatus() {
174 this.activeSession.statusVisibility = !this.activeSession.statusVisibility;
175 this.sessionsService.storeData();
176 }
177
Radek Krejci95bd14c2017-09-21 14:24:13 +0200178 getCapabilities(key: string) {
Radek Krejciae758392017-10-20 10:53:26 +0200179 if (this.activeSession.cpblts) {
Radek Krejci77f77202017-11-03 15:33:50 +0100180 this.activeSession.cpbltsVisibility = true;
181 this.sessionsService.storeData();
Radek Krejciae758392017-10-20 10:53:26 +0200182 return;
183 }
Radek Krejci95bd14c2017-09-21 14:24:13 +0200184 this.sessionsService.getCpblts(key).subscribe(result => {
185 if (result['success']) {
Radek Krejcia1339602017-11-02 13:52:38 +0100186 this.activeSession.cpblts = result['capabilities'];
Radek Krejci77f77202017-11-03 15:33:50 +0100187 this.activeSession.cpbltsVisibility = true;
Radek Krejci95bd14c2017-09-21 14:24:13 +0200188 } else {
Radek Krejcia1339602017-11-02 13:52:38 +0100189 this.activeSession.cpbltsVisibility = false;
190 this.err_msg = result['error-msg'];
Radek Krejci95bd14c2017-09-21 14:24:13 +0200191 }
Radek Krejci77f77202017-11-03 15:33:50 +0100192 this.sessionsService.storeData();
Radek Krejci95bd14c2017-09-21 14:24:13 +0200193 });
194 }
195
Radek Krejciae758392017-10-20 10:53:26 +0200196 parseCapabilityName(cpblt: string): string {
197 let name = cpblt;
198 let pos = cpblt.search('module=');
199 if (pos != -1) {
200 /* schema */
201 pos += 7;
202 name = cpblt.slice(pos);
203 let end = name.search('&');
204 if (end != -1) {
205 name = name.slice(0, end);
206 }
207 } else {
208 /* capability */
209 pos = 0;
210 if (cpblt.match('urn:ietf:params:netconf:capability:*')) {
211 pos = 34;
212 } else if (cpblt.match('urn:ietf:params:netconf:*')) {
213 pos = 23;
214 }
215 name = cpblt.slice(pos);
216
217 let end = name.search('\\?');
218 if (end != -1) {
219 name = name.slice(0, end);
220 }
221 pos = name.lastIndexOf(':')
222 name = name.slice(0, pos);
223 }
224 return name;
225 }
226
227 parseCapabilityRevision(cpblt: string): string {
228 let version = "";
229 let pos = cpblt.search('revision=');
230 if (pos != -1) {
231 pos += 9;
232 version = cpblt.slice(pos);
233 let end = version.search('&');
234 if (end != -1) {
235 version = version.slice(0, end);
236 }
237 return version;
238 } else if (cpblt.match('urn:ietf:params:netconf:*')) {
239 let end = cpblt.search('\\?');
240 if (end != -1) {
241 cpblt = cpblt.slice(0, end);
242 }
243 pos = cpblt.lastIndexOf(':')
244 version = cpblt.slice(pos + 1);
245 }
246 return version;
247 }
248
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100249 cancelChanges() {
Radek Krejci2ca11ba2018-01-26 11:25:42 +0100250 //console.log(JSON.stringify(this.activeSession.modifications))
Radek Krejci9b41f5b2018-01-31 14:17:50 +0100251 this.modsService.cancelModification(this.activeSession);
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100252 this.sessionsService.storeData();
Radek Krejci2ca11ba2018-01-26 11:25:42 +0100253 //console.log(JSON.stringify(this.activeSession.modifications))
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100254 }
255
256 applyChanges() {
257 /* TODO */
258 this.cancelChanges();
259 }
Radek Krejci6e772b22018-01-25 13:28:57 +0100260
Radek Krejci95bd14c2017-09-21 14:24:13 +0200261 ngOnInit(): void {
262 this.sessionsService.checkSessions();
Radek Krejci77f77202017-11-03 15:33:50 +0100263 this.activeSession = this.sessionsService.getActiveSession();
Radek Krejci5a69fa32018-02-01 11:03:04 +0100264 if (!this.activeSession.data) {
265 this.treeService.rpcGet(this.activeSession, false);
266 }
Radek Krejciae758392017-10-20 10:53:26 +0200267 }
268
Radek Krejcib424acd2017-10-20 11:36:46 +0200269 changeActiveSession(key: string) {
Radek Krejci77f77202017-11-03 15:33:50 +0100270 this.activeSession = this.sessionsService.changeActiveSession(key);
Radek Krejci95bd14c2017-09-21 14:24:13 +0200271 }
Radek Krejcid23f0df2017-08-31 16:34:49 +0200272}