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 { Component, InputBEModel, InstancePropertiesAPIMap, FilterPropertiesAssignmentData, OperationModel, CreateOperationResponse, ArtifactModel} from "app/models";
27 import {COMPONENT_FIELDS} from "app/utils";
28 import {ComponentGenericResponse} from "../responses/component-generic-response";
29 import {InstanceBePropertiesMap} from "../../../models/properties-inputs/property-fe-map";
30 import {API_QUERY_PARAMS} from "app/utils";
31 import {ComponentType, ServerTypeUrl, SERVICE_FIELDS} from "../../../utils/constants";
32 import {SdcConfigToken, ISdcConfig} from "../../config/sdc-config.config";
33 import {IDependenciesServerResponse} from "../responses/dependencies-server-response";
34 import {AutomatedUpgradeGenericResponse} from "../responses/automated-upgrade-response";
35 import {IAutomatedUpgradeRequestObj} from "../../pages/automated-upgrade/automated-upgrade.service";
36 import {ComponentInstance} from "../../../models/componentsInstances/componentInstance";
37 import {CommonUtils} from "../../../utils/common-utils";
38 import {RelationshipModel} from "../../../models/graph/relationship";
39 import { HttpClient, HttpParams, HttpHeaders } from "@angular/common/http";
40 import { BEOperationModel, InterfaceModel } from "../../../models/operation";
41 import { PropertyBEModel } from "../../../models/properties-inputs/property-be-model";
42 import { PolicyInstance } from "../../../models/graph/zones/policy-instance";
43 import { ConstraintObject } from "../../components/logic/service-dependencies/service-dependencies.component";
44 import { Requirement } from "../../../models/requirement";
45 import { Capability } from "../../../models/capability";
46 import { OutputBEModel } from "app/models/attributes-outputs/output-be-model";
47 import { HttpHelperService } from '../http-hepler.service';
50 PLEASE DO NOT USE THIS SERVICE IN ANGULAR2! Use the topology-template.service instead
53 export class ComponentServiceNg2 {
57 constructor(protected http: HttpClient, @Inject(SdcConfigToken) sdcConfig:ISdcConfig) {
58 this.baseUrl = sdcConfig.api.root + sdcConfig.api.component_api_root;
61 protected getComponentDataByFieldsName(componentType:string, componentId:string, fields:Array<string>):Observable<ComponentGenericResponse> {
63 let params: HttpParams = new HttpParams();
64 fields.forEach((field:string):void => {
65 params = params.append(API_QUERY_PARAMS.INCLUDE, field);
68 return this.http.get<ComponentGenericResponse>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/filteredDataByParams', {params: params})
70 return new ComponentGenericResponse().deserialize(res);
73 protected getServerTypeUrl = (componentType:string):string => {
74 switch (componentType) {
75 case ComponentType.SERVICE:
76 return ServerTypeUrl.SERVICES;
78 return ServerTypeUrl.RESOURCES;
82 getFullComponent(uniqueId:string):Observable<ComponentGenericResponse> {
83 return this.http.get<ComponentGenericResponse>(this.baseUrl + uniqueId)
85 return new ComponentGenericResponse().deserialize(res);
89 getComponentMetadata(uniqueId:string, type:string):Observable<ComponentGenericResponse> {
90 return this.getComponentDataByFieldsName(type, uniqueId, [COMPONENT_FIELDS.COMPONENT_METADATA]);
94 getComponentInstanceAttributesAndProperties(component:Component):Observable<ComponentGenericResponse> {
95 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES_PROPERTIES, COMPONENT_FIELDS.COMPONENT_INSTANCES_ATTRIBUTES]);
98 getComponentInstanceProperties(component:Component): Observable<ComponentGenericResponse> {
99 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES_PROPERTIES]);
102 getComponentAttributes(component:Component):Observable<ComponentGenericResponse> {
103 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_ATTRIBUTES]);
106 getComponentCompositionData(component:Component):Observable<ComponentGenericResponse> {
107 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]);
110 getComponentResourcePropertiesData(component:Component):Observable<ComponentGenericResponse> {
111 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES, COMPONENT_FIELDS.COMPONENT_POLICIES, COMPONENT_FIELDS.COMPONENT_NON_EXCLUDED_GROUPS]);
114 getComponentResourceAttributesData(component:Component):Observable<ComponentGenericResponse> {
115 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES, COMPONENT_FIELDS.COMPONENT_NON_EXCLUDED_GROUPS]);
118 getComponentResourceInstances(component:Component):Observable<ComponentGenericResponse> {
119 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES]);
122 getComponentInputs(component:Component):Observable<ComponentGenericResponse> {
123 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INPUTS]);
126 getComponentInputsWithProperties(component:Component):Observable<ComponentGenericResponse> {
127 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INPUTS, COMPONENT_FIELDS.COMPONENT_INSTANCES, COMPONENT_FIELDS.COMPONENT_INSTANCES_PROPERTIES, COMPONENT_FIELDS.COMPONENT_PROPERTIES]);
130 getComponentDeploymentArtifacts(component:Component):Observable<ComponentGenericResponse> {
131 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_DEPLOYMENT_ARTIFACTS]);
134 getComponentInformationalArtifacts(component:Component):Observable<ComponentGenericResponse> {
135 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INFORMATIONAL_ARTIFACTS]);
138 getComponentInformationalArtifactsAndInstances(component:Component):Observable<ComponentGenericResponse> {
139 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INFORMATIONAL_ARTIFACTS, COMPONENT_FIELDS.COMPONENT_INSTANCES]);
142 getComponentToscaArtifacts(component:Component):Observable<ComponentGenericResponse> {
143 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_TOSCA_ARTIFACTS]);
146 getComponentProperties(component:Component):Observable<ComponentGenericResponse> {
147 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_PROPERTIES]);
150 getInterfaceOperations(component:Component):Observable<ComponentGenericResponse> {
151 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INTERFACE_OPERATIONS]);
154 getInterfaceOperation(component:Component, operation:OperationModel):Observable<OperationModel> {
155 return this.http.get<OperationModel>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/interfaceOperations/' + operation.uniqueId);
158 // tslint:disable-next-line:member-ordering
159 createInterfaceOperation(component:Component, operation:OperationModel):Observable<OperationModel> {
160 const operationList = {
162 [operation.interfaceType]: {
163 'type': operation.interfaceType,
165 [operation.name]: new BEOperationModel(operation)
170 return this.http.post<any>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/interfaceOperations', operationList)
172 const interf:InterfaceModel = _.find(res.interfaces, interf => interf.type === operation.interfaceType);
173 const newOperation:OperationModel = _.find(interf.operations, op => op.name === operation.name);
174 return new OperationModel({
176 interfaceType: interf.type,
177 interfaceId: interf.uniqueId,
178 artifactFileName: operation.artifactFileName
183 // tslint:disable-next-line:member-ordering
184 updateInterfaceOperation(component:Component, operation:OperationModel):Observable<OperationModel> {
185 const operationList = {
187 [operation.interfaceType]: {
188 type: operation.interfaceType,
190 [operation.name]: new BEOperationModel(operation)
195 return this.http.put<any>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/interfaceOperations', operationList)
197 const interf: InterfaceModel = _.find(res.interfaces, interf => interf.type === operation.interfaceType);
198 const newOperation: OperationModel = _.find(interf.operations, op => op.name === operation.name);
199 return new OperationModel({
201 interfaceType: interf.type,
202 interfaceId: interf.uniqueId,
203 artifactFileName: operation.artifactFileName
209 deleteInterfaceOperation(component: Component, operation:OperationModel):Observable<OperationModel> {
210 return this.http.delete<OperationModel>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/interfaces/' + operation.interfaceId + '/operations/' + operation.uniqueId);
213 getInterfaceTypes(component:Component):Observable<{[id:string]: Array<string>}> {
214 return this.http.get<any>(this.baseUrl + 'interfaceLifecycleTypes' + ((component && component.model) ? '?model=' + component.model : ''))
216 const interfaceMap = {};
220 Object.keys(res).forEach(interfaceName => {
221 const interface1 = res[interfaceName];
222 if (!interface1.toscaPresentation.operations) {
225 interfaceMap[interface1.toscaPresentation.type] = Object.keys(interface1.toscaPresentation.operations);
231 uploadInterfaceOperationArtifact(component:Component, newOperation:OperationModel, oldOperation:OperationModel) {
233 artifactType: "WORKFLOW",
234 artifactName: oldOperation.artifactFileName,
235 description: "Workflow Artifact Description",
236 payloadData: oldOperation.artifactData
239 const headers = new HttpHeaders().append('Content-MD5', HttpHelperService.getHeaderMd5(payload));
241 return this.http.post(this.baseUrl + component.getTypeUrl() + component.uuid + '/interfaces/' + newOperation.interfaceId + '/operations/' + newOperation.uniqueId + '/artifacts/' + newOperation.implementation.artifactUUID,
242 payload, {headers: headers}
243 ).map((res: any) => {
244 const fileName = res.artifactDisplayName || res.artifactName;
245 newOperation.artifactFileName = fileName;
250 getCapabilitiesAndRequirements(componentType: string, componentId:string):Observable<ComponentGenericResponse> {
251 return this.getComponentDataByFieldsName(componentType, componentId, [COMPONENT_FIELDS.COMPONENT_REQUIREMENTS, COMPONENT_FIELDS.COMPONENT_CAPABILITIES]);
258 getDeploymentGraphData(component:Component):Observable<ComponentGenericResponse> {
259 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES_RELATION, COMPONENT_FIELDS.COMPONENT_INSTANCES, COMPONENT_FIELDS.COMPONENT_GROUPS]);
262 createInput(component:Component, inputsToCreate:InstancePropertiesAPIMap, isSelf:boolean):Observable<any> {
263 const inputs = isSelf ? { serviceProperties: inputsToCreate.componentInstanceProperties } : inputsToCreate;
264 return this.http.post<any>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/create/inputs', inputs);
267 createListInput(component:Component, input:any, isSelf:boolean):Observable<any> {
270 // change componentInstanceProperties -> serviceProperties
272 componentInstInputsMap: {
273 serviceProperties: input.componentInstInputsMap.componentInstanceProperties
275 listInput: input.listInput
280 return this.http.post<any>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/create/listInput', inputs);
283 createPolicy(component:Component, policiesToCreate:InstancePropertiesAPIMap, isSelf:boolean):Observable<any> {
286 {'componentPropertiesToPolicies': {
287 ...policiesToCreate.componentInstanceProperties
290 {'componentInstancePropertiesToPolicies': {
291 ...policiesToCreate.componentInstanceProperties
294 return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/create/policies', policiesList);
297 deletePolicy(component:Component, policy: PolicyInstance) {
298 return this.http.put<PolicyInstance>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/policies/' + policy.uniqueId + '/undeclare', policy);
301 restoreComponent(componentType:string, componentId:string) {
302 return this.http.post(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/restore', {})
305 archiveComponent(componentType:string, componentId:string) {
306 return this.http.post(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/archive', {})
309 deleteInput(component:Component, input:InputBEModel):Observable<InputBEModel> {
311 return this.http.delete<InputBEModel>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/delete/' + input.uniqueId + '/input')
313 return new InputBEModel(res);
317 deleteOutput(component:Component, output:OutputBEModel):Observable<OutputBEModel> {
319 return this.http.delete<OutputBEModel>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/delete/' + output.uniqueId + '/output')
321 return new OutputBEModel(res);
325 updateComponentInputs(component:Component, inputs:InputBEModel[]):Observable<InputBEModel[]> {
327 return this.http.post<InputBEModel[]>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/update/inputs', inputs)
329 return res.map((input) => new InputBEModel(input));
333 updateComponentOutputs(component:Component, outputs:OutputBEModel[]):Observable<OutputBEModel[]> {
335 return this.http.post<OutputBEModel[]>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/update/outputs', outputs)
337 return res.map((output) => new OutputBEModel(output));
341 filterComponentInstanceProperties(component:Component, filterData:FilterPropertiesAssignmentData):Observable<InstanceBePropertiesMap> {//instance-property-be-map
342 let params: HttpParams = new HttpParams();
343 filterData.selectedTypes.forEach((type:string) => {
344 params = params.append('resourceType', type);
347 return this.http.get<InstanceBePropertiesMap>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/filteredproperties/' + filterData.propertyName, {params: params});
350 filterComponentInstanceAttributes(component:Component, filterData:FilterPropertiesAssignmentData):Observable<InstanceBePropertiesMap> {//instance-property-be-map
351 let params: HttpParams = new HttpParams();
352 filterData.selectedTypes.forEach((type:string) => {
353 params = params.append('resourceType', type);
356 return this.http.get<InstanceBePropertiesMap>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/filteredproperties/' + filterData.propertyName, {params: params});
359 createServiceProperty(component: Component, propertyModel:PropertyBEModel): Observable<PropertyBEModel> {
360 let serverObject = {};
361 serverObject[propertyModel.name] = propertyModel;
362 return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/properties', serverObject)
363 .map((res: PropertyBEModel) => {
364 const property: PropertyBEModel = new PropertyBEModel(res);
369 getServiceProperties(component: Component): Observable<PropertyBEModel[]> {
370 return this.http.get(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/properties')
371 .map((res: PropertyBEModel[]) => {
373 return new Array<PropertyBEModel>();
375 return CommonUtils.initBeProperties(res);
379 updateServiceProperties(component: Component, properties: PropertyBEModel[]) {
380 return this.http.put<PropertyBEModel[]>( this.baseUrl + component.getTypeUrl() + component.uniqueId + '/properties', properties)
383 return _.map(resJson,
384 (resValue:PropertyBEModel) => new PropertyBEModel(resValue));
388 deleteServiceProperty(component:Component, property:PropertyBEModel):Observable<string> {
389 return this.http.delete(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/properties/' + property.uniqueId )
390 .map((res:Response) => {
391 return property.uniqueId;
395 getDependencies(componentType:string, componentId: string):Observable<IDependenciesServerResponse[]> {
396 return this.http.get<IDependenciesServerResponse[]>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/dependencies');
399 automatedUpgrade(componentType:string, componentId:string, componentsIdsToUpgrade:IAutomatedUpgradeRequestObj[]):Observable<AutomatedUpgradeGenericResponse> {
400 return this.http.post<AutomatedUpgradeGenericResponse>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/automatedupgrade', componentsIdsToUpgrade);
403 updateMultipleComponentInstances(componentId:string, instances:ComponentInstance[]):Observable<ComponentInstance[]> {
404 return this.http.post<ComponentInstance[]>(this.baseUrl + componentId + '/resourceInstance/multipleComponentInstance', instances);
407 getServiceFilterConstraints(component:Component):Observable<ComponentGenericResponse> {
408 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [SERVICE_FIELDS.NODE_FILTER]);
411 createServiceFilterConstraints(component:Component, componentInstance:ComponentInstance, constraint:ConstraintObject):Observable<any> {
412 return this.http.post<any>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/resourceInstances/' + componentInstance.uniqueId + '/nodeFilter', constraint);
415 updateServiceFilterConstraints(component:Component, componentInstance:ComponentInstance, constraints:ConstraintObject[]):Observable<any> {
416 return this.http.put<any>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/resourceInstances/' + componentInstance.uniqueId + '/nodeFilter/', constraints);
419 deleteServiceFilterConstraints(component:Component, componentInstance:ComponentInstance, constraintIndex:number) {
420 return this.http.delete<any>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/resourceInstances/' + componentInstance.uniqueId + '/nodeFilter/' + constraintIndex);
423 protected analyzeComponentDataResponse(res: Response):ComponentGenericResponse {
424 return new ComponentGenericResponse().deserialize(res);