blob: 6b60a128ba75a93cf7912080c73871a4123a4633 [file] [log] [blame]
Radek Krejci77f77202017-11-03 15:33:50 +01001import {Component, OnInit} from '@angular/core';
Radek Krejci95bd14c2017-09-21 14:24:13 +02002import {Router} from '@angular/router';
3
4import {SessionsService} from './sessions.service';
Radek Krejciae758392017-10-20 10:53:26 +02005import {Session} from './session';
Radek Krejcid23f0df2017-08-31 16:34:49 +02006
7@Component({
Radek Krejcib4794962017-09-21 14:16:28 +02008 selector: 'netopeer-config',
9 templateUrl: './config.component.html',
Radek Krejcicd1ebe12018-01-11 11:34:17 +010010 styleUrls: ['./config.component.scss']
Radek Krejcid23f0df2017-08-31 16:34:49 +020011})
12
Radek Krejci77f77202017-11-03 15:33:50 +010013export class ConfigComponent implements OnInit {
Radek Krejci95bd14c2017-09-21 14:24:13 +020014 title = 'Configuration';
Radek Krejciae758392017-10-20 10:53:26 +020015 activeSession: Session;
Radek Krejci95bd14c2017-09-21 14:24:13 +020016 err_msg = "";
17
18 constructor(private sessionsService: SessionsService, private router: Router) {}
19
20 addSession() {
21 this.router.navigateByUrl('/netopeer/inventory/devices');
22 }
23
Radek Krejcia1339602017-11-02 13:52:38 +010024 reloadData() {
Radek Krejciae758392017-10-20 10:53:26 +020025 this.activeSession.data = null;
Radek Krejcia1339602017-11-02 13:52:38 +010026 if (this.activeSession.dataVisibility == 'all') {
27 this.rpcGet(true);
28 } else if(this.activeSession.dataVisibility == 'root') {
29 this.rpcGet(false);
30 }
Radek Krejciae758392017-10-20 10:53:26 +020031 }
32
Radek Krejci95bd14c2017-09-21 14:24:13 +020033 disconnect(key: string) {
34 this.sessionsService.close(key).subscribe(result => {
35 if (result['success']) {
36 if (!this.sessionsService.activeSession) {
37 this.router.navigateByUrl('/netopeer/inventory/devices');
38 }
39 } else {
40 this.err_msg = result['error-msg'];
41 }
42 });
43 }
44
Radek Krejci77f77202017-11-03 15:33:50 +010045 setCpbltsVisibility(value: boolean) {
46 this.activeSession.cpbltsVisibility = value;
47 this.sessionsService.storeData();
48 }
49
50 setDataVisibility(value: string) {
51 this.activeSession.dataVisibility = value;
52 this.sessionsService.storeData();
53 }
54
55 invertStatus() {
56 this.activeSession.statusVisibility = !this.activeSession.statusVisibility;
57 this.sessionsService.storeData();
58 }
59
Radek Krejci95bd14c2017-09-21 14:24:13 +020060 getCapabilities(key: string) {
Radek Krejciae758392017-10-20 10:53:26 +020061 if (this.activeSession.cpblts) {
Radek Krejci77f77202017-11-03 15:33:50 +010062 this.activeSession.cpbltsVisibility = true;
63 this.sessionsService.storeData();
Radek Krejciae758392017-10-20 10:53:26 +020064 return;
65 }
Radek Krejci95bd14c2017-09-21 14:24:13 +020066 this.sessionsService.getCpblts(key).subscribe(result => {
67 if (result['success']) {
Radek Krejcia1339602017-11-02 13:52:38 +010068 this.activeSession.cpblts = result['capabilities'];
Radek Krejci77f77202017-11-03 15:33:50 +010069 this.activeSession.cpbltsVisibility = true;
Radek Krejci95bd14c2017-09-21 14:24:13 +020070 } else {
Radek Krejcia1339602017-11-02 13:52:38 +010071 this.activeSession.cpbltsVisibility = false;
72 this.err_msg = result['error-msg'];
Radek Krejci95bd14c2017-09-21 14:24:13 +020073 }
Radek Krejci77f77202017-11-03 15:33:50 +010074 this.sessionsService.storeData();
Radek Krejci95bd14c2017-09-21 14:24:13 +020075 });
76 }
77
Radek Krejciae758392017-10-20 10:53:26 +020078 parseCapabilityName(cpblt: string): string {
79 let name = cpblt;
80 let pos = cpblt.search('module=');
81 if (pos != -1) {
82 /* schema */
83 pos += 7;
84 name = cpblt.slice(pos);
85 let end = name.search('&');
86 if (end != -1) {
87 name = name.slice(0, end);
88 }
89 } else {
90 /* capability */
91 pos = 0;
92 if (cpblt.match('urn:ietf:params:netconf:capability:*')) {
93 pos = 34;
94 } else if (cpblt.match('urn:ietf:params:netconf:*')) {
95 pos = 23;
96 }
97 name = cpblt.slice(pos);
98
99 let end = name.search('\\?');
100 if (end != -1) {
101 name = name.slice(0, end);
102 }
103 pos = name.lastIndexOf(':')
104 name = name.slice(0, pos);
105 }
106 return name;
107 }
108
109 parseCapabilityRevision(cpblt: string): string {
110 let version = "";
111 let pos = cpblt.search('revision=');
112 if (pos != -1) {
113 pos += 9;
114 version = cpblt.slice(pos);
115 let end = version.search('&');
116 if (end != -1) {
117 version = version.slice(0, end);
118 }
119 return version;
120 } else if (cpblt.match('urn:ietf:params:netconf:*')) {
121 let end = cpblt.search('\\?');
122 if (end != -1) {
123 cpblt = cpblt.slice(0, end);
124 }
125 pos = cpblt.lastIndexOf(':')
126 version = cpblt.slice(pos + 1);
127 }
128 return version;
129 }
130
Radek Krejcia1339602017-11-02 13:52:38 +0100131 rpcGet(all: boolean) {
Radek Krejciae758392017-10-20 10:53:26 +0200132 if (this.activeSession.data) {
Radek Krejcia1339602017-11-02 13:52:38 +0100133 if ((all && this.activeSession.dataVisibility == 'all') ||
134 (!all && this.activeSession.dataVisibility == 'root')) {
135 return;
136 }
Radek Krejciae758392017-10-20 10:53:26 +0200137 }
Radek Krejcia1339602017-11-02 13:52:38 +0100138 this.sessionsService.rpcGetSubtree(this.activeSession.key, all).subscribe(result => {
Radek Krejci2e578562017-10-17 11:11:13 +0200139 if (result['success']) {
Radek Krejcia1339602017-11-02 13:52:38 +0100140 this.activeSession.data = result['data'];
Radek Krejci77f77202017-11-03 15:33:50 +0100141 if (all) {
142 this.activeSession.dataVisibility = 'all';
143 } else {
144 this.activeSession.dataVisibility = 'root';
145 }
Radek Krejci2e578562017-10-17 11:11:13 +0200146 } else {
Radek Krejcia1339602017-11-02 13:52:38 +0100147 this.activeSession.dataVisibility = 'none';
148 if ('error-msg' in result) {
149 this.err_msg = result['error-msg'];
150 } else {
151 this.err_msg = result['error'][0]['message'];
152 }
Radek Krejci2e578562017-10-17 11:11:13 +0200153 }
Radek Krejci77f77202017-11-03 15:33:50 +0100154 this.sessionsService.storeData();
Radek Krejci2e578562017-10-17 11:11:13 +0200155 });
156 }
Radek Krejci6e772b22018-01-25 13:28:57 +0100157
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100158 cancelChangesNode(node, recursion = true) {
Radek Krejci6e772b22018-01-25 13:28:57 +0100159 if ('creatingChild' in node) {
160 delete node['creatingChild'];
161 }
162 if ('deleted' in node) {
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100163 node['dirty'] = false;
Radek Krejci6e772b22018-01-25 13:28:57 +0100164 node['deleted'] = false;
165 }
166
167 if (this.activeSession.modifications) {
168 let record = this.sessionsService.getModificationsRecord(node['path']);
169 if (record) {
170 node['dirty'] = false;
171 if (record['type'] == 'change') {
172 node['value'] = record['original'];
173 }
174 this.sessionsService.removeModificationsRecord(node['path']);
175 if (!this.activeSession.modifications) {
176 return;
177 }
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100178 }
179 }
Radek Krejci2e578562017-10-17 11:11:13 +0200180
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100181 /* recursion */
182 if (recursion && 'children' in node) {
183 for (let child of node['children']) {
184 this.cancelChangesNode(child);
Radek Krejci6e772b22018-01-25 13:28:57 +0100185 }
186 if ('newChildren' in node) {
187 for (let child of node['newChildren']) {
188 this.sessionsService.removeModificationsRecord(child['path']);
189 }
190 delete node['newChildren'];
191 if (('children' in node) && node['children'].length) {
192 node['children'][node['children'].length - 1]['last'] = true;
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100193 }
194 }
195 }
196 }
Radek Krejci6e772b22018-01-25 13:28:57 +0100197
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100198 cancelChanges() {
Radek Krejci6e772b22018-01-25 13:28:57 +0100199 console.log(JSON.stringify(this.activeSession.modifications))
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100200 for (let iter of this.activeSession.data) {
201 this.cancelChangesNode(iter);
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100202 }
203 this.sessionsService.storeData();
Radek Krejci6e772b22018-01-25 13:28:57 +0100204 console.log(JSON.stringify(this.activeSession.modifications))
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100205 }
206
207 applyChanges() {
208 /* TODO */
209 this.cancelChanges();
210 }
Radek Krejci6e772b22018-01-25 13:28:57 +0100211
Radek Krejci95bd14c2017-09-21 14:24:13 +0200212 ngOnInit(): void {
213 this.sessionsService.checkSessions();
Radek Krejci77f77202017-11-03 15:33:50 +0100214 this.activeSession = this.sessionsService.getActiveSession();
Radek Krejciae758392017-10-20 10:53:26 +0200215 }
216
Radek Krejcib424acd2017-10-20 11:36:46 +0200217 changeActiveSession(key: string) {
Radek Krejci77f77202017-11-03 15:33:50 +0100218 this.activeSession = this.sessionsService.changeActiveSession(key);
Radek Krejci95bd14c2017-09-21 14:24:13 +0200219 }
Radek Krejcid23f0df2017-08-31 16:34:49 +0200220}