blob: 07636d7bf9f0f157ed70f01c0d1bda17cb0a0a98 [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 }
Radek Krejcifb0c0462018-01-26 10:01:12 +010039 this.activeSession = this.sessionsService.getActiveSession();
Radek Krejci95bd14c2017-09-21 14:24:13 +020040 } else {
41 this.err_msg = result['error-msg'];
42 }
43 });
44 }
45
Radek Krejci77f77202017-11-03 15:33:50 +010046 setCpbltsVisibility(value: boolean) {
47 this.activeSession.cpbltsVisibility = value;
48 this.sessionsService.storeData();
49 }
50
51 setDataVisibility(value: string) {
52 this.activeSession.dataVisibility = value;
53 this.sessionsService.storeData();
54 }
55
56 invertStatus() {
57 this.activeSession.statusVisibility = !this.activeSession.statusVisibility;
58 this.sessionsService.storeData();
59 }
60
Radek Krejci95bd14c2017-09-21 14:24:13 +020061 getCapabilities(key: string) {
Radek Krejciae758392017-10-20 10:53:26 +020062 if (this.activeSession.cpblts) {
Radek Krejci77f77202017-11-03 15:33:50 +010063 this.activeSession.cpbltsVisibility = true;
64 this.sessionsService.storeData();
Radek Krejciae758392017-10-20 10:53:26 +020065 return;
66 }
Radek Krejci95bd14c2017-09-21 14:24:13 +020067 this.sessionsService.getCpblts(key).subscribe(result => {
68 if (result['success']) {
Radek Krejcia1339602017-11-02 13:52:38 +010069 this.activeSession.cpblts = result['capabilities'];
Radek Krejci77f77202017-11-03 15:33:50 +010070 this.activeSession.cpbltsVisibility = true;
Radek Krejci95bd14c2017-09-21 14:24:13 +020071 } else {
Radek Krejcia1339602017-11-02 13:52:38 +010072 this.activeSession.cpbltsVisibility = false;
73 this.err_msg = result['error-msg'];
Radek Krejci95bd14c2017-09-21 14:24:13 +020074 }
Radek Krejci77f77202017-11-03 15:33:50 +010075 this.sessionsService.storeData();
Radek Krejci95bd14c2017-09-21 14:24:13 +020076 });
77 }
78
Radek Krejciae758392017-10-20 10:53:26 +020079 parseCapabilityName(cpblt: string): string {
80 let name = cpblt;
81 let pos = cpblt.search('module=');
82 if (pos != -1) {
83 /* schema */
84 pos += 7;
85 name = cpblt.slice(pos);
86 let end = name.search('&');
87 if (end != -1) {
88 name = name.slice(0, end);
89 }
90 } else {
91 /* capability */
92 pos = 0;
93 if (cpblt.match('urn:ietf:params:netconf:capability:*')) {
94 pos = 34;
95 } else if (cpblt.match('urn:ietf:params:netconf:*')) {
96 pos = 23;
97 }
98 name = cpblt.slice(pos);
99
100 let end = name.search('\\?');
101 if (end != -1) {
102 name = name.slice(0, end);
103 }
104 pos = name.lastIndexOf(':')
105 name = name.slice(0, pos);
106 }
107 return name;
108 }
109
110 parseCapabilityRevision(cpblt: string): string {
111 let version = "";
112 let pos = cpblt.search('revision=');
113 if (pos != -1) {
114 pos += 9;
115 version = cpblt.slice(pos);
116 let end = version.search('&');
117 if (end != -1) {
118 version = version.slice(0, end);
119 }
120 return version;
121 } else if (cpblt.match('urn:ietf:params:netconf:*')) {
122 let end = cpblt.search('\\?');
123 if (end != -1) {
124 cpblt = cpblt.slice(0, end);
125 }
126 pos = cpblt.lastIndexOf(':')
127 version = cpblt.slice(pos + 1);
128 }
129 return version;
130 }
131
Radek Krejcia1339602017-11-02 13:52:38 +0100132 rpcGet(all: boolean) {
Radek Krejciae758392017-10-20 10:53:26 +0200133 if (this.activeSession.data) {
Radek Krejcia1339602017-11-02 13:52:38 +0100134 if ((all && this.activeSession.dataVisibility == 'all') ||
135 (!all && this.activeSession.dataVisibility == 'root')) {
136 return;
137 }
Radek Krejciae758392017-10-20 10:53:26 +0200138 }
Radek Krejcia1339602017-11-02 13:52:38 +0100139 this.sessionsService.rpcGetSubtree(this.activeSession.key, all).subscribe(result => {
Radek Krejci2e578562017-10-17 11:11:13 +0200140 if (result['success']) {
Radek Krejcia1339602017-11-02 13:52:38 +0100141 this.activeSession.data = result['data'];
Radek Krejci77f77202017-11-03 15:33:50 +0100142 if (all) {
143 this.activeSession.dataVisibility = 'all';
144 } else {
145 this.activeSession.dataVisibility = 'root';
146 }
Radek Krejci2e578562017-10-17 11:11:13 +0200147 } else {
Radek Krejcia1339602017-11-02 13:52:38 +0100148 this.activeSession.dataVisibility = 'none';
149 if ('error-msg' in result) {
150 this.err_msg = result['error-msg'];
151 } else {
152 this.err_msg = result['error'][0]['message'];
153 }
Radek Krejci2e578562017-10-17 11:11:13 +0200154 }
Radek Krejci77f77202017-11-03 15:33:50 +0100155 this.sessionsService.storeData();
Radek Krejci2e578562017-10-17 11:11:13 +0200156 });
157 }
Radek Krejci6e772b22018-01-25 13:28:57 +0100158
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100159 cancelChangesNode(node, recursion = true) {
Radek Krejci6e772b22018-01-25 13:28:57 +0100160 if ('creatingChild' in node) {
161 delete node['creatingChild'];
162 }
163 if ('deleted' in node) {
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100164 node['dirty'] = false;
Radek Krejci6e772b22018-01-25 13:28:57 +0100165 node['deleted'] = false;
166 }
167
168 if (this.activeSession.modifications) {
169 let record = this.sessionsService.getModificationsRecord(node['path']);
170 if (record) {
171 node['dirty'] = false;
172 if (record['type'] == 'change') {
173 node['value'] = record['original'];
174 }
175 this.sessionsService.removeModificationsRecord(node['path']);
176 if (!this.activeSession.modifications) {
177 return;
178 }
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100179 }
180 }
Radek Krejci2e578562017-10-17 11:11:13 +0200181
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100182 /* recursion */
183 if (recursion && 'children' in node) {
184 for (let child of node['children']) {
185 this.cancelChangesNode(child);
Radek Krejci6e772b22018-01-25 13:28:57 +0100186 }
187 if ('newChildren' in node) {
188 for (let child of node['newChildren']) {
189 this.sessionsService.removeModificationsRecord(child['path']);
190 }
191 delete node['newChildren'];
192 if (('children' in node) && node['children'].length) {
193 node['children'][node['children'].length - 1]['last'] = true;
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100194 }
195 }
196 }
197 }
Radek Krejci6e772b22018-01-25 13:28:57 +0100198
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100199 cancelChanges() {
Radek Krejci2ca11ba2018-01-26 11:25:42 +0100200 //console.log(JSON.stringify(this.activeSession.modifications))
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100201 for (let iter of this.activeSession.data) {
202 this.cancelChangesNode(iter);
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100203 }
204 this.sessionsService.storeData();
Radek Krejci2ca11ba2018-01-26 11:25:42 +0100205 //console.log(JSON.stringify(this.activeSession.modifications))
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100206 }
207
208 applyChanges() {
209 /* TODO */
210 this.cancelChanges();
211 }
Radek Krejci6e772b22018-01-25 13:28:57 +0100212
Radek Krejci95bd14c2017-09-21 14:24:13 +0200213 ngOnInit(): void {
214 this.sessionsService.checkSessions();
Radek Krejci77f77202017-11-03 15:33:50 +0100215 this.activeSession = this.sessionsService.getActiveSession();
Radek Krejciae758392017-10-20 10:53:26 +0200216 }
217
Radek Krejcib424acd2017-10-20 11:36:46 +0200218 changeActiveSession(key: string) {
Radek Krejci77f77202017-11-03 15:33:50 +0100219 this.activeSession = this.sessionsService.changeActiveSession(key);
Radek Krejci95bd14c2017-09-21 14:24:13 +0200220 }
Radek Krejcid23f0df2017-08-31 16:34:49 +0200221}