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 | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame^] | 157 | |
Radek Krejci | 26bf2bc | 2018-01-09 15:00:54 +0100 | [diff] [blame] | 158 | cancelChangesNode(node, recursion = true) { |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame^] | 159 | if ('creatingChild' in node) { |
| 160 | delete node['creatingChild']; |
| 161 | } |
| 162 | if ('deleted' in node) { |
Radek Krejci | 26bf2bc | 2018-01-09 15:00:54 +0100 | [diff] [blame] | 163 | node['dirty'] = false; |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame^] | 164 | 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 Krejci | 26bf2bc | 2018-01-09 15:00:54 +0100 | [diff] [blame] | 178 | } |
| 179 | } |
Radek Krejci | 2e57856 | 2017-10-17 11:11:13 +0200 | [diff] [blame] | 180 | |
Radek Krejci | 26bf2bc | 2018-01-09 15:00:54 +0100 | [diff] [blame] | 181 | /* recursion */ |
| 182 | if (recursion && 'children' in node) { |
| 183 | for (let child of node['children']) { |
| 184 | this.cancelChangesNode(child); |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame^] | 185 | } |
| 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 Krejci | 26bf2bc | 2018-01-09 15:00:54 +0100 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | } |
| 196 | } |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame^] | 197 | |
Radek Krejci | 26bf2bc | 2018-01-09 15:00:54 +0100 | [diff] [blame] | 198 | cancelChanges() { |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame^] | 199 | console.log(JSON.stringify(this.activeSession.modifications)) |
Radek Krejci | 26bf2bc | 2018-01-09 15:00:54 +0100 | [diff] [blame] | 200 | for (let iter of this.activeSession.data) { |
| 201 | this.cancelChangesNode(iter); |
Radek Krejci | 26bf2bc | 2018-01-09 15:00:54 +0100 | [diff] [blame] | 202 | } |
| 203 | this.sessionsService.storeData(); |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame^] | 204 | console.log(JSON.stringify(this.activeSession.modifications)) |
Radek Krejci | 26bf2bc | 2018-01-09 15:00:54 +0100 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | applyChanges() { |
| 208 | /* TODO */ |
| 209 | this.cancelChanges(); |
| 210 | } |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame^] | 211 | |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 212 | ngOnInit(): void { |
| 213 | this.sessionsService.checkSessions(); |
Radek Krejci | 77f7720 | 2017-11-03 15:33:50 +0100 | [diff] [blame] | 214 | this.activeSession = this.sessionsService.getActiveSession(); |
Radek Krejci | ae75839 | 2017-10-20 10:53:26 +0200 | [diff] [blame] | 215 | } |
| 216 | |
Radek Krejci | b424acd | 2017-10-20 11:36:46 +0200 | [diff] [blame] | 217 | changeActiveSession(key: string) { |
Radek Krejci | 77f7720 | 2017-11-03 15:33:50 +0100 | [diff] [blame] | 218 | this.activeSession = this.sessionsService.changeActiveSession(key); |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 219 | } |
Radek Krejci | d23f0df | 2017-08-31 16:34:49 +0200 | [diff] [blame] | 220 | } |