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";
25 InstancesInputsPropertiesMap,
41 import {ComponentInstanceFactory, CommonUtils} from "app/utils";
42 import {SharingService} from "app/services-ng2";
43 import {ComponentMetadata} from "../../models/component-metadata";
44 import { DataTypesService } from "app/services";
46 export interface IComponentService {
48 getComponent(id:string);
49 updateComponent(component:Component):ng.IPromise<Component>;
50 changeLifecycleState(component:Component, state:string, userRemarks:AsdcComment):ng.IPromise<ComponentMetadata> ;
51 validateName(newName:string, subtype?:string):ng.IPromise<IValidate>;
52 createComponent(component:Component):ng.IPromise<Component>;
54 importComponent(component: Component): ng.IPromise<Component>;
55 addOrUpdateArtifact(componentId:string, artifact:ArtifactModel):ng.IPromise<ArtifactModel>;
56 deleteArtifact(componentId:string, artifact:string, artifactLabel):ng.IPromise<ArtifactModel>;
57 addProperty(componentId:string, property:PropertyModel):ng.IPromise<PropertyModel>;
58 updateProperty(componentId:string, property:PropertyModel):ng.IPromise<PropertyModel>;
59 addAttribute(componentId:string, attribute:AttributeModel):ng.IPromise<AttributeModel>;
60 updateAttribute(componentId:string, attribute:AttributeModel):ng.IPromise<AttributeModel>;
61 deleteProperty(componentId:string, propertyId:string):ng.IPromise<PropertyModel>;
62 deleteAttribute(componentId:string, attributeId:string):ng.IPromise<AttributeModel>;
63 checkResourceInstanceVersionChange(componentId:string, componentInstanceId:string, componentUid:string):ng.IPromise<any>;
64 changeResourceInstanceVersion(componentId:string, componentInstanceId:string, componentUid:string):ng.IPromise<ComponentInstance>;
65 updateInstanceArtifact(componentId:string, instanceId:string, artifact:ArtifactModel):ng.IPromise<ArtifactModel>;
66 addInstanceArtifact(componentId:string, instanceId:string, artifact:ArtifactModel):ng.IPromise<ArtifactModel>;
67 deleteInstanceArtifact(componentId:string, instanceId:string, artifact:string, artifactLabel):ng.IPromise<ArtifactModel>;
68 createComponentInstance(componentId:string, componentInstance:ComponentInstance):ng.IPromise<ComponentInstance>;
69 updateComponentInstance(componentId:string, componentInstance:ComponentInstance):ng.IPromise<ComponentInstance>;
70 updateMultipleComponentInstances(componentId:string, instances:Array<ComponentInstance>):ng.IPromise< Array<ComponentInstance>>;
71 downloadArtifact(componentId: string, artifactId: string, vendorName?: string): ng.IPromise<IFileDownload>;
72 uploadInstanceEnvFile(componentId:string, instanceId:string, artifact:ArtifactModel):ng.IPromise<ArtifactModel>;
73 downloadInstanceArtifact(componentId:string, instanceId:string, artifactId:string):ng.IPromise<IFileDownload>;
74 deleteComponentInstance(componentId:string, componentInstanceId:string):ng.IPromise<ComponentInstance>;
75 createRelation(componentId:string, link:RelationshipModel):ng.IPromise<RelationshipModel>;
76 deleteRelation(componentId:string, link:RelationshipModel):ng.IPromise<RelationshipModel>;
77 fetchRelation(componentId:string, linkId:string):ng.IPromise<RelationshipModel>;
78 getRequirementsCapabilities(componentId:string):ng.IPromise<any>;
79 updateInstanceProperties(componentId:string, componentInstanceId:string, properties:PropertyModel[]):ng.IPromise<PropertyModel[]>;
80 updateInstanceAttribute(componentId:string, attribute:AttributeModel):ng.IPromise<AttributeModel>;
81 getComponentInstancesFilteredByInputsAndProperties(componentId:string, searchText:string):ng.IPromise<Array<ComponentInstance>>
82 getComponentInstanceInputs(componentId:string, instanceId:string, originComponentUid):ng.IPromise<Array<InputModel>>;
83 getComponentInputs(componentId:string):ng.IPromise<Array<InputModel>>;
84 getComponentInstanceInputProperties(componentId:string, instanceId:string, inputId:string):ng.IPromise<Array<PropertyModel>>;
85 getComponentInstanceProperties(componentId:string, instanceId:string):ng.IPromise<Array<PropertyModel>>;
86 getModuleForDisplay(componentId:string, moduleId:string):ng.IPromise<DisplayModule>;
87 getComponentInstanceModule(componentId:string, componentInstanceId:string, moduleId:string):ng.IPromise<DisplayModule>;
88 updateGroupMetadata(componentId:string, group:Module):ng.IPromise<Module>;
89 getComponentInputInputsAndProperties(serviceId:string, input:string):ng.IPromise<InputsAndProperties>;
90 createInputsFromInstancesInputs(serviceId:string, instancesInputsMap:InstancesInputsPropertiesMap):ng.IPromise<Array<InputModel>>;
91 createInputsFromInstancesInputsProperties(resourceId:string, instanceInputsPropertiesMap:InstancesInputsPropertiesMap):ng.IPromise<Array<PropertyModel>>;
92 deleteComponentInput(serviceId:string, inputId:string):ng.IPromise<InputModel>;
93 getArtifactByGroupType(componentId:string, artifactGroupType:string):ng.IPromise<ArtifactGroupModel>;
94 getComponentInstanceArtifactsByGroupType(componentId:string, componentInstanceId:string, artifactGroupType:string):ng.IPromise<ArtifactGroupModel>;
97 export class ComponentService implements IComponentService {
102 'Sdc.Services.SharingService',
103 'Sdc.Services.DataTypesService',
108 constructor(protected restangular:restangular.IElement,
109 protected sdcConfig:IAppConfigurtaion,
110 protected sharingService:SharingService,
111 protected dataTypeService:DataTypesService,
112 protected $q:ng.IQService,
113 protected $base64:any
116 this.restangular.setBaseUrl(sdcConfig.api.root + sdcConfig.api.component_api_root);
117 this.restangular.setRequestInterceptor(function (elem, operation) {
118 if (operation === "remove") {
123 // this.restangular.setDefaultHeaders({'Content-Type': 'application/json; charset=UTF-8'});
126 //this function is override by each service, we need to change this method to abstract when updtaing typescript version
127 protected createComponentObject = (component:Component):Component => {
131 public getComponent = (id:string):ng.IPromise<Component> => {
132 let deferred = this.$q.defer<Component>();
133 this.restangular.one(id).get().then((response:Component) => {
134 let component:Component = this.createComponentObject(response);
135 //console.log("Component Loaded successfully : ", component);
136 deferred.resolve(component);
138 console.log("Failed to load component with ID: " + id);
139 deferred.reject(err);
141 return deferred.promise;
144 public updateComponent = (component:Component):ng.IPromise<Component> => {
145 // If this is resource
146 if (component instanceof Resource) {
147 let resource:Resource = <Resource>component;
148 if (resource.importedFile) {
149 // Update resource with payload data.
150 return this.updateResourceWithPayload(resource);
152 if (component.csarUUID) {
153 // Update resource without payload data.
154 return this.updateResource(component);
156 // Update resource without payload data (metadata).
157 return this.updateResourceMetadata(component);
161 return this.updateService(component);
165 private updateService = (component:Component):ng.IPromise<Component> => {
166 let deferred = this.$q.defer<Component>();
167 this.restangular.one(component.uniqueId).one("metadata").customPUT(JSON.stringify(component)).then((response:Component) => {
168 let component:Component = this.createComponentObject(response);
169 deferred.resolve(component);
171 deferred.reject(err);
173 return deferred.promise;
176 private updateResource = (component:Component):ng.IPromise<Component> => {
177 let deferred = this.$q.defer<Component>();
178 this.restangular.one(component.uniqueId).customPUT(JSON.stringify(component)).then((response:Component) => {
179 let component:Component = this.createComponentObject(response);
180 deferred.resolve(component);
182 deferred.reject(err);
184 return deferred.promise;
187 private updateResourceMetadata = (component:Component):ng.IPromise<Component> => {
188 let deferred = this.$q.defer<Component>();
189 this.restangular.one(component.uniqueId).one('metadata').customPUT(JSON.stringify(component)).then((response:Component) => {
190 let component:Component = this.createComponentObject(response);
191 deferred.resolve(component);
193 deferred.reject(err);
195 return deferred.promise;
199 * Only resource can be updated with payload data
201 * @returns {IPromise<T>}
203 private updateResourceWithPayload = (resource:Resource):ng.IPromise<Component> => {
204 let deferred = this.$q.defer<Component>();
206 resource.payloadData = resource.importedFile.base64;
207 resource.payloadName = resource.importedFile.filename;
208 let headerObj = this.getHeaderMd5(resource);
210 this.restangular.one(resource.uniqueId).customPUT(JSON.stringify(resource), '', {}, headerObj).then((response:Component) => {
211 let componentResult:Component = this.createComponentObject(response);
212 deferred.resolve(componentResult);
214 deferred.reject(err);
217 return deferred.promise;
220 public createComponent = (component:Component):ng.IPromise<Component> => {
221 let deferred = this.$q.defer<Component>();
222 let headerObj = this.getHeaderMd5(component);
223 this.restangular.customPOST(JSON.stringify(component), '', {}, headerObj).then((response:Component) => {
224 let component:Component = this.createComponentObject(response);
225 deferred.resolve(component);
227 deferred.reject(err);
229 return deferred.promise;
232 public importComponent = (component: Component): ng.IPromise<Component> => {
233 component.vendorName = "xfr";
234 component.vendorRelease = "xfr";
235 let deferred = this.$q.defer<Component>();
236 let headerObj = this.getHeaderMd5(component);
237 this.restangular.customPOST(JSON.stringify(component), 'importService', {}, headerObj).then((response: Component) => {
238 let component: Component = this.createComponentObject(response);
239 this.dataTypeService.loadDataTypesCache(component.model);
240 deferred.resolve(component);
242 deferred.reject(err);
244 return deferred.promise;
247 public validateName = (newName:string, subtype?:string):ng.IPromise<IValidate> => {
248 let deferred = this.$q.defer<IValidate>();
249 this.restangular.one("validate-name").one(newName).get({'subtype': subtype}).then((response:any) => {
250 deferred.resolve(response.plain());
252 deferred.reject(err);
254 return deferred.promise;
257 public changeLifecycleState = (component:Component, state:string, commentObj:AsdcComment):ng.IPromise<ComponentMetadata> => {
258 let deferred = this.$q.defer<ComponentMetadata>();
260 if (commentObj.userRemarks) {
261 headerObj = this.getHeaderMd5(commentObj);
262 this.restangular.one(component.uniqueId).one(state).customPOST(JSON.stringify(commentObj), '', {}, headerObj)
263 .then((response:ComponentMetadata) => {
264 this.sharingService.addUuidValue(response.uniqueId, response.uuid);
265 let component:ComponentMetadata = new ComponentMetadata().deserialize(response);
266 deferred.resolve(component);
268 deferred.reject(err);
271 this.restangular.one(component.uniqueId).one(state).customPOST().then((response:ComponentMetadata) => {
272 this.sharingService.addUuidValue(response.uniqueId, response.uuid);
273 let component:ComponentMetadata = new ComponentMetadata().deserialize(response);
274 deferred.resolve(component);
276 deferred.reject(err);
279 return deferred.promise;
282 // ------------------------------------------------ Artifacts API --------------------------------------------------//
283 public addOrUpdateArtifact = (componentId:string, artifact:ArtifactModel):ng.IPromise<ArtifactModel> => {
284 let deferred = this.$q.defer<ArtifactModel>();
286 if (artifact.payloadData) {
287 headerObj = this.getHeaderMd5(artifact);
289 this.restangular.one(componentId).one("artifacts").customPOST(JSON.stringify(artifact), artifact.uniqueId, {}, headerObj).then((response:any) => {
290 deferred.resolve(response.plain());
292 deferred.reject(err);
294 return deferred.promise;
297 public downloadArtifact = (componentId: string, artifactId: string, vendorName?: string): ng.IPromise<IFileDownload> => {
298 let deferred = this.$q.defer<IFileDownload>();
299 if(vendorName === 'IsService'){
300 this.restangular.one('importService').one(componentId).one("artifacts").one(artifactId).get().then((response: any) => {
301 deferred.resolve(response.plain());
303 deferred.reject(err);
306 this.restangular.one(componentId).one("artifacts").one(artifactId).get().then((response: any) => {
307 deferred.resolve(response.plain());
309 deferred.reject(err);
312 return deferred.promise;
315 public deleteArtifact = (componentId:string, artifactId:string, artifactLabel:string):ng.IPromise<ArtifactModel> => {
316 let deferred = this.$q.defer<ArtifactModel>();
317 this.restangular.one(componentId).one("artifacts").one(artifactId).remove({'operation': artifactLabel}).then((response:ArtifactModel) => {
318 deferred.resolve(response);
320 deferred.reject(err);
322 return deferred.promise;
325 public getArtifactByGroupType = (componentId:string, artifactGroupType:string):ng.IPromise<ArtifactGroupModel> => {
326 let deferred = this.$q.defer<ArtifactGroupModel>();
327 this.restangular.one(componentId).one("artifactsByType").one(artifactGroupType).get().then((response:any) => {
328 var artifacts:ArtifactGroupModel = new ArtifactGroupModel(response.plain());
329 deferred.resolve(artifacts);
331 deferred.reject(err);
333 return deferred.promise;
336 public getComponentInstanceArtifactsByGroupType = (componentId:string, componentInstanceId:string, artifactGroupType:string):ng.IPromise<ArtifactGroupModel> => {
337 let deferred = this.$q.defer<ArtifactGroupModel>();
338 this.restangular.one(componentId).one("resourceInstances").one(componentInstanceId).one("artifactsByType").one(artifactGroupType).get().then((response:any) => {
339 var artifacts:ArtifactGroupModel = new ArtifactGroupModel(response.plain());
340 deferred.resolve(artifacts);
342 deferred.reject(err);
344 return deferred.promise;
348 // ------------------------------------------------ Properties API --------------------------------------------------//
349 public addProperty = (componentId:string, property:PropertyModel):ng.IPromise<PropertyModel> => {
350 let deferred = this.$q.defer<PropertyModel>();
351 this.restangular.one(componentId).one("properties").customPOST(property.convertToServerObject()).then((response:any) => {
352 let property:PropertyModel = new PropertyModel(response[Object.keys(response)[0]]);
353 deferred.resolve(property);
355 deferred.reject(err);
357 return deferred.promise;
360 public updateProperty = (componentId:string, property:PropertyModel):ng.IPromise<PropertyModel> => {
361 let deferred = this.$q.defer<PropertyModel>();
362 this.restangular.one(componentId).one("properties").one(property.uniqueId).customPUT(property.convertToServerObject()).then((response:any) => {
363 let property:PropertyModel = new PropertyModel(response[Object.keys(response)[0]]);
364 deferred.resolve(property);
366 deferred.reject(err);
368 return deferred.promise;
371 public deleteProperty = (componentId:string, propertyId:string):ng.IPromise<PropertyModel> => {
372 let deferred = this.$q.defer<PropertyModel>();
373 this.restangular.one(componentId).one("properties").one(propertyId).remove().then((response:any) => {
374 deferred.resolve(response);
376 deferred.reject(err);
378 return deferred.promise;
381 // ------------------------------------------------ Attributes API --------------------------------------------------//
382 public addAttribute = (componentId:string, attribute:AttributeModel):ng.IPromise<AttributeModel> => {
383 let deferred = this.$q.defer<AttributeModel>();
384 this.restangular.one(componentId).one("attributes").customPOST(attribute.convertToServerObject()).then((response:any) => {
385 let attribute:AttributeModel = new AttributeModel(response);
386 deferred.resolve(attribute);
388 deferred.reject(err);
390 return deferred.promise;
393 public updateAttribute = (componentId:string, attribute:AttributeModel):ng.IPromise<AttributeModel> => {
394 let deferred = this.$q.defer<AttributeModel>();
395 this.restangular.one(componentId).one("attributes").one(attribute.uniqueId).customPUT(attribute.convertToServerObject()).then((response:any) => {
396 let attribute:AttributeModel = new AttributeModel(response);
397 deferred.resolve(attribute);
399 deferred.reject(err);
401 return deferred.promise;
404 public deleteAttribute = (componentId:string, attributeId:string):ng.IPromise<AttributeModel> => {
405 let deferred = this.$q.defer<AttributeModel>();
406 this.restangular.one(componentId).one("attributes").one(attributeId).remove().then((response:any) => {
407 deferred.resolve(response);
409 deferred.reject(err);
411 return deferred.promise;
414 // ------------------------------------------------ Component Instances API --------------------------------------------------//
416 public createComponentInstance = (componentId:string, componentInstance:ComponentInstance):ng.IPromise<ComponentInstance> => {
417 let deferred = this.$q.defer<ComponentInstance>();
418 this.restangular.one(componentId).one("resourceInstance").customPOST(JSON.stringify(componentInstance)).then((response:any) => {
419 let componentInstance:ComponentInstance = ComponentInstanceFactory.createComponentInstance(response);
420 console.log("Component Instance created", componentInstance);
421 deferred.resolve(componentInstance);
423 console.log("Failed to create componentInstance. With Name: " + componentInstance.name);
424 deferred.reject(err);
426 return deferred.promise;
429 public updateComponentInstance = (componentId:string, componentInstance:ComponentInstance):ng.IPromise<ComponentInstance> => {
430 let deferred = this.$q.defer<ComponentInstance>();
431 this.restangular.one(componentId).one("resourceInstance").one(componentInstance.uniqueId).customPOST(JSON.stringify(componentInstance)).then((response:any) => {
432 let componentInstance:ComponentInstance = ComponentInstanceFactory.createComponentInstance(response);
433 console.log("Component Instance was updated", componentInstance);
434 deferred.resolve(componentInstance);
436 console.log("Failed to update componentInstance. With ID: " + componentInstance.uniqueId + "Name: " + componentInstance.name);
437 deferred.reject(err);
439 return deferred.promise;
442 public updateMultipleComponentInstances = (componentId:string, instances:Array<ComponentInstance>):ng.IPromise<Array<ComponentInstance>> => {
443 let deferred = this.$q.defer<Array<ComponentInstance>>();
444 this.restangular.one(componentId).one("resourceInstance/multipleComponentInstance").customPOST(JSON.stringify(instances)).then((response:any) => {
445 console.log("Multiple Component Instances was updated", response);
446 let updateInstances:Array<ComponentInstance> = new Array<ComponentInstance>();
447 _.forEach(response, (componentInstance:ComponentInstance) => {
448 let updatedComponentInstance:ComponentInstance = ComponentInstanceFactory.createComponentInstance(componentInstance);
449 updateInstances.push(updatedComponentInstance);
451 deferred.resolve(updateInstances);
453 console.log("Failed to update Multiple componentInstance.");
454 deferred.reject(err);
456 return deferred.promise;
459 public deleteComponentInstance = (componentId:string, componentInstanceId:string):ng.IPromise<ComponentInstance> => {
460 let deferred = this.$q.defer<ComponentInstance>();
461 this.restangular.one(componentId).one("resourceInstance").one(componentInstanceId).remove().then(() => {
462 console.log("Component Instance was deleted");
465 console.log("Failed to delete componentInstance. With ID: " + componentInstanceId);
466 deferred.reject(err);
468 return deferred.promise;
471 public checkResourceInstanceVersionChange = (componentId:string, componentInstanceId:string, componentUid:string):ng.IPromise<ComponentInstance> => {
472 let deferred = this.$q.defer<ComponentInstance>();
473 this.restangular.one(componentId).one("resourceInstance").one(componentInstanceId).one(componentUid).one("checkForwardingPathOnVersionChange").get().then((response:any) => {
474 deferred.resolve(response);
476 deferred.reject(err);
478 return deferred.promise;
481 public changeResourceInstanceVersion = (componentId:string, componentInstanceId:string, componentUid:string):ng.IPromise<ComponentInstance> => {
482 let deferred = this.$q.defer<ComponentInstance>();
483 this.restangular.one(componentId).one("resourceInstance").one(componentInstanceId).one("changeVersion").customPOST({'componentUid': componentUid}).then((response:any) => {
484 let componentInstance:ComponentInstance = ComponentInstanceFactory.createComponentInstance(response);
485 deferred.resolve(componentInstance);
487 deferred.reject(err);
489 return deferred.promise;
492 public downloadInstanceArtifact = (componentId:string, instanceId:string, artifactId:string):ng.IPromise<IFileDownload> => {
493 let deferred = this.$q.defer<IFileDownload>();
494 this.restangular.one(componentId).one("resourceInstances").one(instanceId).one("artifacts").one(artifactId).get().then((response:any) => {
495 deferred.resolve(response.plain());
497 deferred.reject(err);
499 return deferred.promise;
502 public updateInstanceArtifact = (componentId:string, instanceId:string, artifact:ArtifactModel):ng.IPromise<ArtifactModel> => {
503 let deferred = this.$q.defer<ArtifactModel>();
505 if (artifact.payloadData) {
506 headerObj = this.getHeaderMd5(artifact);
508 this.restangular.one(componentId).one("resourceInstance").one(instanceId).one("artifacts").customPOST(JSON.stringify(artifact), artifact.uniqueId, {}, headerObj).then((response:any) => {
509 let newArtifact = new ArtifactModel(response);
510 deferred.resolve(newArtifact);
512 deferred.reject(err);
514 return deferred.promise;
517 public addInstanceArtifact = (componentId:string, instanceId:string, artifact:ArtifactModel):ng.IPromise<ArtifactModel> => {
518 let deferred = this.$q.defer<ArtifactModel>();
520 if (artifact.payloadData) {
521 headerObj = this.getHeaderMd5(artifact);
523 this.restangular.one(componentId).one("resourceInstance").one(instanceId).one("artifacts").customPOST(JSON.stringify(artifact), artifact.uniqueId, {}, headerObj).then((response:any) => {
524 let artifact:ArtifactModel = new ArtifactModel(response.plain());
525 deferred.resolve(artifact);
527 deferred.reject(err);
529 return deferred.promise;
532 public deleteInstanceArtifact = (componentId:string, instanceId:string, artifactId:string, artifactLabel:string):ng.IPromise<ArtifactModel> => {
533 let deferred = this.$q.defer<ArtifactModel>();
534 this.restangular.one(componentId).one("resourceInstance").one(instanceId).one("artifacts").one(artifactId).remove({'operation': artifactLabel}).then((response:ArtifactModel) => {
535 deferred.resolve(response);
537 deferred.reject(err);
539 return deferred.promise;
542 public uploadInstanceEnvFile = (componentId:string, instanceId:string, artifact:ArtifactModel):ng.IPromise<ArtifactModel> => {
543 let deferred = this.$q.defer<ArtifactModel>();
545 if (artifact.payloadData) {
546 headerObj = this.getHeaderMd5(artifact);
548 this.restangular.one(componentId).one("resourceInstance").one(instanceId).one("artifacts").customPOST(JSON.stringify(artifact), artifact.uniqueId, {}, headerObj).then((response:any) => {
549 let newArtifact = new ArtifactModel(response);
550 deferred.resolve(newArtifact);
552 deferred.reject(err);
554 return deferred.promise;
557 public updateInstanceProperties = (componentId:string, componentInstanceId:string, properties:PropertyModel[]):ng.IPromise<PropertyModel[]> => {
558 let deferred = this.$q.defer<PropertyModel[]>();
559 this.restangular.one(componentId).one("resourceInstance").one(componentInstanceId).one("properties").customPOST(JSON.stringify(properties)).then((response:any) => {
560 const newProperties = response.map((res) => {
561 const newProperty = new PropertyModel(res);
562 newProperty.readonly = true;
563 newProperty.resourceInstanceUniqueId = componentInstanceId;
566 deferred.resolve(newProperties);
568 deferred.reject(err);
570 return deferred.promise;
573 public updateInstanceAttribute = (componentId:string, attribute:AttributeModel):ng.IPromise<AttributeModel> => {
574 let deferred = this.$q.defer<AttributeModel>();
575 let instanceId = attribute.resourceInstanceUniqueId;
576 this.restangular.one(componentId).one("resourceInstance").one(instanceId).one("attribute").customPOST(JSON.stringify(attribute)).then((response:any) => {
577 let newAttribute = new AttributeModel(response);
578 newAttribute.readonly = true;
579 newAttribute.resourceInstanceUniqueId = instanceId;
580 deferred.resolve(newAttribute);
582 deferred.reject(err);
584 return deferred.promise;
587 public createRelation = (componentId:string, link:RelationshipModel):ng.IPromise<RelationshipModel> => {
588 let deferred = this.$q.defer<RelationshipModel>();
589 const linkPayload:RelationshipModel = new RelationshipModel(link);
590 linkPayload.relationships.forEach((rel) => {
591 delete rel.capability;
592 delete rel.requirement;
594 this.restangular.one(componentId).one("resourceInstance").one("associate").customPOST(JSON.stringify(linkPayload)).then((response:any) => {
595 let relation:RelationshipModel = new RelationshipModel(response.plain());
596 console.log("Link created successfully ", relation);
597 deferred.resolve(relation);
599 console.log("Failed to create Link From: " + link.fromNode + "To: " + link.toNode);
600 deferred.reject(err);
602 return deferred.promise;
605 public deleteRelation = (componentId:string, link:RelationshipModel):ng.IPromise<RelationshipModel> => {
606 let deferred = this.$q.defer<RelationshipModel>();
607 const linkPayload:RelationshipModel = new RelationshipModel(link);
608 linkPayload.relationships.forEach((rel) => {
609 delete rel.capability;
610 delete rel.requirement;
612 this.restangular.one(componentId).one("resourceInstance").one("dissociate").customPUT(JSON.stringify(linkPayload)).then((response:any) => {
613 let relation:RelationshipModel = new RelationshipModel(response);
614 console.log("Link deleted successfully ", relation);
615 deferred.resolve(relation);
617 console.log("Failed to delete Link From: " + link.fromNode + "To: " + link.toNode);
618 deferred.reject(err);
620 return deferred.promise;
623 public fetchRelation = (componentId:string, linkId:string):ng.IPromise<RelationshipModel> => {
624 let deferred = this.$q.defer<RelationshipModel>();
625 this.restangular.one(componentId).one("relationId").one(linkId).get().then((response:any) => {
626 let relation:RelationshipModel = new RelationshipModel(response);
627 console.log("Link fetched successfully ", relation);
628 deferred.resolve(relation);
630 console.log("Failed to fetch Link Id: " + linkId);
631 deferred.reject(err);
633 return deferred.promise;
636 public getRequirementsCapabilities = (componentId:string):ng.IPromise<any> => {
637 let deferred = this.$q.defer();
638 this.restangular.one(componentId).one("requirmentsCapabilities").get().then((response:any) => {
639 console.log("Component requirement capabilities recived: ", response);
640 deferred.resolve(response);
642 console.log("Failed to get requirements & capabilities");
643 deferred.reject(err);
645 return deferred.promise;
648 public getModuleForDisplay = (componentId:string, moduleId:string):ng.IPromise<DisplayModule> => {
649 let deferred = this.$q.defer<DisplayModule>();
650 this.restangular.one(componentId).one("groups").one(moduleId).get().then((response:any) => {
651 console.log("module loaded successfully: ", response);
652 let module:DisplayModule = new DisplayModule(response);
653 deferred.resolve(module);
655 console.log("Failed to get module with id: ", moduleId);
656 deferred.reject(err);
658 return deferred.promise;
661 public getComponentInstanceModule = (componentId:string, componentInstanceId:string, moduleId:string):ng.IPromise<DisplayModule> => {
662 let deferred = this.$q.defer<DisplayModule>();
663 this.restangular.one(componentId).one("resourceInstance").one(componentInstanceId).one("groupInstance").one(moduleId).get().then((response:any) => {
664 console.log("module loaded successfully: ", response);
665 let module:DisplayModule = new DisplayModule(response);
666 deferred.resolve(module);
668 console.log("Failed to get module with id: ", moduleId);
669 deferred.reject(err);
671 return deferred.promise;
674 public getComponentInstancesFilteredByInputsAndProperties = (componentId:string, searchText?:string):ng.IPromise<Array<ComponentInstance>> => {
675 let deferred = this.$q.defer<Array<ComponentInstance>>();
676 this.restangular.one(componentId).one("componentInstances").get({'searchText': searchText}).then((response:any) => {
677 console.log("component instances return successfully: ", response);
678 let componentInstances:Array<ComponentInstance> = CommonUtils.initComponentInstances(response);
679 deferred.resolve(componentInstances);
681 console.log("Failed to get component instances of component with id: " + componentId);
682 deferred.reject(err);
685 return deferred.promise;
688 public getComponentInstanceInputs = (componentId:string, instanceId:string, originComponentUid):ng.IPromise<Array<InputModel>> => {
690 let deferred = this.$q.defer<Array<InputModel>>();
691 this.restangular.one(componentId).one("componentInstances").one(instanceId).one(originComponentUid).one("inputs").get().then((response:any) => {
692 console.log("component instance input return successfully: ", response);
693 let inputsArray:Array<InputModel> = new Array<InputModel>();
694 _.forEach(response, (inputObj:InputModel) => {
695 inputsArray.push(new InputModel(inputObj));
697 deferred.resolve(inputsArray);
699 console.log("Failed to get component instance input with id: " + instanceId);
700 deferred.reject(err);
703 return deferred.promise;
706 public getComponentInputs = (componentId:string):ng.IPromise<Array<InputModel>> => {
708 let deferred = this.$q.defer<Array<InputModel>>();
709 this.restangular.one(componentId).one("inputs").get().then((response:any) => {
710 console.log("component inputs return successfully: ", response);
711 let inputsArray:Array<InputModel> = new Array<InputModel>();
712 _.forEach(response, (inputObj:InputModel) => {
713 inputsArray.push(new InputModel(inputObj));
715 deferred.resolve(inputsArray);
717 console.log("Failed to get component inputs for component with id: " + componentId);
718 deferred.reject(err);
721 return deferred.promise;
724 public getComponentInstanceInputProperties = (componentId:string, instanceId:string, inputId:string):ng.IPromise<Array<PropertyModel>> => {
726 let deferred = this.$q.defer<Array<PropertyModel>>();
727 this.restangular.one(componentId).one("componentInstances").one(instanceId).one(inputId).one("properties").get().then((response:any) => {
728 console.log("component instance input properties return successfully: ", response);
729 let propertiesArray:Array<PropertyModel> = new Array<PropertyModel>();
730 _.forEach(response, (propertyObj:PropertyModel) => {
731 propertiesArray.push(new PropertyModel(propertyObj));
733 deferred.resolve(propertiesArray);
735 console.log("Failed to get component instance input properties with instanceId: " + instanceId + "and input id: " + inputId);
736 deferred.reject(err);
739 return deferred.promise;
743 public getComponentInstanceProperties = (componentId:string, instanceId:string):ng.IPromise<Array<PropertyModel>> => {
744 let deferred = this.$q.defer<Array<PropertyModel>>();
745 this.restangular.one(componentId).one("componentInstances").one(instanceId).one("properties").get().then((response:any) => {
746 console.log("component instance properties return successfully: ", response);
747 let propertiesArray:Array<PropertyModel> = new Array<PropertyModel>();
748 _.forEach(response, (propertyObj:PropertyModel) => {
749 propertiesArray.push(new PropertyModel(propertyObj));
751 deferred.resolve(propertiesArray);
753 console.log("Failed to get component instance properties with instanceId: " + instanceId);
754 deferred.reject(err);
757 return deferred.promise;
760 public updateGroupMetadata = (componentId:string, group:Module):ng.IPromise<Module> => {
762 let deferred = this.$q.defer<Module>();
763 this.restangular.one(componentId).one("groups").one(group.uniqueId).one("metadata").customPUT(JSON.stringify(group)).then((response:Module) => {
764 console.log("group metadata updated successfully: ", response);
765 let updatedGroup:Module = new Module(response);
767 deferred.resolve(updatedGroup);
769 console.log("Failed to update group metadata for component: " + componentId + " for group with id: " + group.uniqueId);
770 deferred.reject(err);
773 return deferred.promise;
776 public getComponentInputInputsAndProperties = (serviceId:string, inputId:string):ng.IPromise<InputsAndProperties> => {
777 let defer = this.$q.defer<InputsAndProperties>();
778 this.restangular.one(serviceId).one("inputs").one(inputId).get().then((response:InputsAndProperties) => {
780 let inputsArray:Array<InputModel> = new Array<InputModel>();
781 _.forEach(response.inputs, (inputObj:InputModel) => {
782 inputsArray.push(new InputModel(inputObj));
785 let propertiesArray:Array<PropertyModel> = new Array<PropertyModel>();
786 _.forEach(response.properties, (property:PropertyModel) => {
787 propertiesArray.push(new PropertyModel(property));
790 defer.resolve(new InputsAndProperties(inputsArray, propertiesArray));
792 console.log("failed to get inputs of input : ", err);
795 return defer.promise;
798 createInputsFromInstancesInputsProperties = (resourceId:string, instancePropertyMap:InstancesInputsPropertiesMap):ng.IPromise<Array<PropertyModel>> => {
799 let defer = this.$q.defer<Array<PropertyModel>>();
800 this.restangular.one(resourceId).one("create/properties").customPOST(instancePropertyMap).then((response:any) => {
801 let inputsArray:Array<PropertyModel> = new Array<PropertyModel>();
802 _.forEach(response, (inputObj:PropertyModel) => {
803 inputsArray.push(new PropertyModel(inputObj));
805 defer.resolve(inputsArray);
807 console.log("failed to create service inputs from VF instances inputs : ", err);
810 return defer.promise;
813 createInputsFromInstancesInputs = (serviceId:string, instancesMap:InstancesInputsPropertiesMap):ng.IPromise<Array<InputModel>> => {
814 let defer = this.$q.defer<Array<InputModel>>();
815 this.restangular.one(serviceId).one("create/inputs").customPOST(instancesMap).then((response:any) => {
816 let inputsArray:Array<InputModel> = new Array<InputModel>();
817 _.forEach(response, (inputObj:InputModel) => {
818 inputsArray.push(new InputModel(inputObj));
820 defer.resolve(inputsArray);
822 console.log("failed to create service inputs from VF instances inputs : ", err);
825 return defer.promise;
828 deleteComponentInput = (serviceId:string, inputId:string):ng.IPromise<InputModel> => {
829 let defer = this.$q.defer<InputModel>();
830 this.restangular.one(serviceId).one("delete").one(inputId).one("input").remove().then((response:any) => {
831 let inputToDelete = new InputModel(response);
832 defer.resolve(inputToDelete);
834 console.log("failed to delete input from service: ", err);
837 return defer.promise;
840 private getHeaderMd5 = (object:any):any => {
842 // This is ugly workaround!!!
843 // The md5 result is not correct if we do not add the line JSON.stringify(resource); twice.
844 JSON.stringify(object);
845 let componentString:string = JSON.stringify(object);
846 let md5Result = md5(componentString).toLowerCase();
847 headerObj = {'Content-MD5': this.$base64.encode(md5Result)};