blob: 03868c1eecb2ab529e3bb7d3249448c88ed95486 [file] [log] [blame]
Matteo Scandolofb46ae62017-08-08 09:10:50 -07001
2/*
3 * Copyright 2017-present Open Networking Foundation
4
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8
9 * http://www.apache.org/licenses/LICENSE-2.0
10
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
Matteo Scandolo11b4a632017-02-09 10:28:41 -080019import {IWSEventService} from '../../datasources/websocket/global';
Matteo Scandolo47860fe2017-02-02 12:05:55 -080020import {IXosModelStoreService} from '../../datasources/stores/model.store';
21import * as _ from 'lodash';
22import {Observable} from 'rxjs';
Matteo Scandolo4e870232017-01-30 13:43:05 -080023
24export interface IXosOnboarder {
Matteo Scandolo9b460042017-04-14 16:24:45 -070025 onboard(): ng.IPromise<boolean>;
Matteo Scandolo4e870232017-01-30 13:43:05 -080026}
27
28export class XosOnboarder implements IXosOnboarder {
Matteo Scandolo9b460042017-04-14 16:24:45 -070029 static $inject = [
30 '$timeout',
31 '$log',
32 '$q',
33 'WebSocket',
34 '$ocLazyLoad',
35 'XosModelStore'
36 ];
Matteo Scandolo4e870232017-01-30 13:43:05 -080037
38 constructor(
39 private $timeout: ng.ITimeoutService,
40 private $log: ng.ILogService,
41 private $q: ng.IQService,
42 private webSocket: IWSEventService,
Matteo Scandolo47860fe2017-02-02 12:05:55 -080043 private $ocLazyLoad: any, // TODO add definition
Matteo Scandolo1aee1982017-02-17 08:33:23 -080044 private XosModelStore: IXosModelStoreService
Matteo Scandolo4e870232017-01-30 13:43:05 -080045 ) {
Matteo Scandolo9b460042017-04-14 16:24:45 -070046
47 }
48
49 public onboard(): ng.IPromise<boolean> {
50 const d = this.$q.defer();
51
Matteo Scandolo4e870232017-01-30 13:43:05 -080052 this.$log.info('[XosOnboarder] Setup');
Matteo Scandolo47860fe2017-02-02 12:05:55 -080053
Matteo Scandolo11b4a632017-02-09 10:28:41 -080054 // Load onboarded app
55 const ComponentObservable: Observable<any> = this.XosModelStore.query('XOSGuiExtension');
Matteo Scandolo47860fe2017-02-02 12:05:55 -080056
Matteo Scandolo47860fe2017-02-02 12:05:55 -080057 ComponentObservable.subscribe(
Matteo Scandolo9b460042017-04-14 16:24:45 -070058 (component) => {
59 if (component.length === 0) {
60 return d.resolve();
Matteo Scandolo4e870232017-01-30 13:43:05 -080061 }
Matteo Scandolo9b460042017-04-14 16:24:45 -070062 _.forEach(component, (c) => {
63 this.$log.info(`[XosOnboarder] Loading files for app: ${c.name}`);
64 const files = c.files.split(',').map(s => s.trim());
65 this.loadFile(files)
66 .then((res) => {
67 this.$log.info(`[XosOnboarder] All files loaded for app: ${c.name}`);
68 d.resolve();
69 })
70 .catch(e => {
71 this.$log.info(`[XosOnboarder] Error while onboarding apps: `, e);
72 });
73 });
74 }
75 );
76 return d.promise;
Matteo Scandolo4e870232017-01-30 13:43:05 -080077 }
78
79 // NOTE files needs to be loaded in order, so async loop!
80 private loadFile(files: string[], d?: ng.IDeferred<any>): ng.IPromise<string[]> {
81 if (!angular.isDefined(d)) {
82 d = this.$q.defer();
83 }
84 const file = files.shift();
Matteo Scandolo9b460042017-04-14 16:24:45 -070085
Matteo Scandolo4e870232017-01-30 13:43:05 -080086 this.$log.info(`[XosOnboarder] Loading file: ${file}`);
87 this.$ocLazyLoad.load(file)
88 .then((res) => {
89 this.$log.info(`[XosOnboarder] Loaded file: `, file);
90 if (files.length > 0) {
91 return this.loadFile(files, d);
92 }
93 return d.resolve(file);
94 })
95 .catch((err) => {
96 this.$log.error(`[XosOnboarder] Failed to load file: `, err);
97 d.reject(err);
98 });
99
100 return d.promise;
101 }
102}