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 Requirement, CompositionCiLinkBase, CapabilitiesGroup, RequirementsGroup, Match,
23 CompositionCiNodeBase, Component, Capability
25 import {ComponentInstance} from "../../../../models/componentsInstances/componentInstance";
27 * Created by obarda on 1/1/2017.
30 export class MatchCapabilitiesRequirementsUtils {
33 * Shows + icon in corner of each node passed in
34 * @param filteredNodesData
37 public highlightMatchingComponents(filteredNodesData, cy:Cy.Instance) {
38 _.each(filteredNodesData, (data:any) => {
39 let node = cy.getElementById(data.id);
40 cy.emit('showhandle', [node]);
45 * Adds opacity to each node that cannot be linked to hovered node
46 * @param filteredNodesData
49 * @param hoveredNodeData
51 public fadeNonMachingComponents(filteredNodesData, nodesData, cy:Cy.Instance, hoveredNodeData?) {
52 let fadeNodes = _.xorWith(nodesData, filteredNodesData, (node1, node2) => {
53 return node1.id === node2.id;
55 if (hoveredNodeData) {
56 _.remove(fadeNodes, hoveredNodeData);
59 _.each(fadeNodes, (node) => {
60 cy.getElementById(node.id).style({'background-image-opacity': 0.4});
66 * Resets all nodes to regular opacity
69 public resetFadedNodes(cy:Cy.Instance) {
71 cy.nodes().style({'background-image-opacity': 1});
75 private static isRequirementFulfilled(fromNodeId:string, requirement:any, links:Array<CompositionCiLinkBase>):boolean {
76 return _.some(links, {
78 'fromNode': fromNodeId,
80 'requirementOwnerId': requirement.ownerId,
81 'requirement': requirement.name,
83 'type': requirement.relationship
91 private static isMatch(requirement:Requirement, capability:Capability):boolean {
92 if (capability.type === requirement.capability) {
93 if (requirement.node) {
94 if (_.includes(capability.capabilitySources, requirement.node)) {
104 public getMatchedRequirementsCapabilities(fromComponentInstance:ComponentInstance,
105 toComponentInstance:ComponentInstance,
106 links:Array<CompositionCiLinkBase>):Array<Match> {
107 let fromToMatches:Array<Match> = this.getMatches(fromComponentInstance.requirements,
108 toComponentInstance.capabilities,
110 fromComponentInstance.uniqueId,
111 toComponentInstance.uniqueId, true);
112 let toFromMatches:Array<Match> = this.getMatches(toComponentInstance.requirements,
113 fromComponentInstance.capabilities,
115 fromComponentInstance.uniqueId,
116 toComponentInstance.uniqueId, false);
118 return fromToMatches.concat(toFromMatches);
121 /***** REFACTORED FUNCTIONS START HERE *****/
123 public getMatches(requirements:RequirementsGroup, capabilities:CapabilitiesGroup, links:Array<CompositionCiLinkBase>,
124 fromId:string, toId:string, isFromTo: boolean):Array<Match> {
125 let matches:Array<Match> = [];
126 let unfulfilledReqs = this.getUnfulfilledRequirements(fromId, requirements, links);
127 _.forEach(unfulfilledReqs, (req)=> {
128 _.forEach(_.flatten(_.values(capabilities)), (capability:Capability)=> {
129 if (MatchCapabilitiesRequirementsUtils.isMatch(req, capability)) {
131 matches.push(new Match(req, capability, isFromTo, fromId, toId));
133 matches.push(new Match(req, capability, isFromTo, toId, fromId));
141 public getUnfulfilledRequirements = (fromNodeId:string, requirements:RequirementsGroup, links:Array<CompositionCiLinkBase>):Array<Requirement>=> {
143 let requirementArray:Array<Requirement> = [];
144 _.forEach(_.flatten(_.values(requirements)), (requirement:Requirement)=> {
145 if (requirement.name !== "dependency" && !MatchCapabilitiesRequirementsUtils.isRequirementFulfilled(fromNodeId, requirement, links)) {
146 requirementArray.push(requirement);
149 return requirementArray;
154 * Returns true if there is a match between the capabilities and requirements that are passed in
155 * @param requirements
156 * @param capabilities
159 public containsMatch = (requirements:Array<Requirement>, capabilities:CapabilitiesGroup):boolean => {
160 return _.some(requirements, (req:Requirement)=> {
161 return _.some(_.flatten(_.values(capabilities)), (capability:Capability) => {
162 return MatchCapabilitiesRequirementsUtils.isMatch(req, capability);
168 * Returns array of nodes that can connect to the component.
169 * In order to connect, one of the following conditions must be met:
170 * 1. component has an unfulfilled requirement that matches a node's capabilities
171 * 2. node has an unfulfilled requirement that matches the component's capabilities
172 * 3. vl is passed in which has the capability to fulfill requirement from component and requirement on node.
174 public findMatchingNodes(component:Component, nodeDataArray:Array<CompositionCiNodeBase>,
175 links:Array<CompositionCiLinkBase>):Array<any> //TODO allow for VL array and TEST
177 let componentRequirements:Array<Requirement> = this.getUnfulfilledRequirements(component.uniqueId, component.requirements, links);
178 return _.filter(nodeDataArray, (node:any)=> {
179 if (node && node.componentInstance) {
181 //Check if component has an unfulfilled requirement that can be met by one of nodes's capabilities (#1)
182 if (componentRequirements.length && node.category !== 'groupCp' && this.containsMatch(componentRequirements, node.componentInstance.capabilities)) {
185 } else { //Check if node has unfulfilled requirement that can be filled by component (#2)
186 let nodeRequirements:Array<Requirement> = this.getUnfulfilledRequirements(node.componentInstance.uniqueId, node.componentInstance.requirements, links);
187 if (!nodeRequirements.length) return false;
188 if (this.containsMatch(nodeRequirements, component.capabilities)) {
197 MatchCapabilitiesRequirementsUtils.$inject = [];