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 {Inject, Injectable} from '@angular/core';
23 import {Observable} from 'rxjs/Observable';
24 import 'rxjs/add/operator/map';
25 import 'rxjs/add/operator/toPromise';
28 FilterPropertiesAssignmentData,
30 InstancePropertiesAPIMap,
33 import {API_QUERY_PARAMS, COMPONENT_FIELDS} from "app/utils";
34 import {ComponentGenericResponse} from "../responses/component-generic-response";
35 import {InstanceBePropertiesMap} from "../../../models/properties-inputs/property-fe-map";
36 import {ComponentType, ServerTypeUrl, SERVICE_FIELDS} from "../../../utils/constants";
37 import {ISdcConfig, SdcConfigToken} from "../../config/sdc-config.config";
38 import {IDependenciesServerResponse} from "../responses/dependencies-server-response";
39 import {AutomatedUpgradeGenericResponse} from "../responses/automated-upgrade-response";
40 import {IAutomatedUpgradeRequestObj} from "../../pages/automated-upgrade/automated-upgrade.service";
41 import {ComponentInstance} from "../../../models/componentsInstances/componentInstance";
42 import {CommonUtils} from "../../../utils/common-utils";
43 import {HttpClient, HttpHeaders, HttpParams} from "@angular/common/http";
44 import {BEOperationModel, InterfaceModel} from "../../../models/operation";
45 import {PropertyBEModel} from "../../../models/properties-inputs/property-be-model";
46 import {PolicyInstance} from "../../../models/graph/zones/policy-instance";
47 import {OutputBEModel} from "app/models/attributes-outputs/output-be-model";
48 import {HttpHelperService} from '../http-hepler.service';
50 BEInterfaceOperationModel,
51 InterfaceOperationModel
52 } from "../../../models/interfaceOperation";
53 import {FilterConstraint} from "../../../models/filter-constraint";
56 PLEASE DO NOT USE THIS SERVICE IN ANGULAR2! Use the topology-template.service instead
59 export class ComponentServiceNg2 {
63 constructor(protected http: HttpClient, @Inject(SdcConfigToken) sdcConfig: ISdcConfig) {
64 this.baseUrl = sdcConfig.api.root + sdcConfig.api.component_api_root;
67 protected getComponentDataByFieldsName(componentType: string, componentId: string, fields: Array<string>): Observable<ComponentGenericResponse> {
69 let params: HttpParams = new HttpParams();
70 fields.forEach((field: string): void => {
71 params = params.append(API_QUERY_PARAMS.INCLUDE, field);
74 return this.http.get<ComponentGenericResponse>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/filteredDataByParams', {params: params})
76 return new ComponentGenericResponse().deserialize(res);
80 protected getServerTypeUrl = (componentType: string): string => {
81 switch (componentType) {
82 case ComponentType.SERVICE:
83 return ServerTypeUrl.SERVICES;
85 return ServerTypeUrl.RESOURCES;
89 getFullComponent(uniqueId: string): Observable<ComponentGenericResponse> {
90 return this.http.get<ComponentGenericResponse>(this.baseUrl + uniqueId)
92 return new ComponentGenericResponse().deserialize(res);
96 getComponentMetadata(uniqueId: string, type: string): Observable<ComponentGenericResponse> {
97 return this.getComponentDataByFieldsName(type, uniqueId, [COMPONENT_FIELDS.COMPONENT_METADATA]);
100 getComponentInstanceAttributesAndProperties(component: Component): Observable<ComponentGenericResponse> {
101 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES_PROPERTIES, COMPONENT_FIELDS.COMPONENT_INSTANCES_ATTRIBUTES]);
104 getComponentInstanceProperties(component: Component): Observable<ComponentGenericResponse> {
105 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES_PROPERTIES]);
108 getComponentAttributes(component: Component): Observable<ComponentGenericResponse> {
109 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_ATTRIBUTES]);
112 getComponentCompositionData(component: Component): Observable<ComponentGenericResponse> {
113 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]);
116 getComponentResourcePropertiesData(component: Component): Observable<ComponentGenericResponse> {
117 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES, COMPONENT_FIELDS.COMPONENT_POLICIES, COMPONENT_FIELDS.COMPONENT_NON_EXCLUDED_GROUPS]);
120 getComponentResourceAttributesData(component: Component): Observable<ComponentGenericResponse> {
121 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES, COMPONENT_FIELDS.COMPONENT_NON_EXCLUDED_GROUPS]);
124 getComponentResourceInstances(component: Component): Observable<ComponentGenericResponse> {
125 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES]);
128 getComponentInputs(component: Component): Observable<ComponentGenericResponse> {
129 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INPUTS]);
132 getComponentInputsWithProperties(component: Component): Observable<ComponentGenericResponse> {
133 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INPUTS, COMPONENT_FIELDS.COMPONENT_INSTANCES, COMPONENT_FIELDS.COMPONENT_INSTANCES_PROPERTIES, COMPONENT_FIELDS.COMPONENT_PROPERTIES]);
136 getComponentDeploymentArtifacts(component: Component): Observable<ComponentGenericResponse> {
137 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_DEPLOYMENT_ARTIFACTS]);
140 getComponentInformationalArtifacts(component: Component): Observable<ComponentGenericResponse> {
141 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INFORMATIONAL_ARTIFACTS]);
144 getComponentInformationalArtifactsAndInstances(component: Component): Observable<ComponentGenericResponse> {
145 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INFORMATIONAL_ARTIFACTS, COMPONENT_FIELDS.COMPONENT_INSTANCES]);
148 getComponentToscaArtifacts(component: Component): Observable<ComponentGenericResponse> {
149 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_TOSCA_ARTIFACTS]);
152 getComponentProperties(component: Component): Observable<ComponentGenericResponse> {
153 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_PROPERTIES]);
156 getInterfaceOperations(component: Component): Observable<ComponentGenericResponse> {
157 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INTERFACE_OPERATIONS]);
160 getInterfaceOperation(component: Component, operation: OperationModel): Observable<OperationModel> {
161 return this.http.get<OperationModel>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/interfaceOperations/' + operation.uniqueId);
164 // tslint:disable-next-line:member-ordering
165 createInterfaceOperation(component: Component, operation: OperationModel): Observable<OperationModel> {
166 const operationList = {
168 [operation.interfaceType]: {
169 'type': operation.interfaceType,
171 [operation.name]: new BEOperationModel(operation)
176 return this.http.post<any>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/interfaceOperations', operationList)
178 const interf: InterfaceModel = _.find(res.interfaces, interf => interf.type === operation.interfaceType);
179 const newOperation: OperationModel = _.find(interf.operations, op => op.name === operation.name);
180 return new OperationModel({
182 interfaceType: interf.type,
183 interfaceId: interf.uniqueId,
184 artifactFileName: operation.artifactFileName
189 // tslint:disable-next-line:member-ordering
190 updateInterfaceOperation(component: Component, operation: OperationModel): Observable<OperationModel> {
191 const operationList = {
193 [operation.interfaceType]: {
194 type: operation.interfaceType,
196 [operation.name]: new BEOperationModel(operation)
201 return this.http.put<any>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/interfaceOperations', operationList)
203 const interf: InterfaceModel = _.find(res.interfaces, interf => interf.type === operation.interfaceType);
204 const newOperation: OperationModel = _.find(interf.operations, op => op.name === operation.name);
205 return new OperationModel({
207 interfaceType: interf.type,
208 interfaceId: interf.uniqueId,
209 artifactFileName: operation.artifactFileName
214 updateComponentInterfaceOperation(componentMetaDataId: string,
215 operation: InterfaceOperationModel): Observable<InterfaceOperationModel> {
216 const operationList = {
218 [operation.interfaceType]: {
219 type: operation.interfaceType,
221 [operation.name]: new BEInterfaceOperationModel(operation)
226 return this.http.put<any>(this.baseUrl + 'resources/' + componentMetaDataId + '/interfaceOperation', operationList)
228 const interf: InterfaceModel = _.find(res.interfaces, interf => interf.type === operation.interfaceType);
229 const newOperation: OperationModel = _.find(interf.operations, op => op.name === operation.name);
231 return new InterfaceOperationModel({
233 interfaceType: interf.type,
234 interfaceId: interf.uniqueId,
239 createComponentInterfaceOperation(componentMetaDataId: string,
240 componentMetaDataType: string,
241 operation: InterfaceOperationModel): Observable<InterfaceOperationModel> {
242 const operationList = {
244 [operation.interfaceType]: {
245 type: operation.interfaceType,
247 [operation.name]: new BEInterfaceOperationModel(operation)
252 console.log(operationList);
253 console.log(this.baseUrl + componentMetaDataType + componentMetaDataId + '/resource/interfaceOperation')
254 return this.http.post<any>(this.baseUrl + componentMetaDataType + componentMetaDataId + '/resource/interfaceOperation', operationList)
256 const interf: InterfaceModel = _.find(res.interfaces, interf => interf.type === operation.interfaceType);
257 const newOperation: OperationModel = _.find(interf.operations, op => op.name === operation.name);
259 return new InterfaceOperationModel({
261 interfaceType: interf.type,
262 interfaceId: interf.uniqueId,
267 deleteInterfaceOperation(component: Component, operation: OperationModel): Observable<OperationModel> {
268 return this.http.delete<OperationModel>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/interfaces/' + operation.interfaceId + '/operations/' + operation.uniqueId);
271 getInterfaceTypes(component: Component): Observable<{ [id: string]: Array<string> }> {
272 return this.getInterfaceTypesByModel(component && component.model);
275 getInterfaceTypesByModel(model: string): Observable<{ [id: string]: Array<string> }> {
276 return this.http.get<any>(this.baseUrl + 'interfaceLifecycleTypes' + ((model) ? '?model=' + model : ''))
278 const interfaceMap = {};
282 Object.keys(res).forEach(interfaceName => {
283 const interface1 = res[interfaceName];
284 if (!interface1.toscaPresentation.operations) {
287 interfaceMap[interface1.toscaPresentation.type] = Object.keys(interface1.toscaPresentation.operations);
293 uploadInterfaceOperationArtifact(component: Component, newOperation: OperationModel, oldOperation: OperationModel) {
295 artifactType: "WORKFLOW",
296 artifactName: oldOperation.artifactFileName,
297 description: "Workflow Artifact Description",
298 payloadData: oldOperation.artifactData
301 const headers = new HttpHeaders().append('Content-MD5', HttpHelperService.getHeaderMd5(payload));
303 return this.http.post(this.baseUrl + component.getTypeUrl() + component.uuid + '/interfaces/' + newOperation.interfaceId + '/operations/' + newOperation.uniqueId + '/artifacts/' + newOperation.implementation.artifactUUID,
304 payload, {headers: headers}
305 ).map((res: any) => {
306 const fileName = res.artifactDisplayName || res.artifactName;
307 newOperation.artifactFileName = fileName;
312 getCapabilitiesAndRequirements(componentType: string, componentId: string): Observable<ComponentGenericResponse> {
313 return this.getComponentDataByFieldsName(componentType, componentId, [COMPONENT_FIELDS.COMPONENT_REQUIREMENTS, COMPONENT_FIELDS.COMPONENT_CAPABILITIES]);
316 getDeploymentGraphData(component: Component): Observable<ComponentGenericResponse> {
317 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES_RELATION, COMPONENT_FIELDS.COMPONENT_INSTANCES, COMPONENT_FIELDS.COMPONENT_GROUPS]);
320 createInput(component: Component, inputsToCreate: InstancePropertiesAPIMap, isSelf: boolean): Observable<any> {
321 const inputs = isSelf ? {serviceProperties: inputsToCreate.componentInstanceProperties} : inputsToCreate;
322 return this.http.post<any>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/create/inputs', inputs);
325 createListInput(component: Component, input: any, isSelf: boolean): Observable<any> {
328 // change componentInstanceProperties -> serviceProperties
330 componentInstInputsMap: {
331 serviceProperties: input.componentInstInputsMap.componentInstanceProperties
333 listInput: input.listInput
338 return this.http.post<any>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/create/listInput', inputs);
341 createPolicy(component: Component, policiesToCreate: InstancePropertiesAPIMap, isSelf: boolean): Observable<any> {
345 'componentPropertiesToPolicies': {
346 ...policiesToCreate.componentInstanceProperties
350 'componentInstancePropertiesToPolicies': {
351 ...policiesToCreate.componentInstanceProperties
354 return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/create/policies', policiesList);
357 deletePolicy(component: Component, policy: PolicyInstance) {
358 return this.http.put<PolicyInstance>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/policies/' + policy.uniqueId + '/undeclare', policy);
361 restoreComponent(componentType: string, componentId: string) {
362 return this.http.post(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/restore', {})
365 archiveComponent(componentType: string, componentId: string) {
366 return this.http.post(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/archive', {})
369 deleteInput(component: Component, input: InputBEModel): Observable<InputBEModel> {
371 return this.http.delete<InputBEModel>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/delete/' + input.uniqueId + '/input')
373 return new InputBEModel(res);
377 deleteOutput(component: Component, output: OutputBEModel): Observable<OutputBEModel> {
379 return this.http.delete<OutputBEModel>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/delete/' + output.uniqueId + '/output')
381 return new OutputBEModel(res);
385 updateComponentInputs(component: Component, inputs: InputBEModel[]): Observable<InputBEModel[]> {
387 return this.http.post<InputBEModel[]>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/update/inputs', inputs)
389 return res.map((input) => new InputBEModel(input));
393 updateComponentOutputs(component: Component, outputs: OutputBEModel[]): Observable<OutputBEModel[]> {
395 return this.http.post<OutputBEModel[]>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/update/outputs', outputs)
397 return res.map((output) => new OutputBEModel(output));
401 filterComponentInstanceProperties(component: Component, filterData: FilterPropertiesAssignmentData): Observable<InstanceBePropertiesMap> {//instance-property-be-map
402 let params: HttpParams = new HttpParams();
403 filterData.selectedTypes.forEach((type: string) => {
404 params = params.append('resourceType', type);
407 return this.http.get<InstanceBePropertiesMap>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/filteredproperties/' + filterData.propertyName, {params: params});
410 filterComponentInstanceAttributes(component: Component, filterData: FilterPropertiesAssignmentData): Observable<InstanceBePropertiesMap> {//instance-property-be-map
411 let params: HttpParams = new HttpParams();
412 filterData.selectedTypes.forEach((type: string) => {
413 params = params.append('resourceType', type);
416 return this.http.get<InstanceBePropertiesMap>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/filteredproperties/' + filterData.propertyName, {params: params});
419 deleteComponent(componentType: string, componentId: string) {
420 return this.http.delete(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + "?deleteAction=DELETE", {});
423 createServiceProperty(component: Component, propertyModel: PropertyBEModel): Observable<PropertyBEModel> {
424 let serverObject = {};
425 serverObject[propertyModel.name] = propertyModel;
426 return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/properties', serverObject)
427 .map((res: PropertyBEModel) => {
428 const property: PropertyBEModel = new PropertyBEModel(res);
433 getServiceProperties(component: Component): Observable<PropertyBEModel[]> {
434 return this.http.get(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/properties')
435 .map((res: PropertyBEModel[]) => {
437 return new Array<PropertyBEModel>();
439 return CommonUtils.initBeProperties(res);
443 updateServiceProperties(component: Component, properties: PropertyBEModel[]) {
444 return this.http.put<PropertyBEModel[]>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/properties', properties)
447 return _.map(resJson,
448 (resValue: PropertyBEModel) => new PropertyBEModel(resValue));
452 deleteServiceProperty(component: Component, property: PropertyBEModel): Observable<string> {
453 return this.http.delete(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/properties/' + property.uniqueId)
454 .map((res: Response) => {
455 return property.uniqueId;
459 getDependencies(componentType: string, componentId: string): Observable<IDependenciesServerResponse[]> {
460 return this.http.get<IDependenciesServerResponse[]>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/dependencies');
463 automatedUpgrade(componentType: string, componentId: string, componentsIdsToUpgrade: IAutomatedUpgradeRequestObj[]): Observable<AutomatedUpgradeGenericResponse> {
464 return this.http.post<AutomatedUpgradeGenericResponse>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/automatedupgrade', componentsIdsToUpgrade);
467 updateMultipleComponentInstances(componentId: string, instances: ComponentInstance[]): Observable<ComponentInstance[]> {
468 return this.http.post<ComponentInstance[]>(this.baseUrl + componentId + '/resourceInstance/multipleComponentInstance', instances);
471 getServiceFilterConstraints(component: Component): Observable<ComponentGenericResponse> {
472 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [SERVICE_FIELDS.NODE_FILTER]);
475 createServiceFilterConstraints(component: Component, componentInstance: ComponentInstance, constraint: FilterConstraint): Observable<any> {
476 return this.http.post<any>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/resourceInstances/' + componentInstance.uniqueId + '/nodeFilter', constraint);
479 updateServiceFilterConstraints(component: Component, componentInstance: ComponentInstance, constraints: FilterConstraint[]): Observable<any> {
480 return this.http.put<any>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/resourceInstances/' + componentInstance.uniqueId + '/nodeFilter/', constraints);
483 deleteServiceFilterConstraints(component: Component, componentInstance: ComponentInstance, constraintIndex: number) {
484 return this.http.delete<any>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/resourceInstances/' + componentInstance.uniqueId + '/nodeFilter/' + constraintIndex);
487 protected analyzeComponentDataResponse(res: Response): ComponentGenericResponse {
488 return new ComponentGenericResponse().deserialize(res);