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=========================================================
22 * Created by obarda on 6/28/2016.
24 import * as _ from "lodash";
25 import {GraphUIObjects} from "app/utils";
26 import {LoaderService} from "app/services";
31 CompositionCiNodeBase,
40 import {CommonGraphUtils} from "../../common/common-graph-utils";
41 import {CompositionGraphGeneralUtils} from "./composition-graph-general-utils";
42 import {MatchCapabilitiesRequirementsUtils} from "./match-capability-requierment-utils";
43 import {CompositionCiServicePathLink} from "../../../../models/graph/graph-links/composition-graph-links/composition-ci-service-path-link";
45 export class CompositionGraphLinkUtils {
47 constructor(private linksFactory:LinksFactory,
48 private loaderService:LoaderService,
49 private generalGraphUtils:CompositionGraphGeneralUtils,
50 private commonGraphUtils:CommonGraphUtils,
51 private matchCapabilitiesRequirementsUtils:MatchCapabilitiesRequirementsUtils) {
56 * Delete the link on server and then remove it from graph
58 * @param releaseLoading - true/false release the loader when finished
59 * @param link - the link to delete
61 public deleteLink = (cy:Cy.Instance, component:Component, releaseLoading:boolean, link:Cy.CollectionEdges) => {
63 this.loaderService.showLoader('composition-graph');
64 let onSuccessDeleteRelation = (response) => {
68 if (!releaseLoading) {
69 this.generalGraphUtils.getGraphUtilsServerUpdateQueue().addBlockingUIAction(
70 () => component.deleteRelation(link.data().relation).then(onSuccessDeleteRelation)
73 this.generalGraphUtils.getGraphUtilsServerUpdateQueue().addBlockingUIActionWithReleaseCallback(
74 () => component.deleteRelation(link.data().relation).then(onSuccessDeleteRelation),
75 () => this.loaderService.hideLoader('composition-graph'));
80 * create the link on server and than draw it on graph
81 * @param link - the link to create
85 public createLink = (link:CompositionCiLinkBase, cy:Cy.Instance, component:Component):void => {
87 this.loaderService.showLoader('composition-graph');
89 let onSuccess:(response:RelationshipModel) => void = (relation:RelationshipModel) => {
90 link.setRelation(relation);
91 this.commonGraphUtils.insertLinkToGraph(cy, link, component.getRelationRequirementCapability.bind(component));
94 link.updateLinkDirection();
96 this.generalGraphUtils.getGraphUtilsServerUpdateQueue().addBlockingUIActionWithReleaseCallback(
97 () => component.createRelation(link.relation).then(onSuccess),
98 () => this.loaderService.hideLoader('composition-graph')
102 private createSimpleLink = (match:Match, cy:Cy.Instance, component:Component):void => {
103 let newRelation:RelationshipModel = match.matchToRelationModel();
104 let linkObg:CompositionCiLinkBase = this.linksFactory.createGraphLink(cy, newRelation, newRelation.relationships[0]);
105 this.createLink(linkObg, cy, component);
108 public createLinkFromMenu = (cy:Cy.Instance, chosenMatch:Match, component:Component):void => {
111 if (chosenMatch && chosenMatch instanceof Match) {
112 this.createSimpleLink(chosenMatch, cy, component);
119 * Filters the matches for UCPE links so that shown requirements and capabilites are only related to the selected ucpe-cp
122 * @param matchesArray
123 * @returns {Array<MatchBase>}
125 public filterUcpeLinks(fromNode:CompositionCiNodeBase, toNode:CompositionCiNodeBase, matchesArray:Array<Match>):any {
127 let matchLink:Array<Match>;
129 if (fromNode.isUcpePart) {
130 matchLink = _.filter(matchesArray, (match:Match) => {
131 return match.isOwner(fromNode.id);
135 if (toNode.isUcpePart) {
136 matchLink = _.filter(matchesArray, (match:Match) => {
137 return match.isOwner(toNode.id);
140 return matchLink ? matchLink : matchesArray;
145 * open the connect link menu if the link drawn is valid - match requirements & capabilities
151 public onLinkDrawn(cy:Cy.Instance, fromNode:Cy.CollectionFirstNode, toNode:Cy.CollectionFirstNode):ConnectRelationModel {
153 if (!this.commonGraphUtils.nodeLocationsCompatible(cy, fromNode, toNode)) {
156 let linkModel:Array<CompositionCiLinkBase> = this.generalGraphUtils.getAllCompositionCiLinks(cy);
158 let possibleRelations:Array<Match> = this.matchCapabilitiesRequirementsUtils.getMatchedRequirementsCapabilities(fromNode.data().componentInstance,
159 toNode.data().componentInstance, linkModel);
161 //filter relations found to limit to specific ucpe-cp
162 possibleRelations = this.filterUcpeLinks(fromNode.data(), toNode.data(), possibleRelations);
164 //if found possibleRelations between the nodes we create relation menu directive and open the link menu
165 if (possibleRelations.length) {
166 // let menuPosition = this.generalGraphUtils.getLinkMenuPosition(cy, toNode.renderedPoint());
167 return new ConnectRelationModel(fromNode.data(), toNode.data(), possibleRelations);
174 * when we drag instance in to UCPE or out of UCPE - get all links we need to delete - one node in ucpe and one node outside of ucpe
175 * @param node - the node we dragged into or out of the ucpe
177 public deleteLinksWhenNodeMovedFromOrToUCPE(component:Component, cy:Cy.Instance, nodeMoved:Cy.CollectionNodes, vlsPendingDeletion?:Cy.CollectionNodes):void {
180 let linksToDelete:Cy.CollectionElements = cy.collection();
181 _.forEach(nodeMoved.neighborhood('node'), (neighborNode)=> {
183 if (neighborNode.data().isUcpePart) { //existing connections to ucpe or ucpe-cp - we want to delete even though nodeLocationsCompatible will technically return true
184 linksToDelete = linksToDelete.add(nodeMoved.edgesWith(neighborNode)); // This will delete the ucpe-host-link, or the vl-ucpe-link if nodeMoved is vl
185 } else if (!this.commonGraphUtils.nodeLocationsCompatible(cy, nodeMoved, neighborNode)) { //connection to regular node or vl - check if locations are compatible
186 if (!vlsPendingDeletion || !vlsPendingDeletion.intersect(neighborNode).length) { //Check if this is a link to a VL pending deletion, to prevent double deletion of between the node moved and vl
187 linksToDelete = linksToDelete.add(nodeMoved.edgesWith(neighborNode));
192 linksToDelete.each((i, link)=> {
193 this.deleteLink(cy, component, false, link);
199 * Creates a hostedOn link between a VF and UCPE
205 public createVfToUcpeLink = (component:Component, cy:Cy.Instance, ucpeNode:NodeUcpe, vfNode:CompositionCiNodeVf):void => {
206 let hostedOnMatch:Match = this.generalGraphUtils.canBeHostedOn(cy, ucpeNode.componentInstance, vfNode.componentInstance);
207 /* create relation */
208 let newRelation = new RelationshipModel();
209 newRelation.fromNode = ucpeNode.id;
210 newRelation.toNode = vfNode.id;
212 let link:CompositionCiLinkBase = this.linksFactory.createUcpeHostLink(newRelation);
213 link.relation = hostedOnMatch.matchToRelationModel();
214 this.createLink(link, cy, component);
217 private handlePathLink(cy:Cy.Instance, event:Cy.EventObject) {
218 let linkData = event.cyTarget.data();
219 let selectedPathId = linkData.pathId;
220 let pathEdges = cy.collection(`[pathId='${selectedPathId}']`);
221 if (pathEdges.length > 1) {
228 private handleVLLink(event:Cy.EventObject) {
229 let vl:Cy.CollectionNodes = event.cyTarget[0].target('.vl-node');
230 let connectedEdges:Cy.CollectionEdges = vl.connectedEdges(`[type!="${CompositionCiServicePathLink.LINK_TYPE}"]`);
231 if (vl.length && connectedEdges.length > 1) {
234 connectedEdges.select();
241 * Handles click event on links.
242 * If one edge selected: do nothing.
243 * Two or more edges: first click - select all, secondary click - select single.
247 public handleLinkClick(cy:Cy.Instance, event:Cy.EventObject) {
248 if (cy.$('edge:selected').length > 1 && event.cyTarget[0].selected()) {
249 cy.$(':selected').unselect();
251 if (event.cyTarget[0].data().type === CompositionCiServicePathLink.LINK_TYPE) {
252 this.handlePathLink(cy, event);
255 this.handleVLLink(event);
262 * Calculates the position for the menu that modifies an existing link
264 * @param elementWidth
265 * @param elementHeight
268 public calculateLinkMenuPosition(event, elementWidth, elementHeight):Point {
269 let point:Point = new Point(event.originalEvent.clientX, event.originalEvent.clientY);
270 if (event.originalEvent.view.screen.height - elementHeight < point.y) {
271 point.y = event.originalEvent.view.screen.height - elementHeight;
273 if (event.originalEvent.view.screen.width - elementWidth < point.x) {
274 point.x = event.originalEvent.view.screen.width - elementWidth;
281 * Gets the menu that is displayed when you click an existing link.
284 * @returns {LinkMenu}
286 public getModifyLinkMenu(link:Cy.CollectionFirstEdge, event:Cy.EventObject):LinkMenu {
287 let point:Point = this.calculateLinkMenuPosition(event, GraphUIObjects.MENU_LINK_VL_WIDTH_OFFSET, GraphUIObjects.MENU_LINK_VL_HEIGHT_OFFSET);
288 let menu:LinkMenu = new LinkMenu(point, true, link);
295 CompositionGraphLinkUtils.$inject = [
298 'CompositionGraphGeneralUtils',
300 'MatchCapabilitiesRequirementsUtils'