Matteo Scandolo | fb46ae6 | 2017-08-08 09:10:50 -0700 | [diff] [blame] | 1 | |
| 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 Scandolo | 11b4a63 | 2017-02-09 10:28:41 -0800 | [diff] [blame] | 19 | import {IWSEventService} from '../../datasources/websocket/global'; |
Matteo Scandolo | 47860fe | 2017-02-02 12:05:55 -0800 | [diff] [blame] | 20 | import {IXosModelStoreService} from '../../datasources/stores/model.store'; |
| 21 | import * as _ from 'lodash'; |
| 22 | import {Observable} from 'rxjs'; |
Matteo Scandolo | 4e87023 | 2017-01-30 13:43:05 -0800 | [diff] [blame] | 23 | |
| 24 | export interface IXosOnboarder { |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 25 | onboard(): ng.IPromise<boolean>; |
Matteo Scandolo | 4e87023 | 2017-01-30 13:43:05 -0800 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | export class XosOnboarder implements IXosOnboarder { |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 29 | static $inject = [ |
| 30 | '$timeout', |
| 31 | '$log', |
| 32 | '$q', |
| 33 | 'WebSocket', |
| 34 | '$ocLazyLoad', |
| 35 | 'XosModelStore' |
| 36 | ]; |
Matteo Scandolo | 4e87023 | 2017-01-30 13:43:05 -0800 | [diff] [blame] | 37 | |
| 38 | constructor( |
| 39 | private $timeout: ng.ITimeoutService, |
| 40 | private $log: ng.ILogService, |
| 41 | private $q: ng.IQService, |
| 42 | private webSocket: IWSEventService, |
Matteo Scandolo | 47860fe | 2017-02-02 12:05:55 -0800 | [diff] [blame] | 43 | private $ocLazyLoad: any, // TODO add definition |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 44 | private XosModelStore: IXosModelStoreService |
Matteo Scandolo | 4e87023 | 2017-01-30 13:43:05 -0800 | [diff] [blame] | 45 | ) { |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 46 | |
| 47 | } |
| 48 | |
| 49 | public onboard(): ng.IPromise<boolean> { |
| 50 | const d = this.$q.defer(); |
| 51 | |
Matteo Scandolo | 4e87023 | 2017-01-30 13:43:05 -0800 | [diff] [blame] | 52 | this.$log.info('[XosOnboarder] Setup'); |
Matteo Scandolo | 47860fe | 2017-02-02 12:05:55 -0800 | [diff] [blame] | 53 | |
Matteo Scandolo | 11b4a63 | 2017-02-09 10:28:41 -0800 | [diff] [blame] | 54 | // Load onboarded app |
| 55 | const ComponentObservable: Observable<any> = this.XosModelStore.query('XOSGuiExtension'); |
Matteo Scandolo | 47860fe | 2017-02-02 12:05:55 -0800 | [diff] [blame] | 56 | |
Matteo Scandolo | 47860fe | 2017-02-02 12:05:55 -0800 | [diff] [blame] | 57 | ComponentObservable.subscribe( |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 58 | (component) => { |
| 59 | if (component.length === 0) { |
| 60 | return d.resolve(); |
Matteo Scandolo | 4e87023 | 2017-01-30 13:43:05 -0800 | [diff] [blame] | 61 | } |
Matteo Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 62 | _.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 Scandolo | 4e87023 | 2017-01-30 13:43:05 -0800 | [diff] [blame] | 77 | } |
| 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 Scandolo | 9b46004 | 2017-04-14 16:24:45 -0700 | [diff] [blame] | 85 | |
Matteo Scandolo | 4e87023 | 2017-01-30 13:43:05 -0800 | [diff] [blame] | 86 | 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 | } |