blob: 2e7b2e1eedfeed021b5156cdeeb839153e3372d3 [file] [log] [blame]
Michael Landodd603392017-07-12 00:54:52 +03001/*-
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
Michael Landoed64b5e2017-06-09 03:19:04 +030021'use strict';
Michael Lando5b593492018-07-29 16:13:45 +030022import * as _ from "lodash";
Michael Lando75aacbb2017-07-17 21:12:03 +030023import { Service, IApi, IAppConfigurtaion, Resource, Component} from "../models";
Michael Landoed64b5e2017-06-09 03:19:04 +030024import {SharingService} from "./sharing-service";
25import {ComponentFactory} from "../utils/component-factory";
26import {CacheService} from "./cache-service";
Tal Gitelman51d50f02017-12-10 18:55:03 +020027import {ResourceType} from "app/utils";
Michael Landoed64b5e2017-06-09 03:19:04 +030028
29interface IEntityService {
Michael Lando5b593492018-07-29 16:13:45 +030030 getAllComponents(smallObjects?:boolean):ng.IPromise<Array<Component>>;
Michael Landoed64b5e2017-06-09 03:19:04 +030031}
32
33interface IComponentsArray {
34 services:Array<Service>;
35 resources:Array<Resource>;
Michael Landoed64b5e2017-06-09 03:19:04 +030036}
37
38export class EntityService implements IEntityService {
39 static '$inject' = ['$http', '$q', 'sdcConfig', 'Sdc.Services.SharingService', 'ComponentFactory', 'Sdc.Services.CacheService'];
Michael Lando5b593492018-07-29 16:13:45 +030040 private _smallObjectAttributes = ['uniqueId', 'name', 'componentType', 'resourceType', 'lastUpdateDate', 'lifecycleState', 'distributionStatus', 'icon', 'version'];
Michael Landoed64b5e2017-06-09 03:19:04 +030041 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>>();
Tal Gitelman51d50f02017-12-10 18:55:03 +020054 this.$http.get(this.api.root + this.api.GET_catalog, {params: {excludeTypes: [ResourceType.VFCMT, ResourceType.CONFIGURATION]}})
Michael Landoed64b5e2017-06-09 03:19:04 +030055 .then((response:any) => {
56 let followedResponse: IComponentsArray = response.data;
57 let componentsList:Array<Component> = new Array();
58
Michael Landoa5445102018-03-04 14:53:33 +020059 followedResponse.services && followedResponse.services.forEach((serviceResponse:Service) => {
Michael Landoed64b5e2017-06-09 03:19:04 +030060 let component:Service = this.ComponentFactory.createService(serviceResponse); // new Service(serviceResponse);
61 componentsList.push(component);
62 this.sharingService.addUuidValue(component.uniqueId, component.uuid);
63 });
64
Michael Landoa5445102018-03-04 14:53:33 +020065 followedResponse.resources && followedResponse.resources.forEach((resourceResponse:Resource) => {
Michael Landoed64b5e2017-06-09 03:19:04 +030066 let component:Resource = this.ComponentFactory.createResource(resourceResponse);
67 componentsList.push(component);
68 this.sharingService.addUuidValue(component.uniqueId, component.uuid);
69 });
70
Michael Landoed64b5e2017-06-09 03:19:04 +030071 defer.resolve(componentsList);
72 },(responce) => {
73 defer.reject(responce);
74 });
75 return defer.promise;
76 };
77
Michael Lando5b593492018-07-29 16:13:45 +030078 getAllComponents = (smallObjects?:boolean):ng.IPromise<Array<Component>> => {
Michael Landoed64b5e2017-06-09 03:19:04 +030079 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) => {
Michael Lando5b593492018-07-29 16:13:45 +030086 serviceResponse = (smallObjects) ? _.pick(serviceResponse, this._smallObjectAttributes) : serviceResponse;
Michael Landoed64b5e2017-06-09 03:19:04 +030087 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) => {
Michael Lando5b593492018-07-29 16:13:45 +030093 resourceResponse = (smallObjects) ? _.pick(resourceResponse, this._smallObjectAttributes) : resourceResponse;
Michael Landoed64b5e2017-06-09 03:19:04 +030094 let component:Resource = this.ComponentFactory.createResource(resourceResponse);
95 componentsList.push(component);
96 this.sharingService.addUuidValue(component.uniqueId, component.uuid);
97 });
Michael Lando75aacbb2017-07-17 21:12:03 +030098
Michael Landoed64b5e2017-06-09 03:19:04 +030099 defer.resolve(componentsList);
100 });
101
102 return defer.promise;
103 };
104}