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 {ArtifactModel, IFileDownload, InstancesInputsPropertiesMap, InputModel, IValidate, RelationshipModel, PropertyModel, Component, ComponentInstance,
23 AttributeModel, IAppConfigurtaion, Resource, Module, DisplayModule, ArtifactGroupModel, InputsAndProperties} from "app/models";
24 import {ComponentInstanceFactory, CommonUtils} from "app/utils";
25 import {SharingService} from "../sharing-service";
26 import {ComponentMetadata} from "../../models/component-metadata";
28 export interface IComponentService {
30 getComponent(id:string);
31 updateComponent(component:Component):ng.IPromise<Component>;
32 changeLifecycleState(component:Component, state:string, userRemarks:any):ng.IPromise<ComponentMetadata> ;
33 validateName(newName:string, subtype?:string):ng.IPromise<IValidate>;
34 createComponent(component:Component):ng.IPromise<Component>;
35 addOrUpdateArtifact(componentId:string, artifact:ArtifactModel):ng.IPromise<ArtifactModel>;
36 deleteArtifact(componentId:string, artifact:string, artifactLabel):ng.IPromise<ArtifactModel>;
37 addProperty(componentId:string, property:PropertyModel):ng.IPromise<PropertyModel>;
38 updateProperty(componentId:string, property:PropertyModel):ng.IPromise<PropertyModel>;
39 addAttribute(componentId:string, attribute:AttributeModel):ng.IPromise<AttributeModel>;
40 updateAttribute(componentId:string, attribute:AttributeModel):ng.IPromise<AttributeModel>;
41 deleteProperty(componentId:string, propertyId:string):ng.IPromise<PropertyModel>;
42 deleteAttribute(componentId:string, attributeId:string):ng.IPromise<AttributeModel>;
43 checkResourceInstanceVersionChange(componentId:string, componentInstanceId:string, componentUid:string):ng.IPromise<any>;
44 changeResourceInstanceVersion(componentId:string, componentInstanceId:string, componentUid:string):ng.IPromise<ComponentInstance>;
45 updateInstanceArtifact(componentId:string, instanceId:string, artifact:ArtifactModel):ng.IPromise<ArtifactModel>;
46 addInstanceArtifact(componentId:string, instanceId:string, artifact:ArtifactModel):ng.IPromise<ArtifactModel>;
47 deleteInstanceArtifact(componentId:string, instanceId:string, artifact:string, artifactLabel):ng.IPromise<ArtifactModel>;
48 createComponentInstance(componentId:string, componentInstance:ComponentInstance):ng.IPromise<ComponentInstance>;
49 updateComponentInstance(componentId:string, componentInstance:ComponentInstance):ng.IPromise<ComponentInstance>;
50 updateMultipleComponentInstances(componentId:string, instances:Array<ComponentInstance>):ng.IPromise< Array<ComponentInstance>>;
51 downloadArtifact(componentId:string, artifactId:string):ng.IPromise<IFileDownload>;
52 uploadInstanceEnvFile(componentId:string, instanceId:string, artifact:ArtifactModel):ng.IPromise<ArtifactModel>;
53 downloadInstanceArtifact(componentId:string, instanceId:string, artifactId:string):ng.IPromise<IFileDownload>;
54 deleteComponentInstance(componentId:string, componentInstanceId:string):ng.IPromise<ComponentInstance>;
55 createRelation(componentId:string, link:RelationshipModel):ng.IPromise<RelationshipModel>;
56 deleteRelation(componentId:string, link:RelationshipModel):ng.IPromise<RelationshipModel>;
57 fetchRelation(componentId:string, linkId:string):ng.IPromise<RelationshipModel>;
58 getRequirementsCapabilities(componentId:string):ng.IPromise<any>;
59 updateInstanceProperties(componentId:string, componentInstanceId:string, properties:PropertyModel[]):ng.IPromise<PropertyModel[]>;
60 updateInstanceAttribute(componentId:string, attribute:AttributeModel):ng.IPromise<AttributeModel>;
61 getComponentInstancesFilteredByInputsAndProperties(componentId:string, searchText:string):ng.IPromise<Array<ComponentInstance>>
62 getComponentInstanceInputs(componentId:string, instanceId:string, originComponentUid):ng.IPromise<Array<InputModel>>;
63 getComponentInputs(componentId:string):ng.IPromise<Array<InputModel>>;
64 getComponentInstanceInputProperties(componentId:string, instanceId:string, inputId:string):ng.IPromise<Array<PropertyModel>>;
65 getComponentInstanceProperties(componentId:string, instanceId:string):ng.IPromise<Array<PropertyModel>>;
66 getModuleForDisplay(componentId:string, moduleId:string):ng.IPromise<DisplayModule>;
67 getComponentInstanceModule(componentId:string, componentInstanceId:string, moduleId:string):ng.IPromise<DisplayModule>;
68 updateGroupMetadata(componentId:string, group:Module):ng.IPromise<Module>;
69 getComponentInputInputsAndProperties(serviceId:string, input:string):ng.IPromise<InputsAndProperties>;
70 createInputsFromInstancesInputs(serviceId:string, instancesInputsMap:InstancesInputsPropertiesMap):ng.IPromise<Array<InputModel>>;
71 createInputsFromInstancesInputsProperties(resourceId:string, instanceInputsPropertiesMap:InstancesInputsPropertiesMap):ng.IPromise<Array<PropertyModel>>;
72 deleteComponentInput(serviceId:string, inputId:string):ng.IPromise<InputModel>;
73 getArtifactByGroupType(componentId:string, artifactGroupType:string):ng.IPromise<ArtifactGroupModel>;
74 getComponentInstanceArtifactsByGroupType(componentId:string, componentInstanceId:string, artifactGroupType:string):ng.IPromise<ArtifactGroupModel>;
77 export class ComponentService implements IComponentService {
82 'Sdc.Services.SharingService',
87 constructor(protected restangular:restangular.IElement,
88 protected sdcConfig:IAppConfigurtaion,
89 protected sharingService:SharingService,
90 protected $q:ng.IQService,
94 this.restangular.setBaseUrl(sdcConfig.api.root + sdcConfig.api.component_api_root);
95 this.restangular.setRequestInterceptor(function (elem, operation) {
96 if (operation === "remove") {
101 // this.restangular.setDefaultHeaders({'Content-Type': 'application/json; charset=UTF-8'});
104 //this function is override by each service, we need to change this method to abstract when updtaing typescript version
105 protected createComponentObject = (component:Component):Component => {
109 public getComponent = (id:string):ng.IPromise<Component> => {
110 let deferred = this.$q.defer<Component>();
111 this.restangular.one(id).get().then((response:Component) => {
112 let component:Component = this.createComponentObject(response);
113 //console.log("Component Loaded successfully : ", component);
114 deferred.resolve(component);
116 console.log("Failed to load component with ID: " + id);
117 deferred.reject(err);
119 return deferred.promise;
122 public updateComponent = (component:Component):ng.IPromise<Component> => {
123 // If this is resource
124 if (component instanceof Resource) {
125 let resource:Resource = <Resource>component;
126 if (resource.importedFile) {
127 // Update resource with payload data.
128 return this.updateResourceWithPayload(resource);
130 if (component.csarUUID) {
131 // Update resource without payload data.
132 return this.updateResource(component);
134 // Update resource without payload data (metadata).
135 return this.updateResourceMetadata(component);
139 return this.updateService(component);
143 private updateService = (component:Component):ng.IPromise<Component> => {
144 let deferred = this.$q.defer<Component>();
145 this.restangular.one(component.uniqueId).one("metadata").customPUT(JSON.stringify(component)).then((response:Component) => {
146 let component:Component = this.createComponentObject(response);
147 deferred.resolve(component);
149 deferred.reject(err);
151 return deferred.promise;
154 private updateResource = (component:Component):ng.IPromise<Component> => {
155 let deferred = this.$q.defer<Component>();
156 this.restangular.one(component.uniqueId).customPUT(JSON.stringify(component)).then((response:Component) => {
157 let component:Component = this.createComponentObject(response);
158 deferred.resolve(component);
160 deferred.reject(err);
162 return deferred.promise;
165 private updateResourceMetadata = (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;
177 * Only resource can be updated with payload data
179 * @returns {IPromise<T>}
181 private updateResourceWithPayload = (resource:Resource):ng.IPromise<Component> => {
182 let deferred = this.$q.defer<Component>();
184 resource.payloadData = resource.importedFile.base64;
185 resource.payloadName = resource.importedFile.filename;
186 let headerObj = this.getHeaderMd5(resource);
188 this.restangular.one(resource.uniqueId).customPUT(JSON.stringify(resource), '', {}, headerObj).then((response:Component) => {
189 let componentResult:Component = this.createComponentObject(response);
190 deferred.resolve(componentResult);
192 deferred.reject(err);
195 return deferred.promise;
198 public createComponent = (component:Component):ng.IPromise<Component> => {
199 let deferred = this.$q.defer<Component>();
200 let headerObj = this.getHeaderMd5(component);
201 this.restangular.customPOST(JSON.stringify(component), '', {}, headerObj).then((response:Component) => {
202 let component:Component = this.createComponentObject(response);
203 deferred.resolve(component);
205 deferred.reject(err);
207 return deferred.promise;
210 public validateName = (newName:string, subtype?:string):ng.IPromise<IValidate> => {
211 let deferred = this.$q.defer<IValidate>();
212 this.restangular.one("validate-name").one(newName).get({'subtype': subtype}).then((response:any) => {
213 deferred.resolve(response.plain());
215 deferred.reject(err);
217 return deferred.promise;
220 public changeLifecycleState = (component:Component, state:string, userRemarks:any):ng.IPromise<ComponentMetadata> => {
221 let deferred = this.$q.defer<ComponentMetadata>();
222 this.restangular.one(component.uniqueId).one(state).customPOST(userRemarks).then((response:ComponentMetadata) => {
223 this.sharingService.addUuidValue(response.uniqueId, response.uuid);
224 let component:ComponentMetadata = new ComponentMetadata().deserialize(response);
225 deferred.resolve(component);
227 deferred.reject(err);
229 return deferred.promise;
232 // ------------------------------------------------ Artifacts API --------------------------------------------------//
233 public addOrUpdateArtifact = (componentId:string, artifact:ArtifactModel):ng.IPromise<ArtifactModel> => {
234 let deferred = this.$q.defer<ArtifactModel>();
236 if (artifact.payloadData) {
237 headerObj = this.getHeaderMd5(artifact);
239 this.restangular.one(componentId).one("artifacts").customPOST(JSON.stringify(artifact), artifact.uniqueId, {}, headerObj).then((response:any) => {
240 deferred.resolve(response.plain());
242 deferred.reject(err);
244 return deferred.promise;
247 public downloadArtifact = (componentId:string, artifactId:string):ng.IPromise<IFileDownload> => {
248 let deferred = this.$q.defer<IFileDownload>();
249 this.restangular.one(componentId).one("artifacts").one(artifactId).get().then((response:any) => {
250 deferred.resolve(response.plain());
252 deferred.reject(err);
254 return deferred.promise;
257 public deleteArtifact = (componentId:string, artifactId:string, artifactLabel:string):ng.IPromise<ArtifactModel> => {
258 let deferred = this.$q.defer<ArtifactModel>();
259 this.restangular.one(componentId).one("artifacts").one(artifactId).remove({'operation': artifactLabel}).then((response:ArtifactModel) => {
260 deferred.resolve(response);
262 deferred.reject(err);
264 return deferred.promise;
267 public getArtifactByGroupType = (componentId:string, artifactGroupType:string):ng.IPromise<ArtifactGroupModel> => {
268 let deferred = this.$q.defer<ArtifactGroupModel>();
269 this.restangular.one(componentId).one("artifactsByType").one(artifactGroupType).get().then((response:any) => {
270 var artifacts:ArtifactGroupModel = new ArtifactGroupModel(response.plain());
271 deferred.resolve(artifacts);
273 deferred.reject(err);
275 return deferred.promise;
278 public getComponentInstanceArtifactsByGroupType = (componentId:string, componentInstanceId:string, artifactGroupType:string):ng.IPromise<ArtifactGroupModel> => {
279 let deferred = this.$q.defer<ArtifactGroupModel>();
280 this.restangular.one(componentId).one("resourceInstances").one(componentInstanceId).one("artifactsByType").one(artifactGroupType).get().then((response:any) => {
281 var artifacts:ArtifactGroupModel = new ArtifactGroupModel(response.plain());
282 deferred.resolve(artifacts);
284 deferred.reject(err);
286 return deferred.promise;
290 // ------------------------------------------------ Properties API --------------------------------------------------//
291 public addProperty = (componentId:string, property:PropertyModel):ng.IPromise<PropertyModel> => {
292 let deferred = this.$q.defer<PropertyModel>();
293 this.restangular.one(componentId).one("properties").customPOST(property.convertToServerObject()).then((response:any) => {
294 let property:PropertyModel = new PropertyModel(response[Object.keys(response)[0]]);
295 deferred.resolve(property);
297 deferred.reject(err);
299 return deferred.promise;
302 public updateProperty = (componentId:string, property:PropertyModel):ng.IPromise<PropertyModel> => {
303 let deferred = this.$q.defer<PropertyModel>();
304 this.restangular.one(componentId).one("properties").one(property.uniqueId).customPUT(property.convertToServerObject()).then((response:any) => {
305 let property:PropertyModel = new PropertyModel(response[Object.keys(response)[0]]);
306 deferred.resolve(property);
308 deferred.reject(err);
310 return deferred.promise;
313 public deleteProperty = (componentId:string, propertyId:string):ng.IPromise<PropertyModel> => {
314 let deferred = this.$q.defer<PropertyModel>();
315 this.restangular.one(componentId).one("properties").one(propertyId).remove().then((response:any) => {
316 deferred.resolve(response);
318 deferred.reject(err);
320 return deferred.promise;
323 // ------------------------------------------------ Attributes API --------------------------------------------------//
324 public addAttribute = (componentId:string, attribute:AttributeModel):ng.IPromise<AttributeModel> => {
325 let deferred = this.$q.defer<AttributeModel>();
326 this.restangular.one(componentId).one("attributes").customPOST(attribute.convertToServerObject()).then((response:any) => {
327 let attribute:AttributeModel = new AttributeModel(response);
328 deferred.resolve(attribute);
330 deferred.reject(err);
332 return deferred.promise;
335 public updateAttribute = (componentId:string, attribute:AttributeModel):ng.IPromise<AttributeModel> => {
336 let deferred = this.$q.defer<AttributeModel>();
337 this.restangular.one(componentId).one("attributes").one(attribute.uniqueId).customPUT(attribute.convertToServerObject()).then((response:any) => {
338 let attribute:AttributeModel = new AttributeModel(response);
339 deferred.resolve(attribute);
341 deferred.reject(err);
343 return deferred.promise;
346 public deleteAttribute = (componentId:string, attributeId:string):ng.IPromise<AttributeModel> => {
347 let deferred = this.$q.defer<AttributeModel>();
348 this.restangular.one(componentId).one("attributes").one(attributeId).remove().then((response:any) => {
349 deferred.resolve(response);
351 deferred.reject(err);
353 return deferred.promise;
356 // ------------------------------------------------ Component Instances API --------------------------------------------------//
358 public createComponentInstance = (componentId:string, componentInstance:ComponentInstance):ng.IPromise<ComponentInstance> => {
359 let deferred = this.$q.defer<ComponentInstance>();
360 this.restangular.one(componentId).one("resourceInstance").customPOST(JSON.stringify(componentInstance)).then((response:any) => {
361 let componentInstance:ComponentInstance = ComponentInstanceFactory.createComponentInstance(response);
362 console.log("Component Instance created", componentInstance);
363 deferred.resolve(componentInstance);
365 console.log("Failed to create componentInstance. With Name: " + componentInstance.name);
366 deferred.reject(err);
368 return deferred.promise;
371 public updateComponentInstance = (componentId:string, componentInstance:ComponentInstance):ng.IPromise<ComponentInstance> => {
372 let deferred = this.$q.defer<ComponentInstance>();
373 this.restangular.one(componentId).one("resourceInstance").one(componentInstance.uniqueId).customPOST(JSON.stringify(componentInstance)).then((response:any) => {
374 let componentInstance:ComponentInstance = ComponentInstanceFactory.createComponentInstance(response);
375 console.log("Component Instance was updated", componentInstance);
376 deferred.resolve(componentInstance);
378 console.log("Failed to update componentInstance. With ID: " + componentInstance.uniqueId + "Name: " + componentInstance.name);
379 deferred.reject(err);
381 return deferred.promise;
384 public updateMultipleComponentInstances = (componentId:string, instances:Array<ComponentInstance>):ng.IPromise<Array<ComponentInstance>> => {
385 let deferred = this.$q.defer<Array<ComponentInstance>>();
386 this.restangular.one(componentId).one("resourceInstance/multipleComponentInstance").customPOST(JSON.stringify(instances)).then((response:any) => {
387 console.log("Multiple Component Instances was updated", response);
388 let updateInstances:Array<ComponentInstance> = new Array<ComponentInstance>();
389 _.forEach(response, (componentInstance:ComponentInstance) => {
390 let updatedComponentInstance:ComponentInstance = ComponentInstanceFactory.createComponentInstance(componentInstance);
391 updateInstances.push(updatedComponentInstance);
393 deferred.resolve(updateInstances);
395 console.log("Failed to update Multiple componentInstance.");
396 deferred.reject(err);
398 return deferred.promise;
401 public deleteComponentInstance = (componentId:string, componentInstanceId:string):ng.IPromise<ComponentInstance> => {
402 let deferred = this.$q.defer<ComponentInstance>();
403 this.restangular.one(componentId).one("resourceInstance").one(componentInstanceId).remove().then(() => {
404 console.log("Component Instance was deleted");
407 console.log("Failed to delete componentInstance. With ID: " + componentInstanceId);
408 deferred.reject(err);
410 return deferred.promise;
413 public checkResourceInstanceVersionChange = (componentId:string, componentInstanceId:string, componentUid:string):ng.IPromise<ComponentInstance> => {
414 let deferred = this.$q.defer<ComponentInstance>();
415 this.restangular.one(componentId).one("resourceInstance").one(componentInstanceId).one(componentUid).one("checkForwardingPathOnVersionChange").get().then((response:any) => {
416 deferred.resolve(response);
418 deferred.reject(err);
420 return deferred.promise;
423 public changeResourceInstanceVersion = (componentId:string, componentInstanceId:string, componentUid:string):ng.IPromise<ComponentInstance> => {
424 let deferred = this.$q.defer<ComponentInstance>();
425 this.restangular.one(componentId).one("resourceInstance").one(componentInstanceId).one("changeVersion").customPOST({'componentUid': componentUid}).then((response:any) => {
426 let componentInstance:ComponentInstance = ComponentInstanceFactory.createComponentInstance(response);
427 deferred.resolve(componentInstance);
429 deferred.reject(err);
431 return deferred.promise;
434 public downloadInstanceArtifact = (componentId:string, instanceId:string, artifactId:string):ng.IPromise<IFileDownload> => {
435 let deferred = this.$q.defer<IFileDownload>();
436 this.restangular.one(componentId).one("resourceInstances").one(instanceId).one("artifacts").one(artifactId).get().then((response:any) => {
437 deferred.resolve(response.plain());
439 deferred.reject(err);
441 return deferred.promise;
444 public updateInstanceArtifact = (componentId:string, instanceId:string, artifact:ArtifactModel):ng.IPromise<ArtifactModel> => {
445 let deferred = this.$q.defer<ArtifactModel>();
447 if (artifact.payloadData) {
448 headerObj = this.getHeaderMd5(artifact);
450 this.restangular.one(componentId).one("resourceInstance").one(instanceId).one("artifacts").customPOST(JSON.stringify(artifact), artifact.uniqueId, {}, headerObj).then((response:any) => {
451 let newArtifact = new ArtifactModel(response);
452 deferred.resolve(newArtifact);
454 deferred.reject(err);
456 return deferred.promise;
459 public addInstanceArtifact = (componentId:string, instanceId:string, artifact:ArtifactModel):ng.IPromise<ArtifactModel> => {
460 let deferred = this.$q.defer<ArtifactModel>();
462 if (artifact.payloadData) {
463 headerObj = this.getHeaderMd5(artifact);
465 this.restangular.one(componentId).one("resourceInstance").one(instanceId).one("artifacts").customPOST(JSON.stringify(artifact), artifact.uniqueId, {}, headerObj).then((response:any) => {
466 let artifact:ArtifactModel = new ArtifactModel(response.plain());
467 deferred.resolve(artifact);
469 deferred.reject(err);
471 return deferred.promise;
474 public deleteInstanceArtifact = (componentId:string, instanceId:string, artifactId:string, artifactLabel:string):ng.IPromise<ArtifactModel> => {
475 let deferred = this.$q.defer<ArtifactModel>();
476 this.restangular.one(componentId).one("resourceInstance").one(instanceId).one("artifacts").one(artifactId).remove({'operation': artifactLabel}).then((response:ArtifactModel) => {
477 deferred.resolve(response);
479 deferred.reject(err);
481 return deferred.promise;
484 public uploadInstanceEnvFile = (componentId:string, instanceId:string, artifact:ArtifactModel):ng.IPromise<ArtifactModel> => {
485 let deferred = this.$q.defer<ArtifactModel>();
487 if (artifact.payloadData) {
488 headerObj = this.getHeaderMd5(artifact);
490 this.restangular.one(componentId).one("resourceInstance").one(instanceId).one("artifacts").customPOST(JSON.stringify(artifact), artifact.uniqueId, {}, headerObj).then((response:any) => {
491 let newArtifact = new ArtifactModel(response);
492 deferred.resolve(newArtifact);
494 deferred.reject(err);
496 return deferred.promise;
499 public updateInstanceProperties = (componentId:string, componentInstanceId:string, properties:PropertyModel[]):ng.IPromise<PropertyModel[]> => {
500 let deferred = this.$q.defer<PropertyModel[]>();
501 this.restangular.one(componentId).one("resourceInstance").one(componentInstanceId).one("properties").customPOST(JSON.stringify(properties)).then((response:any) => {
502 const newProperties = response.map((res) => {
503 const newProperty = new PropertyModel(res);
504 newProperty.readonly = true;
505 newProperty.resourceInstanceUniqueId = componentInstanceId;
508 deferred.resolve(newProperties);
510 deferred.reject(err);
512 return deferred.promise;
515 public updateInstanceAttribute = (componentId:string, attribute:AttributeModel):ng.IPromise<AttributeModel> => {
516 let deferred = this.$q.defer<AttributeModel>();
517 let instanceId = attribute.resourceInstanceUniqueId;
518 this.restangular.one(componentId).one("resourceInstance").one(instanceId).one("attribute").customPOST(JSON.stringify(attribute)).then((response:any) => {
519 let newAttribute = new AttributeModel(response);
520 newAttribute.readonly = true;
521 newAttribute.resourceInstanceUniqueId = instanceId;
522 deferred.resolve(newAttribute);
524 deferred.reject(err);
526 return deferred.promise;
529 public createRelation = (componentId:string, link:RelationshipModel):ng.IPromise<RelationshipModel> => {
530 let deferred = this.$q.defer<RelationshipModel>();
531 const linkPayload:RelationshipModel = new RelationshipModel(link);
532 linkPayload.relationships.forEach((rel) => {
533 delete rel.capability;
534 delete rel.requirement;
536 this.restangular.one(componentId).one("resourceInstance").one("associate").customPOST(JSON.stringify(linkPayload)).then((response:any) => {
537 let relation:RelationshipModel = new RelationshipModel(response.plain());
538 console.log("Link created successfully ", relation);
539 deferred.resolve(relation);
541 console.log("Failed to create Link From: " + link.fromNode + "To: " + link.toNode);
542 deferred.reject(err);
544 return deferred.promise;
547 public deleteRelation = (componentId:string, link:RelationshipModel):ng.IPromise<RelationshipModel> => {
548 let deferred = this.$q.defer<RelationshipModel>();
549 const linkPayload:RelationshipModel = new RelationshipModel(link);
550 linkPayload.relationships.forEach((rel) => {
551 delete rel.capability;
552 delete rel.requirement;
554 this.restangular.one(componentId).one("resourceInstance").one("dissociate").customPUT(JSON.stringify(linkPayload)).then((response:any) => {
555 let relation:RelationshipModel = new RelationshipModel(response);
556 console.log("Link deleted successfully ", relation);
557 deferred.resolve(relation);
559 console.log("Failed to delete Link From: " + link.fromNode + "To: " + link.toNode);
560 deferred.reject(err);
562 return deferred.promise;
565 public fetchRelation = (componentId:string, linkId:string):ng.IPromise<RelationshipModel> => {
566 let deferred = this.$q.defer<RelationshipModel>();
567 this.restangular.one(componentId).one("relationId").one(linkId).get().then((response:any) => {
568 let relation:RelationshipModel = new RelationshipModel(response);
569 console.log("Link fetched successfully ", relation);
570 deferred.resolve(relation);
572 console.log("Failed to fetch Link Id: " + linkId);
573 deferred.reject(err);
575 return deferred.promise;
578 public getRequirementsCapabilities = (componentId:string):ng.IPromise<any> => {
579 let deferred = this.$q.defer();
580 this.restangular.one(componentId).one("requirmentsCapabilities").get().then((response:any) => {
581 console.log("Component requirement capabilities recived: ", response);
582 deferred.resolve(response);
584 console.log("Failed to get requirements & capabilities");
585 deferred.reject(err);
587 return deferred.promise;
590 public getModuleForDisplay = (componentId:string, moduleId:string):ng.IPromise<DisplayModule> => {
591 let deferred = this.$q.defer<DisplayModule>();
592 this.restangular.one(componentId).one("groups").one(moduleId).get().then((response:any) => {
593 console.log("module loaded successfully: ", response);
594 let module:DisplayModule = new DisplayModule(response);
595 deferred.resolve(module);
597 console.log("Failed to get module with id: ", moduleId);
598 deferred.reject(err);
600 return deferred.promise;
603 public getComponentInstanceModule = (componentId:string, componentInstanceId:string, moduleId:string):ng.IPromise<DisplayModule> => {
604 let deferred = this.$q.defer<DisplayModule>();
605 this.restangular.one(componentId).one("resourceInstance").one(componentInstanceId).one("groupInstance").one(moduleId).get().then((response:any) => {
606 console.log("module loaded successfully: ", response);
607 let module:DisplayModule = new DisplayModule(response);
608 deferred.resolve(module);
610 console.log("Failed to get module with id: ", moduleId);
611 deferred.reject(err);
613 return deferred.promise;
616 public getComponentInstancesFilteredByInputsAndProperties = (componentId:string, searchText?:string):ng.IPromise<Array<ComponentInstance>> => {
617 let deferred = this.$q.defer<Array<ComponentInstance>>();
618 this.restangular.one(componentId).one("componentInstances").get({'searchText': searchText}).then((response:any) => {
619 console.log("component instances return successfully: ", response);
620 let componentInstances:Array<ComponentInstance> = CommonUtils.initComponentInstances(response);
621 deferred.resolve(componentInstances);
623 console.log("Failed to get component instances of component with id: " + componentId);
624 deferred.reject(err);
627 return deferred.promise;
630 public getComponentInstanceInputs = (componentId:string, instanceId:string, originComponentUid):ng.IPromise<Array<InputModel>> => {
632 let deferred = this.$q.defer<Array<InputModel>>();
633 this.restangular.one(componentId).one("componentInstances").one(instanceId).one(originComponentUid).one("inputs").get().then((response:any) => {
634 console.log("component instance input return successfully: ", response);
635 let inputsArray:Array<InputModel> = new Array<InputModel>();
636 _.forEach(response, (inputObj:InputModel) => {
637 inputsArray.push(new InputModel(inputObj));
639 deferred.resolve(inputsArray);
641 console.log("Failed to get component instance input with id: " + instanceId);
642 deferred.reject(err);
645 return deferred.promise;
648 public getComponentInputs = (componentId:string):ng.IPromise<Array<InputModel>> => {
650 let deferred = this.$q.defer<Array<InputModel>>();
651 this.restangular.one(componentId).one("inputs").get().then((response:any) => {
652 console.log("component inputs return successfully: ", response);
653 let inputsArray:Array<InputModel> = new Array<InputModel>();
654 _.forEach(response, (inputObj:InputModel) => {
655 inputsArray.push(new InputModel(inputObj));
657 deferred.resolve(inputsArray);
659 console.log("Failed to get component inputs for component with id: " + componentId);
660 deferred.reject(err);
663 return deferred.promise;
666 public getComponentInstanceInputProperties = (componentId:string, instanceId:string, inputId:string):ng.IPromise<Array<PropertyModel>> => {
668 let deferred = this.$q.defer<Array<PropertyModel>>();
669 this.restangular.one(componentId).one("componentInstances").one(instanceId).one(inputId).one("properties").get().then((response:any) => {
670 console.log("component instance input properties return successfully: ", response);
671 let propertiesArray:Array<PropertyModel> = new Array<PropertyModel>();
672 _.forEach(response, (propertyObj:PropertyModel) => {
673 propertiesArray.push(new PropertyModel(propertyObj));
675 deferred.resolve(propertiesArray);
677 console.log("Failed to get component instance input properties with instanceId: " + instanceId + "and input id: " + inputId);
678 deferred.reject(err);
681 return deferred.promise;
685 public getComponentInstanceProperties = (componentId:string, instanceId:string):ng.IPromise<Array<PropertyModel>> => {
687 let deferred = this.$q.defer<Array<PropertyModel>>();
688 this.restangular.one(componentId).one("componentInstances").one(instanceId).one("properties").get().then((response:any) => {
689 console.log("component instance properties return successfully: ", response);
690 let propertiesArray:Array<PropertyModel> = new Array<PropertyModel>();
691 _.forEach(response, (propertyObj:PropertyModel) => {
692 propertiesArray.push(new PropertyModel(propertyObj));
694 deferred.resolve(propertiesArray);
696 console.log("Failed to get component instance properties with instanceId: " + instanceId);
697 deferred.reject(err);
700 return deferred.promise;
703 public updateGroupMetadata = (componentId:string, group:Module):ng.IPromise<Module> => {
705 let deferred = this.$q.defer<Module>();
706 this.restangular.one(componentId).one("groups").one(group.uniqueId).one("metadata").customPUT(JSON.stringify(group)).then((response:Module) => {
707 console.log("group metadata updated successfully: ", response);
708 let updatedGroup:Module = new Module(response);
710 deferred.resolve(updatedGroup);
712 console.log("Failed to update group metadata for component: " + componentId + " for group with id: " + group.uniqueId);
713 deferred.reject(err);
716 return deferred.promise;
719 public getComponentInputInputsAndProperties = (serviceId:string, inputId:string):ng.IPromise<InputsAndProperties> => {
720 let defer = this.$q.defer<InputsAndProperties>();
721 this.restangular.one(serviceId).one("inputs").one(inputId).get().then((response:InputsAndProperties) => {
723 let inputsArray:Array<InputModel> = new Array<InputModel>();
724 _.forEach(response.inputs, (inputObj:InputModel) => {
725 inputsArray.push(new InputModel(inputObj));
728 let propertiesArray:Array<PropertyModel> = new Array<PropertyModel>();
729 _.forEach(response.properties, (property:PropertyModel) => {
730 propertiesArray.push(new PropertyModel(property));
733 defer.resolve(new InputsAndProperties(inputsArray, propertiesArray));
735 console.log("failed to get inputs of input : ", err);
738 return defer.promise;
741 createInputsFromInstancesInputsProperties = (resourceId:string, instancePropertyMap:InstancesInputsPropertiesMap):ng.IPromise<Array<PropertyModel>> => {
742 let defer = this.$q.defer<Array<PropertyModel>>();
743 this.restangular.one(resourceId).one("create/properties").customPOST(instancePropertyMap).then((response:any) => {
744 let inputsArray:Array<PropertyModel> = new Array<PropertyModel>();
745 _.forEach(response, (inputObj:PropertyModel) => {
746 inputsArray.push(new PropertyModel(inputObj));
748 defer.resolve(inputsArray);
750 console.log("failed to create service inputs from VF instances inputs : ", err);
753 return defer.promise;
756 createInputsFromInstancesInputs = (serviceId:string, instancesMap:InstancesInputsPropertiesMap):ng.IPromise<Array<InputModel>> => {
757 let defer = this.$q.defer<Array<InputModel>>();
758 this.restangular.one(serviceId).one("create/inputs").customPOST(instancesMap).then((response:any) => {
759 let inputsArray:Array<InputModel> = new Array<InputModel>();
760 _.forEach(response, (inputObj:InputModel) => {
761 inputsArray.push(new InputModel(inputObj));
763 defer.resolve(inputsArray);
765 console.log("failed to create service inputs from VF instances inputs : ", err);
768 return defer.promise;
771 deleteComponentInput = (serviceId:string, inputId:string):ng.IPromise<InputModel> => {
772 let defer = this.$q.defer<InputModel>();
773 this.restangular.one(serviceId).one("delete").one(inputId).one("input").remove().then((response:any) => {
774 let inputToDelete = new InputModel(response);
775 defer.resolve(inputToDelete);
777 console.log("failed to delete input from service: ", err);
780 return defer.promise;
783 private getHeaderMd5 = (object:any):any => {
785 // This is ugly workaround!!!
786 // The md5 result is not correct if we do not add the line JSON.stringify(resource); twice.
787 JSON.stringify(object);
788 let componentString:string = JSON.stringify(object);
789 let md5Result = md5(componentString).toLowerCase();
790 headerObj = {'Content-MD5': this.$base64.encode(md5Result)};