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