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";
23 ArtifactModel, IFileDownload, InstancesInputsPropertiesMap, InputModel, IValidate, RelationshipModel, PropertyModel, Component, ComponentInstance,
24 AttributeModel, IAppConfigurtaion, Resource, Module, DisplayModule, ArtifactGroupModel, InputsAndProperties
26 import { ComponentInstanceFactory, CommonUtils } from "app/utils";
27 import { SharingService } from "../sharing-service";
28 import { ComponentMetadata } from "../../models/component-metadata";
30 export interface IComponentService {
32 getComponent(id: string);
33 updateComponent(component: Component): ng.IPromise<Component>;
34 changeLifecycleState(component: Component, state: string, userRemarks: any): ng.IPromise<ComponentMetadata>;
35 validateName(newName: string, subtype?: string): ng.IPromise<IValidate>;
36 createComponent(component: Component): ng.IPromise<Component>;
37 addOrUpdateArtifact(componentId: string, artifact: ArtifactModel): ng.IPromise<ArtifactModel>;
38 deleteArtifact(componentId: string, artifact: string, artifactLabel): ng.IPromise<ArtifactModel>;
39 addProperty(componentId: string, property: PropertyModel): ng.IPromise<PropertyModel>;
40 updateProperty(componentId: string, property: PropertyModel): ng.IPromise<PropertyModel>;
41 addAttribute(componentId: string, attribute: AttributeModel): ng.IPromise<AttributeModel>;
42 updateAttribute(componentId: string, attribute: AttributeModel): ng.IPromise<AttributeModel>;
43 deleteProperty(componentId: string, propertyId: string): ng.IPromise<PropertyModel>;
44 deleteAttribute(componentId: string, attributeId: string): ng.IPromise<AttributeModel>;
45 checkResourceInstanceVersionChange(componentId: string, componentInstanceId: string, componentUid: string): ng.IPromise<any>;
46 changeResourceInstanceVersion(componentId: string, componentInstanceId: string, componentUid: string): ng.IPromise<ComponentInstance>;
47 updateInstanceArtifact(componentId: string, instanceId: string, artifact: ArtifactModel): ng.IPromise<ArtifactModel>;
48 addInstanceArtifact(componentId: string, instanceId: string, artifact: ArtifactModel): ng.IPromise<ArtifactModel>;
49 deleteInstanceArtifact(componentId: string, instanceId: string, artifact: string, artifactLabel): ng.IPromise<ArtifactModel>;
50 createComponentInstance(componentId: string, componentInstance: ComponentInstance): ng.IPromise<ComponentInstance>;
51 updateComponentInstance(componentId: string, componentInstance: ComponentInstance): ng.IPromise<ComponentInstance>;
52 updateMultipleComponentInstances(componentId: string, instances: Array<ComponentInstance>): ng.IPromise<Array<ComponentInstance>>;
53 downloadArtifact(componentId: string, artifactId: string): ng.IPromise<IFileDownload>;
54 uploadInstanceEnvFile(componentId: string, instanceId: string, artifact: ArtifactModel): ng.IPromise<ArtifactModel>;
55 downloadInstanceArtifact(componentId: string, instanceId: string, artifactId: string): ng.IPromise<IFileDownload>;
56 deleteComponentInstance(componentId: string, componentInstanceId: string): ng.IPromise<ComponentInstance>;
57 createRelation(componentId: string, link: RelationshipModel): ng.IPromise<RelationshipModel>;
58 deleteRelation(componentId: string, link: RelationshipModel): ng.IPromise<RelationshipModel>;
59 fetchRelation(componentId: string, linkId: string): ng.IPromise<RelationshipModel>;
60 getRequirementsCapabilities(componentId: string): ng.IPromise<any>;
61 updateInstanceProperties(componentId: string, componentInstanceId: string, properties: PropertyModel[]): ng.IPromise<PropertyModel[]>;
62 updateInstanceAttribute(componentId: string, attribute: AttributeModel): ng.IPromise<AttributeModel>;
63 getComponentInstancesFilteredByInputsAndProperties(componentId: string, searchText: string): ng.IPromise<Array<ComponentInstance>>
64 getComponentInstanceInputs(componentId: string, instanceId: string, originComponentUid): ng.IPromise<Array<InputModel>>;
65 getComponentInputs(componentId: string): ng.IPromise<Array<InputModel>>;
66 getComponentInstanceInputProperties(componentId: string, instanceId: string, inputId: string): ng.IPromise<Array<PropertyModel>>;
67 getComponentInstanceProperties(componentId: string, instanceId: string): ng.IPromise<Array<PropertyModel>>;
68 getModuleForDisplay(componentId: string, moduleId: string): ng.IPromise<DisplayModule>;
69 getComponentInstanceModule(componentId: string, componentInstanceId: string, moduleId: string): ng.IPromise<DisplayModule>;
70 updateGroupMetadata(componentId: string, group: Module): ng.IPromise<Module>;
71 getComponentInputInputsAndProperties(serviceId: string, input: string): ng.IPromise<InputsAndProperties>;
72 createInputsFromInstancesInputs(serviceId: string, instancesInputsMap: InstancesInputsPropertiesMap): ng.IPromise<Array<InputModel>>;
73 createInputsFromInstancesInputsProperties(resourceId: string, instanceInputsPropertiesMap: InstancesInputsPropertiesMap): ng.IPromise<Array<PropertyModel>>;
74 deleteComponentInput(serviceId: string, inputId: string): ng.IPromise<InputModel>;
75 getArtifactByGroupType(componentId: string, artifactGroupType: string): ng.IPromise<ArtifactGroupModel>;
76 getComponentInstanceArtifactsByGroupType(componentId: string, componentInstanceId: string, artifactGroupType: string): ng.IPromise<ArtifactGroupModel>;
77 batchDeleteComponentInstance(componentId: string, componentInstanceIdList: Array<string>): ng.IPromise<any>;
78 pasteMenuComponentInstance(componentId: string, srcComponentId: string, msg: string): ng.IPromise<string>;
79 batchDeleteRelation(componentId: string, links: Array<RelationshipModel>): ng.IPromise<Array<RelationshipModel>>
83 export class ComponentService implements IComponentService {
88 'Sdc.Services.SharingService',
93 constructor(protected restangular: restangular.IElement,
94 protected sdcConfig: IAppConfigurtaion,
95 protected sharingService: SharingService,
96 protected $q: ng.IQService,
97 protected $base64: any
100 this.restangular.setBaseUrl(sdcConfig.api.root + sdcConfig.api.component_api_root);
101 this.restangular.setRequestInterceptor(function (elem, operation) {
102 if (operation === "remove") {
107 // this.restangular.setDefaultHeaders({'Content-Type': 'application/json; charset=UTF-8'});
110 //this function is override by each service, we need to change this method to abstract when updtaing typescript version
111 protected createComponentObject = (component: Component): Component => {
115 public getComponent = (id: string): ng.IPromise<Component> => {
116 let deferred = this.$q.defer<Component>();
117 this.restangular.one(id).get().then((response: Component) => {
118 let component: Component = this.createComponentObject(response);
119 //console.log("Component Loaded successfully : ", component);
120 deferred.resolve(component);
122 console.log("Failed to load component with ID: " + id);
123 deferred.reject(err);
125 return deferred.promise;
128 public updateComponent = (component: Component): ng.IPromise<Component> => {
129 // If this is resource
130 if (component instanceof Resource) {
131 let resource: Resource = <Resource>component;
132 if (resource.importedFile) {
133 // Update resource with payload data.
134 return this.updateResourceWithPayload(resource);
136 if (component.csarUUID) {
137 // Update resource without payload data.
138 return this.updateResource(component);
140 // Update resource without payload data (metadata).
141 return this.updateResourceMetadata(component);
145 return this.updateService(component);
149 private updateService = (component: Component): ng.IPromise<Component> => {
150 let deferred = this.$q.defer<Component>();
151 this.restangular.one(component.uniqueId).one("metadata").customPUT(JSON.stringify(component)).then((response: Component) => {
152 let component: Component = this.createComponentObject(response);
153 deferred.resolve(component);
155 deferred.reject(err);
157 return deferred.promise;
160 private updateResource = (component: Component): ng.IPromise<Component> => {
161 let deferred = this.$q.defer<Component>();
162 this.restangular.one(component.uniqueId).customPUT(JSON.stringify(component)).then((response: Component) => {
163 let component: Component = this.createComponentObject(response);
164 deferred.resolve(component);
166 deferred.reject(err);
168 return deferred.promise;
171 private updateResourceMetadata = (component: Component): ng.IPromise<Component> => {
172 let deferred = this.$q.defer<Component>();
173 this.restangular.one(component.uniqueId).one('metadata').customPUT(JSON.stringify(component)).then((response: Component) => {
174 let component: Component = this.createComponentObject(response);
175 deferred.resolve(component);
177 deferred.reject(err);
179 return deferred.promise;
183 * Only resource can be updated with payload data
185 * @returns {IPromise<T>}
187 private updateResourceWithPayload = (resource: Resource): ng.IPromise<Component> => {
188 let deferred = this.$q.defer<Component>();
190 resource.payloadData = resource.importedFile.base64;
191 resource.payloadName = resource.importedFile.filename;
192 let headerObj = this.getHeaderMd5(resource);
194 this.restangular.one(resource.uniqueId).customPUT(JSON.stringify(resource), '', {}, headerObj).then((response: Component) => {
195 let componentResult: Component = this.createComponentObject(response);
196 deferred.resolve(componentResult);
198 deferred.reject(err);
201 return deferred.promise;
204 public createComponent = (component: Component): ng.IPromise<Component> => {
205 let deferred = this.$q.defer<Component>();
206 let headerObj = this.getHeaderMd5(component);
207 this.restangular.customPOST(JSON.stringify(component), '', {}, headerObj).then((response: Component) => {
208 let component: Component = this.createComponentObject(response);
209 deferred.resolve(component);
211 deferred.reject(err);
213 return deferred.promise;
216 public validateName = (newName: string, subtype?: string): ng.IPromise<IValidate> => {
217 let deferred = this.$q.defer<IValidate>();
218 this.restangular.one("validate-name").one(newName).get({ 'subtype': subtype }).then((response: any) => {
219 deferred.resolve(response.plain());
221 deferred.reject(err);
223 return deferred.promise;
226 public changeLifecycleState = (component: Component, state: string, userRemarks: any): ng.IPromise<ComponentMetadata> => {
227 let deferred = this.$q.defer<ComponentMetadata>();
228 this.restangular.one(component.uniqueId).one(state).customPOST(userRemarks).then((response: ComponentMetadata) => {
229 this.sharingService.addUuidValue(response.uniqueId, response.uuid);
230 let component: ComponentMetadata = new ComponentMetadata().deserialize(response);
231 deferred.resolve(component);
233 deferred.reject(err);
235 return deferred.promise;
238 // ------------------------------------------------ Artifacts API --------------------------------------------------//
239 public addOrUpdateArtifact = (componentId: string, artifact: ArtifactModel): ng.IPromise<ArtifactModel> => {
240 let deferred = this.$q.defer<ArtifactModel>();
242 if (artifact.payloadData) {
243 headerObj = this.getHeaderMd5(artifact);
245 this.restangular.one(componentId).one("artifacts").customPOST(JSON.stringify(artifact), artifact.uniqueId, {}, headerObj).then((response: any) => {
246 deferred.resolve(response.plain());
248 deferred.reject(err);
250 return deferred.promise;
253 public downloadArtifact = (componentId: string, artifactId: string): ng.IPromise<IFileDownload> => {
254 let deferred = this.$q.defer<IFileDownload>();
255 this.restangular.one(componentId).one("artifacts").one(artifactId).get().then((response: any) => {
256 deferred.resolve(response.plain());
258 deferred.reject(err);
260 return deferred.promise;
263 public deleteArtifact = (componentId: string, artifactId: string, artifactLabel: string): ng.IPromise<ArtifactModel> => {
264 let deferred = this.$q.defer<ArtifactModel>();
265 this.restangular.one(componentId).one("artifacts").one(artifactId).remove({ 'operation': artifactLabel }).then((response: ArtifactModel) => {
266 deferred.resolve(response);
268 deferred.reject(err);
270 return deferred.promise;
273 public getArtifactByGroupType = (componentId: string, artifactGroupType: string): ng.IPromise<ArtifactGroupModel> => {
274 let deferred = this.$q.defer<ArtifactGroupModel>();
275 this.restangular.one(componentId).one("artifactsByType").one(artifactGroupType).get().then((response: any) => {
276 var artifacts: ArtifactGroupModel = new ArtifactGroupModel(response.plain());
277 deferred.resolve(artifacts);
279 deferred.reject(err);
281 return deferred.promise;
284 public getComponentInstanceArtifactsByGroupType = (componentId: string, componentInstanceId: string, artifactGroupType: string): ng.IPromise<ArtifactGroupModel> => {
285 let deferred = this.$q.defer<ArtifactGroupModel>();
286 this.restangular.one(componentId).one("resourceInstances").one(componentInstanceId).one("artifactsByType").one(artifactGroupType).get().then((response: any) => {
287 var artifacts: ArtifactGroupModel = new ArtifactGroupModel(response.plain());
288 deferred.resolve(artifacts);
290 deferred.reject(err);
292 return deferred.promise;
296 // ------------------------------------------------ Properties API --------------------------------------------------//
297 public addProperty = (componentId: string, property: PropertyModel): ng.IPromise<PropertyModel> => {
298 let deferred = this.$q.defer<PropertyModel>();
299 this.restangular.one(componentId).one("properties").customPOST(property.convertToServerObject()).then((response: any) => {
300 let property: PropertyModel = new PropertyModel(response[Object.keys(response)[0]]);
301 deferred.resolve(property);
303 deferred.reject(err);
305 return deferred.promise;
308 public updateProperty = (componentId: string, property: PropertyModel): ng.IPromise<PropertyModel> => {
309 let deferred = this.$q.defer<PropertyModel>();
310 this.restangular.one(componentId).one("properties").one(property.uniqueId).customPUT(property.convertToServerObject()).then((response: any) => {
311 let property: PropertyModel = new PropertyModel(response[Object.keys(response)[0]]);
312 deferred.resolve(property);
314 deferred.reject(err);
316 return deferred.promise;
319 public deleteProperty = (componentId: string, propertyId: string): ng.IPromise<PropertyModel> => {
320 let deferred = this.$q.defer<PropertyModel>();
321 this.restangular.one(componentId).one("properties").one(propertyId).remove().then((response: any) => {
322 deferred.resolve(response);
324 deferred.reject(err);
326 return deferred.promise;
329 // ------------------------------------------------ Attributes API --------------------------------------------------//
330 public addAttribute = (componentId: string, attribute: AttributeModel): ng.IPromise<AttributeModel> => {
331 let deferred = this.$q.defer<AttributeModel>();
332 this.restangular.one(componentId).one("attributes").customPOST(attribute.convertToServerObject()).then((response: any) => {
333 let attribute: AttributeModel = new AttributeModel(response);
334 deferred.resolve(attribute);
336 deferred.reject(err);
338 return deferred.promise;
341 public updateAttribute = (componentId: string, attribute: AttributeModel): ng.IPromise<AttributeModel> => {
342 let deferred = this.$q.defer<AttributeModel>();
343 this.restangular.one(componentId).one("attributes").one(attribute.uniqueId).customPUT(attribute.convertToServerObject()).then((response: any) => {
344 let attribute: AttributeModel = new AttributeModel(response);
345 deferred.resolve(attribute);
347 deferred.reject(err);
349 return deferred.promise;
352 public deleteAttribute = (componentId: string, attributeId: string): ng.IPromise<AttributeModel> => {
353 let deferred = this.$q.defer<AttributeModel>();
354 this.restangular.one(componentId).one("attributes").one(attributeId).remove().then((response: any) => {
355 deferred.resolve(response);
357 deferred.reject(err);
359 return deferred.promise;
362 // ------------------------------------------------ Component Instances API --------------------------------------------------//
364 public createComponentInstance = (componentId: string, componentInstance: ComponentInstance): ng.IPromise<ComponentInstance> => {
365 let deferred = this.$q.defer<ComponentInstance>();
366 this.restangular.one(componentId).one("resourceInstance").customPOST(JSON.stringify(componentInstance)).then((response: any) => {
367 let componentInstance: ComponentInstance = ComponentInstanceFactory.createComponentInstance(response);
368 console.log("Component Instance created", componentInstance);
369 deferred.resolve(componentInstance);
371 console.log("Failed to create componentInstance. With Name: " + componentInstance.name);
372 deferred.reject(err);
374 return deferred.promise;
377 public updateComponentInstance = (componentId: string, componentInstance: ComponentInstance): ng.IPromise<ComponentInstance> => {
378 let deferred = this.$q.defer<ComponentInstance>();
379 this.restangular.one(componentId).one("resourceInstance").one(componentInstance.uniqueId).customPOST(JSON.stringify(componentInstance)).then((response: any) => {
380 let componentInstance: ComponentInstance = ComponentInstanceFactory.createComponentInstance(response);
381 console.log("Component Instance was updated", componentInstance);
382 deferred.resolve(componentInstance);
384 console.log("Failed to update componentInstance. With ID: " + componentInstance.uniqueId + "Name: " + componentInstance.name);
385 deferred.reject(err);
387 return deferred.promise;
390 public updateMultipleComponentInstances = (componentId: string, instances: Array<ComponentInstance>): ng.IPromise<Array<ComponentInstance>> => {
391 let deferred = this.$q.defer<Array<ComponentInstance>>();
392 this.restangular.one(componentId).one("resourceInstance/multipleComponentInstance").customPOST(JSON.stringify(instances)).then((response: any) => {
393 console.log("Multiple Component Instances was updated", response);
394 let updateInstances: Array<ComponentInstance> = new Array<ComponentInstance>();
395 _.forEach(response, (componentInstance: ComponentInstance) => {
396 let updatedComponentInstance: ComponentInstance = ComponentInstanceFactory.createComponentInstance(componentInstance);
397 updateInstances.push(updatedComponentInstance);
399 deferred.resolve(updateInstances);
401 console.log("Failed to update Multiple componentInstance.");
402 deferred.reject(err);
404 return deferred.promise;
407 public deleteComponentInstance = (componentId: string, componentInstanceId: string): ng.IPromise<ComponentInstance> => {
408 let deferred = this.$q.defer<ComponentInstance>();
409 this.restangular.one(componentId).one("resourceInstance").one(componentInstanceId).remove().then(() => {
410 console.log("Component Instance was deleted");
413 console.log("Failed to delete componentInstance. With ID: " + componentInstanceId);
414 deferred.reject(err);
416 return deferred.promise;
419 public checkResourceInstanceVersionChange = (componentId: string, componentInstanceId: string, componentUid: string): ng.IPromise<ComponentInstance> => {
420 let deferred = this.$q.defer<ComponentInstance>();
421 this.restangular.one(componentId).one("resourceInstance").one(componentInstanceId).one(componentUid).one("checkForwardingPathOnVersionChange").get().then((response: any) => {
422 deferred.resolve(response);
424 deferred.reject(err);
426 return deferred.promise;
429 public changeResourceInstanceVersion = (componentId: string, componentInstanceId: string, componentUid: string): ng.IPromise<ComponentInstance> => {
430 let deferred = this.$q.defer<ComponentInstance>();
431 this.restangular.one(componentId).one("resourceInstance").one(componentInstanceId).one("changeVersion").customPOST({ 'componentUid': componentUid }).then((response: any) => {
432 let componentInstance: ComponentInstance = ComponentInstanceFactory.createComponentInstance(response);
433 deferred.resolve(componentInstance);
435 deferred.reject(err);
437 return deferred.promise;
440 public downloadInstanceArtifact = (componentId: string, instanceId: string, artifactId: string): ng.IPromise<IFileDownload> => {
441 let deferred = this.$q.defer<IFileDownload>();
442 this.restangular.one(componentId).one("resourceInstances").one(instanceId).one("artifacts").one(artifactId).get().then((response: any) => {
443 deferred.resolve(response.plain());
445 deferred.reject(err);
447 return deferred.promise;
450 public updateInstanceArtifact = (componentId: string, instanceId: string, artifact: ArtifactModel): ng.IPromise<ArtifactModel> => {
451 let deferred = this.$q.defer<ArtifactModel>();
453 if (artifact.payloadData) {
454 headerObj = this.getHeaderMd5(artifact);
456 this.restangular.one(componentId).one("resourceInstance").one(instanceId).one("artifacts").customPOST(JSON.stringify(artifact), artifact.uniqueId, {}, headerObj).then((response: any) => {
457 let newArtifact = new ArtifactModel(response);
458 deferred.resolve(newArtifact);
460 deferred.reject(err);
462 return deferred.promise;
465 public addInstanceArtifact = (componentId: string, instanceId: string, artifact: ArtifactModel): ng.IPromise<ArtifactModel> => {
466 let deferred = this.$q.defer<ArtifactModel>();
468 if (artifact.payloadData) {
469 headerObj = this.getHeaderMd5(artifact);
471 this.restangular.one(componentId).one("resourceInstance").one(instanceId).one("artifacts").customPOST(JSON.stringify(artifact), artifact.uniqueId, {}, headerObj).then((response: any) => {
472 let artifact: ArtifactModel = new ArtifactModel(response.plain());
473 deferred.resolve(artifact);
475 deferred.reject(err);
477 return deferred.promise;
480 public deleteInstanceArtifact = (componentId: string, instanceId: string, artifactId: string, artifactLabel: string): ng.IPromise<ArtifactModel> => {
481 let deferred = this.$q.defer<ArtifactModel>();
482 this.restangular.one(componentId).one("resourceInstance").one(instanceId).one("artifacts").one(artifactId).remove({ 'operation': artifactLabel }).then((response: ArtifactModel) => {
483 deferred.resolve(response);
485 deferred.reject(err);
487 return deferred.promise;
490 public uploadInstanceEnvFile = (componentId: string, instanceId: string, artifact: ArtifactModel): ng.IPromise<ArtifactModel> => {
491 let deferred = this.$q.defer<ArtifactModel>();
493 if (artifact.payloadData) {
494 headerObj = this.getHeaderMd5(artifact);
496 this.restangular.one(componentId).one("resourceInstance").one(instanceId).one("artifacts").customPOST(JSON.stringify(artifact), artifact.uniqueId, {}, headerObj).then((response: any) => {
497 let newArtifact = new ArtifactModel(response);
498 deferred.resolve(newArtifact);
500 deferred.reject(err);
502 return deferred.promise;
505 public updateInstanceProperties = (componentId: string, componentInstanceId: string, properties: PropertyModel[]): ng.IPromise<PropertyModel[]> => {
506 let deferred = this.$q.defer<PropertyModel[]>();
507 this.restangular.one(componentId).one("resourceInstance").one(componentInstanceId).one("properties").customPOST(JSON.stringify(properties)).then((response: any) => {
508 const newProperties = response.map((res) => {
509 const newProperty = new PropertyModel(res);
510 newProperty.readonly = true;
511 newProperty.resourceInstanceUniqueId = componentInstanceId;
514 deferred.resolve(newProperties);
516 deferred.reject(err);
518 return deferred.promise;
521 public updateInstanceAttribute = (componentId: string, attribute: AttributeModel): ng.IPromise<AttributeModel> => {
522 let deferred = this.$q.defer<AttributeModel>();
523 let instanceId = attribute.resourceInstanceUniqueId;
524 this.restangular.one(componentId).one("resourceInstance").one(instanceId).one("attribute").customPOST(JSON.stringify(attribute)).then((response: any) => {
525 let newAttribute = new AttributeModel(response);
526 newAttribute.readonly = true;
527 newAttribute.resourceInstanceUniqueId = instanceId;
528 deferred.resolve(newAttribute);
530 deferred.reject(err);
532 return deferred.promise;
535 public createRelation = (componentId: string, link: RelationshipModel): ng.IPromise<RelationshipModel> => {
536 let deferred = this.$q.defer<RelationshipModel>();
537 const linkPayload: RelationshipModel = new RelationshipModel(link);
538 linkPayload.relationships.forEach((rel) => {
539 delete rel.capability;
540 delete rel.requirement;
542 this.restangular.one(componentId).one("resourceInstance").one("associate").customPOST(JSON.stringify(linkPayload)).then((response: any) => {
543 let relation: RelationshipModel = new RelationshipModel(response.plain());
544 console.log("Link created successfully ", relation);
545 deferred.resolve(relation);
547 console.log("Failed to create Link From: " + link.fromNode + "To: " + link.toNode);
548 deferred.reject(err);
550 return deferred.promise;
553 public deleteRelation = (componentId: string, link: RelationshipModel): ng.IPromise<RelationshipModel> => {
554 let deferred = this.$q.defer<RelationshipModel>();
555 const linkPayload: RelationshipModel = new RelationshipModel(link);
556 linkPayload.relationships.forEach((rel) => {
557 delete rel.capability;
558 delete rel.requirement;
560 this.restangular.one(componentId).one("resourceInstance").one("dissociate").customPUT(JSON.stringify(linkPayload)).then((response: any) => {
561 let relation: RelationshipModel = new RelationshipModel(response);
562 console.log("Link deleted successfully ", relation);
563 deferred.resolve(relation);
565 console.log("Failed to delete Link From: " + link.fromNode + "To: " + link.toNode);
566 deferred.reject(err);
568 return deferred.promise;
571 public fetchRelation = (componentId: string, linkId: string): ng.IPromise<RelationshipModel> => {
572 let deferred = this.$q.defer<RelationshipModel>();
573 this.restangular.one(componentId).one("relationId").one(linkId).get().then((response: any) => {
574 let relation: RelationshipModel = new RelationshipModel(response);
575 console.log("Link fetched successfully ", relation);
576 deferred.resolve(relation);
578 console.log("Failed to fetch Link Id: " + linkId);
579 deferred.reject(err);
581 return deferred.promise;
584 public getRequirementsCapabilities = (componentId: string): ng.IPromise<any> => {
585 let deferred = this.$q.defer();
586 this.restangular.one(componentId).one("requirmentsCapabilities").get().then((response: any) => {
587 console.log("Component requirement capabilities recived: ", response);
588 deferred.resolve(response);
590 console.log("Failed to get requirements & capabilities");
591 deferred.reject(err);
593 return deferred.promise;
596 public getModuleForDisplay = (componentId: string, moduleId: string): ng.IPromise<DisplayModule> => {
597 let deferred = this.$q.defer<DisplayModule>();
598 this.restangular.one(componentId).one("groups").one(moduleId).get().then((response: any) => {
599 console.log("module loaded successfully: ", response);
600 let module: DisplayModule = new DisplayModule(response);
601 deferred.resolve(module);
603 console.log("Failed to get module with id: ", moduleId);
604 deferred.reject(err);
606 return deferred.promise;
609 public getComponentInstanceModule = (componentId: string, componentInstanceId: string, moduleId: string): ng.IPromise<DisplayModule> => {
610 let deferred = this.$q.defer<DisplayModule>();
611 this.restangular.one(componentId).one("resourceInstance").one(componentInstanceId).one("groupInstance").one(moduleId).get().then((response: any) => {
612 console.log("module loaded successfully: ", response);
613 let module: DisplayModule = new DisplayModule(response);
614 deferred.resolve(module);
616 console.log("Failed to get module with id: ", moduleId);
617 deferred.reject(err);
619 return deferred.promise;
622 public getComponentInstancesFilteredByInputsAndProperties = (componentId: string, searchText?: string): ng.IPromise<Array<ComponentInstance>> => {
623 let deferred = this.$q.defer<Array<ComponentInstance>>();
624 this.restangular.one(componentId).one("componentInstances").get({ 'searchText': searchText }).then((response: any) => {
625 console.log("component instances return successfully: ", response);
626 let componentInstances: Array<ComponentInstance> = CommonUtils.initComponentInstances(response);
627 deferred.resolve(componentInstances);
629 console.log("Failed to get component instances of component with id: " + componentId);
630 deferred.reject(err);
633 return deferred.promise;
636 public getComponentInstanceInputs = (componentId: string, instanceId: string, originComponentUid): ng.IPromise<Array<InputModel>> => {
638 let deferred = this.$q.defer<Array<InputModel>>();
639 this.restangular.one(componentId).one("componentInstances").one(instanceId).one(originComponentUid).one("inputs").get().then((response: any) => {
640 console.log("component instance input return successfully: ", response);
641 let inputsArray: Array<InputModel> = new Array<InputModel>();
642 _.forEach(response, (inputObj: InputModel) => {
643 inputsArray.push(new InputModel(inputObj));
645 deferred.resolve(inputsArray);
647 console.log("Failed to get component instance input with id: " + instanceId);
648 deferred.reject(err);
651 return deferred.promise;
654 public getComponentInputs = (componentId: string): ng.IPromise<Array<InputModel>> => {
656 let deferred = this.$q.defer<Array<InputModel>>();
657 this.restangular.one(componentId).one("inputs").get().then((response: any) => {
658 console.log("component inputs return successfully: ", response);
659 let inputsArray: Array<InputModel> = new Array<InputModel>();
660 _.forEach(response, (inputObj: InputModel) => {
661 inputsArray.push(new InputModel(inputObj));
663 deferred.resolve(inputsArray);
665 console.log("Failed to get component inputs for component with id: " + componentId);
666 deferred.reject(err);
669 return deferred.promise;
672 public getComponentInstanceInputProperties = (componentId: string, instanceId: string, inputId: string): ng.IPromise<Array<PropertyModel>> => {
674 let deferred = this.$q.defer<Array<PropertyModel>>();
675 this.restangular.one(componentId).one("componentInstances").one(instanceId).one(inputId).one("properties").get().then((response: any) => {
676 console.log("component instance input properties return successfully: ", response);
677 let propertiesArray: Array<PropertyModel> = new Array<PropertyModel>();
678 _.forEach(response, (propertyObj: PropertyModel) => {
679 propertiesArray.push(new PropertyModel(propertyObj));
681 deferred.resolve(propertiesArray);
683 console.log("Failed to get component instance input properties with instanceId: " + instanceId + "and input id: " + inputId);
684 deferred.reject(err);
687 return deferred.promise;
691 public getComponentInstanceProperties = (componentId: string, instanceId: string): ng.IPromise<Array<PropertyModel>> => {
693 let deferred = this.$q.defer<Array<PropertyModel>>();
694 this.restangular.one(componentId).one("componentInstances").one(instanceId).one("properties").get().then((response: any) => {
695 console.log("component instance properties return successfully: ", response);
696 let propertiesArray: Array<PropertyModel> = new Array<PropertyModel>();
697 _.forEach(response, (propertyObj: PropertyModel) => {
698 propertiesArray.push(new PropertyModel(propertyObj));
700 deferred.resolve(propertiesArray);
702 console.log("Failed to get component instance properties with instanceId: " + instanceId);
703 deferred.reject(err);
706 return deferred.promise;
709 public updateGroupMetadata = (componentId: string, group: Module): ng.IPromise<Module> => {
711 let deferred = this.$q.defer<Module>();
712 this.restangular.one(componentId).one("groups").one(group.uniqueId).one("metadata").customPUT(JSON.stringify(group)).then((response: Module) => {
713 console.log("group metadata updated successfully: ", response);
714 let updatedGroup: Module = new Module(response);
716 deferred.resolve(updatedGroup);
718 console.log("Failed to update group metadata for component: " + componentId + " for group with id: " + group.uniqueId);
719 deferred.reject(err);
722 return deferred.promise;
725 public getComponentInputInputsAndProperties = (serviceId: string, inputId: string): ng.IPromise<InputsAndProperties> => {
726 let defer = this.$q.defer<InputsAndProperties>();
727 this.restangular.one(serviceId).one("inputs").one(inputId).get().then((response: InputsAndProperties) => {
729 let inputsArray: Array<InputModel> = new Array<InputModel>();
730 _.forEach(response.inputs, (inputObj: InputModel) => {
731 inputsArray.push(new InputModel(inputObj));
734 let propertiesArray: Array<PropertyModel> = new Array<PropertyModel>();
735 _.forEach(response.properties, (property: PropertyModel) => {
736 propertiesArray.push(new PropertyModel(property));
739 defer.resolve(new InputsAndProperties(inputsArray, propertiesArray));
741 console.log("failed to get inputs of input : ", err);
744 return defer.promise;
747 public batchDeleteComponentInstance = (componentId: string, componentInstanceIdList: Array<string>): ng.IPromise<any> => {
748 let deferred = this.$q.defer();
749 this.restangular.one(componentId).one("batchDeleteResourceInstances").customPOST(JSON.stringify(componentInstanceIdList)).then((response: any) => {
750 deferred.resolve(response.deleteFailedIds);
752 console.log("Failed to delete componentInstanceIdList. With IDs: " + componentInstanceIdList);
753 deferred.reject(err);
755 return deferred.promise;
758 public batchDeleteRelation = (componentId: string, links: Array<RelationshipModel>): ng.IPromise<Array<RelationshipModel>> => {
759 let deferred = this.$q.defer<Array<RelationshipModel>>();
761 _.forEach(links, (link: RelationshipModel) => {
762 let linkPayload: RelationshipModel = new RelationshipModel(link);
763 linkPayload.relationships.forEach((rel) => {
764 delete rel.capability;
765 delete rel.requirement;
769 this.restangular.one(componentId).one("resourceInstance").one("batchDissociate").customPUT(JSON.stringify(links)).then((response: any) => {
770 console.log("Link batch deleted successfully result is ", response);
771 let relationshipModelArray: Array<RelationshipModel> = new Array<RelationshipModel>();
772 _.forEach(response, (relationshipModelObj: RelationshipModel) => {
773 relationshipModelArray.push(new RelationshipModel(relationshipModelObj));
775 deferred.resolve(relationshipModelArray);
776 //deferred.resolve(response);
778 console.log("Failed to batch delete links", links);
779 deferred.reject(err);
781 return deferred.promise;
784 public pasteMenuComponentInstance = (componentId: string, srcComponentId: string, msg: string): ng.IPromise<string> => {
785 let deferred = this.$q.defer<string>();
786 this.restangular.one(componentId).one("copyComponentInstance").one(srcComponentId).customPOST(msg).then((response: any) => {
787 deferred.resolve(response);
789 deferred.reject(err);
791 return deferred.promise;
797 createInputsFromInstancesInputsProperties = (resourceId: string, instancePropertyMap: InstancesInputsPropertiesMap): ng.IPromise<Array<PropertyModel>> => {
798 let defer = this.$q.defer<Array<PropertyModel>>();
799 this.restangular.one(resourceId).one("create/properties").customPOST(instancePropertyMap).then((response: any) => {
800 let inputsArray: Array<PropertyModel> = new Array<PropertyModel>();
801 _.forEach(response, (inputObj: PropertyModel) => {
802 inputsArray.push(new PropertyModel(inputObj));
804 defer.resolve(inputsArray);
806 console.log("failed to create service inputs from VF instances inputs : ", err);
809 return defer.promise;
812 createInputsFromInstancesInputs = (serviceId: string, instancesMap: InstancesInputsPropertiesMap): ng.IPromise<Array<InputModel>> => {
813 let defer = this.$q.defer<Array<InputModel>>();
814 this.restangular.one(serviceId).one("create/inputs").customPOST(instancesMap).then((response: any) => {
815 let inputsArray: Array<InputModel> = new Array<InputModel>();
816 _.forEach(response, (inputObj: InputModel) => {
817 inputsArray.push(new InputModel(inputObj));
819 defer.resolve(inputsArray);
821 console.log("failed to create service inputs from VF instances inputs : ", err);
824 return defer.promise;
827 deleteComponentInput = (serviceId: string, inputId: string): ng.IPromise<InputModel> => {
828 let defer = this.$q.defer<InputModel>();
829 this.restangular.one(serviceId).one("delete").one(inputId).one("input").remove().then((response: any) => {
830 let inputToDelete = new InputModel(response);
831 defer.resolve(inputToDelete);
833 console.log("failed to delete input from service: ", err);
836 return defer.promise;
839 private getHeaderMd5 = (object: any): any => {
841 // This is ugly workaround!!!
842 // The md5 result is not correct if we do not add the line JSON.stringify(resource); twice.
843 JSON.stringify(object);
844 let componentString: string = JSON.stringify(object);
845 let md5Result = md5(componentString).toLowerCase();
846 headerObj = { 'Content-MD5': this.$base64.encode(md5Result) };