Migration to AGL gerrit (update go import)
[src/xds/xds-agent.git] / webapp / src / app / @core-xds / services / sdk.service.ts
1 /**
2 * @license
3 * Copyright (C) 2017-2018 "IoT.bzh"
4 * Author Sebastien Douheret <sebastien@iot.bzh>
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 *   http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 import { Injectable, SecurityContext, isDevMode } from '@angular/core';
20 import { Observable } from 'rxjs/Observable';
21 import { BehaviorSubject } from 'rxjs/BehaviorSubject';
22
23 import { XDSAgentService } from '../services/xdsagent.service';
24
25 import 'rxjs/add/observable/throw';
26
27 /* FIXME: syntax only compatible with TS>2.4.0
28 export enum StatusType {
29   DISABLE = 'Disable',
30   NOT_INSTALLED = 'Not Installed',
31   INSTALLING = 'Installing',
32   UNINSTALLING = 'Un-installing',
33   INSTALLED = 'Installed'
34 }
35 */
36 export type StatusTypeEnum = 'Disable' | 'Not Installed' | 'Installing' | 'Un-installing' | 'Installed';
37 export const StatusType = {
38   DISABLE: 'Disable',
39   NOT_INSTALLED: 'Not Installed',
40   INSTALLING: 'Installing',
41   UNINSTALLING: 'Un-installing',
42   INSTALLED: 'Installed',
43 };
44
45 export interface ISdk {
46   id: string;
47   name: string;
48   description: string;
49   profile: string;
50   version: string;
51   arch: string;
52   path: string;
53   url: string;
54   status: string;
55   date: string;
56   size: string;
57   md5sum: string;
58   setupFile: string;
59   lastError: string;
60 }
61
62 export interface ISdkManagementMsg {
63   cmdID: string;
64   timestamp: string;
65   sdk: ISdk;
66   stdout: string;
67   stderr: string;
68   progress: number;
69   exited: boolean;
70   code: number;
71   error: string;
72 }
73
74 @Injectable()
75 export class SdkService {
76   public Sdks$: Observable<ISdk[]>;
77   public curSdk$: Observable<ISdk>;
78
79   private _sdksList = [];
80   private sdksSubject = <BehaviorSubject<ISdk[]>>new BehaviorSubject(this._sdksList);
81   private current: ISdk;
82   private curSdkSubject = <BehaviorSubject<ISdk>>new BehaviorSubject(this.current);
83   private curServerID;
84
85   constructor(private xdsSvr: XDSAgentService) {
86     this.current = null;
87     this.Sdks$ = this.sdksSubject.asObservable();
88     this.curSdk$ = this.curSdkSubject.asObservable();
89
90     this.xdsSvr.XdsConfig$.subscribe(cfg => {
91       if (!cfg || cfg.servers.length < 1) {
92         return;
93       }
94       // FIXME support multiple server
95       // cfg.servers.forEach(svr => {
96       this.curServerID = cfg.servers[0].id;
97       this.xdsSvr.getSdks(this.curServerID).subscribe((sdks) => {
98         this._sdksList = [];
99         sdks.forEach(s => {
100           this._addSdk(s, true);
101         });
102
103         // TODO: get previous val from xds-config service / cookie
104         if (this._sdksList.length > 0) {
105           this.current = this._sdksList[0];
106           this.curSdkSubject.next(this.current);
107         }
108
109         this.sdksSubject.next(this._sdksList);
110       });
111     });
112
113     // Add listener on sdk creation, deletion and change events
114     this.xdsSvr.onSdkInstall().subscribe(evMgt => {
115       this._addSdk(evMgt.sdk);
116     });
117     this.xdsSvr.onSdkRemove().subscribe(evMgt => {
118       if (evMgt.sdk.status !== StatusType.NOT_INSTALLED) {
119         /* tslint:disable:no-console */
120         console.log('Error: received event:sdk-remove with invalid status: evMgt=', evMgt);
121         return;
122       }
123       this._delSdk(evMgt.sdk);
124     });
125
126   }
127
128   public setCurrent(s: ISdk) {
129     this.current = s;
130   }
131
132   public getCurrent(): ISdk {
133     return this.current;
134   }
135
136   public getCurrentId(): string {
137     if (this.current && this.current.id) {
138       return this.current.id;
139     }
140     return '';
141   }
142
143   public install(sdk: ISdk): Observable<ISdk> {
144     return this.xdsSvr.installSdk(this.curServerID, sdk.id);
145   }
146
147   public onInstall(): Observable<ISdkManagementMsg> {
148     return this.xdsSvr.onSdkInstall();
149   }
150
151   public abortInstall(sdk: ISdk): Observable<ISdk> {
152     return this.xdsSvr.abortInstall(this.curServerID, sdk.id);
153   }
154
155   public remove(sdk: ISdk): Observable<ISdk> {
156     return this.xdsSvr.removeSdk(this.curServerID, sdk.id);
157   }
158
159   /** Private **/
160
161   private _addSdk(sdk: ISdk, noNext?: boolean): ISdk {
162
163     // check if sdk already exists
164     const idx = this._sdksList.findIndex(s => s.id === sdk.id);
165     if (idx >= 0) {
166       this._sdksList[idx] = sdk;
167     } else {
168       // add new sdk
169       this._sdksList.push(sdk);
170     }
171
172     // sort sdk array
173     this._sdksList.sort((a, b) => {
174       if (a.name < b.name) {
175         return -1;
176       }
177       if (a.name > b.name) {
178         return 1;
179       }
180       return 0;
181     });
182
183     if (!noNext) {
184       this.sdksSubject.next(this._sdksList);
185     }
186
187     return sdk;
188   }
189
190   private _delSdk(sdk: ISdk) {
191     const idx = this._sdksList.findIndex(item => item.id === sdk.id);
192     if (idx === -1) {
193       if (isDevMode) {
194         /* tslint:disable:no-console */
195         console.log('Warning: Try to delete sdk unknown id: sdk=', sdk);
196       }
197       return;
198     }
199     const delId = this._sdksList[idx].id;
200     this._sdksList.splice(idx, 1);
201     if (delId === this.current.id) {
202       this.setCurrent(this._sdksList[0]);
203     }
204     this.sdksSubject.next(this._sdksList);
205   }
206
207 }