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