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";
49 PLEASE DO NOT USE THIS SERVICE IN ANGULAR2! Use the topology-template.service instead
52 export class ComponentServiceNg2 {
56 constructor(protected http: HttpClient, @Inject(SdcConfigToken) sdcConfig:ISdcConfig) {
57 this.baseUrl = sdcConfig.api.root + sdcConfig.api.component_api_root;
60 protected getComponentDataByFieldsName(componentType:string, componentId:string, fields:Array<string>):Observable<ComponentGenericResponse> {
62 let params: HttpParams = new HttpParams();
63 fields.forEach((field:string):void => {
64 params = params.append(API_QUERY_PARAMS.INCLUDE, field);
67 return this.http.get<ComponentGenericResponse>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/filteredDataByParams', {params: params})
69 return new ComponentGenericResponse().deserialize(res);
72 protected getServerTypeUrl = (componentType:string):string => {
73 switch (componentType) {
74 case ComponentType.SERVICE:
75 return ServerTypeUrl.SERVICES;
77 return ServerTypeUrl.RESOURCES;
81 getFullComponent(uniqueId:string):Observable<ComponentGenericResponse> {
82 return this.http.get<ComponentGenericResponse>(this.baseUrl + uniqueId)
84 return new ComponentGenericResponse().deserialize(res);
88 getComponentMetadata(uniqueId:string, type:string):Observable<ComponentGenericResponse> {
89 return this.getComponentDataByFieldsName(type, uniqueId, [COMPONENT_FIELDS.COMPONENT_METADATA]);
93 getComponentInstanceAttributesAndProperties(component:Component):Observable<ComponentGenericResponse> {
94 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES_PROPERTIES, COMPONENT_FIELDS.COMPONENT_INSTANCES_ATTRIBUTES]);
97 getComponentInstanceProperties(component:Component): Observable<ComponentGenericResponse> {
98 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES_PROPERTIES]);
101 getComponentAttributes(component:Component):Observable<ComponentGenericResponse> {
102 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_ATTRIBUTES]);
105 getComponentCompositionData(component:Component):Observable<ComponentGenericResponse> {
106 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]);
109 getComponentResourcePropertiesData(component:Component):Observable<ComponentGenericResponse> {
110 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES, COMPONENT_FIELDS.COMPONENT_POLICIES, COMPONENT_FIELDS.COMPONENT_NON_EXCLUDED_GROUPS]);
113 getComponentResourceAttributesData(component:Component):Observable<ComponentGenericResponse> {
114 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES, COMPONENT_FIELDS.COMPONENT_NON_EXCLUDED_GROUPS]);
117 getComponentResourceInstances(component:Component):Observable<ComponentGenericResponse> {
118 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES]);
121 getComponentInputs(component:Component):Observable<ComponentGenericResponse> {
122 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INPUTS]);
125 getComponentInputsWithProperties(component:Component):Observable<ComponentGenericResponse> {
126 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INPUTS, COMPONENT_FIELDS.COMPONENT_INSTANCES, COMPONENT_FIELDS.COMPONENT_INSTANCES_PROPERTIES, COMPONENT_FIELDS.COMPONENT_PROPERTIES]);
129 getComponentDeploymentArtifacts(component:Component):Observable<ComponentGenericResponse> {
130 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_DEPLOYMENT_ARTIFACTS]);
133 getComponentInformationalArtifacts(component:Component):Observable<ComponentGenericResponse> {
134 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INFORMATIONAL_ARTIFACTS]);
137 getComponentInformationalArtifactsAndInstances(component:Component):Observable<ComponentGenericResponse> {
138 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INFORMATIONAL_ARTIFACTS, COMPONENT_FIELDS.COMPONENT_INSTANCES]);
141 getComponentToscaArtifacts(component:Component):Observable<ComponentGenericResponse> {
142 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_TOSCA_ARTIFACTS]);
145 getComponentProperties(component:Component):Observable<ComponentGenericResponse> {
146 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_PROPERTIES]);
149 getInterfaceOperations(component:Component):Observable<ComponentGenericResponse> {
150 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INTERFACE_OPERATIONS]);
153 getInterfaceOperation(component:Component, operation:OperationModel):Observable<OperationModel> {
154 return this.http.get<OperationModel>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/interfaceOperations/' + operation.uniqueId);
157 // tslint:disable-next-line:member-ordering
158 createInterfaceOperation(component:Component, operation:OperationModel):Observable<OperationModel> {
159 const operationList = {
161 [operation.interfaceType]: {
162 'type': operation.interfaceType,
164 [operation.name]: new BEOperationModel(operation)
169 return this.http.post<any>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/interfaceOperations', operationList)
171 const interf:InterfaceModel = _.find(res.interfaces, interf => interf.type === operation.interfaceType);
172 const newOperation:OperationModel = _.find(interf.operations, op => op.name === operation.name);
173 return new OperationModel({
175 interfaceType: interf.type,
176 interfaceId: interf.uniqueId,
177 artifactFileName: operation.artifactFileName
182 // tslint:disable-next-line:member-ordering
183 updateInterfaceOperation(component:Component, operation:OperationModel):Observable<OperationModel> {
184 const operationList = {
186 [operation.interfaceType]: {
187 type: operation.interfaceType,
189 [operation.name]: new BEOperationModel(operation)
194 return this.http.put<any>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/interfaceOperations', operationList)
196 const interf: InterfaceModel = _.find(res.interfaces, interf => interf.type === operation.interfaceType);
197 const newOperation: OperationModel = _.find(interf.operations, op => op.name === operation.name);
198 return new OperationModel({
200 interfaceType: interf.type,
201 interfaceId: interf.uniqueId,
202 artifactFileName: operation.artifactFileName
208 deleteInterfaceOperation(component: Component, operation:OperationModel):Observable<OperationModel> {
209 return this.http.delete<OperationModel>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/interfaces/' + operation.interfaceId + '/operations/' + operation.uniqueId);
212 getInterfaceTypes(component:Component):Observable<{[id:string]: Array<string>}> {
213 return this.http.get<any>(this.baseUrl + 'interfaceLifecycleTypes' + ((component && component.model) ? '?model=' + component.model : ''))
215 const interfaceMap = {};
219 Object.keys(res).forEach(interfaceName => {
220 const interface1 = res[interfaceName];
221 if (!interface1.toscaPresentation.operations) {
224 interfaceMap[interface1.toscaPresentation.type] = Object.keys(interface1.toscaPresentation.operations);
230 uploadInterfaceOperationArtifact(component:Component, newOperation:OperationModel, oldOperation:OperationModel) {
232 artifactType: "WORKFLOW",
233 artifactName: oldOperation.artifactFileName,
234 description: "Workflow Artifact Description",
235 payloadData: oldOperation.artifactData
239 JSON.stringify(payload);
240 const payloadString = JSON.stringify(payload, null, ' ');
241 const md5Result = md5(payloadString).toLowerCase();
242 const headers = new HttpHeaders().append('Content-MD5', btoa(md5Result));
244 return this.http.post(this.baseUrl + component.getTypeUrl() + component.uuid + '/interfaces/' + newOperation.interfaceId + '/operations/' + newOperation.uniqueId + '/artifacts/' + newOperation.implementation.artifactUUID,
246 ).map((res: any) => {
247 const fileName = res.artifactDisplayName || res.artifactName;
248 newOperation.artifactFileName = fileName;
253 getCapabilitiesAndRequirements(componentType: string, componentId:string):Observable<ComponentGenericResponse> {
254 return this.getComponentDataByFieldsName(componentType, componentId, [COMPONENT_FIELDS.COMPONENT_REQUIREMENTS, COMPONENT_FIELDS.COMPONENT_CAPABILITIES]);
261 getDeploymentGraphData(component:Component):Observable<ComponentGenericResponse> {
262 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [COMPONENT_FIELDS.COMPONENT_INSTANCES_RELATION, COMPONENT_FIELDS.COMPONENT_INSTANCES, COMPONENT_FIELDS.COMPONENT_GROUPS]);
265 createInput(component:Component, inputsToCreate:InstancePropertiesAPIMap, isSelf:boolean):Observable<any> {
266 const inputs = isSelf ? { serviceProperties: inputsToCreate.componentInstanceProperties } : inputsToCreate;
267 return this.http.post<any>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/create/inputs', inputs);
270 createListInput(component:Component, input:any, isSelf:boolean):Observable<any> {
273 // change componentInstanceProperties -> serviceProperties
275 componentInstInputsMap: {
276 serviceProperties: input.componentInstInputsMap.componentInstanceProperties
278 listInput: input.listInput
283 return this.http.post<any>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/create/listInput', inputs);
286 createPolicy(component:Component, policiesToCreate:InstancePropertiesAPIMap, isSelf:boolean):Observable<any> {
289 {'componentPropertiesToPolicies': {
290 ...policiesToCreate.componentInstanceProperties
293 {'componentInstancePropertiesToPolicies': {
294 ...policiesToCreate.componentInstanceProperties
297 return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/create/policies', policiesList);
300 deletePolicy(component:Component, policy: PolicyInstance) {
301 return this.http.put<PolicyInstance>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/policies/' + policy.uniqueId + '/undeclare', policy);
304 restoreComponent(componentType:string, componentId:string) {
305 return this.http.post(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/restore', {})
308 archiveComponent(componentType:string, componentId:string) {
309 return this.http.post(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/archive', {})
312 deleteInput(component:Component, input:InputBEModel):Observable<InputBEModel> {
314 return this.http.delete<InputBEModel>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/delete/' + input.uniqueId + '/input')
316 return new InputBEModel(res);
320 deleteOutput(component:Component, output:OutputBEModel):Observable<OutputBEModel> {
322 return this.http.delete<OutputBEModel>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/delete/' + output.uniqueId + '/output')
324 return new OutputBEModel(res);
328 updateComponentInputs(component:Component, inputs:InputBEModel[]):Observable<InputBEModel[]> {
330 return this.http.post<InputBEModel[]>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/update/inputs', inputs)
332 return res.map((input) => new InputBEModel(input));
336 updateComponentOutputs(component:Component, outputs:OutputBEModel[]):Observable<OutputBEModel[]> {
338 return this.http.post<OutputBEModel[]>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/update/outputs', outputs)
340 return res.map((output) => new OutputBEModel(output));
344 filterComponentInstanceProperties(component:Component, filterData:FilterPropertiesAssignmentData):Observable<InstanceBePropertiesMap> {//instance-property-be-map
345 let params: HttpParams = new HttpParams();
346 filterData.selectedTypes.forEach((type:string) => {
347 params = params.append('resourceType', type);
350 return this.http.get<InstanceBePropertiesMap>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/filteredproperties/' + filterData.propertyName, {params: params});
353 filterComponentInstanceAttributes(component:Component, filterData:FilterPropertiesAssignmentData):Observable<InstanceBePropertiesMap> {//instance-property-be-map
354 let params: HttpParams = new HttpParams();
355 filterData.selectedTypes.forEach((type:string) => {
356 params = params.append('resourceType', type);
359 return this.http.get<InstanceBePropertiesMap>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/filteredproperties/' + filterData.propertyName, {params: params});
362 createServiceProperty(component: Component, propertyModel:PropertyBEModel): Observable<PropertyBEModel> {
363 let serverObject = {};
364 serverObject[propertyModel.name] = propertyModel;
365 return this.http.post(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/properties', serverObject)
366 .map((res: PropertyBEModel) => {
367 const property: PropertyBEModel = new PropertyBEModel(res);
372 getServiceProperties(component: Component): Observable<PropertyBEModel[]> {
373 return this.http.get(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/properties')
374 .map((res: PropertyBEModel[]) => {
376 return new Array<PropertyBEModel>();
378 return CommonUtils.initBeProperties(res);
382 updateServiceProperties(component: Component, properties: PropertyBEModel[]) {
383 return this.http.put<PropertyBEModel[]>( this.baseUrl + component.getTypeUrl() + component.uniqueId + '/properties', properties)
386 return _.map(resJson,
387 (resValue:PropertyBEModel) => new PropertyBEModel(resValue));
391 deleteServiceProperty(component:Component, property:PropertyBEModel):Observable<string> {
392 return this.http.delete(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/properties/' + property.uniqueId )
393 .map((res:Response) => {
394 return property.uniqueId;
398 getDependencies(componentType:string, componentId: string):Observable<IDependenciesServerResponse[]> {
399 return this.http.get<IDependenciesServerResponse[]>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/dependencies');
402 automatedUpgrade(componentType:string, componentId:string, componentsIdsToUpgrade:IAutomatedUpgradeRequestObj[]):Observable<AutomatedUpgradeGenericResponse> {
403 return this.http.post<AutomatedUpgradeGenericResponse>(this.baseUrl + this.getServerTypeUrl(componentType) + componentId + '/automatedupgrade', componentsIdsToUpgrade);
406 updateMultipleComponentInstances(componentId:string, instances:ComponentInstance[]):Observable<ComponentInstance[]> {
407 return this.http.post<ComponentInstance[]>(this.baseUrl + componentId + '/resourceInstance/multipleComponentInstance', instances);
410 getServiceFilterConstraints(component:Component):Observable<ComponentGenericResponse> {
411 return this.getComponentDataByFieldsName(component.componentType, component.uniqueId, [SERVICE_FIELDS.NODE_FILTER]);
414 createServiceFilterConstraints(component:Component, componentInstance:ComponentInstance, constraint:ConstraintObject):Observable<any> {
415 return this.http.post<any>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/resourceInstances/' + componentInstance.uniqueId + '/nodeFilter', constraint);
418 updateServiceFilterConstraints(component:Component, componentInstance:ComponentInstance, constraints:ConstraintObject[]):Observable<any> {
419 return this.http.put<any>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/resourceInstances/' + componentInstance.uniqueId + '/nodeFilter/', constraints);
422 deleteServiceFilterConstraints(component:Component, componentInstance:ComponentInstance, constraintIndex:number) {
423 return this.http.delete<any>(this.baseUrl + component.getTypeUrl() + component.uniqueId + '/resourceInstances/' + componentInstance.uniqueId + '/nodeFilter/' + constraintIndex);
426 protected analyzeComponentDataResponse(res: Response):ComponentGenericResponse {
427 return new ComponentGenericResponse().deserialize(res);