blob: c8def42233256ef02d05bde19843d2957771f1a7 [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 Scandolo9b460042017-04-14 16:24:45 -070019import {IXosModelDiscovererService} from '../../datasources/helpers/model-discoverer.service';
20import {IXosOnboarder} from '../../extender/services/onboard.service';
Matteo Scandolobd60dee2017-05-08 17:53:25 -070021import {IXosAuthService} from '../../datasources/rest/auth.rest';
Matteo Scandolo29edc0f2018-04-26 17:19:10 +020022import './loader.scss';
23
Matteo Scandolo9b460042017-04-14 16:24:45 -070024class LoaderCtrl {
25 static $inject = [
26 '$log',
27 '$rootScope',
28 '$location',
29 '$timeout',
Matteo Scandolobf4e8402019-06-24 12:22:49 -070030 '$interval',
Matteo Scandolobd60dee2017-05-08 17:53:25 -070031 '$state',
32 'AuthService',
Matteo Scandolo9b460042017-04-14 16:24:45 -070033 'XosConfig',
34 'XosModelDiscoverer',
35 `XosOnboarder`
36 ];
37
Matteo Scandolo29edc0f2018-04-26 17:19:10 +020038 public loader: boolean = true;
Matteo Scandolobf4e8402019-06-24 12:22:49 -070039 public message: string = 'Loading data...';
Matteo Scandolo29edc0f2018-04-26 17:19:10 +020040 public error: string;
41
Matteo Scandolobf4e8402019-06-24 12:22:49 -070042 private getMessageInterval: ng.IPromise<any>;
43
Matteo Scandolo9b460042017-04-14 16:24:45 -070044 constructor (
45 private $log: ng.ILogService,
46 private $rootScope: ng.IScope,
47 private $location: ng.ILocationService,
48 private $timeout: ng.ITimeoutService,
Matteo Scandolobf4e8402019-06-24 12:22:49 -070049 private $interval: ng.IIntervalService,
Matteo Scandolobd60dee2017-05-08 17:53:25 -070050 private $state: ng.ui.IStateService,
51 private XosAuthService: IXosAuthService,
Matteo Scandolo9b460042017-04-14 16:24:45 -070052 private XosConfig: any,
53 private XosModelDiscoverer: IXosModelDiscovererService,
54 private XosOnboarder: IXosOnboarder
55 ) {
Matteo Scandolobf4e8402019-06-24 12:22:49 -070056 this.getMessage();
Matteo Scandolo9b460042017-04-14 16:24:45 -070057 this.run();
58 }
59
60 public run() {
61 if (this.XosModelDiscoverer.areModelsLoaded()) {
Matteo Scandolo29edc0f2018-04-26 17:19:10 +020062 this.$log.debug(`[XosLoader] Models are already loaded, moving to: ${this.XosConfig.lastVisitedUrl}`);
Matteo Scandolo9b460042017-04-14 16:24:45 -070063 this.moveOnTo(this.XosConfig.lastVisitedUrl);
64 }
Matteo Scandolobd60dee2017-05-08 17:53:25 -070065 else if (!this.XosAuthService.isAuthenticated()) {
Matteo Scandolo29edc0f2018-04-26 17:19:10 +020066 this.$log.debug(`[XosLoader] Not authenticated, send to login`);
Matteo Scandolobd60dee2017-05-08 17:53:25 -070067 this.$state.go('xos.login');
68 }
Matteo Scandolo9b460042017-04-14 16:24:45 -070069 else {
Matteo Scandolo9b460042017-04-14 16:24:45 -070070 // NOTE loading XOS Models
Matteo Scandolo0f3692e2017-07-10 14:06:41 -070071 this.XosModelDiscoverer.discover()
Matteo Scandolo9b460042017-04-14 16:24:45 -070072 .then((res) => {
Matteo Scandolo29edc0f2018-04-26 17:19:10 +020073 this.$log.info('[XosLoader] res: ' + res, res, typeof res);
74 if (res === 'chameleon') {
75 this.loader = false;
76 this.error = 'chameleon';
77 return 'chameleon';
78 }
79 else if (res) {
Matteo Scandolo9b460042017-04-14 16:24:45 -070080 this.$log.info('[XosLoader] All models loaded');
Matteo Scandolo29edc0f2018-04-26 17:19:10 +020081 // NOTE loading GUI Extensions
82 this.XosOnboarder.onboard();
83 return true;
Matteo Scandolo9b460042017-04-14 16:24:45 -070084 }
85 else {
86 this.$log.info('[XosLoader] Failed to load some models, moving on.');
Matteo Scandolo29edc0f2018-04-26 17:19:10 +020087 return true;
Matteo Scandolo9b460042017-04-14 16:24:45 -070088 }
Matteo Scandolo9b460042017-04-14 16:24:45 -070089 })
Matteo Scandolo29edc0f2018-04-26 17:19:10 +020090 .then((res) => {
91 if (res === true) {
92 this.moveOnTo(this.XosConfig.lastVisitedUrl);
93 }
94 // NOTE otherwise stay here since we're printing some error messages
95 })
96 .catch(() => {
97 // XosModelDiscoverer.discover reject only in case of authentication error
98 this.XosAuthService.clearUser();
99 this.moveOnTo('/login');
100
Matteo Scandolo9b460042017-04-14 16:24:45 -0700101 })
102 .finally(() => {
103 // NOTE it is in a timeout as the searchService is loaded after that
Matteo Scandolo9b460042017-04-14 16:24:45 -0700104 this.$timeout(() => {
105 this.$rootScope.$emit('xos.core.modelSetup');
106 }, 500);
107 });
108 }
109 }
110
111 public moveOnTo(url: string) {
112 this.$log.info(`[XosLoader] Redirecting to: ${url}`);
113 switch (url) {
114 case '':
115 case '/':
116 case '/loader':
117 case '/login':
118 this.$location.path('/dashboard');
119 break;
120 default:
121 this.$timeout(() => {
122 this.$location.path(url);
123 }, 500);
124 break;
125 }
126 }
Matteo Scandolobf4e8402019-06-24 12:22:49 -0700127
128 /**
129 * This method query the model-discoverer service to have the status of the loading
130 */
131 public getMessage() {
132 this.getMessageInterval = this.$interval(() => {
133 this.message = this.XosModelDiscoverer.getStatusMessage();
134 }, 1000);
135 this.message = this.XosModelDiscoverer.getStatusMessage();
136 }
137
138 $onDestroy() {
139 this.$interval.cancel(this.getMessageInterval);
140 }
Matteo Scandolo9b460042017-04-14 16:24:45 -0700141}
142
143export const xosLoader: angular.IComponentOptions = {
144 template: `
Matteo Scandolobf4e8402019-06-24 12:22:49 -0700145 <div class="loader-container">
146 <div ng-show="vm.loader" class="loader"></div>
147 </div>
148 <div class="row">
149 <div class="col-sm-6 col-sm-offset-3">
150 <div class="alert alert-accent">
151 {{ vm.message }}
152 </div>
153 </div>
154 </div>
Matteo Scandolo29edc0f2018-04-26 17:19:10 +0200155 <div class="row" ng-show="vm.error == 'chameleon'">
156 <div class="col-sm-6 col-sm-offset-3">
157 <div class="alert alert-danger">
158 <div class="row">
159 <div class="col-xs-2">
160 <i class="fa fa-exclamation-triangle"></i>
161 </div>
162 <div class="col-xs-10">
163 <strong>Cannot load models definition.</strong><br>
164 Please check that the Chameleon container is running
165 </div>
166 </div>
167 </div>
168 </div>
169 </div>
Matteo Scandolo9b460042017-04-14 16:24:45 -0700170 `,
171 controllerAs: 'vm',
172 controller: LoaderCtrl
173};