2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
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, DataTypeModel, 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 import {DataTypesService} from "../../services/data-types-service";
30 import {DataTypeCatalogComponent} from "../../models/data-type-catalog-component";
31 import {zip} from "rxjs";
32 import {map} from "rxjs/operators";
35 export class CatalogService {
37 protected baseUrl:string;
38 protected baseMicroServiceUrl:string;
40 constructor(private http: HttpClient,
41 @Inject(SdcConfigToken) sdcConfig:ISdcConfig,
42 private componentFactory:ComponentFactory,
43 private dataTypesService:DataTypesService,
44 private sharingService:SharingService) {
45 this.api = sdcConfig.api;
46 this.baseUrl = sdcConfig.api.root ;
47 this.baseMicroServiceUrl = sdcConfig.api.uicache_root;
50 public getCatalog(): Observable<Array<Component | DataTypeCatalogComponent>> {
51 let searchParams = new HttpParams();
52 searchParams = searchParams.append('excludeTypes', ResourceType.VFCMT).append('excludeTypes', ResourceType.CONFIGURATION);
53 const observableComponents = this.http.get<IComponentsArray>(this.baseMicroServiceUrl + this.api.GET_uicache_catalog, {params: searchParams});
54 const observableDataTypes = this.dataTypesService.getDataTypesFromAllModelExcludePrimitives();
55 return zip(observableComponents, observableDataTypes)
56 .pipe(map(res => this.processComponentsResponse(res, true)));
59 public getArchiveCatalog() {
60 return this.http.get<IComponentsArray>(this.baseUrl + '/v1/catalog/archive/', {})
61 .map(res => this.processComponentsResponse(res[0]));
64 private processComponentsResponse(componentsArr: [IComponentsArray, DataTypeModel[]], addSharing:boolean = false) {
65 const componentsList:Array<Component | DataTypeCatalogComponent> = [];
66 if (componentsArr[0].resources) {
67 componentsList.push(...this.getResourceItems(componentsArr[0].resources));
69 if (componentsArr[0].services) {
70 componentsList.push(...this.getServiceItems(componentsArr[0].services));
72 if (componentsArr[1]) {
73 componentsList.push(...this.getDataTypesItems(componentsArr[1]));
76 componentsList.forEach((item) => this.sharingService.addUuidValue(item.uniqueId, item.uuid));
78 return componentsList;
81 private getResourceItems(resources){
82 let resourceItems = resources.map((resource)=>{
83 return this.componentFactory.createResource(resource)
88 private getServiceItems(services){
89 let serviceItems = services.map((service)=>{
90 return this.componentFactory.createService(service)
95 private getDataTypesItems(dataTypes: Array<DataTypeModel>):Array<DataTypeCatalogComponent> {
96 return dataTypes.map(dataType => new DataTypeCatalogComponent(dataType));