Fix issue in ui load
[sdc.git] / catalog-ui / src / app / ng2 / services / config.service.ts
1 /*
2  * Copyright (c) 2018 AT&T Intellectual Property.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  *
17  */
18
19 import { Injectable, Inject } from '@angular/core';
20 import { Http, Response } from '@angular/http';
21 import 'rxjs/add/operator/toPromise';
22 import {IAppConfigurtaion, ValidationConfiguration, Validations, Plugins, PluginsConfiguration} from "app/models";
23 import {IApi} from "app/models/app-config";
24 import {SdcConfigToken, ISdcConfig} from "../config/sdc-config.config";
25
26 @Injectable()
27 export class ConfigService {
28
29     private baseUrl;
30     public configuration: IAppConfigurtaion;
31     public api:IApi;
32
33     constructor(private http: Http, @Inject(SdcConfigToken) private sdcConfig:ISdcConfig) {
34         this.api = this.sdcConfig.api;
35         this.baseUrl = this.api.root + this.sdcConfig.api.component_api_root;
36     }
37
38     loadValidationConfiguration(): Promise<ValidationConfiguration> {
39         let url: string = this.sdcConfig.validationConfigPath;
40         let promise: Promise<ValidationConfiguration> = this.http.get(url).map((res: Response) => res.json()).toPromise();
41         promise.then((validationData: Validations) => {
42             ValidationConfiguration.validation = validationData;
43         }).catch((ex) => {
44             console.error('Error loading validation.json configuration file, using fallback data', ex);
45
46             let fallback:Validations = {
47                 "propertyValue": {
48                     "max": 2500,
49                     "min": 0
50                 },
51
52                 "validationPatterns": {
53                     "string": "^[\\sa-zA-Z0-9+-]+$",
54                     "comment": "^[\\sa-zA-Z0-9+-_\\{\\}\"]+$",
55                     "integer": "^(([-+]?\\d+)|([-+]?0x[0-9a-fA-F]+))$"
56                 }
57             };
58
59             ValidationConfiguration.validation = fallback;
60
61         });
62
63         return promise;
64     }
65
66     loadPluginsConfiguration(): Promise<PluginsConfiguration> {
67         let url:string = this.api.no_proxy_root + this.api.GET_plugins_configuration;
68         let promise: Promise<any> = this.http.get(url).map((res: Response) => res.json()).toPromise();
69         return new Promise<PluginsConfiguration>((resolve) => {
70             promise.then((pluginsData: Plugins) => {
71                 PluginsConfiguration.plugins = pluginsData;
72                 resolve();
73             }).catch((ex) => {
74                 console.error("Error loading plugins configuration from FE", ex);
75
76                 PluginsConfiguration.plugins = [] as Plugins;
77                 resolve();
78             });
79         });
80     }
81
82 }