blob: 168ddac433b2067e05a9b1b81c3f6f0ab3d5249c [file] [log] [blame]
Radek Krejcid23f0df2017-08-31 16:34:49 +02001import { Injectable } from '@angular/core';
Radek Krejci6be087d2018-02-14 08:53:20 +01002import { Http, Headers, Response, RequestOptions, URLSearchParams } from '@angular/http';
Radek Krejcid23f0df2017-08-31 16:34:49 +02003import { Observable } from 'rxjs/Observable';
4import 'rxjs/add/operator/catch';
5import 'rxjs/add/operator/map';
6
Radek Krejci6be087d2018-02-14 08:53:20 +01007import { Schema } from '../inventory/schema';
Radek Krejcid23f0df2017-08-31 16:34:49 +02008
9@Injectable()
10export class SchemasService {
Radek Krejci5a1571a2018-02-16 13:45:53 +010011 public schemas;
Radek Krejci6be087d2018-02-14 08:53:20 +010012 public activeSchema: string;
Radek Krejcid23f0df2017-08-31 16:34:49 +020013
Radek Krejci6be087d2018-02-14 08:53:20 +010014 constructor( private http: Http ) {
15 this.loadData();
16 this.activeSchema = localStorage.getItem('activeSchema');
17 if (!this.activeSchema) {
18 this.activeSchema = "";
19 this.schemas = [];
20 }
21 }
Radek Krejcid23f0df2017-08-31 16:34:49 +020022
Radek Krejci6be087d2018-02-14 08:53:20 +010023 storeData() {
24 localStorage.setItem('schemas', JSON.stringify(this.schemas));
25 }
Radek Krejcic58f3472017-09-08 16:17:01 +020026
Radek Krejci6be087d2018-02-14 08:53:20 +010027 loadData() {
28 this.schemas = JSON.parse(localStorage.getItem('schemas'));
29 }
30
Radek Krejci5a1571a2018-02-16 13:45:53 +010031 schemasKeys() {
32 if (this.schemas) {
33 return Object.keys(this.schemas);
Radek Krejci6be087d2018-02-14 08:53:20 +010034 }
Radek Krejci6be087d2018-02-14 08:53:20 +010035 }
36
Radek Krejci5a1571a2018-02-16 13:45:53 +010037 getSchemaKey(schema: Schema) {
38 if (!schema) {
39 return null
40 } else if ('revision' in schema) {
41 return schema.name + '@' + schema.revision + '.yang';
42 } else {
43 return schema.name + '.yang';
44 }
45 }
46
47 getActiveSchema(key: string = this.activeSchema): Schema {
48 if (key in this.schemas) {
49 return this.schemas[key];
50 } else {
51 return null;
52 }
53 }
54
55 changeActiveSchemaKey(key: string): Schema {
56 if (key && (key in this.schemas)) {
Radek Krejci6be087d2018-02-14 08:53:20 +010057 this.activeSchema = key;
58 localStorage.setItem('activeSession', this.activeSchema);
59 }
Radek Krejci5a1571a2018-02-16 13:45:53 +010060 return this.schemas[key];
Radek Krejci6be087d2018-02-14 08:53:20 +010061 }
62
63 getSchemas() {
64 return this.http.get( '/netopeer/inventory/schemas' )
65 .map(( resp: Response ) => resp.json()).toPromise();
66 }
67
Radek Krejci5a1571a2018-02-16 13:45:53 +010068 show(key: string, schema: Schema) {
Radek Krejci6be087d2018-02-14 08:53:20 +010069 let newSchema = true;
Radek Krejci5a1571a2018-02-16 13:45:53 +010070
71 if (key in this.schemas) {
72 newSchema = false;
73 schema = this.schemas[key];
Radek Krejci6be087d2018-02-14 08:53:20 +010074 }
75
76 if (!('data' in schema)) {
77 let params = new URLSearchParams();
Radek Krejci5a1571a2018-02-16 13:45:53 +010078 params.set('key', key);
Radek Krejci6be087d2018-02-14 08:53:20 +010079 let options = new RequestOptions({ search: params });
80 this.http.get('/netopeer/inventory/schema', options)
81 .map((resp: Response) => resp.json()).toPromise().then(result => {
82 console.log(result)
83 if (result['success']) {
84 schema['data'] = result['data'];
85 this.storeData();
Radek Krejci6be087d2018-02-14 08:53:20 +010086 }
87 });
88 }
89
90 if (newSchema) {
Radek Krejci5a1571a2018-02-16 13:45:53 +010091 this.schemas[key] = schema;
Radek Krejci6be087d2018-02-14 08:53:20 +010092 this.storeData();
93 }
94 }
95
96 addSchema( schema: File ) {
97 let headers = new Headers( { 'specific-content-type': '' } );
98 let options = new RequestOptions( { headers: headers } );
99 let input = new FormData();
100 input.append( "schema", schema );
101 return this.http.post( '/netopeer/inventory/schemas', input, options )
102 .map(( resp: Response ) => resp.json() )
103 .catch(( err: Response | any ) => Observable.throw( err ) );
104 }
105
Radek Krejci5a1571a2018-02-16 13:45:53 +0100106 rmSchema(key: string) {
107 let options = new RequestOptions( { body: JSON.stringify(key) } );
Radek Krejci6be087d2018-02-14 08:53:20 +0100108 return this.http.delete( '/netopeer/inventory/schemas', options )
109 .map(( resp: Response ) => resp.json() )
110 .catch(( err: Response | any ) => Observable.throw( err ) );
111 }
Radek Krejcid23f0df2017-08-31 16:34:49 +0200112}