blob: af12bff7c99b0516c994f3bb9f5368fc872389a5 [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
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
8@Component({
Radek Krejcib4794962017-09-21 14:16:28 +02009 selector: 'netopeer-config',
10 templateUrl: './config.component.html',
Radek Krejcicd1ebe12018-01-11 11:34:17 +010011 styleUrls: ['./config.component.scss']
Radek Krejcid23f0df2017-08-31 16:34:49 +020012})
13
Radek Krejci77f77202017-11-03 15:33:50 +010014export class ConfigComponent implements OnInit {
Radek Krejci95bd14c2017-09-21 14:24:13 +020015 title = 'Configuration';
Radek Krejciae758392017-10-20 10:53:26 +020016 activeSession: Session;
Radek Krejci95bd14c2017-09-21 14:24:13 +020017 err_msg = "";
Radek Krejci49904942018-01-29 13:32:11 +010018 loading = false;
Radek Krejci95bd14c2017-09-21 14:24:13 +020019
Radek Krejci9b41f5b2018-01-31 14:17:50 +010020 constructor(private sessionsService: SessionsService,
21 private modsService: ModificationsService,
22 private router: Router) {}
Radek Krejci95bd14c2017-09-21 14:24:13 +020023
24 addSession() {
25 this.router.navigateByUrl('/netopeer/inventory/devices');
26 }
27
Radek Krejcia1339602017-11-02 13:52:38 +010028 reloadData() {
Radek Krejciae758392017-10-20 10:53:26 +020029 this.activeSession.data = null;
Radek Krejcia1339602017-11-02 13:52:38 +010030 if (this.activeSession.dataVisibility == 'all') {
31 this.rpcGet(true);
32 } else if(this.activeSession.dataVisibility == 'root') {
33 this.rpcGet(false);
34 }
Radek Krejciae758392017-10-20 10:53:26 +020035 }
36
Radek Krejci95bd14c2017-09-21 14:24:13 +020037 disconnect(key: string) {
38 this.sessionsService.close(key).subscribe(result => {
39 if (result['success']) {
40 if (!this.sessionsService.activeSession) {
41 this.router.navigateByUrl('/netopeer/inventory/devices');
42 }
Radek Krejcifb0c0462018-01-26 10:01:12 +010043 this.activeSession = this.sessionsService.getActiveSession();
Radek Krejci95bd14c2017-09-21 14:24:13 +020044 } else {
45 this.err_msg = result['error-msg'];
46 }
47 });
48 }
49
Radek Krejci77f77202017-11-03 15:33:50 +010050 setCpbltsVisibility(value: boolean) {
51 this.activeSession.cpbltsVisibility = value;
52 this.sessionsService.storeData();
53 }
54
55 setDataVisibility(value: string) {
56 this.activeSession.dataVisibility = value;
57 this.sessionsService.storeData();
58 }
59
60 invertStatus() {
61 this.activeSession.statusVisibility = !this.activeSession.statusVisibility;
62 this.sessionsService.storeData();
63 }
64
Radek Krejci95bd14c2017-09-21 14:24:13 +020065 getCapabilities(key: string) {
Radek Krejciae758392017-10-20 10:53:26 +020066 if (this.activeSession.cpblts) {
Radek Krejci77f77202017-11-03 15:33:50 +010067 this.activeSession.cpbltsVisibility = true;
68 this.sessionsService.storeData();
Radek Krejciae758392017-10-20 10:53:26 +020069 return;
70 }
Radek Krejci95bd14c2017-09-21 14:24:13 +020071 this.sessionsService.getCpblts(key).subscribe(result => {
72 if (result['success']) {
Radek Krejcia1339602017-11-02 13:52:38 +010073 this.activeSession.cpblts = result['capabilities'];
Radek Krejci77f77202017-11-03 15:33:50 +010074 this.activeSession.cpbltsVisibility = true;
Radek Krejci95bd14c2017-09-21 14:24:13 +020075 } else {
Radek Krejcia1339602017-11-02 13:52:38 +010076 this.activeSession.cpbltsVisibility = false;
77 this.err_msg = result['error-msg'];
Radek Krejci95bd14c2017-09-21 14:24:13 +020078 }
Radek Krejci77f77202017-11-03 15:33:50 +010079 this.sessionsService.storeData();
Radek Krejci95bd14c2017-09-21 14:24:13 +020080 });
81 }
82
Radek Krejciae758392017-10-20 10:53:26 +020083 parseCapabilityName(cpblt: string): string {
84 let name = cpblt;
85 let pos = cpblt.search('module=');
86 if (pos != -1) {
87 /* schema */
88 pos += 7;
89 name = cpblt.slice(pos);
90 let end = name.search('&');
91 if (end != -1) {
92 name = name.slice(0, end);
93 }
94 } else {
95 /* capability */
96 pos = 0;
97 if (cpblt.match('urn:ietf:params:netconf:capability:*')) {
98 pos = 34;
99 } else if (cpblt.match('urn:ietf:params:netconf:*')) {
100 pos = 23;
101 }
102 name = cpblt.slice(pos);
103
104 let end = name.search('\\?');
105 if (end != -1) {
106 name = name.slice(0, end);
107 }
108 pos = name.lastIndexOf(':')
109 name = name.slice(0, pos);
110 }
111 return name;
112 }
113
114 parseCapabilityRevision(cpblt: string): string {
115 let version = "";
116 let pos = cpblt.search('revision=');
117 if (pos != -1) {
118 pos += 9;
119 version = cpblt.slice(pos);
120 let end = version.search('&');
121 if (end != -1) {
122 version = version.slice(0, end);
123 }
124 return version;
125 } else if (cpblt.match('urn:ietf:params:netconf:*')) {
126 let end = cpblt.search('\\?');
127 if (end != -1) {
128 cpblt = cpblt.slice(0, end);
129 }
130 pos = cpblt.lastIndexOf(':')
131 version = cpblt.slice(pos + 1);
132 }
133 return version;
134 }
135
Radek Krejcia1339602017-11-02 13:52:38 +0100136 rpcGet(all: boolean) {
Radek Krejciae758392017-10-20 10:53:26 +0200137 if (this.activeSession.data) {
Radek Krejcia1339602017-11-02 13:52:38 +0100138 if ((all && this.activeSession.dataVisibility == 'all') ||
139 (!all && this.activeSession.dataVisibility == 'root')) {
140 return;
141 }
Radek Krejciae758392017-10-20 10:53:26 +0200142 }
Radek Krejci49904942018-01-29 13:32:11 +0100143 this.loading = true;
Radek Krejcia1339602017-11-02 13:52:38 +0100144 this.sessionsService.rpcGetSubtree(this.activeSession.key, all).subscribe(result => {
Radek Krejci2e578562017-10-17 11:11:13 +0200145 if (result['success']) {
Radek Krejci9b41f5b2018-01-31 14:17:50 +0100146 for (let iter of result['data']) {
147 this.modsService.setDirty(this.activeSession, iter);
148 }
149 this.activeSession.data = {};
150 this.activeSession.data['path'] = '/';
151 this.activeSession.data['info'] = {};
152 this.activeSession.data['info']['path'] = '/';
153 this.activeSession.data['children'] = result['data'];
Radek Krejci77f77202017-11-03 15:33:50 +0100154 if (all) {
155 this.activeSession.dataVisibility = 'all';
156 } else {
157 this.activeSession.dataVisibility = 'root';
158 }
Radek Krejci9b41f5b2018-01-31 14:17:50 +0100159 console.log(this.activeSession.data);
Radek Krejci2e578562017-10-17 11:11:13 +0200160 } else {
Radek Krejcia1339602017-11-02 13:52:38 +0100161 this.activeSession.dataVisibility = 'none';
162 if ('error-msg' in result) {
163 this.err_msg = result['error-msg'];
164 } else {
165 this.err_msg = result['error'][0]['message'];
166 }
Radek Krejci2e578562017-10-17 11:11:13 +0200167 }
Radek Krejci77f77202017-11-03 15:33:50 +0100168 this.sessionsService.storeData();
Radek Krejci49904942018-01-29 13:32:11 +0100169 this.loading = false;
Radek Krejci2e578562017-10-17 11:11:13 +0200170 });
171 }
Radek Krejci6e772b22018-01-25 13:28:57 +0100172
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100173 cancelChanges() {
Radek Krejci2ca11ba2018-01-26 11:25:42 +0100174 //console.log(JSON.stringify(this.activeSession.modifications))
Radek Krejci9b41f5b2018-01-31 14:17:50 +0100175 this.modsService.cancelModification(this.activeSession);
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100176 this.sessionsService.storeData();
Radek Krejci2ca11ba2018-01-26 11:25:42 +0100177 //console.log(JSON.stringify(this.activeSession.modifications))
Radek Krejci26bf2bc2018-01-09 15:00:54 +0100178 }
179
180 applyChanges() {
181 /* TODO */
182 this.cancelChanges();
183 }
Radek Krejci6e772b22018-01-25 13:28:57 +0100184
Radek Krejci95bd14c2017-09-21 14:24:13 +0200185 ngOnInit(): void {
186 this.sessionsService.checkSessions();
Radek Krejci77f77202017-11-03 15:33:50 +0100187 this.activeSession = this.sessionsService.getActiveSession();
Radek Krejciae758392017-10-20 10:53:26 +0200188 }
189
Radek Krejcib424acd2017-10-20 11:36:46 +0200190 changeActiveSession(key: string) {
Radek Krejci77f77202017-11-03 15:33:50 +0100191 this.activeSession = this.sessionsService.changeActiveSession(key);
Radek Krejci95bd14c2017-09-21 14:24:13 +0200192 }
Radek Krejcid23f0df2017-08-31 16:34:49 +0200193}