re base code
[sdc.git] / catalog-ui / src / app / ng2 / services / component-services / 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 import { Injectable, Inject } from '@angular/core';
22 import { Observable } from 'rxjs/Observable';
23 import 'rxjs/add/operator/map';
24 import 'rxjs/add/operator/toPromise';
25 import { Response, URLSearchParams } from '@angular/http';
26 import {Service} from "app/models";
27 import { downgradeInjectable } from '@angular/upgrade/static';
28 import { HttpService } from '../http.service';
29
30 import {SdcConfigToken, ISdcConfig} from "../../config/sdc-config.config";
31 import {ForwardingPath} from "app/models/forwarding-path";
32 import {ComponentMetadata} from "app/models/component-metadata";
33 import {ComponentType} from "app/utils";
34 import {Component} from "app/models/components/component";
35 import {ComponentGenericResponse} from "app/ng2/services/responses/component-generic-response";
36 import {COMPONENT_FIELDS, SERVICE_FIELDS} from "app/utils/constants";
37 import {ComponentServiceNg2} from "./component.service";
38 import {ServiceGenericResponse} from "app/ng2/services/responses/service-generic-response";
39 import {ServicePathMapItem} from "app/models/graph/nodes-and-links-map";
40
41
42 @Injectable()
43 export class ServiceServiceNg2 extends ComponentServiceNg2 {
44
45     protected baseUrl = "";
46
47     constructor(protected http: HttpService, @Inject(SdcConfigToken) sdcConfig:ISdcConfig) {
48         super(http, sdcConfig);
49         this.baseUrl = sdcConfig.api.root + sdcConfig.api.component_api_root;
50     }
51
52     validateConformanceLevel(service: Service): Observable<boolean> {
53
54         return this.http.get(this.baseUrl + service.getTypeUrl() + service.uuid + '/conformanceLevelValidation')
55             .map((res: Response) => {
56                 return res.json();
57             });
58     }
59
60     getNodesAndLinksMap(service: Service):Observable<Array<ServicePathMapItem>> {
61         return this.http.get(this.baseUrl + service.getTypeUrl() + service.uniqueId + '/linksMap').map(res => {
62             return <Array<ServicePathMapItem>>res.json();
63         });
64     }
65
66     getServicePath(service: Service, id: string):Observable<any> {
67         return this.http.get(this.baseUrl  + service.getTypeUrl() + service.uniqueId +  '/paths/' + id)
68             .map(res => {
69                 return res.json();
70             })
71     }
72
73     getServicePaths(service: Service):Observable<any> {
74         return this.http.get(this.baseUrl  + service.getTypeUrl() + service.uniqueId +  '/paths')
75             .map(res => {
76                 return res.json();
77             })
78     }
79
80     createOrUpdateServicePath(service: Service, inputsToCreate: ForwardingPath):Observable<ForwardingPath> {
81         if (inputsToCreate.uniqueId) {
82             return this.updateServicePath(service, inputsToCreate);
83         } else {
84             return this.createServicePath(service, inputsToCreate);
85         }
86     }
87
88     createServicePath(service: Service, inputsToCreate: ForwardingPath):Observable<ForwardingPath> {
89         let input = new ServicePathRequestData(inputsToCreate);
90
91         return this.http.post(this.baseUrl  + service.getTypeUrl() + service.uniqueId +   '/paths', input)
92             .map(res => {
93                 return this.parseServicePathResponse(res);
94             });
95     }
96
97     deleteServicePath(service: Service, id: string):Observable<any> {
98         return this.http.delete(this.baseUrl  + service.getTypeUrl() + service.uniqueId + '/paths/' + id )
99             .map((res) => {
100                 return res.json();
101             });
102     }
103
104     updateServicePath(service: Service, inputsToUpdate:ForwardingPath):Observable<ForwardingPath> {
105         let input = new ServicePathRequestData(inputsToUpdate);
106
107         return this.http.put(this.baseUrl  + service.getTypeUrl() + service.uniqueId + '/paths', input)
108             .map((res) => {
109                 return this.parseServicePathResponse(res);
110             });
111     }
112
113     checkComponentInstanceVersionChange(service: Service, newVersionId: string):Observable<Array<string>> {
114         let instanceId = service.selectedInstance.uniqueId;
115         let queries = {componentInstanceId: instanceId, newComponentInstanceId: newVersionId};
116
117         let params:URLSearchParams = new URLSearchParams();
118         _.map(_.keys(queries), (key:string):void => {
119             params.append(key, queries[key]);
120         });
121
122         let url = this.baseUrl + service.getTypeUrl() + service.uniqueId + '/paths-to-delete';
123         return this.http.get(url, {search: params}).map((res: Response) => {
124             return res.json().forwardingPathToDelete;
125         });
126     }
127
128     getComponentCompositionData(component:Component):Observable<ComponentGenericResponse> {
129         return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES_RELATION, COMPONENT_FIELDS.COMPONENT_INSTANCES, SERVICE_FIELDS.FORWARDING_PATHS, COMPONENT_FIELDS.COMPONENT_NON_EXCLUDED_POLICIES, COMPONENT_FIELDS.COMPONENT_NON_EXCLUDED_GROUPS]);
130     }
131
132     protected analyzeComponentDataResponse(res: Response):ComponentGenericResponse {
133         return new ServiceGenericResponse().deserialize(res.json());
134     }
135
136     private parseServicePathResponse(res: Response):ForwardingPath {
137         let resJSON = res.json();
138         let pathId = Object.keys(resJSON.forwardingPaths)[0];
139         let forwardingPath = resJSON.forwardingPaths[pathId];
140         let path:ForwardingPath = new ForwardingPath();
141         path.deserialize(forwardingPath);
142         path.uniqueId = pathId;
143         return path;
144     }
145 }
146
147 class ServicePathRequestData {
148     forwardingPaths: { [key:string]:ForwardingPath } = {};
149     componentMetadataDefinition: ComponentMetadata;
150     toscaType: string = "topology_template";
151
152     constructor(fp? : ForwardingPath) {
153         this.componentMetadataDefinition = new ComponentMetadata();
154         this.componentMetadataDefinition.ecompGeneratedNaming = true;
155         this.componentMetadataDefinition.componentType = ComponentType.SERVICE;
156         if (fp) {
157             let id = fp.uniqueId ? fp.uniqueId : "NEW";
158             this.forwardingPaths[fp.uniqueId] = fp;
159         }
160     }
161 }
162