Catalog alignment
[sdc.git] / catalog-ui / src / app / ng2 / services / catalog.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 import { Injectable, Inject } from "@angular/core";
22 import { Observable } from "rxjs/Observable";
23 import { SdcConfigToken, ISdcConfig } from "../config/sdc-config.config";
24 import { Component, IApi, IComponentsArray } from "app/models";
25 import { ComponentFactory } from 'app/utils/component-factory';
26 import {ResourceType} from "../../utils/constants";
27 import {SharingService} from "./sharing.service";
28 import { HttpClient, HttpParams } from "@angular/common/http";
29
30 @Injectable()
31 export class CatalogService {
32     protected api:IApi;
33     protected baseUrl:string;
34     protected baseMicroServiceUrl:string;
35
36     constructor(private http: HttpClient,
37                 @Inject(SdcConfigToken) sdcConfig:ISdcConfig,
38                 private componentFactory:ComponentFactory,
39                 private sharingService:SharingService) {
40         this.api = sdcConfig.api;
41         this.baseUrl = sdcConfig.api.root ;
42         this.baseMicroServiceUrl = sdcConfig.api.uicache_root;
43     }
44
45     public getCatalog(): Observable<Array<Component>> {
46         let searchParams = new HttpParams();
47         searchParams = searchParams.append('excludeTypes', ResourceType.VFCMT).append('excludeTypes', ResourceType.CONFIGURATION);
48         return this.http.get<IComponentsArray>(this.baseMicroServiceUrl + this.api.GET_uicache_catalog, {params: searchParams})
49             .map(res => this.processComponentsResponse(res, true));
50     }
51
52     public getArchiveCatalog() {
53         return this.http.get<IComponentsArray>(this.baseUrl + '/v1/catalog/archive/', {})
54             .map(res => this.processComponentsResponse(res));
55     }
56
57     private processComponentsResponse(componentsArr: IComponentsArray, addSharing:boolean = false) {
58         const componentsList: Component[] = [];
59         if (componentsArr.resources) {
60             componentsList.push(...this.getResourceItems(componentsArr.resources));
61         }
62         if (componentsArr.services) {
63             componentsList.push(...this.getServiceItems(componentsArr.services));
64         }
65         if (addSharing) {
66             componentsList.forEach((item) => this.sharingService.addUuidValue(item.uniqueId, item.uuid));
67         }
68         return componentsList;
69     }
70     
71     private getResourceItems(resources){
72         let resourceItems = resources.map((resource)=>{
73             return this.componentFactory.createResource(resource)
74         })
75         return resourceItems;
76     }
77
78     private getServiceItems(services){
79         let serviceItems = services.map((service)=>{
80             return this.componentFactory.createService(service)
81         })
82         return serviceItems;
83     }
84
85 }