Catalog alignment
[sdc.git] / catalog-ui / src / app / services / components / service-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 /**
22  * Created by obarda on 2/4/2016.
23  */
24 'use strict';
25 import * as _ from "lodash";
26 import {IComponentService, ComponentService} from "./component-service";
27 import {Distribution, DistributionComponent, Service, PropertyModel, Component, IAppConfigurtaion} from "app/models";
28 import {SharingService} from "app/services-ng2";
29
30 export interface IServiceService extends IComponentService {
31     getDistributionsList(uuid:string):ng.IPromise<Array<Distribution>>;
32     getDistributionComponents(distributionId:string):ng.IPromise<Array<DistributionComponent>>;
33     markAsDeployed(serviceId:string, distributionId:string):ng.IPromise<any>;
34     updateGroupInstanceProperties(serviceId:string, resourceInstanceId:string, groupInstanceId:string, groupInstanceProperties:Array<PropertyModel>):ng.IPromise<Array<PropertyModel>>;
35 }
36
37 export class ServiceService extends ComponentService implements IServiceService {
38
39     static '$inject' = [
40         'Restangular',
41         'sdcConfig',
42         'Sdc.Services.SharingService',
43         '$q',
44         '$base64'
45     ];
46
47     public distribution:string = "distribution";
48
49     constructor(protected restangular:restangular.IElement,
50                 protected sdcConfig:IAppConfigurtaion,
51                 protected sharingService:SharingService,
52                 protected $q:ng.IQService,
53                 protected $base64:any) {
54         super(restangular, sdcConfig, sharingService, $q, $base64);
55
56         this.restangular = restangular.one("services");
57     }
58
59     getDistributionsList = (uuid:string):ng.IPromise<Array<Distribution>> => {
60         let defer = this.$q.defer<Array<Distribution>>();
61         this.restangular.one(uuid).one("distribution").get().then((distributions:any) => {
62             defer.resolve(<Array<Distribution>> distributions.distributionStatusOfServiceList);
63         }, (err)=> {
64             defer.reject(err);
65         });
66         return defer.promise;
67     };
68
69     getDistributionComponents = (distributionId:string):ng.IPromise<Array<DistributionComponent>> => {
70         let defer = this.$q.defer<Array<DistributionComponent>>();
71         this.restangular.one("distribution").one(distributionId).get().then((distributions:any) => {
72             defer.resolve(<Array<DistributionComponent>> distributions.distributionStatusList);
73         }, (err)=> {
74             defer.reject(err);
75         });
76         return defer.promise;
77     };
78
79     markAsDeployed = (serviceId:string, distributionId:string):ng.IPromise<any> => {
80         let defer = this.$q.defer<any>();
81         this.restangular.one(serviceId).one("distribution").one(distributionId).one("markDeployed").customPOST().then((result:any) => {
82             defer.resolve(result);
83         }, (err)=> {
84
85             defer.reject(err);
86         });
87         return defer.promise;
88     };
89
90     createComponentObject = (component:Component):Component => {
91         return new Service(this, this.$q, <Service>component);
92     };
93
94     updateGroupInstanceProperties = (serviceId:string, resourceInstanceId:string, groupInstanceId:string, groupInstanceProperties:Array<PropertyModel>):ng.IPromise<Array<PropertyModel>> => {
95         let defer = this.$q.defer<Array<PropertyModel>>();
96         this.restangular.one(serviceId).one("resourceInstance").one(resourceInstanceId).one('groupInstance').one(groupInstanceId).customPUT(JSON.stringify(groupInstanceProperties)).then((updatedProperties:any) => {
97             let propertiesArray:Array<PropertyModel> = new Array<PropertyModel>();
98             _.forEach(updatedProperties, (propertyObj:PropertyModel) => {
99                 propertiesArray.push(new PropertyModel(propertyObj));
100             });
101             defer.resolve(propertiesArray);
102         }, (err)=> {
103             defer.reject(err);
104         });
105         return defer.promise;
106     };
107 }