[CORD-2827] Fixed unauthorized error handling

Change-Id: I6ddef7f869c17db4d8479f23f6e8734f6002d8fc
diff --git a/src/app/core/loader/loader.scss b/src/app/core/loader/loader.scss
new file mode 100644
index 0000000..0f7021a
--- /dev/null
+++ b/src/app/core/loader/loader.scss
@@ -0,0 +1,25 @@
+/*
+ * Copyright 2017-present Open Networking Foundation
+
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+
+ * http://www.apache.org/licenses/LICENSE-2.0
+
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+xos-loader {
+  .alert.alert-danger {
+    margin-top: 100px;
+
+    .fa.fa-exclamation-triangle {
+      font-size: 40px;
+    }
+  }
+}
\ No newline at end of file
diff --git a/src/app/core/loader/loader.spec.ts b/src/app/core/loader/loader.spec.ts
index 44fc60f..0744c11 100644
--- a/src/app/core/loader/loader.spec.ts
+++ b/src/app/core/loader/loader.spec.ts
@@ -77,86 +77,116 @@
     timeout = $timeout;
     location = $location;
     spyOn(location, 'path');
-
-    MockDiscover.discover = jasmine.createSpy('discover')
-      .and.callFake(() => {
-        const d = $q.defer();
-        d.resolve(true);
-        return d.promise;
-      });
-
-    MockOnboarder.onboard = jasmine.createSpy('onboard')
-      .and.callFake(() => {
-        const d = $q.defer();
-        d.resolve();
-        return d.promise;
-      });
   }));
 
-  describe('when models are already loaded', () => {
-
-    beforeEach(() => {
-      compileElement();
-      spyOn(isolatedScope, 'moveOnTo');
-      isolatedScope.run();
-      timeout.flush();
-    });
-
-    it('should redirect to the last visited page', (done) => {
-      window.setTimeout(() => {
-        expect(isolatedScope.moveOnTo).toHaveBeenCalledWith('/test');
-        expect(location.path).toHaveBeenCalledWith('/test');
-        done();
-      }, 600);
-    });
-  });
-
-  describe('when the last visited page is "loader"', () => {
-
-    beforeEach(() => {
-      MockConfig.lastVisitedUrl = '/loader';
-      compileElement();
-      spyOn(isolatedScope, 'moveOnTo');
-      isolatedScope.run();
-    });
-
-    it('should redirect to the "dashboard" page', (done) => {
-      window.setTimeout(() => {
-        expect(isolatedScope.moveOnTo).toHaveBeenCalledWith('/loader');
-        expect(location.path).toHaveBeenCalledWith('/dashboard');
-        done();
-      }, 600);
-    });
-  });
-
-  describe('when user is not authenticated', () => {
-
-    beforeEach(() => {
+  describe('when chameleon is not responding', () => {
+    beforeEach(inject(($q: ng.IQService) => {
       loaded = false;
-      authenticated = false;
-      compileElement();
-      isolatedScope.run();
-    });
-
-    it('should redirect to the login page', () => {
-      expect(MockState.go).toHaveBeenCalledWith('xos.login');
-    });
-
-    afterEach(() => {
       authenticated = true;
+      MockDiscover.discover = jasmine.createSpy('discover')
+        .and.callFake(() => {
+          const d = $q.defer();
+          d.resolve('chameleon');
+          return d.promise;
+        });
+      compileElement();
+      spyOn(isolatedScope, 'moveOnTo');
+      isolatedScope.run();
+    }));
+
+    it('should print an error', () => {
+      expect(isolatedScope.moveOnTo).not.toHaveBeenCalled();
+      expect(isolatedScope.error).toBe('chameleon');
+      expect(isolatedScope.loader).toBeFalsy();
     });
   });
 
-  describe('when models are not loaded', () => {
+  describe('when chameleon is available', () => {
 
-    beforeEach(() => {
-      loaded = false;
-      compileElement();
-      spyOn(isolatedScope, 'moveOnTo');
+    beforeEach(inject(($q: ng.IQService) => {
+      loaded = true;
+      authenticated = true;
+      MockDiscover.discover = jasmine.createSpy('discover')
+        .and.callFake(() => {
+          const d = $q.defer();
+          d.resolve(true);
+          return d.promise;
+        });
+
+      MockOnboarder.onboard = jasmine.createSpy('onboard')
+        .and.callFake(() => {
+          const d = $q.defer();
+          d.resolve();
+          return d.promise;
+        });
+    }));
+
+    describe('when models are already loaded', () => {
+
+      beforeEach(() => {
+        compileElement();
+        spyOn(isolatedScope, 'moveOnTo');
+        isolatedScope.run();
+        timeout.flush();
+      });
+
+      it('should redirect to the last visited page', (done) => {
+        window.setTimeout(() => {
+          expect(isolatedScope.moveOnTo).toHaveBeenCalledWith('/test');
+          expect(location.path).toHaveBeenCalledWith('/test');
+          done();
+        }, 600);
+      });
     });
 
-    it('should call XosModelDiscoverer.discover', () => {
-      expect(MockDiscover.discover).toHaveBeenCalled();
+    describe('when the last visited page is "loader"', () => {
+
+      beforeEach(() => {
+        MockConfig.lastVisitedUrl = '/loader';
+        compileElement();
+        spyOn(isolatedScope, 'moveOnTo');
+        isolatedScope.run();
+      });
+
+      it('should redirect to the "dashboard" page', (done) => {
+        window.setTimeout(() => {
+          expect(isolatedScope.moveOnTo).toHaveBeenCalledWith('/loader');
+          expect(location.path).toHaveBeenCalledWith('/dashboard');
+          done();
+        }, 600);
+      });
+    });
+
+    describe('when user is not authenticated', () => {
+
+      beforeEach(() => {
+        loaded = false;
+        authenticated = false;
+        compileElement();
+        isolatedScope.run();
+      });
+
+      it('should redirect to the login page', () => {
+        expect(MockState.go).toHaveBeenCalledWith('xos.login');
+      });
+
+      afterEach(() => {
+        authenticated = true;
+      });
+    });
+
+    describe('when models are not loaded', () => {
+
+      beforeEach(() => {
+        loaded = false;
+        authenticated = true;
+        compileElement();
+        spyOn(isolatedScope, 'moveOnTo');
+      });
+
+      it('should call XosModelDiscoverer.discover', () => {
+        expect(MockDiscover.discover).toHaveBeenCalled();
+      });
     });
   });
 
diff --git a/src/app/core/loader/loader.ts b/src/app/core/loader/loader.ts
index b93aebf..3606ca8 100644
--- a/src/app/core/loader/loader.ts
+++ b/src/app/core/loader/loader.ts
@@ -19,6 +19,8 @@
 import {IXosModelDiscovererService} from '../../datasources/helpers/model-discoverer.service';
 import {IXosOnboarder} from '../../extender/services/onboard.service';
 import {IXosAuthService} from '../../datasources/rest/auth.rest';
+import './loader.scss';
+
 class LoaderCtrl {
   static $inject = [
     '$log',
@@ -32,6 +34,9 @@
     `XosOnboarder`
   ];
 
+  public loader: boolean = true;
+  public error: string;
+
   constructor (
     private $log: ng.ILogService,
     private $rootScope: ng.IScope,
@@ -49,26 +54,45 @@
 
   public run() {
     if (this.XosModelDiscoverer.areModelsLoaded()) {
+      this.$log.debug(`[XosLoader] Models are already loaded, moving to: ${this.XosConfig.lastVisitedUrl}`);
       this.moveOnTo(this.XosConfig.lastVisitedUrl);
     }
     else if (!this.XosAuthService.isAuthenticated()) {
+      this.$log.debug(`[XosLoader] Not authenticated, send to login`);
       this.$state.go('xos.login');
     }
     else {
       // NOTE loading XOS Models
       this.XosModelDiscoverer.discover()
         .then((res) => {
-          if (res) {
+          this.$log.info('[XosLoader] res: ' + res, res, typeof res);
+          if (res === 'chameleon') {
+            this.loader = false;
+            this.error = 'chameleon';
+            return 'chameleon';
+          }
+          else if (res) {
             this.$log.info('[XosLoader] All models loaded');
+            // NOTE loading GUI Extensions
+            this.XosOnboarder.onboard();
+            return true;
           }
           else {
             this.$log.info('[XosLoader] Failed to load some models, moving on.');
+            return true;
           }
-          // NOTE loading GUI Extensions
-          return this.XosOnboarder.onboard();
         })
-        .then(() => {
-          this.moveOnTo(this.XosConfig.lastVisitedUrl);
+        .then((res) => {
+          if (res === true) {
+            this.moveOnTo(this.XosConfig.lastVisitedUrl);
+          }
+          // NOTE otherwise stay here since we're printing some error messages
+        })
+        .catch(() => {
+          // XosModelDiscoverer.discover reject only in case of authentication error
+          this.XosAuthService.clearUser();
+          this.moveOnTo('/login');
+
         })
         .finally(() => {
           // NOTE it is in a timeout as the searchService is loaded after that
@@ -99,7 +123,22 @@
 
 export const xosLoader: angular.IComponentOptions = {
   template: `
-    <div class="loader"></div>
+    <div ng-show="vm.loader" class="loader"></div>
+    <div class="row" ng-show="vm.error == 'chameleon'">
+      <div class="col-sm-6 col-sm-offset-3">
+        <div class="alert alert-danger">
+          <div class="row">
+            <div class="col-xs-2">
+              <i class="fa fa-exclamation-triangle"></i>
+            </div>
+            <div class="col-xs-10">
+              <strong>Cannot load models definition.</strong><br>
+              Please check that the Chameleon container is running
+            </div>
+          </div>
+        </div>
+      </div>
+    </div>
   `,
   controllerAs: 'vm',
   controller: LoaderCtrl