Catalog alignment
[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 { HttpClient } from '@angular/common/http';
20 import { Inject, Injectable, Injector } from '@angular/core';
21 import { IAppConfigurtaion, Plugins, PluginsConfiguration, ValidationConfiguration, Validations } from 'app/models';
22 import { IApi } from 'app/models/app-config';
23 import 'rxjs/add/operator/toPromise';
24 import { ISdcConfig, SdcConfigToken } from '../config/sdc-config.config';
25 import { CacheService } from './cache.service';
26
27 @Injectable()
28 export class ConfigService {
29
30     public configuration: IAppConfigurtaion;
31     public api: IApi;
32     private baseUrl;
33
34     constructor(
35                 @Inject(SdcConfigToken) private sdcConfig: ISdcConfig,
36                 private cacheService: CacheService,
37                 private injector: Injector,
38                 private http: HttpClient
39         ) {
40             this.api = this.sdcConfig.api;
41             this.baseUrl = this.api.root + this.sdcConfig.api.component_api_root;
42     }
43
44     loadSdcSetupData = (): Promise<void> =>  {
45         const url: string = this.api.root + this.api.GET_SDC_Setup_Data;
46         const promise: Promise<any> = this.http.get<any>(url).toPromise();
47         promise.then((response) => {
48             this.cacheService.set('version', response.version);
49             this.cacheService.set('serviceCategories', response.categories.serviceCategories);
50             this.cacheService.set('resourceCategories', response.categories.resourceCategories);
51             this.cacheService.set('UIConfiguration', response.configuration);
52         });
53         return promise;
54     }
55
56     loadValidationConfiguration(): Promise<ValidationConfiguration> {
57         const url: string = this.sdcConfig.validationConfigPath;
58         const promise: Promise<ValidationConfiguration> = this.http.get<ValidationConfiguration>(url).toPromise();
59         promise.then((validationData: Validations) => {
60             ValidationConfiguration.validation = validationData;
61             this.cacheService.set('validation', validationData);
62         }).catch((ex) => {
63             console.error('Error loading validation.json configuration file, using fallback data', ex);
64
65             const fallback = {
66                 propertyValue: {
67                     max: 2500,
68                     min: 0
69                 },
70                 validationPatterns: {
71                     string: '^[\\sa-zA-Z0-9+-]+$',
72                     stringOrEmpty: '^[\\sa-zA-Z0-9&-]*$',
73                     comment: '^[\\sa-zA-Z0-9+-_\\{\\}"]+$',
74                     integer: '^(([-+]?\\d+)|([-+]?0x[0-9a-fA-F]+))$'
75                 }
76             };
77
78             this.cacheService.set('validation', fallback);
79         });
80
81         return promise;
82     }
83
84     loadPluginsConfiguration = (): Promise<PluginsConfiguration> =>  {
85         const url: string = this.api.no_proxy_root + this.api.GET_plugins_configuration;
86         const promise: Promise<any> = this.http.get<PluginsConfiguration>(url).toPromise();
87         return new Promise<PluginsConfiguration>((resolve) => {
88             promise.then((pluginsData: Plugins) => {
89                 PluginsConfiguration.plugins = pluginsData;
90                 resolve();
91             }).catch((ex) => {
92                 console.error('Error loading plugins configuration from FE', ex);
93
94                 PluginsConfiguration.plugins = [] as Plugins;
95                 resolve();
96             });
97         });
98     }
99
100 }