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";
45 export interface IComponentService {
47 getComponent(id:string);
48 updateComponent(component:Component):ng.IPromise<Component>;
49 changeLifecycleState(component:Component, state:string, userRemarks:AsdcComment):ng.IPromise<ComponentMetadata> ;
50 validateName(newName:string, subtype?:string):ng.IPromise<IValidate>;
51 createComponent(component:Component):ng.IPromise<Component>;
53 importComponent(component: Component): ng.IPromise<Component>;
54 addOrUpdateArtifact(componentId:string, artifact:ArtifactModel):ng.IPromise<ArtifactModel>;
55 deleteArtifact(componentId:string, artifact:string, artifactLabel):ng.IPromise<ArtifactModel>;
56 addProperty(componentId:string, property:PropertyModel):ng.IPromise<PropertyModel>;
57 updateProperty(componentId:string, property:PropertyModel):ng.IPromise<PropertyModel>;
58 addAttribute(componentId:string, attribute:AttributeModel):ng.IPromise<AttributeModel>;
59 updateAttribute(componentId:string, attribute:AttributeModel):ng.IPromise<AttributeModel>;
60 deleteProperty(componentId:string, propertyId:string):ng.IPromise<PropertyModel>;
61 deleteAttribute(componentId:string, attributeId:string):ng.IPromise<AttributeModel>;
62 checkResourceInstanceVersionChange(componentId:string, componentInstanceId:string, componentUid:string):ng.IPromise<any>;
63 changeResourceInstanceVersion(componentId:string, componentInstanceId:string, componentUid:string):ng.IPromise<ComponentInstance>;
64 updateInstanceArtifact(componentId:string, instanceId:string, artifact:ArtifactModel):ng.IPromise<ArtifactModel>;
65 addInstanceArtifact(componentId:string, instanceId:string, artifact:ArtifactModel):ng.IPromise<ArtifactModel>;
66 deleteInstanceArtifact(componentId:string, instanceId:string, artifact:string, artifactLabel):ng.IPromise<ArtifactModel>;
67 createComponentInstance(componentId:string, componentInstance:ComponentInstance):ng.IPromise<ComponentInstance>;
68 updateComponentInstance(componentId:string, componentInstance:ComponentInstance):ng.IPromise<ComponentInstance>;
69 updateMultipleComponentInstances(componentId:string, instances:Array<ComponentInstance>):ng.IPromise< Array<ComponentInstance>>;
70 downloadArtifact(componentId: string, artifactId: string, vendorName?: string): ng.IPromise<IFileDownload>;
71 uploadInstanceEnvFile(componentId:string, instanceId:string, artifact:ArtifactModel):ng.IPromise<ArtifactModel>;
72 downloadInstanceArtifact(componentId:string, instanceId:string, artifactId:string):ng.IPromise<IFileDownload>;
73 deleteComponentInstance(componentId:string, componentInstanceId:string):ng.IPromise<ComponentInstance>;
74 createRelation(componentId:string, link:RelationshipModel):ng.IPromise<RelationshipModel>;
75 deleteRelation(componentId:string, link:RelationshipModel):ng.IPromise<RelationshipModel>;
76 fetchRelation(componentId:string, linkId:string):ng.IPromise<RelationshipModel>;
77 getRequirementsCapabilities(componentId:string):ng.IPromise<any>;
78 updateInstanceProperties(componentId:string, componentInstanceId:string, properties:PropertyModel[]):ng.IPromise<PropertyModel[]>;
79 updateInstanceAttribute(componentId:string, attribute:AttributeModel):ng.IPromise<AttributeModel>;
80 getComponentInstancesFilteredByInputsAndProperties(componentId:string, searchText:string):ng.IPromise<Array<ComponentInstance>>
81 getComponentInstanceInputs(componentId:string, instanceId:string, originComponentUid):ng.IPromise<Array<InputModel>>;
82 getComponentInputs(componentId:string):ng.IPromise<Array<InputModel>>;
83 getComponentInstanceInputProperties(componentId:string, instanceId:string, inputId:string):ng.IPromise<Array<PropertyModel>>;
84 getComponentInstanceProperties(componentId:string, instanceId:string):ng.IPromise<Array<PropertyModel>>;
85 getModuleForDisplay(componentId:string, moduleId:string):ng.IPromise<DisplayModule>;
86 getComponentInstanceModule(componentId:string, componentInstanceId:string, moduleId:string):ng.IPromise<DisplayModule>;
87 updateGroupMetadata(componentId:string, group:Module):ng.IPromise<Module>;
88 getComponentInputInputsAndProperties(serviceId:string, input:string):ng.IPromise<InputsAndProperties>;
89 createInputsFromInstancesInputs(serviceId:string, instancesInputsMap:InstancesInputsPropertiesMap):ng.IPromise<Array<InputModel>>;
90 createInputsFromInstancesInputsProperties(resourceId:string, instanceInputsPropertiesMap:InstancesInputsPropertiesMap):ng.IPromise<Array<PropertyModel>>;
91 deleteComponentInput(serviceId:string, inputId:string):ng.IPromise<InputModel>;
92 getArtifactByGroupType(componentId:string, artifactGroupType:string):ng.IPromise<ArtifactGroupModel>;
93 getComponentInstanceArtifactsByGroupType(componentId:string, componentInstanceId:string, artifactGroupType:string):ng.IPromise<ArtifactGroupModel>;
96 export class ComponentService implements IComponentService {
101 'Sdc.Services.SharingService',
106 constructor(protected restangular:restangular.IElement,
107 protected sdcConfig:IAppConfigurtaion,
108 protected sharingService:SharingService,
109 protected $q:ng.IQService,
110 protected $base64:any
113 this.restangular.setBaseUrl(sdcConfig.api.root + sdcConfig.api.component_api_root);
114 this.restangular.setRequestInterceptor(function (elem, operation) {
115 if (operation === "remove") {
120 // this.restangular.setDefaultHeaders({'Content-Type': 'application/json; charset=UTF-8'});
123 //this function is override by each service, we need to change this method to abstract when updtaing typescript version
124 protected createComponentObject = (component:Component):Component => {
128 public getComponent = (id:string):ng.IPromise<Component> => {
129 let deferred = this.$q.defer<Component>();
130 this.restangular.one(id).get().then((response:Component) => {
131 let component:Component = this.createComponentObject(response);
132 //console.log("Component Loaded successfully : ", component);
133 deferred.resolve(component);
135 console.log("Failed to load component with ID: " + id);
136 deferred.reject(err);
138 return deferred.promise;
141 public updateComponent = (component:Component):ng.IPromise<Component> => {
142 // If this is resource
143 if (component instanceof Resource) {
144 let resource:Resource = <Resource>component;
145 if (resource.importedFile) {
146 // Update resource with payload data.
147 return this.updateResourceWithPayload(resource);
149 if (component.csarUUID) {
150 // Update resource without payload data.
151 return this.updateResource(component);
153 // Update resource without payload data (metadata).
154 return this.updateResourceMetadata(component);
158 return this.updateService(component);
162 private updateService = (component:Component):ng.IPromise<Component> => {
163 let deferred = this.$q.defer<Component>();
164 this.restangular.one(component.uniqueId).one("metadata").customPUT(JSON.stringify(component)).then((response:Component) => {
165 let component:Component = this.createComponentObject(response);
166 deferred.resolve(component);
168 deferred.reject(err);
170 return deferred.promise;
173 private updateResource = (component:Component):ng.IPromise<Component> => {
174 let deferred = this.$q.defer<Component>();
175 this.restangular.one(component.uniqueId).customPUT(JSON.stringify(component)).then((response:Component) => {
176 let component:Component = this.createComponentObject(response);
177 deferred.resolve(component);
179 deferred.reject(err);
181 return deferred.promise;
184 private updateResourceMetadata = (component:Component):ng.IPromise<Component> => {
185 let deferred = this.$q.defer<Component>();
186 this.restangular.one(component.uniqueId).one('metadata').customPUT(JSON.stringify(component)).then((response:Component) => {
187 let component:Component = this.createComponentObject(response);
188 deferred.resolve(component);
190 deferred.reject(err);
192 return deferred.promise;
196 * Only resource can be updated with payload data
198 * @returns {IPromise<T>}
200 private updateResourceWithPayload = (resource:Resource):ng.IPromise<Component> => {
201 let deferred = this.$q.defer<Component>();
203 resource.payloadData = resource.importedFile.base64;
204 resource.payloadName = resource.importedFile.filename;
205 let headerObj = this.getHeaderMd5(resource);
207 this.restangular.one(resource.uniqueId).customPUT(JSON.stringify(resource), '', {}, headerObj).then((response:Component) => {
208 let componentResult:Component = this.createComponentObject(response);
209 deferred.resolve(componentResult);
211 deferred.reject(err);
214 return deferred.promise;
217 public createComponent = (component:Component):ng.IPromise<Component> => {
218 let deferred = this.$q.defer<Component>();
219 let headerObj = this.getHeaderMd5(component);
220 this.restangular.customPOST(JSON.stringify(component), '', {}, headerObj).then((response:Component) => {
221 let component:Component = this.createComponentObject(response);
222 deferred.resolve(component);
224 deferred.reject(err);
226 return deferred.promise;
229 public importComponent = (component: Component): ng.IPromise<Component> => {
230 component.vendorName = "xfr";
231 component.vendorRelease = "xfr";
232 let deferred = this.$q.defer<Component>();
233 let headerObj = this.getHeaderMd5(component);
234 this.restangular.customPOST(JSON.stringify(component), 'importService', {}, headerObj).then((response: Component) => {
235 let component: Component = this.createComponentObject(response);
236 deferred.resolve(component);
238 deferred.reject(err);
240 return deferred.promise;
243 public validateName = (newName:string, subtype?:string):ng.IPromise<IValidate> => {
244 let deferred = this.$q.defer<IValidate>();
245 this.restangular.one("validate-name").one(newName).get({'subtype': subtype}).then((response:any) => {
246 deferred.resolve(response.plain());
248 deferred.reject(err);
250 return deferred.promise;
253 public changeLifecycleState = (component:Component, state:string, commentObj:AsdcComment):ng.IPromise<ComponentMetadata> => {
254 let deferred = this.$q.defer<ComponentMetadata>();
256 if (commentObj.userRemarks) {
257 headerObj = this.getHeaderMd5(commentObj);
258 this.restangular.one(component.uniqueId).one(state).customPOST(JSON.stringify(commentObj), '', {}, headerObj)
259 .then((response:ComponentMetadata) => {
260 this.sharingService.addUuidValue(response.uniqueId, response.uuid);
261 let component:ComponentMetadata = new ComponentMetadata().deserialize(response);
262 deferred.resolve(component);
264 deferred.reject(err);
267 this.restangular.one(component.uniqueId).one(state).customPOST().then((response:ComponentMetadata) => {
268 this.sharingService.addUuidValue(response.uniqueId, response.uuid);
269 let component:ComponentMetadata = new ComponentMetadata().deserialize(response);
270 deferred.resolve(component);
272 deferred.reject(err);
275 return deferred.promise;
278 // ------------------------------------------------ Artifacts API --------------------------------------------------//
279 public addOrUpdateArtifact = (componentId:string, artifact:ArtifactModel):ng.IPromise<ArtifactModel> => {
280 let deferred = this.$q.defer<ArtifactModel>();
282 if (artifact.payloadData) {
283 headerObj = this.getHeaderMd5(artifact);
285 this.restangular.one(componentId).one("artifacts").customPOST(JSON.stringify(artifact), artifact.uniqueId, {}, headerObj).then((response:any) => {
286 deferred.resolve(response.plain());
288 deferred.reject(err);
290 return deferred.promise;
293 public downloadArtifact = (componentId: string, artifactId: string, vendorName?: string): ng.IPromise<IFileDownload> => {
294 let deferred = this.$q.defer<IFileDownload>();
295 if(vendorName === 'IsService'){
296 this.restangular.one('importService').one(componentId).one("artifacts").one(artifactId).get().then((response: any) => {
297 deferred.resolve(response.plain());
299 deferred.reject(err);
302 this.restangular.one(componentId).one("artifacts").one(artifactId).get().then((response: any) => {
303 deferred.resolve(response.plain());
305 deferred.reject(err);
308 return deferred.promise;
311 public deleteArtifact = (componentId:string, artifactId:string, artifactLabel:string):ng.IPromise<ArtifactModel> => {
312 let deferred = this.$q.defer<ArtifactModel>();
313 this.restangular.one(componentId).one("artifacts").one(artifactId).remove({'operation': artifactLabel}).then((response:ArtifactModel) => {
314 deferred.resolve(response);
316 deferred.reject(err);
318 return deferred.promise;
321 public getArtifactByGroupType = (componentId:string, artifactGroupType:string):ng.IPromise<ArtifactGroupModel> => {
322 let deferred = this.$q.defer<ArtifactGroupModel>();
323 this.restangular.one(componentId).one("artifactsByType").one(artifactGroupType).get().then((response:any) => {
324 var artifacts:ArtifactGroupModel = new ArtifactGroupModel(response.plain());
325 deferred.resolve(artifacts);
327 deferred.reject(err);
329 return deferred.promise;
332 public getComponentInstanceArtifactsByGroupType = (componentId:string, componentInstanceId:string, artifactGroupType:string):ng.IPromise<ArtifactGroupModel> => {
333 let deferred = this.$q.defer<ArtifactGroupModel>();
334 this.restangular.one(componentId).one("resourceInstances").one(componentInstanceId).one("artifactsByType").one(artifactGroupType).get().then((response:any) => {
335 var artifacts:ArtifactGroupModel = new ArtifactGroupModel(response.plain());
336 deferred.resolve(artifacts);
338 deferred.reject(err);
340 return deferred.promise;
344 // ------------------------------------------------ Properties API --------------------------------------------------//
345 public addProperty = (componentId:string, property:PropertyModel):ng.IPromise<PropertyModel> => {
346 let deferred = this.$q.defer<PropertyModel>();
347 this.restangular.one(componentId).one("properties").customPOST(property.convertToServerObject()).then((response:any) => {
348 let property:PropertyModel = new PropertyModel(response[Object.keys(response)[0]]);
349 deferred.resolve(property);
351 deferred.reject(err);
353 return deferred.promise;
356 public updateProperty = (componentId:string, property:PropertyModel):ng.IPromise<PropertyModel> => {
357 let deferred = this.$q.defer<PropertyModel>();
358 this.restangular.one(componentId).one("properties").one(property.uniqueId).customPUT(property.convertToServerObject()).then((response:any) => {
359 let property:PropertyModel = new PropertyModel(response[Object.keys(response)[0]]);
360 deferred.resolve(property);
362 deferred.reject(err);
364 return deferred.promise;
367 public deleteProperty = (componentId:string, propertyId:string):ng.IPromise<PropertyModel> => {
368 let deferred = this.$q.defer<PropertyModel>();
369 this.restangular.one(componentId).one("properties").one(propertyId).remove().then((response:any) => {
370 deferred.resolve(response);
372 deferred.reject(err);
374 return deferred.promise;
377 // ------------------------------------------------ Attributes API --------------------------------------------------//
378 public addAttribute = (componentId:string, attribute:AttributeModel):ng.IPromise<AttributeModel> => {
379 let deferred = this.$q.defer<AttributeModel>();
380 this.restangular.one(componentId).one("attributes").customPOST(attribute.convertToServerObject()).then((response:any) => {
381 let attribute:AttributeModel = new AttributeModel(response);
382 deferred.resolve(attribute);
384 deferred.reject(err);
386 return deferred.promise;
389 public updateAttribute = (componentId:string, attribute:AttributeModel):ng.IPromise<AttributeModel> => {
390 let deferred = this.$q.defer<AttributeModel>();
391 this.restangular.one(componentId).one("attributes").one(attribute.uniqueId).customPUT(attribute.convertToServerObject()).then((response:any) => {
392 let attribute:AttributeModel = new AttributeModel(response);
393 deferred.resolve(attribute);
395 deferred.reject(err);
397 return deferred.promise;
400 public deleteAttribute = (componentId:string, attributeId:string):ng.IPromise<AttributeModel> => {
401 let deferred = this.$q.defer<AttributeModel>();
402 this.restangular.one(componentId).one("attributes").one(attributeId).remove().then((response:any) => {
403 deferred.resolve(response);
405 deferred.reject(err);
407 return deferred.promise;
410 // ------------------------------------------------ Component Instances API --------------------------------------------------//
412 public createComponentInstance = (componentId:string, componentInstance:ComponentInstance):ng.IPromise<ComponentInstance> => {
413 let deferred = this.$q.defer<ComponentInstance>();
414 this.restangular.one(componentId).one("resourceInstance").customPOST(JSON.stringify(componentInstance)).then((response:any) => {
415 let componentInstance:ComponentInstance = ComponentInstanceFactory.createComponentInstance(response);
416 console.log("Component Instance created", componentInstance);
417 deferred.resolve(componentInstance);
419 console.log("Failed to create componentInstance. With Name: " + componentInstance.name);
420 deferred.reject(err);
422 return deferred.promise;
425 public updateComponentInstance = (componentId:string, componentInstance:ComponentInstance):ng.IPromise<ComponentInstance> => {
426 let deferred = this.$q.defer<ComponentInstance>();
427 this.restangular.one(componentId).one("resourceInstance").one(componentInstance.uniqueId).customPOST(JSON.stringify(componentInstance)).then((response:any) => {
428 let componentInstance:ComponentInstance = ComponentInstanceFactory.createComponentInstance(response);
429 console.log("Component Instance was updated", componentInstance);
430 deferred.resolve(componentInstance);
432 console.log("Failed to update componentInstance. With ID: " + componentInstance.uniqueId + "Name: " + componentInstance.name);
433 deferred.reject(err);
435 return deferred.promise;
438 public updateMultipleComponentInstances = (componentId:string, instances:Array<ComponentInstance>):ng.IPromise<Array<ComponentInstance>> => {
439 let deferred = this.$q.defer<Array<ComponentInstance>>();
440 this.restangular.one(componentId).one("resourceInstance/multipleComponentInstance").customPOST(JSON.stringify(instances)).then((response:any) => {
441 console.log("Multiple Component Instances was updated", response);
442 let updateInstances:Array<ComponentInstance> = new Array<ComponentInstance>();
443 _.forEach(response, (componentInstance:ComponentInstance) => {
444 let updatedComponentInstance:ComponentInstance = ComponentInstanceFactory.createComponentInstance(componentInstance);
445 updateInstances.push(updatedComponentInstance);
447 deferred.resolve(updateInstances);
449 console.log("Failed to update Multiple componentInstance.");
450 deferred.reject(err);
452 return deferred.promise;
455 public deleteComponentInstance = (componentId:string, componentInstanceId:string):ng.IPromise<ComponentInstance> => {
456 let deferred = this.$q.defer<ComponentInstance>();
457 this.restangular.one(componentId).one("resourceInstance").one(componentInstanceId).remove().then(() => {
458 console.log("Component Instance was deleted");
461 console.log("Failed to delete componentInstance. With ID: " + componentInstanceId);
462 deferred.reject(err);
464 return deferred.promise;
467 public checkResourceInstanceVersionChange = (componentId:string, componentInstanceId:string, componentUid:string):ng.IPromise<ComponentInstance> => {
468 let deferred = this.$q.defer<ComponentInstance>();
469 this.restangular.one(componentId).one("resourceInstance").one(componentInstanceId).one(componentUid).one("checkForwardingPathOnVersionChange").get().then((response:any) => {
470 deferred.resolve(response);
472 deferred.reject(err);
474 return deferred.promise;
477 public changeResourceInstanceVersion = (componentId:string, componentInstanceId:string, componentUid:string):ng.IPromise<ComponentInstance> => {
478 let deferred = this.$q.defer<ComponentInstance>();
479 this.restangular.one(componentId).one("resourceInstance").one(componentInstanceId).one("changeVersion").customPOST({'componentUid': componentUid}).then((response:any) => {
480 let componentInstance:ComponentInstance = ComponentInstanceFactory.createComponentInstance(response);
481 deferred.resolve(componentInstance);
483 deferred.reject(err);
485 return deferred.promise;
488 public downloadInstanceArtifact = (componentId:string, instanceId:string, artifactId:string):ng.IPromise<IFileDownload> => {
489 let deferred = this.$q.defer<IFileDownload>();
490 this.restangular.one(componentId).one("resourceInstances").one(instanceId).one("artifacts").one(artifactId).get().then((response:any) => {
491 deferred.resolve(response.plain());
493 deferred.reject(err);
495 return deferred.promise;
498 public updateInstanceArtifact = (componentId:string, instanceId:string, artifact:ArtifactModel):ng.IPromise<ArtifactModel> => {
499 let deferred = this.$q.defer<ArtifactModel>();
501 if (artifact.payloadData) {
502 headerObj = this.getHeaderMd5(artifact);
504 this.restangular.one(componentId).one("resourceInstance").one(instanceId).one("artifacts").customPOST(JSON.stringify(artifact), artifact.uniqueId, {}, headerObj).then((response:any) => {
505 let newArtifact = new ArtifactModel(response);
506 deferred.resolve(newArtifact);
508 deferred.reject(err);
510 return deferred.promise;
513 public addInstanceArtifact = (componentId:string, instanceId:string, artifact:ArtifactModel):ng.IPromise<ArtifactModel> => {
514 let deferred = this.$q.defer<ArtifactModel>();
516 if (artifact.payloadData) {
517 headerObj = this.getHeaderMd5(artifact);
519 this.restangular.one(componentId).one("resourceInstance").one(instanceId).one("artifacts").customPOST(JSON.stringify(artifact), artifact.uniqueId, {}, headerObj).then((response:any) => {
520 let artifact:ArtifactModel = new ArtifactModel(response.plain());
521 deferred.resolve(artifact);
523 deferred.reject(err);
525 return deferred.promise;
528 public deleteInstanceArtifact = (componentId:string, instanceId:string, artifactId:string, artifactLabel:string):ng.IPromise<ArtifactModel> => {
529 let deferred = this.$q.defer<ArtifactModel>();
530 this.restangular.one(componentId).one("resourceInstance").one(instanceId).one("artifacts").one(artifactId).remove({'operation': artifactLabel}).then((response:ArtifactModel) => {
531 deferred.resolve(response);
533 deferred.reject(err);
535 return deferred.promise;
538 public uploadInstanceEnvFile = (componentId:string, instanceId:string, artifact:ArtifactModel):ng.IPromise<ArtifactModel> => {
539 let deferred = this.$q.defer<ArtifactModel>();
541 if (artifact.payloadData) {
542 headerObj = this.getHeaderMd5(artifact);
544 this.restangular.one(componentId).one("resourceInstance").one(instanceId).one("artifacts").customPOST(JSON.stringify(artifact), artifact.uniqueId, {}, headerObj).then((response:any) => {
545 let newArtifact = new ArtifactModel(response);
546 deferred.resolve(newArtifact);
548 deferred.reject(err);
550 return deferred.promise;
553 public updateInstanceProperties = (componentId:string, componentInstanceId:string, properties:PropertyModel[]):ng.IPromise<PropertyModel[]> => {
554 let deferred = this.$q.defer<PropertyModel[]>();
555 this.restangular.one(componentId).one("resourceInstance").one(componentInstanceId).one("properties").customPOST(JSON.stringify(properties)).then((response:any) => {
556 const newProperties = response.map((res) => {
557 const newProperty = new PropertyModel(res);
558 newProperty.readonly = true;
559 newProperty.resourceInstanceUniqueId = componentInstanceId;
562 deferred.resolve(newProperties);
564 deferred.reject(err);
566 return deferred.promise;
569 public updateInstanceAttribute = (componentId:string, attribute:AttributeModel):ng.IPromise<AttributeModel> => {
570 let deferred = this.$q.defer<AttributeModel>();
571 let instanceId = attribute.resourceInstanceUniqueId;
572 this.restangular.one(componentId).one("resourceInstance").one(instanceId).one("attribute").customPOST(JSON.stringify(attribute)).then((response:any) => {
573 let newAttribute = new AttributeModel(response);
574 newAttribute.readonly = true;
575 newAttribute.resourceInstanceUniqueId = instanceId;
576 deferred.resolve(newAttribute);
578 deferred.reject(err);
580 return deferred.promise;
583 public createRelation = (componentId:string, link:RelationshipModel):ng.IPromise<RelationshipModel> => {
584 let deferred = this.$q.defer<RelationshipModel>();
585 const linkPayload:RelationshipModel = new RelationshipModel(link);
586 linkPayload.relationships.forEach((rel) => {
587 delete rel.capability;
588 delete rel.requirement;
590 this.restangular.one(componentId).one("resourceInstance").one("associate").customPOST(JSON.stringify(linkPayload)).then((response:any) => {
591 let relation:RelationshipModel = new RelationshipModel(response.plain());
592 console.log("Link created successfully ", relation);
593 deferred.resolve(relation);
595 console.log("Failed to create Link From: " + link.fromNode + "To: " + link.toNode);
596 deferred.reject(err);
598 return deferred.promise;
601 public deleteRelation = (componentId:string, link:RelationshipModel):ng.IPromise<RelationshipModel> => {
602 let deferred = this.$q.defer<RelationshipModel>();
603 const linkPayload:RelationshipModel = new RelationshipModel(link);
604 linkPayload.relationships.forEach((rel) => {
605 delete rel.capability;
606 delete rel.requirement;
608 this.restangular.one(componentId).one("resourceInstance").one("dissociate").customPUT(JSON.stringify(linkPayload)).then((response:any) => {
609 let relation:RelationshipModel = new RelationshipModel(response);
610 console.log("Link deleted successfully ", relation);
611 deferred.resolve(relation);
613 console.log("Failed to delete Link From: " + link.fromNode + "To: " + link.toNode);
614 deferred.reject(err);
616 return deferred.promise;
619 public fetchRelation = (componentId:string, linkId:string):ng.IPromise<RelationshipModel> => {
620 let deferred = this.$q.defer<RelationshipModel>();
621 this.restangular.one(componentId).one("relationId").one(linkId).get().then((response:any) => {
622 let relation:RelationshipModel = new RelationshipModel(response);
623 console.log("Link fetched successfully ", relation);
624 deferred.resolve(relation);
626 console.log("Failed to fetch Link Id: " + linkId);
627 deferred.reject(err);
629 return deferred.promise;
632 public getRequirementsCapabilities = (componentId:string):ng.IPromise<any> => {
633 let deferred = this.$q.defer();
634 this.restangular.one(componentId).one("requirmentsCapabilities").get().then((response:any) => {
635 console.log("Component requirement capabilities recived: ", response);
636 deferred.resolve(response);
638 console.log("Failed to get requirements & capabilities");
639 deferred.reject(err);
641 return deferred.promise;
644 public getModuleForDisplay = (componentId:string, moduleId:string):ng.IPromise<DisplayModule> => {
645 let deferred = this.$q.defer<DisplayModule>();
646 this.restangular.one(componentId).one("groups").one(moduleId).get().then((response:any) => {
647 console.log("module loaded successfully: ", response);
648 let module:DisplayModule = new DisplayModule(response);
649 deferred.resolve(module);
651 console.log("Failed to get module with id: ", moduleId);
652 deferred.reject(err);
654 return deferred.promise;
657 public getComponentInstanceModule = (componentId:string, componentInstanceId:string, moduleId:string):ng.IPromise<DisplayModule> => {
658 let deferred = this.$q.defer<DisplayModule>();
659 this.restangular.one(componentId).one("resourceInstance").one(componentInstanceId).one("groupInstance").one(moduleId).get().then((response:any) => {
660 console.log("module loaded successfully: ", response);
661 let module:DisplayModule = new DisplayModule(response);
662 deferred.resolve(module);
664 console.log("Failed to get module with id: ", moduleId);
665 deferred.reject(err);
667 return deferred.promise;
670 public getComponentInstancesFilteredByInputsAndProperties = (componentId:string, searchText?:string):ng.IPromise<Array<ComponentInstance>> => {
671 let deferred = this.$q.defer<Array<ComponentInstance>>();
672 this.restangular.one(componentId).one("componentInstances").get({'searchText': searchText}).then((response:any) => {
673 console.log("component instances return successfully: ", response);
674 let componentInstances:Array<ComponentInstance> = CommonUtils.initComponentInstances(response);
675 deferred.resolve(componentInstances);
677 console.log("Failed to get component instances of component with id: " + componentId);
678 deferred.reject(err);
681 return deferred.promise;
684 public getComponentInstanceInputs = (componentId:string, instanceId:string, originComponentUid):ng.IPromise<Array<InputModel>> => {
686 let deferred = this.$q.defer<Array<InputModel>>();
687 this.restangular.one(componentId).one("componentInstances").one(instanceId).one(originComponentUid).one("inputs").get().then((response:any) => {
688 console.log("component instance input return successfully: ", response);
689 let inputsArray:Array<InputModel> = new Array<InputModel>();
690 _.forEach(response, (inputObj:InputModel) => {
691 inputsArray.push(new InputModel(inputObj));
693 deferred.resolve(inputsArray);
695 console.log("Failed to get component instance input with id: " + instanceId);
696 deferred.reject(err);
699 return deferred.promise;
702 public getComponentInputs = (componentId:string):ng.IPromise<Array<InputModel>> => {
704 let deferred = this.$q.defer<Array<InputModel>>();
705 this.restangular.one(componentId).one("inputs").get().then((response:any) => {
706 console.log("component inputs return successfully: ", response);
707 let inputsArray:Array<InputModel> = new Array<InputModel>();
708 _.forEach(response, (inputObj:InputModel) => {
709 inputsArray.push(new InputModel(inputObj));
711 deferred.resolve(inputsArray);
713 console.log("Failed to get component inputs for component with id: " + componentId);
714 deferred.reject(err);
717 return deferred.promise;
720 public getComponentInstanceInputProperties = (componentId:string, instanceId:string, inputId:string):ng.IPromise<Array<PropertyModel>> => {
722 let deferred = this.$q.defer<Array<PropertyModel>>();
723 this.restangular.one(componentId).one("componentInstances").one(instanceId).one(inputId).one("properties").get().then((response:any) => {
724 console.log("component instance input properties return successfully: ", response);
725 let propertiesArray:Array<PropertyModel> = new Array<PropertyModel>();
726 _.forEach(response, (propertyObj:PropertyModel) => {
727 propertiesArray.push(new PropertyModel(propertyObj));
729 deferred.resolve(propertiesArray);
731 console.log("Failed to get component instance input properties with instanceId: " + instanceId + "and input id: " + inputId);
732 deferred.reject(err);
735 return deferred.promise;
739 public getComponentInstanceProperties = (componentId:string, instanceId:string):ng.IPromise<Array<PropertyModel>> => {
740 let deferred = this.$q.defer<Array<PropertyModel>>();
741 this.restangular.one(componentId).one("componentInstances").one(instanceId).one("properties").get().then((response:any) => {
742 console.log("component instance properties return successfully: ", response);
743 let propertiesArray:Array<PropertyModel> = new Array<PropertyModel>();
744 _.forEach(response, (propertyObj:PropertyModel) => {
745 propertiesArray.push(new PropertyModel(propertyObj));
747 deferred.resolve(propertiesArray);
749 console.log("Failed to get component instance properties with instanceId: " + instanceId);
750 deferred.reject(err);
753 return deferred.promise;
756 public updateGroupMetadata = (componentId:string, group:Module):ng.IPromise<Module> => {
758 let deferred = this.$q.defer<Module>();
759 this.restangular.one(componentId).one("groups").one(group.uniqueId).one("metadata").customPUT(JSON.stringify(group)).then((response:Module) => {
760 console.log("group metadata updated successfully: ", response);
761 let updatedGroup:Module = new Module(response);
763 deferred.resolve(updatedGroup);
765 console.log("Failed to update group metadata for component: " + componentId + " for group with id: " + group.uniqueId);
766 deferred.reject(err);
769 return deferred.promise;
772 public getComponentInputInputsAndProperties = (serviceId:string, inputId:string):ng.IPromise<InputsAndProperties> => {
773 let defer = this.$q.defer<InputsAndProperties>();
774 this.restangular.one(serviceId).one("inputs").one(inputId).get().then((response:InputsAndProperties) => {
776 let inputsArray:Array<InputModel> = new Array<InputModel>();
777 _.forEach(response.inputs, (inputObj:InputModel) => {
778 inputsArray.push(new InputModel(inputObj));
781 let propertiesArray:Array<PropertyModel> = new Array<PropertyModel>();
782 _.forEach(response.properties, (property:PropertyModel) => {
783 propertiesArray.push(new PropertyModel(property));
786 defer.resolve(new InputsAndProperties(inputsArray, propertiesArray));
788 console.log("failed to get inputs of input : ", err);
791 return defer.promise;
794 createInputsFromInstancesInputsProperties = (resourceId:string, instancePropertyMap:InstancesInputsPropertiesMap):ng.IPromise<Array<PropertyModel>> => {
795 let defer = this.$q.defer<Array<PropertyModel>>();
796 this.restangular.one(resourceId).one("create/properties").customPOST(instancePropertyMap).then((response:any) => {
797 let inputsArray:Array<PropertyModel> = new Array<PropertyModel>();
798 _.forEach(response, (inputObj:PropertyModel) => {
799 inputsArray.push(new PropertyModel(inputObj));
801 defer.resolve(inputsArray);
803 console.log("failed to create service inputs from VF instances inputs : ", err);
806 return defer.promise;
809 createInputsFromInstancesInputs = (serviceId:string, instancesMap:InstancesInputsPropertiesMap):ng.IPromise<Array<InputModel>> => {
810 let defer = this.$q.defer<Array<InputModel>>();
811 this.restangular.one(serviceId).one("create/inputs").customPOST(instancesMap).then((response:any) => {
812 let inputsArray:Array<InputModel> = new Array<InputModel>();
813 _.forEach(response, (inputObj:InputModel) => {
814 inputsArray.push(new InputModel(inputObj));
816 defer.resolve(inputsArray);
818 console.log("failed to create service inputs from VF instances inputs : ", err);
821 return defer.promise;
824 deleteComponentInput = (serviceId:string, inputId:string):ng.IPromise<InputModel> => {
825 let defer = this.$q.defer<InputModel>();
826 this.restangular.one(serviceId).one("delete").one(inputId).one("input").remove().then((response:any) => {
827 let inputToDelete = new InputModel(response);
828 defer.resolve(inputToDelete);
830 console.log("failed to delete input from service: ", err);
833 return defer.promise;
836 private getHeaderMd5 = (object:any):any => {
838 // This is ugly workaround!!!
839 // The md5 result is not correct if we do not add the line JSON.stringify(resource); twice.
840 JSON.stringify(object);
841 let componentString:string = JSON.stringify(object);
842 let md5Result = md5(componentString).toLowerCase();
843 headerObj = {'Content-MD5': this.$base64.encode(md5Result)};