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