re base code
[sdc.git] / catalog-ui / src / app / services / entity-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 'use strict';
22 import * as _ from "lodash";
23 import { Service, IApi, IAppConfigurtaion, Resource, Component} from "../models";
24 import {SharingService} from "./sharing-service";
25 import {ComponentFactory} from "../utils/component-factory";
26 import {CacheService} from "./cache-service";
27 import {ResourceType} from "app/utils";
28
29 interface IEntityService {
30     getAllComponents(smallObjects?:boolean):ng.IPromise<Array<Component>>;
31 }
32
33 interface IComponentsArray {
34     services:Array<Service>;
35     resources:Array<Resource>;
36 }
37
38 export class EntityService implements IEntityService {
39     static '$inject' = ['$http', '$q', 'sdcConfig', 'Sdc.Services.SharingService', 'ComponentFactory', 'Sdc.Services.CacheService'];
40     private _smallObjectAttributes = ['uniqueId', 'name', 'componentType', 'resourceType', 'lastUpdateDate', 'lifecycleState', 'distributionStatus', 'icon', 'version'];
41     private api:IApi;
42
43     constructor(private $http:ng.IHttpService,
44                 private $q:ng.IQService,
45                 private sdcConfig:IAppConfigurtaion,
46                 private sharingService:SharingService,
47                 private ComponentFactory:ComponentFactory,
48                 private cacheService:CacheService) {
49         this.api = sdcConfig.api;
50     }
51
52     getCatalog = ():ng.IPromise<Array<Component>> => {
53         let defer = this.$q.defer<Array<Component>>();
54         this.$http.get(this.api.root + this.api.GET_catalog, {params: {excludeTypes: [ResourceType.VFCMT, ResourceType.CONFIGURATION]}})
55             .then((response:any) => {
56                 let followedResponse: IComponentsArray =  response.data;
57                 let componentsList:Array<Component> = new Array();
58
59                 followedResponse.services && followedResponse.services.forEach((serviceResponse:Service) => {
60                     let component:Service = this.ComponentFactory.createService(serviceResponse); // new Service(serviceResponse);
61                     componentsList.push(component);
62                     this.sharingService.addUuidValue(component.uniqueId, component.uuid);
63                 });
64
65                 followedResponse.resources && followedResponse.resources.forEach((resourceResponse:Resource) => {
66                     let component:Resource = this.ComponentFactory.createResource(resourceResponse);
67                     componentsList.push(component);
68                     this.sharingService.addUuidValue(component.uniqueId, component.uuid);
69                 });
70
71                 defer.resolve(componentsList);
72             },(responce) => {
73                 defer.reject(responce);
74             });
75         return defer.promise;
76     };
77
78     getAllComponents = (smallObjects?:boolean):ng.IPromise<Array<Component>> => {
79         let defer = this.$q.defer<Array<Component>>();
80         this.$http.get(this.api.root + this.api.GET_element)
81             .then((response:any) => {
82                 let componentResponse:IComponentsArray = response.data;
83                 let componentsList:Array<Component> = [];
84
85                 componentResponse.services && componentResponse.services.forEach((serviceResponse:Service) => {
86                     serviceResponse = (smallObjects) ? _.pick(serviceResponse, this._smallObjectAttributes) : serviceResponse;
87                     let component:Service = this.ComponentFactory.createService(serviceResponse);
88                     componentsList.push(component);
89                     this.sharingService.addUuidValue(component.uniqueId, component.uuid);
90                 });
91
92                 componentResponse.resources && componentResponse.resources.forEach((resourceResponse:Resource) => {
93                     resourceResponse = (smallObjects) ? _.pick(resourceResponse, this._smallObjectAttributes) : resourceResponse;
94                     let component:Resource = this.ComponentFactory.createResource(resourceResponse);
95                     componentsList.push(component);
96                     this.sharingService.addUuidValue(component.uniqueId, component.uuid);
97                 });
98                 
99                 defer.resolve(componentsList);
100             });
101
102         return defer.promise;
103     };
104 }