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_INTERFACE_OPERATIONS, 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]);
162 getInterfaceOperation(component: Component, operation: OperationModel): Observable<OperationModel> {
163 return this.http.get<OperationModel>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/interfaceOperations/' + operation.uniqueId);
166 // tslint:disable-next-line:member-ordering
167 createInterfaceOperation(component: Component, operation: OperationModel): Observable<OperationModel> {
168 const operationList = {
170 [operation.interfaceType]: {
171 'type': operation.interfaceType,
173 [operation.name]: new BEOperationModel(operation)
178 return this.http.post<any>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/interfaceOperations', operationList)
180 const interf: InterfaceModel = _.find(res.interfaces, interf => interf.type === operation.interfaceType);
181 const newOperation: OperationModel = _.find(interf.operations, op => op.name === operation.name);
182 return new OperationModel({
184 interfaceType: interf.type,
185 interfaceId: interf.uniqueId,
186 artifactFileName: operation.artifactFileName
191 // tslint:disable-next-line:member-ordering
192 updateInterfaceOperation(component: Component, operation: OperationModel): Observable<OperationModel> {
193 const operationList = {
195 [operation.interfaceType]: {
196 type: operation.interfaceType,
198 [operation.name]: new BEOperationModel(operation)
203 return this.http.put<any>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/interfaceOperations', operationList)
205 const interf: InterfaceModel = _.find(res.interfaces, interf => interf.type === operation.interfaceType);
206 const newOperation: OperationModel = _.find(interf.operations, op => op.name === operation.name);
207 return new OperationModel({
209 interfaceType: interf.type,
210 interfaceId: interf.uniqueId,
211 artifactFileName: operation.artifactFileName
216 updateComponentInterfaceOperation(componentMetaDataId: string,
217 operation: InterfaceOperationModel): Observable<InterfaceOperationModel> {
218 const operationList = {
220 [operation.interfaceType]: {
221 type: operation.interfaceType,
223 [operation.name]: new BEInterfaceOperationModel(operation)
228 return this.http.put<any>(this.baseUrl + 'resources/' + componentMetaDataId + '/interfaceOperation', operationList)
230 const interf: InterfaceModel = _.find(res.interfaces, interf => interf.type === operation.interfaceType);
231 const newOperation: OperationModel = _.find(interf.operations, op => op.name === operation.name);
233 return new InterfaceOperationModel({
235 interfaceType: interf.type,
236 interfaceId: interf.uniqueId,
241 createComponentInterfaceOperation(componentMetaDataId: string,
242 componentMetaDataType: string,
243 operation: InterfaceOperationModel): Observable<InterfaceOperationModel> {
244 const operationList = {
246 [operation.interfaceType]: {
247 type: operation.interfaceType,
249 [operation.name]: new BEInterfaceOperationModel(operation)
254 console.log(operationList);
255 console.log(this.baseUrl + componentMetaDataType + componentMetaDataId + '/resource/interfaceOperation')
256 return this.http.post<any>(this.baseUrl + componentMetaDataType + componentMetaDataId + '/resource/interfaceOperation', operationList)
258 const interf: InterfaceModel = _.find(res.interfaces, interf => interf.type === operation.interfaceType);
259 const newOperation: OperationModel = _.find(interf.operations, op => op.name === operation.name);
261 return new InterfaceOperationModel({
263 interfaceType: interf.type,
264 interfaceId: interf.uniqueId,
269 deleteInterfaceOperation(component: Component, operation: OperationModel): Observable<OperationModel> {
270 return this.http.delete<OperationModel>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/interfaces/' + operation.interfaceId + '/operations/' + operation.uniqueId);
273 getInterfaceTypes(component: Component): Observable<{ [id: string]: Array<string> }> {
274 return this.getInterfaceTypesByModel(component && component.model);
277 getInterfaceTypesByModel(model: string): Observable<{ [id: string]: Array<string> }> {
278 return this.http.get<any>(this.baseUrl + 'interfaceLifecycleTypes' + ((model) ? '?model=' + model : ''))
280 const interfaceMap = {};
284 Object.keys(res).forEach(interfaceName => {
285 const interface1 = res[interfaceName];
286 if (!interface1.toscaPresentation.operations) {
289 interfaceMap[interface1.toscaPresentation.type] = Object.keys(interface1.toscaPresentation.operations);
295 uploadInterfaceOperationArtifact(component: Component, newOperation: OperationModel, oldOperation: OperationModel) {
297 artifactType: "WORKFLOW",
298 artifactName: oldOperation.artifactFileName,
299 description: "Workflow Artifact Description",
300 payloadData: oldOperation.artifactData
303 const headers = new HttpHeaders().append('Content-MD5', HttpHelperService.getHeaderMd5(payload));
305 return this.http.post(this.baseUrl + component.getTypeUrl() + component.uuid + '/interfaces/' + newOperation.interfaceId + '/operations/' + newOperation.uniqueId + '/artifacts/' + newOperation.implementation.artifactUUID,
306 payload, {headers: headers}
307 ).map((res: any) => {
308 const fileName = res.artifactDisplayName || res.artifactName;
309 newOperation.artifactFileName = fileName;
314 getCapabilitiesAndRequirements(componentType: string, componentId: string): Observable<ComponentGenericResponse> {
315 return this.getComponentDataByFieldsName(componentType, componentId, [COMPONENT_FIELDS.COMPONENT_REQUIREMENTS, COMPONENT_FIELDS.COMPONENT_CAPABILITIES]);
318 getDeploymentGraphData(component: Component): Observable<ComponentGenericResponse> {
319 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES_RELATION, COMPONENT_FIELDS.COMPONENT_INSTANCES, COMPONENT_FIELDS.COMPONENT_GROUPS]);
322 createInput(component: Component, inputsToCreate: InstancePropertiesAPIMap, isSelf: boolean): Observable<any> {
323 const inputs = isSelf ? {serviceProperties: inputsToCreate.componentInstanceProperties} : inputsToCreate;
324 return this.http.post<any>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/create/inputs', inputs);
327 createListInput(component: Component, input: any, isSelf: boolean): Observable<any> {
330 // change componentInstanceProperties -> serviceProperties
332 componentInstInputsMap: {
333 serviceProperties: input.componentInstInputsMap.componentInstanceProperties
335 listInput: input.listInput
340 return this.http.post<any>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/create/listInput', inputs);
343 createPolicy(component: Component, policiesToCreate: InstancePropertiesAPIMap, isSelf: boolean): Observable<any> {
347 'componentPropertiesToPolicies': {
348 ...policiesToCreate.componentInstanceProperties
352 'componentInstancePropertiesToPolicies': {
353 ...policiesToCreate.componentInstanceProperties
356 return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/create/policies', policiesList);
359 deletePolicy(component: Component, policy: PolicyInstance) {
360 return this.http.put<PolicyInstance>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/policies/' + policy.uniqueId + '/undeclare', policy);
363 restoreComponent(componentType: string, componentId: string) {
364 return this.http.post(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/restore', {})
367 archiveComponent(componentType: string, componentId: string) {
368 return this.http.post(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/archive', {})
371 deleteInput(component: Component, input: InputBEModel): Observable<InputBEModel> {
373 return this.http.delete<InputBEModel>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/delete/' + input.uniqueId + '/input')
375 return new InputBEModel(res);
379 deleteOutput(component: Component, output: OutputBEModel): Observable<OutputBEModel> {
381 return this.http.delete<OutputBEModel>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/delete/' + output.uniqueId + '/output')
383 return new OutputBEModel(res);
387 updateComponentInputs(component: Component, inputs: InputBEModel[]): Observable<InputBEModel[]> {
389 return this.http.post<InputBEModel[]>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/update/inputs', inputs)
391 return res.map((input) => new InputBEModel(input));
395 updateComponentOutputs(component: Component, outputs: OutputBEModel[]): Observable<OutputBEModel[]> {
397 return this.http.post<OutputBEModel[]>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/update/outputs', outputs)
399 return res.map((output) => new OutputBEModel(output));
403 filterComponentInstanceProperties(component: Component, filterData: FilterPropertiesAssignmentData): Observable<InstanceBePropertiesMap> {//instance-property-be-map
404 let params: HttpParams = new HttpParams();
405 filterData.selectedTypes.forEach((type: string) => {
406 params = params.append('resourceType', type);
409 return this.http.get<InstanceBePropertiesMap>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/filteredproperties/' + filterData.propertyName, {params: params});
412 filterComponentInstanceAttributes(component: Component, filterData: FilterPropertiesAssignmentData): Observable<InstanceBePropertiesMap> {//instance-property-be-map
413 let params: HttpParams = new HttpParams();
414 filterData.selectedTypes.forEach((type: string) => {
415 params = params.append('resourceType', type);
418 return this.http.get<InstanceBePropertiesMap>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/filteredproperties/' + filterData.propertyName, {params: params});
421 deleteComponent(componentType: string, componentId: string) {
422 return this.http.delete(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + "?deleteAction=DELETE", {});
425 createServiceProperty(component: Component, propertyModel: PropertyBEModel): Observable<PropertyBEModel> {
426 let serverObject = {};
427 serverObject[propertyModel.name] = propertyModel;
428 return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/properties', serverObject)
429 .map((res: PropertyBEModel) => {
430 const property: PropertyBEModel = new PropertyBEModel(res);
435 getServiceProperties(component: Component): Observable<PropertyBEModel[]> {
436 return this.http.get(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/properties')
437 .map((res: PropertyBEModel[]) => {
439 return new Array<PropertyBEModel>();
441 return CommonUtils.initBeProperties(res);
445 updateServiceProperties(component: Component, properties: PropertyBEModel[]) {
446 return this.http.put<PropertyBEModel[]>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/properties', properties)
449 return _.map(resJson,
450 (resValue: PropertyBEModel) => new PropertyBEModel(resValue));
454 deleteServiceProperty(component: Component, property: PropertyBEModel): Observable<string> {
455 return this.http.delete(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/properties/' + property.uniqueId)
456 .map((res: Response) => {
457 return property.uniqueId;
461 getDependencies(componentType: string, componentId: string): Observable<IDependenciesServerResponse[]> {
462 return this.http.get<IDependenciesServerResponse[]>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/dependencies');
465 automatedUpgrade(componentType: string, componentId: string, componentsIdsToUpgrade: IAutomatedUpgradeRequestObj[]): Observable<AutomatedUpgradeGenericResponse> {
466 return this.http.post<AutomatedUpgradeGenericResponse>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/automatedupgrade', componentsIdsToUpgrade);
469 updateMultipleComponentInstances(componentId: string, instances: ComponentInstance[]): Observable<ComponentInstance[]> {
470 return this.http.post<ComponentInstance[]>(this.baseUrl + componentId + '/resourceInstance/multipleComponentInstance', instances);
473 getServiceFilterConstraints(component: Component): Observable<ComponentGenericResponse> {
474 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [SERVICE_FIELDS.NODE_FILTER]);
477 createServiceFilterConstraints(component: Component, componentInstance: ComponentInstance, constraint: FilterConstraint): Observable<any> {
478 return this.http.post<any>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/resourceInstances/' + componentInstance.uniqueId + '/nodeFilter', constraint);
481 updateServiceFilterConstraints(component: Component, componentInstance: ComponentInstance, constraints: FilterConstraint[]): Observable<any> {
482 return this.http.put<any>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/resourceInstances/' + componentInstance.uniqueId + '/nodeFilter/', constraints);
485 deleteServiceFilterConstraints(component: Component, componentInstance: ComponentInstance, constraintIndex: number) {
486 return this.http.delete<any>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/resourceInstances/' + componentInstance.uniqueId + '/nodeFilter/' + constraintIndex);
489 protected analyzeComponentDataResponse(res: Response): ComponentGenericResponse {
490 return new ComponentGenericResponse().deserialize(res);