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 | } |
Radek Krejci | fb0c046 | 2018-01-26 10:01:12 +0100 | [diff] [blame] | 39 | this.activeSession = this.sessionsService.getActiveSession(); |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 40 | } else { |
| 41 | this.err_msg = result['error-msg']; |
| 42 | } |
| 43 | }); |
| 44 | } |
| 45 | |
Radek Krejci | 77f7720 | 2017-11-03 15:33:50 +0100 | [diff] [blame] | 46 | 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 Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 61 | getCapabilities(key: string) { |
Radek Krejci | ae75839 | 2017-10-20 10:53:26 +0200 | [diff] [blame] | 62 | if (this.activeSession.cpblts) { |
Radek Krejci | 77f7720 | 2017-11-03 15:33:50 +0100 | [diff] [blame] | 63 | this.activeSession.cpbltsVisibility = true; |
| 64 | this.sessionsService.storeData(); |
Radek Krejci | ae75839 | 2017-10-20 10:53:26 +0200 | [diff] [blame] | 65 | return; |
| 66 | } |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 67 | this.sessionsService.getCpblts(key).subscribe(result => { |
| 68 | if (result['success']) { |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 69 | this.activeSession.cpblts = result['capabilities']; |
Radek Krejci | 77f7720 | 2017-11-03 15:33:50 +0100 | [diff] [blame] | 70 | this.activeSession.cpbltsVisibility = true; |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 71 | } else { |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 72 | this.activeSession.cpbltsVisibility = false; |
| 73 | this.err_msg = result['error-msg']; |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 74 | } |
Radek Krejci | 77f7720 | 2017-11-03 15:33:50 +0100 | [diff] [blame] | 75 | this.sessionsService.storeData(); |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 76 | }); |
| 77 | } |
| 78 | |
Radek Krejci | ae75839 | 2017-10-20 10:53:26 +0200 | [diff] [blame] | 79 | 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 Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 132 | rpcGet(all: boolean) { |
Radek Krejci | ae75839 | 2017-10-20 10:53:26 +0200 | [diff] [blame] | 133 | if (this.activeSession.data) { |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 134 | if ((all && this.activeSession.dataVisibility == 'all') || |
| 135 | (!all && this.activeSession.dataVisibility == 'root')) { |
| 136 | return; |
| 137 | } |
Radek Krejci | ae75839 | 2017-10-20 10:53:26 +0200 | [diff] [blame] | 138 | } |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 139 | this.sessionsService.rpcGetSubtree(this.activeSession.key, all).subscribe(result => { |
Radek Krejci | 2e57856 | 2017-10-17 11:11:13 +0200 | [diff] [blame] | 140 | if (result['success']) { |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 141 | this.activeSession.data = result['data']; |
Radek Krejci | 77f7720 | 2017-11-03 15:33:50 +0100 | [diff] [blame] | 142 | if (all) { |
| 143 | this.activeSession.dataVisibility = 'all'; |
| 144 | } else { |
| 145 | this.activeSession.dataVisibility = 'root'; |
| 146 | } |
Radek Krejci | 2e57856 | 2017-10-17 11:11:13 +0200 | [diff] [blame] | 147 | } else { |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 148 | 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 Krejci | 2e57856 | 2017-10-17 11:11:13 +0200 | [diff] [blame] | 154 | } |
Radek Krejci | 77f7720 | 2017-11-03 15:33:50 +0100 | [diff] [blame] | 155 | this.sessionsService.storeData(); |
Radek Krejci | 2e57856 | 2017-10-17 11:11:13 +0200 | [diff] [blame] | 156 | }); |
| 157 | } |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 158 | |
Radek Krejci | 26bf2bc | 2018-01-09 15:00:54 +0100 | [diff] [blame] | 159 | cancelChangesNode(node, recursion = true) { |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 160 | if ('creatingChild' in node) { |
| 161 | delete node['creatingChild']; |
| 162 | } |
| 163 | if ('deleted' in node) { |
Radek Krejci | 26bf2bc | 2018-01-09 15:00:54 +0100 | [diff] [blame] | 164 | node['dirty'] = false; |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 165 | 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 Krejci | 26bf2bc | 2018-01-09 15:00:54 +0100 | [diff] [blame] | 179 | } |
| 180 | } |
Radek Krejci | 2e57856 | 2017-10-17 11:11:13 +0200 | [diff] [blame] | 181 | |
Radek Krejci | 26bf2bc | 2018-01-09 15:00:54 +0100 | [diff] [blame] | 182 | /* recursion */ |
| 183 | if (recursion && 'children' in node) { |
| 184 | for (let child of node['children']) { |
| 185 | this.cancelChangesNode(child); |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 186 | } |
| 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 Krejci | 26bf2bc | 2018-01-09 15:00:54 +0100 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | } |
| 197 | } |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 198 | |
Radek Krejci | 26bf2bc | 2018-01-09 15:00:54 +0100 | [diff] [blame] | 199 | cancelChanges() { |
Radek Krejci | 2ca11ba | 2018-01-26 11:25:42 +0100 | [diff] [blame^] | 200 | //console.log(JSON.stringify(this.activeSession.modifications)) |
Radek Krejci | 26bf2bc | 2018-01-09 15:00:54 +0100 | [diff] [blame] | 201 | for (let iter of this.activeSession.data) { |
| 202 | this.cancelChangesNode(iter); |
Radek Krejci | 26bf2bc | 2018-01-09 15:00:54 +0100 | [diff] [blame] | 203 | } |
| 204 | this.sessionsService.storeData(); |
Radek Krejci | 2ca11ba | 2018-01-26 11:25:42 +0100 | [diff] [blame^] | 205 | //console.log(JSON.stringify(this.activeSession.modifications)) |
Radek Krejci | 26bf2bc | 2018-01-09 15:00:54 +0100 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | applyChanges() { |
| 209 | /* TODO */ |
| 210 | this.cancelChanges(); |
| 211 | } |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 212 | |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 213 | ngOnInit(): void { |
| 214 | this.sessionsService.checkSessions(); |
Radek Krejci | 77f7720 | 2017-11-03 15:33:50 +0100 | [diff] [blame] | 215 | this.activeSession = this.sessionsService.getActiveSession(); |
Radek Krejci | ae75839 | 2017-10-20 10:53:26 +0200 | [diff] [blame] | 216 | } |
| 217 | |
Radek Krejci | b424acd | 2017-10-20 11:36:46 +0200 | [diff] [blame] | 218 | changeActiveSession(key: string) { |
Radek Krejci | 77f7720 | 2017-11-03 15:33:50 +0100 | [diff] [blame] | 219 | this.activeSession = this.sessionsService.changeActiveSession(key); |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 220 | } |
Radek Krejci | d23f0df | 2017-08-31 16:34:49 +0200 | [diff] [blame] | 221 | } |