Radek Krejci | 77f7720 | 2017-11-03 15:33:50 +0100 | [diff] [blame] | 1 | import {Component, OnInit} from '@angular/core'; |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 2 | import {Router} from '@angular/router'; |
| 3 | |
| 4 | import {SessionsService} from './sessions.service'; |
Radek Krejci | ae75839 | 2017-10-20 10:53:26 +0200 | [diff] [blame] | 5 | import {Session} from './session'; |
Radek Krejci | d23f0df | 2017-08-31 16:34:49 +0200 | [diff] [blame] | 6 | |
| 7 | @Component({ |
Radek Krejci | b479496 | 2017-09-21 14:16:28 +0200 | [diff] [blame] | 8 | selector: 'netopeer-config', |
| 9 | templateUrl: './config.component.html', |
Radek Krejci | cd1ebe1 | 2018-01-11 11:34:17 +0100 | [diff] [blame^] | 10 | styleUrls: ['./config.component.scss'] |
Radek Krejci | d23f0df | 2017-08-31 16:34:49 +0200 | [diff] [blame] | 11 | }) |
| 12 | |
Radek Krejci | 77f7720 | 2017-11-03 15:33:50 +0100 | [diff] [blame] | 13 | export class ConfigComponent implements OnInit { |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 14 | title = 'Configuration'; |
Radek Krejci | ae75839 | 2017-10-20 10:53:26 +0200 | [diff] [blame] | 15 | activeSession: Session; |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 16 | err_msg = ""; |
| 17 | |
| 18 | constructor(private sessionsService: SessionsService, private router: Router) {} |
| 19 | |
| 20 | addSession() { |
| 21 | this.router.navigateByUrl('/netopeer/inventory/devices'); |
| 22 | } |
| 23 | |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 24 | reloadData() { |
Radek Krejci | ae75839 | 2017-10-20 10:53:26 +0200 | [diff] [blame] | 25 | this.activeSession.data = null; |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 26 | if (this.activeSession.dataVisibility == 'all') { |
| 27 | this.rpcGet(true); |
| 28 | } else if(this.activeSession.dataVisibility == 'root') { |
| 29 | this.rpcGet(false); |
| 30 | } |
Radek Krejci | ae75839 | 2017-10-20 10:53:26 +0200 | [diff] [blame] | 31 | } |
| 32 | |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 33 | 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 Krejci | 77f7720 | 2017-11-03 15:33:50 +0100 | [diff] [blame] | 45 | 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 Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 60 | getCapabilities(key: string) { |
Radek Krejci | ae75839 | 2017-10-20 10:53:26 +0200 | [diff] [blame] | 61 | if (this.activeSession.cpblts) { |
Radek Krejci | 77f7720 | 2017-11-03 15:33:50 +0100 | [diff] [blame] | 62 | this.activeSession.cpbltsVisibility = true; |
| 63 | this.sessionsService.storeData(); |
Radek Krejci | ae75839 | 2017-10-20 10:53:26 +0200 | [diff] [blame] | 64 | return; |
| 65 | } |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 66 | this.sessionsService.getCpblts(key).subscribe(result => { |
| 67 | if (result['success']) { |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 68 | this.activeSession.cpblts = result['capabilities']; |
Radek Krejci | 77f7720 | 2017-11-03 15:33:50 +0100 | [diff] [blame] | 69 | this.activeSession.cpbltsVisibility = true; |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 70 | } else { |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 71 | this.activeSession.cpbltsVisibility = false; |
| 72 | this.err_msg = result['error-msg']; |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 73 | } |
Radek Krejci | 77f7720 | 2017-11-03 15:33:50 +0100 | [diff] [blame] | 74 | this.sessionsService.storeData(); |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 75 | }); |
| 76 | } |
| 77 | |
Radek Krejci | ae75839 | 2017-10-20 10:53:26 +0200 | [diff] [blame] | 78 | 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 Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 131 | rpcGet(all: boolean) { |
Radek Krejci | ae75839 | 2017-10-20 10:53:26 +0200 | [diff] [blame] | 132 | if (this.activeSession.data) { |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 133 | if ((all && this.activeSession.dataVisibility == 'all') || |
| 134 | (!all && this.activeSession.dataVisibility == 'root')) { |
| 135 | return; |
| 136 | } |
Radek Krejci | ae75839 | 2017-10-20 10:53:26 +0200 | [diff] [blame] | 137 | } |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 138 | this.sessionsService.rpcGetSubtree(this.activeSession.key, all).subscribe(result => { |
Radek Krejci | 2e57856 | 2017-10-17 11:11:13 +0200 | [diff] [blame] | 139 | if (result['success']) { |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 140 | this.activeSession.data = result['data']; |
Radek Krejci | 77f7720 | 2017-11-03 15:33:50 +0100 | [diff] [blame] | 141 | if (all) { |
| 142 | this.activeSession.dataVisibility = 'all'; |
| 143 | } else { |
| 144 | this.activeSession.dataVisibility = 'root'; |
| 145 | } |
Radek Krejci | 2e57856 | 2017-10-17 11:11:13 +0200 | [diff] [blame] | 146 | } else { |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 147 | 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 Krejci | 2e57856 | 2017-10-17 11:11:13 +0200 | [diff] [blame] | 153 | } |
Radek Krejci | 77f7720 | 2017-11-03 15:33:50 +0100 | [diff] [blame] | 154 | this.sessionsService.storeData(); |
Radek Krejci | 2e57856 | 2017-10-17 11:11:13 +0200 | [diff] [blame] | 155 | }); |
| 156 | } |
Radek Krejci | 26bf2bc | 2018-01-09 15:00:54 +0100 | [diff] [blame] | 157 | |
| 158 | cancelChangesNode(node, recursion = true) { |
| 159 | |
| 160 | if (node['path'] in this.activeSession.modifications) { |
| 161 | node['dirty'] = false; |
| 162 | if (this.activeSession.modifications[node['path']]['type'] == 'change') { |
| 163 | node['value'] = this.activeSession.modifications[node['path']]['original']; |
| 164 | } |
| 165 | delete this.activeSession.modifications[node['path']]; |
| 166 | if (!Object.keys(this.activeSession.modifications).length) { |
| 167 | delete this.activeSession.modifications; |
| 168 | return; |
| 169 | } |
| 170 | } |
Radek Krejci | 2e57856 | 2017-10-17 11:11:13 +0200 | [diff] [blame] | 171 | |
Radek Krejci | 26bf2bc | 2018-01-09 15:00:54 +0100 | [diff] [blame] | 172 | /* recursion */ |
| 173 | if (recursion && 'children' in node) { |
| 174 | for (let child of node['children']) { |
| 175 | this.cancelChangesNode(child); |
| 176 | if (!this.activeSession.modifications) { |
| 177 | return; |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | cancelChanges() { |
| 184 | for (let iter of this.activeSession.data) { |
| 185 | this.cancelChangesNode(iter); |
| 186 | if (!this.activeSession.modifications) { |
| 187 | break; |
| 188 | } |
| 189 | } |
| 190 | this.sessionsService.storeData(); |
| 191 | } |
| 192 | |
| 193 | applyChanges() { |
| 194 | /* TODO */ |
| 195 | this.cancelChanges(); |
| 196 | } |
| 197 | |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 198 | ngOnInit(): void { |
| 199 | this.sessionsService.checkSessions(); |
Radek Krejci | 77f7720 | 2017-11-03 15:33:50 +0100 | [diff] [blame] | 200 | this.activeSession = this.sessionsService.getActiveSession(); |
Radek Krejci | ae75839 | 2017-10-20 10:53:26 +0200 | [diff] [blame] | 201 | } |
| 202 | |
Radek Krejci | b424acd | 2017-10-20 11:36:46 +0200 | [diff] [blame] | 203 | changeActiveSession(key: string) { |
Radek Krejci | 77f7720 | 2017-11-03 15:33:50 +0100 | [diff] [blame] | 204 | this.activeSession = this.sessionsService.changeActiveSession(key); |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 205 | } |
Radek Krejci | d23f0df | 2017-08-31 16:34:49 +0200 | [diff] [blame] | 206 | } |