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 Requirement, CompositionCiLinkBase, CapabilitiesGroup, RequirementsGroup, Match,
24 CompositionCiNodeBase, Component, Capability
26 import {ComponentInstance} from "../../../../models/componentsInstances/componentInstance";
28 * Created by obarda on 1/1/2017.
31 export class MatchCapabilitiesRequirementsUtils {
34 * Shows + icon in corner of each node passed in
35 * @param filteredNodesData
38 public highlightMatchingComponents(filteredNodesData, cy:Cy.Instance) {
39 _.each(filteredNodesData, (data:any) => {
40 let node = cy.getElementById(data.id);
41 cy.emit('showhandle', [node]);
46 * Adds opacity to each node that cannot be linked to hovered node
47 * @param filteredNodesData
50 * @param hoveredNodeData
52 public fadeNonMachingComponents(filteredNodesData, nodesData, cy:Cy.Instance, hoveredNodeData?) {
53 let fadeNodes = _.xorWith(nodesData, filteredNodesData, (node1, node2) => {
54 return node1.id === node2.id;
56 if (hoveredNodeData) {
57 _.remove(fadeNodes, hoveredNodeData);
60 _.each(fadeNodes, (node) => {
61 cy.getElementById(node.id).style({'background-image-opacity': 0.4});
67 * Resets all nodes to regular opacity
70 public resetFadedNodes(cy:Cy.Instance) {
72 cy.nodes().style({'background-image-opacity': 1});
76 private static isRequirementFulfilled(fromNodeId:string, requirement:any, links:Array<CompositionCiLinkBase>):boolean {
77 return _.some(links, {
79 'fromNode': fromNodeId,
81 'requirementOwnerId': requirement.ownerId,
82 'requirement': requirement.name,
84 'type': requirement.relationship
92 private static isMatch(requirement:Requirement, capability:Capability):boolean {
93 if (capability.type === requirement.capability) {
94 if (requirement.node) {
95 if (_.includes(capability.capabilitySources, requirement.node)) {
105 public getMatchedRequirementsCapabilities(fromComponentInstance:ComponentInstance,
106 toComponentInstance:ComponentInstance,
107 links:Array<CompositionCiLinkBase>):Array<Match> {
108 let fromToMatches:Array<Match> = this.getMatches(fromComponentInstance.requirements,
109 toComponentInstance.capabilities,
111 fromComponentInstance.uniqueId,
112 toComponentInstance.uniqueId, true);
113 let toFromMatches:Array<Match> = this.getMatches(toComponentInstance.requirements,
114 fromComponentInstance.capabilities,
116 fromComponentInstance.uniqueId,
117 toComponentInstance.uniqueId, false);
119 return fromToMatches.concat(toFromMatches);
122 /***** REFACTORED FUNCTIONS START HERE *****/
124 public getMatches(requirements:RequirementsGroup, capabilities:CapabilitiesGroup, links:Array<CompositionCiLinkBase>,
125 fromId:string, toId:string, isFromTo: boolean):Array<Match> {
126 let matches:Array<Match> = [];
127 let unfulfilledReqs = this.getUnfulfilledRequirements(fromId, requirements, links);
128 _.forEach(unfulfilledReqs, (req)=> {
129 _.forEach(_.flatten(_.values(capabilities)), (capability:Capability)=> {
130 if (MatchCapabilitiesRequirementsUtils.isMatch(req, capability)) {
132 matches.push(new Match(req, capability, isFromTo, fromId, toId));
134 matches.push(new Match(req, capability, isFromTo, toId, fromId));
142 public getUnfulfilledRequirements = (fromNodeId:string, requirements:RequirementsGroup, links:Array<CompositionCiLinkBase>):Array<Requirement>=> {
144 let requirementArray:Array<Requirement> = [];
145 _.forEach(_.flatten(_.values(requirements)), (requirement:Requirement)=> {
146 if (requirement.name !== 'dependency' && requirement.parentName !== 'dependency' && !MatchCapabilitiesRequirementsUtils.isRequirementFulfilled(fromNodeId, requirement, links)) {
147 requirementArray.push(requirement);
150 return requirementArray;
155 * Returns true if there is a match between the capabilities and requirements that are passed in
156 * @param requirements
157 * @param capabilities
160 public containsMatch = (requirements:Array<Requirement>, capabilities:CapabilitiesGroup):boolean => {
161 return _.some(requirements, (req:Requirement)=> {
162 return _.some(_.flatten(_.values(capabilities)), (capability:Capability) => {
163 return MatchCapabilitiesRequirementsUtils.isMatch(req, capability);
169 * Returns array of nodes that can connect to the component.
170 * In order to connect, one of the following conditions must be met:
171 * 1. component has an unfulfilled requirement that matches a node's capabilities
172 * 2. node has an unfulfilled requirement that matches the component's capabilities
173 * 3. vl is passed in which has the capability to fulfill requirement from component and requirement on node.
175 public findMatchingNodes(component:Component, nodeDataArray:Array<CompositionCiNodeBase>,
176 links:Array<CompositionCiLinkBase>):Array<any> //TODO allow for VL array and TEST
178 let componentRequirements:Array<Requirement> = this.getUnfulfilledRequirements(component.uniqueId, component.requirements, links);
179 return _.filter(nodeDataArray, (node:any)=> {
180 if (node && node.componentInstance) {
182 //Check if component has an unfulfilled requirement that can be met by one of nodes's capabilities (#1)
183 if (componentRequirements.length && node.category !== 'groupCp' && this.containsMatch(componentRequirements, node.componentInstance.capabilities)) {
186 } else { //Check if node has unfulfilled requirement that can be filled by component (#2)
187 let nodeRequirements:Array<Requirement> = this.getUnfulfilledRequirements(node.componentInstance.uniqueId, node.componentInstance.requirements, links);
188 if (!nodeRequirements.length) return false;
189 if (this.containsMatch(nodeRequirements, component.capabilities)) {
198 MatchCapabilitiesRequirementsUtils.$inject = [];