Radek Krejci | 5a69fa3 | 2018-02-01 11:03:04 +0100 | [diff] [blame] | 1 | import {Component, Injectable, OnInit} from '@angular/core'; |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 2 | import {Router} from '@angular/router'; |
| 3 | |
Radek Krejci | 9b41f5b | 2018-01-31 14:17:50 +0100 | [diff] [blame] | 4 | import {ModificationsService} from './modifications.service'; |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 5 | import {SessionsService} from './sessions.service'; |
Radek Krejci | ae75839 | 2017-10-20 10:53:26 +0200 | [diff] [blame] | 6 | import {Session} from './session'; |
Radek Krejci | d23f0df | 2017-08-31 16:34:49 +0200 | [diff] [blame] | 7 | |
Radek Krejci | 5a69fa3 | 2018-02-01 11:03:04 +0100 | [diff] [blame] | 8 | @Injectable() |
| 9 | export class TreeService { |
| 10 | loading = false; |
| 11 | |
| 12 | constructor(private sessionsService: SessionsService, private modsService: ModificationsService) {} |
| 13 | |
| 14 | rpcGet(activeSession, all: boolean) { |
| 15 | if (activeSession.data) { |
| 16 | if ((all && activeSession.dataVisibility == 'all') || |
| 17 | (!all && activeSession.dataVisibility == 'root')) { |
| 18 | return; |
| 19 | } |
| 20 | } |
| 21 | this.loading = true; |
| 22 | delete activeSession.data; |
| 23 | this.sessionsService.rpcGetSubtree(activeSession.key, all).subscribe(result => { |
| 24 | if (result['success']) { |
| 25 | for (let iter of result['data']) { |
| 26 | this.modsService.setDirty(activeSession, iter); |
| 27 | } |
| 28 | activeSession.data = {}; |
| 29 | activeSession.data['path'] = '/'; |
| 30 | activeSession.data['info'] = {}; |
| 31 | activeSession.data['info']['path'] = '/'; |
| 32 | activeSession.data['children'] = result['data']; |
| 33 | if (all) { |
| 34 | activeSession.dataVisibility = 'all'; |
| 35 | } else { |
| 36 | activeSession.dataVisibility = 'root'; |
| 37 | } |
Radek Krejci | 5a69fa3 | 2018-02-01 11:03:04 +0100 | [diff] [blame] | 38 | } |
| 39 | this.sessionsService.storeData(); |
| 40 | this.loading = false; |
| 41 | }); |
| 42 | } |
| 43 | |
| 44 | expandable(node): boolean { |
| 45 | if (node['info']['type'] == 1 || /* container */ |
| 46 | node['info']['type'] == 16) { /* list */ |
| 47 | return true; |
| 48 | } |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | hasHiddenChild(node, clean=false): boolean { |
| 53 | if (!clean && 'hasHiddenChild' in node) { |
| 54 | return node['hasHiddenChild']; |
| 55 | } |
| 56 | node['hasHiddenChild'] = false; |
| 57 | if (!this.expandable(node)) { |
| 58 | /* terminal node (leaf or leaf-list) */ |
| 59 | return node['hasHiddenChild']; |
| 60 | } else if (!('children' in node)) { |
| 61 | /* internal node without children */ |
| 62 | node['hasHiddenChild'] = true; |
| 63 | } else { |
| 64 | /* go recursively */ |
| 65 | for (let child of node['children']) { |
| 66 | if (this.hasHiddenChild(child, clean)) { |
| 67 | node['hasHiddenChild'] = true; |
| 68 | break; |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | return node['hasHiddenChild']; |
| 73 | } |
| 74 | |
| 75 | updateHiddenFlags(activeSession) { |
| 76 | let mixed = false; |
| 77 | let rootsonly = true; |
| 78 | for (let root of activeSession.data['children']) { |
| 79 | if (this.hasHiddenChild(root, true)) { |
| 80 | mixed = true; |
| 81 | } else { |
| 82 | rootsonly = false; |
| 83 | } |
| 84 | } |
| 85 | if (mixed) { |
| 86 | if (rootsonly) { |
| 87 | activeSession.dataVisibility = 'root'; |
| 88 | } else { |
| 89 | activeSession.dataVisibility = 'mixed'; |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | collapse(activeSession, node = null) { |
| 95 | if (node) { |
| 96 | delete node['children']; |
| 97 | activeSession.dataVisibility = 'mixed'; |
| 98 | } else { |
| 99 | for (let root of activeSession.data['children']) { |
| 100 | delete root['children']; |
| 101 | } |
| 102 | activeSession.dataVisibility = 'root'; |
| 103 | } |
| 104 | this.updateHiddenFlags(activeSession); |
| 105 | this.sessionsService.storeData(); |
| 106 | } |
| 107 | |
| 108 | expand(activeSession, node, all: boolean) { |
| 109 | node['loading'] = true; |
| 110 | this.sessionsService.rpcGetSubtree(activeSession.key, all, node['path']).subscribe(result => { |
| 111 | if (result['success']) { |
| 112 | for (let iter of result['data']['children']) { |
| 113 | this.modsService.setDirty(activeSession, iter); |
| 114 | } |
| 115 | node['children'] = result['data']['children']; |
| 116 | this.updateHiddenFlags(activeSession); |
| 117 | delete node['loading']; |
| 118 | this.sessionsService.storeData(); |
| 119 | } |
| 120 | }); |
| 121 | } |
| 122 | } |
| 123 | |
Radek Krejci | d23f0df | 2017-08-31 16:34:49 +0200 | [diff] [blame] | 124 | @Component({ |
Radek Krejci | b479496 | 2017-09-21 14:16:28 +0200 | [diff] [blame] | 125 | selector: 'netopeer-config', |
| 126 | templateUrl: './config.component.html', |
Radek Krejci | 5a69fa3 | 2018-02-01 11:03:04 +0100 | [diff] [blame] | 127 | styleUrls: ['./config.component.scss'], |
| 128 | providers: [ModificationsService, TreeService] |
Radek Krejci | d23f0df | 2017-08-31 16:34:49 +0200 | [diff] [blame] | 129 | }) |
| 130 | |
Radek Krejci | 77f7720 | 2017-11-03 15:33:50 +0100 | [diff] [blame] | 131 | export class ConfigComponent implements OnInit { |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 132 | title = 'Configuration'; |
Radek Krejci | ae75839 | 2017-10-20 10:53:26 +0200 | [diff] [blame] | 133 | activeSession: Session; |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 134 | err_msg = ""; |
Radek Krejci | fc908f3 | 2018-02-08 14:53:31 +0100 | [diff] [blame] | 135 | commit_error = []; |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 136 | |
Radek Krejci | 9b41f5b | 2018-01-31 14:17:50 +0100 | [diff] [blame] | 137 | constructor(private sessionsService: SessionsService, |
| 138 | private modsService: ModificationsService, |
Radek Krejci | 5a69fa3 | 2018-02-01 11:03:04 +0100 | [diff] [blame] | 139 | private treeService: TreeService, |
Radek Krejci | 9b41f5b | 2018-01-31 14:17:50 +0100 | [diff] [blame] | 140 | private router: Router) {} |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 141 | |
| 142 | addSession() { |
| 143 | this.router.navigateByUrl('/netopeer/inventory/devices'); |
| 144 | } |
| 145 | |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 146 | reloadData() { |
Radek Krejci | ae75839 | 2017-10-20 10:53:26 +0200 | [diff] [blame] | 147 | this.activeSession.data = null; |
Radek Krejci | 5a69fa3 | 2018-02-01 11:03:04 +0100 | [diff] [blame] | 148 | if (this.activeSession.dataVisibility == 'root') { |
| 149 | this.treeService.rpcGet(this.activeSession, false); |
| 150 | } else { |
| 151 | this.treeService.rpcGet(this.activeSession, true); |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 152 | } |
Radek Krejci | ae75839 | 2017-10-20 10:53:26 +0200 | [diff] [blame] | 153 | } |
| 154 | |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 155 | disconnect(key: string) { |
| 156 | this.sessionsService.close(key).subscribe(result => { |
| 157 | if (result['success']) { |
| 158 | if (!this.sessionsService.activeSession) { |
| 159 | this.router.navigateByUrl('/netopeer/inventory/devices'); |
| 160 | } |
Radek Krejci | fb0c046 | 2018-01-26 10:01:12 +0100 | [diff] [blame] | 161 | this.activeSession = this.sessionsService.getActiveSession(); |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 162 | } else { |
| 163 | this.err_msg = result['error-msg']; |
| 164 | } |
| 165 | }); |
| 166 | } |
| 167 | |
Radek Krejci | 77f7720 | 2017-11-03 15:33:50 +0100 | [diff] [blame] | 168 | setCpbltsVisibility(value: boolean) { |
| 169 | this.activeSession.cpbltsVisibility = value; |
| 170 | this.sessionsService.storeData(); |
| 171 | } |
| 172 | |
Radek Krejci | 77f7720 | 2017-11-03 15:33:50 +0100 | [diff] [blame] | 173 | invertStatus() { |
| 174 | this.activeSession.statusVisibility = !this.activeSession.statusVisibility; |
| 175 | this.sessionsService.storeData(); |
| 176 | } |
| 177 | |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 178 | getCapabilities(key: string) { |
Radek Krejci | ae75839 | 2017-10-20 10:53:26 +0200 | [diff] [blame] | 179 | if (this.activeSession.cpblts) { |
Radek Krejci | 77f7720 | 2017-11-03 15:33:50 +0100 | [diff] [blame] | 180 | this.activeSession.cpbltsVisibility = true; |
| 181 | this.sessionsService.storeData(); |
Radek Krejci | ae75839 | 2017-10-20 10:53:26 +0200 | [diff] [blame] | 182 | return; |
| 183 | } |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 184 | this.sessionsService.getCpblts(key).subscribe(result => { |
| 185 | if (result['success']) { |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 186 | this.activeSession.cpblts = result['capabilities']; |
Radek Krejci | 77f7720 | 2017-11-03 15:33:50 +0100 | [diff] [blame] | 187 | this.activeSession.cpbltsVisibility = true; |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 188 | } else { |
Radek Krejci | a133960 | 2017-11-02 13:52:38 +0100 | [diff] [blame] | 189 | this.activeSession.cpbltsVisibility = false; |
| 190 | this.err_msg = result['error-msg']; |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 191 | } |
Radek Krejci | 77f7720 | 2017-11-03 15:33:50 +0100 | [diff] [blame] | 192 | this.sessionsService.storeData(); |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 193 | }); |
| 194 | } |
| 195 | |
Radek Krejci | ae75839 | 2017-10-20 10:53:26 +0200 | [diff] [blame] | 196 | parseCapabilityName(cpblt: string): string { |
| 197 | let name = cpblt; |
| 198 | let pos = cpblt.search('module='); |
| 199 | if (pos != -1) { |
| 200 | /* schema */ |
| 201 | pos += 7; |
| 202 | name = cpblt.slice(pos); |
| 203 | let end = name.search('&'); |
| 204 | if (end != -1) { |
| 205 | name = name.slice(0, end); |
| 206 | } |
| 207 | } else { |
| 208 | /* capability */ |
| 209 | pos = 0; |
| 210 | if (cpblt.match('urn:ietf:params:netconf:capability:*')) { |
| 211 | pos = 34; |
| 212 | } else if (cpblt.match('urn:ietf:params:netconf:*')) { |
| 213 | pos = 23; |
| 214 | } |
| 215 | name = cpblt.slice(pos); |
| 216 | |
| 217 | let end = name.search('\\?'); |
| 218 | if (end != -1) { |
| 219 | name = name.slice(0, end); |
| 220 | } |
| 221 | pos = name.lastIndexOf(':') |
| 222 | name = name.slice(0, pos); |
| 223 | } |
| 224 | return name; |
| 225 | } |
| 226 | |
| 227 | parseCapabilityRevision(cpblt: string): string { |
| 228 | let version = ""; |
| 229 | let pos = cpblt.search('revision='); |
| 230 | if (pos != -1) { |
| 231 | pos += 9; |
| 232 | version = cpblt.slice(pos); |
| 233 | let end = version.search('&'); |
| 234 | if (end != -1) { |
| 235 | version = version.slice(0, end); |
| 236 | } |
| 237 | return version; |
| 238 | } else if (cpblt.match('urn:ietf:params:netconf:*')) { |
| 239 | let end = cpblt.search('\\?'); |
| 240 | if (end != -1) { |
| 241 | cpblt = cpblt.slice(0, end); |
| 242 | } |
| 243 | pos = cpblt.lastIndexOf(':') |
| 244 | version = cpblt.slice(pos + 1); |
| 245 | } |
| 246 | return version; |
| 247 | } |
| 248 | |
Radek Krejci | 26bf2bc | 2018-01-09 15:00:54 +0100 | [diff] [blame] | 249 | cancelChanges() { |
Radek Krejci | 2ca11ba | 2018-01-26 11:25:42 +0100 | [diff] [blame] | 250 | //console.log(JSON.stringify(this.activeSession.modifications)) |
Radek Krejci | 9b41f5b | 2018-01-31 14:17:50 +0100 | [diff] [blame] | 251 | this.modsService.cancelModification(this.activeSession); |
Radek Krejci | fc908f3 | 2018-02-08 14:53:31 +0100 | [diff] [blame] | 252 | this.commit_error = []; |
Radek Krejci | 26bf2bc | 2018-01-09 15:00:54 +0100 | [diff] [blame] | 253 | this.sessionsService.storeData(); |
Radek Krejci | 2ca11ba | 2018-01-26 11:25:42 +0100 | [diff] [blame] | 254 | //console.log(JSON.stringify(this.activeSession.modifications)) |
Radek Krejci | 26bf2bc | 2018-01-09 15:00:54 +0100 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | applyChanges() { |
Radek Krejci | 62fec6a | 2018-02-06 10:24:09 +0100 | [diff] [blame] | 258 | this.modsService.applyModification(this.activeSession).then(result => { |
| 259 | if (result['success']) { |
| 260 | this.reloadData(); |
Radek Krejci | fc908f3 | 2018-02-08 14:53:31 +0100 | [diff] [blame] | 261 | this.commit_error = []; |
Radek Krejci | 62fec6a | 2018-02-06 10:24:09 +0100 | [diff] [blame] | 262 | } else { |
Radek Krejci | fc908f3 | 2018-02-08 14:53:31 +0100 | [diff] [blame] | 263 | this.commit_error = result['error']; |
Radek Krejci | 62fec6a | 2018-02-06 10:24:09 +0100 | [diff] [blame] | 264 | } |
| 265 | }) |
Radek Krejci | 26bf2bc | 2018-01-09 15:00:54 +0100 | [diff] [blame] | 266 | } |
Radek Krejci | 6e772b2 | 2018-01-25 13:28:57 +0100 | [diff] [blame] | 267 | |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 268 | ngOnInit(): void { |
| 269 | this.sessionsService.checkSessions(); |
Radek Krejci | 77f7720 | 2017-11-03 15:33:50 +0100 | [diff] [blame] | 270 | this.activeSession = this.sessionsService.getActiveSession(); |
Radek Krejci | 6be087d | 2018-02-14 08:53:20 +0100 | [diff] [blame] | 271 | if (this.activeSession && !this.activeSession.data) { |
Radek Krejci | 5a69fa3 | 2018-02-01 11:03:04 +0100 | [diff] [blame] | 272 | this.treeService.rpcGet(this.activeSession, false); |
| 273 | } |
Radek Krejci | ae75839 | 2017-10-20 10:53:26 +0200 | [diff] [blame] | 274 | } |
| 275 | |
Radek Krejci | b424acd | 2017-10-20 11:36:46 +0200 | [diff] [blame] | 276 | changeActiveSession(key: string) { |
Radek Krejci | 77f7720 | 2017-11-03 15:33:50 +0100 | [diff] [blame] | 277 | this.activeSession = this.sessionsService.changeActiveSession(key); |
Radek Krejci | 95bd14c | 2017-09-21 14:24:13 +0200 | [diff] [blame] | 278 | } |
Radek Krejci | d23f0df | 2017-08-31 16:34:49 +0200 | [diff] [blame] | 279 | } |