2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 import * as _ from "lodash";
22 import {Injectable, Inject} from '@angular/core';
23 import {Observable} from 'rxjs/Observable';
24 import 'rxjs/add/operator/map';
25 import 'rxjs/add/operator/toPromise';
26 import {Response, URLSearchParams} from '@angular/http';
27 import { Component, InputBEModel, InstancePropertiesAPIMap, FilterPropertiesAssignmentData, PropertyBEModel, OperationModel, BEOperationModel, CreateOperationResponse} from "app/models";
28 import {downgradeInjectable} from '@angular/upgrade/static';
29 import {COMPONENT_FIELDS, CommonUtils} from "app/utils";
30 import {ComponentGenericResponse} from "../responses/component-generic-response";
31 import {InstanceBePropertiesMap} from "../../../models/properties-inputs/property-fe-map";
32 import {API_QUERY_PARAMS} from "app/utils";
33 import { ComponentType, ServerTypeUrl } from "../../../utils/constants";
34 import { HttpService } from '../http.service';
35 import {SdcConfigToken, ISdcConfig} from "../../config/sdc-config.config";
36 import {IDependenciesServerResponse} from "../responses/dependencies-server-response";
37 import {AutomatedUpgradeGenericResponse} from "../responses/automated-upgrade-response";
38 import {IAutomatedUpgradeRequestObj} from "../../pages/automated-upgrade/automated-upgrade.service";
40 declare var angular:angular.IAngularStatic;
43 export class ComponentServiceNg2 {
47 constructor(protected http:HttpService, @Inject(SdcConfigToken) sdcConfig:ISdcConfig) {
48 this.baseUrl = sdcConfig.api.root + sdcConfig.api.component_api_root;
51 protected getComponentDataByFieldsName(componentType:string, componentId: string, fields:Array<string>):Observable<ComponentGenericResponse> {
53 let params:URLSearchParams = new URLSearchParams();
54 _.forEach(fields, (field:string):void => {
55 params.append(API_QUERY_PARAMS.INCLUDE, field);
58 return this.http.get(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/filteredDataByParams', {search: params})
59 .map((res:Response) => {
60 return this.analyzeComponentDataResponse(res);
64 protected analyzeComponentDataResponse(res: Response):ComponentGenericResponse {
65 return new ComponentGenericResponse().deserialize(res.json());
68 private getServerTypeUrl = (componentType:string):string => {
69 switch (componentType) {
70 case ComponentType.SERVICE:
71 return ServerTypeUrl.SERVICES;
73 return ServerTypeUrl.RESOURCES;
77 getComponentMetadata(component:Component):Observable<ComponentGenericResponse> {
78 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_METADATA]);
81 getComponentInstanceAttributesAndProperties(component:Component):Observable<ComponentGenericResponse> {
82 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES_PROPERTIES, COMPONENT_FIELDS.COMPONENT_INSTANCES_ATTRIBUTES]);
85 getComponentAttributes(component:Component):Observable<ComponentGenericResponse> {
86 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_ATTRIBUTES]);
89 getComponentCompositionData(component:Component):Observable<ComponentGenericResponse> {
90 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES_RELATION, COMPONENT_FIELDS.COMPONENT_INSTANCES, COMPONENT_FIELDS.COMPONENT_NON_EXCLUDED_POLICIES, COMPONENT_FIELDS.COMPONENT_NON_EXCLUDED_GROUPS]);
93 getComponentResourcePropertiesData(component:Component):Observable<ComponentGenericResponse> {
94 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES, COMPONENT_FIELDS.COMPONENT_POLICIES, COMPONENT_FIELDS.COMPONENT_NON_EXCLUDED_GROUPS]);
97 getComponentResourceInstances(component:Component):Observable<ComponentGenericResponse> {
98 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES]);
101 getComponentInputs(component:Component):Observable<ComponentGenericResponse> {
102 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INPUTS]);
105 getComponentDeploymentArtifacts(component:Component):Observable<ComponentGenericResponse> {
106 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_DEPLOYMENT_ARTIFACTS]);
109 getComponentInformationalArtifacts(component:Component):Observable<ComponentGenericResponse> {
110 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INFORMATIONAL_ARTIFACTS]);
113 getComponentInformationalArtifactsAndInstances(component:Component):Observable<ComponentGenericResponse> {
114 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INFORMATIONAL_ARTIFACTS, COMPONENT_FIELDS.COMPONENT_INSTANCES]);
117 getComponentToscaArtifacts(component:Component):Observable<ComponentGenericResponse> {
118 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_TOSCA_ARTIFACTS]);
121 getComponentProperties(component:Component):Observable<ComponentGenericResponse> {
122 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_PROPERTIES]);
125 getInterfaces(component:Component):Observable<ComponentGenericResponse> {
126 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INTERFACE_OPERATIONS]);
129 getInterfaceOperation(component:Component, operation:OperationModel):Observable<OperationModel> {
130 return this.http.get(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/interfaces/' + operation.interfaceId + '/operations/' + operation.uniqueId)
131 .map((res:Response) => {
136 createInterfaceOperation(component:Component, operation:OperationModel):Observable<CreateOperationResponse> {
137 const operationList = {
139 [operation.interfaceType]: {
140 'type': operation.interfaceType,
142 [operation.name]: new BEOperationModel(operation)
147 return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/interfaceOperations', operationList)
148 .map((res:Response) => {
149 const interf = _.find(res.json().interfaces, (interf: any) => interf.type === operation.interfaceType);
150 const newOperation = _.find(interf.operations, (op:OperationModel) => op.name === operation.name);
151 return new CreateOperationResponse({
153 interfaceType: interf.type,
154 interfaceId: interf.uniqueId
159 updateInterfaceOperation(component:Component, operation:OperationModel):Observable<CreateOperationResponse> {
160 const operationList = {
162 [operation.interfaceType]: {
163 'type': operation.interfaceType,
165 [operation.name]: new BEOperationModel(operation)
170 return this.http.put(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/interfaceOperations', operationList)
171 .map((res:Response) => {
172 const interf = _.find(res.json().interfaces, (interf: any) => interf.type === operation.interfaceType);
173 const newOperation = _.find(interf.operations, (op:OperationModel) => op.name === operation.name);
174 return new CreateOperationResponse({
176 interfaceType: interf.type,
177 interfaceId: interf.uniqueId
182 deleteInterfaceOperation(component:Component, operation:OperationModel):Observable<OperationModel> {
183 return this.http.delete(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/interfaces/' + operation.interfaceId + '/operations/' + operation.uniqueId)
184 .map((res:Response) => {
189 getInterfaceTypes(component:Component):Observable<{[id:string]: Array<string>}> {
190 return this.http.get(this.baseUrl + 'interfaceLifecycleTypes')
191 .map((res:Response) => {
192 const interfaceMap = {};
193 _.forEach(res.json(), (interf:any) => {
194 interfaceMap[interf.toscaPresentation.type] = _.keys(interf.toscaPresentation.operations);
200 getCapabilitiesAndRequirements(componentType: string, componentId:string):Observable<ComponentGenericResponse> {
201 return this.getComponentDataByFieldsName(componentType, componentId, [COMPONENT_FIELDS.COMPONENT_REQUIREMENTS, COMPONENT_FIELDS.COMPONENT_CAPABILITIES]);
204 getDeploymentGraphData(component:Component):Observable<ComponentGenericResponse> {
205 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES_RELATION, COMPONENT_FIELDS.COMPONENT_INSTANCES, COMPONENT_FIELDS.COMPONENT_GROUPS]);
208 createInput(component:Component, inputsToCreate:InstancePropertiesAPIMap, isSelf:boolean):Observable<any> {
209 let inputs = isSelf ? { serviceProperties: inputsToCreate.componentInstanceProperties } : inputsToCreate;
210 return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/create/inputs', inputs)
216 restoreComponent(componentType:string, componentId:string){
217 return this.http.post(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/restore', {})
220 archiveComponent(componentType:string, componentId:string){
221 return this.http.post(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/archive', {})
225 deleteInput(component:Component, input:InputBEModel):Observable<InputBEModel> {
226 return this.http.delete(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/delete/' + input.uniqueId + '/input')
227 .map((res:Response) => {
228 return new InputBEModel(res.json());
232 updateComponentInputs(component:Component, inputs:InputBEModel[]):Observable<InputBEModel[]> {
233 return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/update/inputs', inputs)
234 .map((res:Response) => {
235 return res.json().map((input) => new InputBEModel(input));
239 filterComponentInstanceProperties(component: Component, filterData:FilterPropertiesAssignmentData): Observable<InstanceBePropertiesMap> {//instance-property-be-map
240 let params: URLSearchParams = new URLSearchParams();
241 _.forEach(filterData.selectedTypes, (type:string) => {
242 params.append('resourceType', type);
245 return this.http.get(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/filteredproperties/' + filterData.propertyName, {search: params})
246 .map((res: Response) => {
251 createServiceProperty(component: Component, propertyModel:PropertyBEModel): Observable<PropertyBEModel> {
252 let serverObject = {};
253 serverObject[propertyModel.name] = propertyModel;
254 return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/properties', serverObject)
256 let property:PropertyBEModel = new PropertyBEModel(res.json());
261 getServiceProperties(component: Component): Observable<Array<PropertyBEModel>> {
262 return this.http.get(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/properties')
263 .map((res: Response) => {
265 return new Array<PropertyBEModel>();
267 return CommonUtils.initBeProperties(res.json());
271 updateServiceProperties(component: Component, properties: PropertyBEModel[]) {
272 return this.http.put( this.baseUrl + component.getTypeUrl() + component.uniqueId + '/properties', properties)
273 .map((res: Response) => {
274 const resJson = res.json();
275 return _.map(resJson,
276 (resValue:PropertyBEModel) => new PropertyBEModel(resValue));
280 deleteServiceProperty(component:Component, property:PropertyBEModel):Observable<string> {
281 return this.http.delete(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/properties/' + property.uniqueId )
282 .map((res:Response) => {
283 return property.uniqueId;
287 getDependencies(componentType:string, componentId: string):Observable<Array<IDependenciesServerResponse>> {
288 return this.http.get(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/dependencies')
289 .map((res:Response) => {
294 automatedUpgrade(componentType:string, componentId: string, componentsIdsToUpgrade:Array<IAutomatedUpgradeRequestObj>):Observable<AutomatedUpgradeGenericResponse> {
295 return this.http.post(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/automatedupgrade', componentsIdsToUpgrade)
296 .map((res:Response) => {