2 * Copyright (c) 2015 Huawei, Inc. and others. All rights reserved.
\r
4 * This program and the accompanying materials are made available under the
\r
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
\r
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
\r
9 package org.opendaylight.nemo.intent;
\r
11 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.hosts.PhysicalHost;
\r
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.virtual.network.rev151010.virtual.networks.virtual.network.virtual.links.VirtualLink;
\r
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.virtual.network.rev151010.virtual.networks.virtual.network.virtual.nodes.VirtualNode;
\r
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.intent.mapping.result.rev151010.intent.vn.mapping.results.user.intent.vn.mapping.IntentVnMappingResult;
\r
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.intent.mapping.result.rev151010.intent.vn.mapping.results.user.intent.vn.mapping.intent.vn.mapping.result.VirtualResource;
\r
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.*;
\r
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.engine.common.rev151010.PhysicalHostId;
\r
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.engine.common.rev151010.PhysicalNodeId;
\r
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.engine.common.rev151010.VirtualNodeId;
\r
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.Objects;
\r
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.objects.Connection;
\r
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.objects.Flow;
\r
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.objects.Node;
\r
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.operations.Operation;
\r
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.flow.instance.MatchItem;
\r
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.node.instance.Property;
\r
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.node.instance.SubNode;
\r
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.property.instance.property.values.StringValue;
\r
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.operation.rev151010.operation.instance.Action;
\r
30 import org.opendaylight.yangtools.yang.binding.DataObject;
\r
32 import java.util.ArrayList;
\r
33 import java.util.List;
\r
36 * Implement the common utilities frequently used in
\r
37 * the intent resolution.
\r
39 * @author Zhigang Ji
\r
41 public class IntentResolverUtils {
\r
43 * Check whether the node is an external layer2 group or layer3 group.
\r
45 * @param node The node to be checked.
\r
46 * @return True if the node is an external layer3 group.
\r
48 protected static boolean checkExternalLayer3Group(Node node) {
\r
49 PropertyName propertyName = new PropertyName("ac-info-network");
\r
50 Property property = getNodeProperty(node.getProperty(), propertyName);
\r
52 if ( null != property ) {
\r
53 String propertyValue = property.getPropertyValues().getStringValue().get(0).getValue();
\r
55 if ( propertyValue.equals("layer3") ) {
\r
66 * @param physicalHosts TODO
\r
70 protected static PhysicalHost getPhysicalHost(List<PhysicalHost> physicalHosts, Node node) {
\r
71 PhysicalHostId physicalHostId = new PhysicalHostId(node.getNodeId().getValue());
\r
73 return getPhysicalHost(physicalHosts, physicalHostId);
\r
79 * @param properties TODO
\r
80 * @param propertyName TODO
\r
83 protected static Property getNodeProperty(List<Property> properties, PropertyName propertyName) {
\r
84 if ( null != properties ) {
\r
85 for ( Property property : properties ) {
\r
86 if ( property.getPropertyName().equals(propertyName) ) {
\r
98 * @param property TODO
\r
101 protected static PhysicalNodeId generatePhysicalNodeIdFromNodeLocationProperty(Property property) {
\r
102 String propertyValue = property.getPropertyValues().getStringValue().get(0).getValue();
\r
104 return new PhysicalNodeId(propertyValue.substring(0, propertyValue.lastIndexOf(':')));
\r
110 * @param intentVnMappingResults TODO
\r
111 * @param intentId TODO
\r
114 protected static IntentVnMappingResult getIntentVnMappingResult(
\r
115 List<IntentVnMappingResult> intentVnMappingResults, IntentId intentId) {
\r
116 for ( IntentVnMappingResult intentVnMappingResult : intentVnMappingResults ) {
\r
117 if ( intentVnMappingResult.getIntentId().equals(intentId) ) {
\r
118 return intentVnMappingResult;
\r
128 * @param virtualNodes TODO
\r
129 * @param virtualNodeId TODO
\r
132 protected static VirtualNode getVirtualNode(List<VirtualNode> virtualNodes,
\r
133 VirtualNodeId virtualNodeId) {
\r
134 for ( VirtualNode virtualNode : virtualNodes ) {
\r
135 if ( virtualNode.getNodeId().equals(virtualNodeId) ) {
\r
136 return virtualNode;
\r
146 * @param nodes TODO
\r
147 * @param nodeId TODO
\r
150 protected static Node getNode(List<Node> nodes, NodeId nodeId) {
\r
151 for ( Node node : nodes ) {
\r
152 if ( node.getNodeId().equals(nodeId) ) {
\r
163 * @param properties TODO
\r
164 * @param propertyName TODO
\r
167 protected static org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.connection.instance.Property getConnectionProperty(
\r
168 List<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.connection.instance.Property> properties,
\r
169 PropertyName propertyName) {
\r
170 if ( null != properties ) {
\r
171 for ( org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.connection.instance.Property
\r
172 property : properties ) {
\r
173 if ( property.getPropertyName().equals(propertyName) ) {
\r
185 * @param subNodes TODO
\r
188 protected static List<SubNode> sortSubNodes(List<SubNode> subNodes) {
\r
189 if ( subNodes.isEmpty() || 1 == subNodes.size() ) {
\r
193 List<SubNode> sortedSubNodes = new ArrayList<SubNode>(subNodes.size());
\r
194 sortedSubNodes.addAll(subNodes);
\r
196 for ( SubNode subNode : subNodes ) {
\r
197 sortedSubNodes.set(subNode.getOrder().intValue(), subNode);
\r
200 return sortedSubNodes;
\r
206 * @param subNodes TODO
\r
207 * @param nodes TODO
\r
210 protected static boolean checkAllLayer2OperatingMode(List<SubNode> subNodes, List<Node> nodes) {
\r
211 if ( subNodes.isEmpty() ) {
\r
216 PropertyName propertyName = new PropertyName("operating-mode");
\r
218 String propertyValue;
\r
220 for ( SubNode subNode : subNodes ) {
\r
221 node = getNode(nodes, subNode.getNodeId());
\r
223 if ( null == node ) {
\r
227 property = getNodeProperty(node.getProperty(), propertyName);
\r
229 if ( null == property ) {
\r
233 propertyValue = property.getPropertyValues().getStringValue().get(0).getValue();
\r
235 if ( !propertyValue.equals("layer2") ) {
\r
246 * @param subNodes TODO
\r
247 * @param nodes TODO
\r
250 protected static boolean checkAllLayer3OperatingMode(List<SubNode> subNodes, List<Node> nodes) {
\r
251 if ( subNodes.isEmpty() ) {
\r
256 PropertyName propertyName = new PropertyName("operating-mode");
\r
258 String propertyValue;
\r
260 for ( SubNode subNode : subNodes ) {
\r
261 node = getNode(nodes, subNode.getNodeId());
\r
263 if ( null == node ) {
\r
267 property = getNodeProperty(node.getProperty(), propertyName);
\r
269 if ( null == property ) {
\r
273 propertyValue = property.getPropertyValues().getStringValue().get(0).getValue();
\r
275 if ( !propertyValue.equals("layer3") ) {
\r
286 * @param virtualLinks TODO
\r
287 * @param srcVirtualNodeId TODO
\r
288 * @param destVirtualNodeId TODO
\r
291 protected static VirtualLink getVirtualLink(List<VirtualLink> virtualLinks,
\r
292 VirtualNodeId srcVirtualNodeId,
\r
293 VirtualNodeId destVirtualNodeId) {
\r
294 for ( VirtualLink virtualLink : virtualLinks ) {
\r
295 if ( virtualLink.getSrcNodeId().equals(srcVirtualNodeId)
\r
296 && virtualLink.getDestNodeId().equals(destVirtualNodeId) ) {
\r
297 return virtualLink;
\r
307 * @param objects TODO
\r
308 * @param objectId TODO
\r
311 protected static DataObject getObject(Objects objects, ObjectId objectId) {
\r
312 List<Node> nodes = objects.getNode();
\r
314 if ( null != nodes ) {
\r
315 NodeId nodeId = new NodeId(objectId.getValue());
\r
316 Node node = getNode(nodes, nodeId);
\r
318 if ( null != node ) {
\r
323 List<Connection> connections = objects.getConnection();
\r
325 if ( null != connections ) {
\r
326 ConnectionId connectionId = new ConnectionId(objectId.getValue());
\r
327 Connection connection = getConnection(connections, connectionId);
\r
329 if ( null != connection ) {
\r
334 List<Flow> flows = objects.getFlow();
\r
336 if ( null != flows ) {
\r
337 FlowId flowId = new FlowId(objectId.getValue());
\r
338 Flow flow = getFlow(flows, flowId);
\r
340 if ( null != flow ) {
\r
351 * @param operations TODO
\r
352 * @param operation TODO
\r
355 protected static List<Operation> getSameTargetObjectOperations(List<Operation> operations,
\r
356 Operation operation) {
\r
359 return new ArrayList<Operation>(0);
\r
365 * @param operations TODO
\r
366 * @param operation TODO
\r
367 * @param greaterPriorityOperations TODO
\r
368 * @param equalPriorityOperations TODO
\r
370 protected static void getGreaterAndEqualPriorityOperations(List<Operation> operations, Operation operation,
\r
371 List<Operation> greaterPriorityOperations,
\r
372 List<Operation> equalPriorityOperations) {
\r
381 * @param operations TODO
\r
382 * @param operation TODO
\r
385 protected static Operation getConflictingOperation(List<Operation> operations,
\r
386 Operation operation) {
\r
395 * @param operations TODO
\r
396 * @param operation TODO
\r
399 protected static List<Operation> getConflictingOperations(List<Operation> operations,
\r
400 Operation operation) {
\r
409 * @param actions TODO
\r
410 * @param actionName TODO
\r
413 protected static Action getAction(List<Action> actions, ActionName actionName) {
\r
414 for ( Action action : actions ) {
\r
415 if ( action.getActionName().equals(actionName) ) {
\r
426 * @param virtualNodes TODO
\r
428 * @param nodes TODO
\r
429 * @param intentVnMappingResults TODO
\r
432 protected static VirtualNode getSourceVirtualRouterOfFlow(List<VirtualNode> virtualNodes,
\r
433 Flow flow, List<Node> nodes,
\r
434 List<IntentVnMappingResult> intentVnMappingResults) {
\r
435 MatchItemName matchItemName = new MatchItemName("src-ip");
\r
436 MatchItem matchItem = getMatchItem(flow.getMatchItem(), matchItemName);
\r
438 if ( null == matchItem ) {
\r
442 String matchItemValue = matchItem.getMatchItemValue().getStringValue();
\r
443 VirtualNode virtualNode = getVirtualRouterWithIpPrefix(virtualNodes,
\r
444 matchItemValue, nodes, intentVnMappingResults);
\r
446 return virtualNode;
\r
452 * @param virtualNodes TODO
\r
454 * @param nodes TODO
\r
455 * @param intentVnMappingResults TODO
\r
458 protected static VirtualNode getDestinationVirtualRouterOfFlow(List<VirtualNode> virtualNodes,
\r
459 Flow flow, List<Node> nodes,
\r
460 List<IntentVnMappingResult> intentVnMappingResults) {
\r
461 MatchItemName matchItemName = new MatchItemName("dst-ip");
\r
462 MatchItem matchItem = getMatchItem(flow.getMatchItem(), matchItemName);
\r
464 if ( null == matchItem ) {
\r
468 String matchItemValue = matchItem.getMatchItemValue().getStringValue();
\r
469 VirtualNode virtualNode = getVirtualRouterWithIpPrefix(virtualNodes,
\r
470 matchItemValue, nodes, intentVnMappingResults);
\r
472 return virtualNode;
\r
478 * @param virtualResources TODO
\r
481 protected static List<VirtualResource> sortVirtualResources(List<VirtualResource> virtualResources) {
\r
482 if ( virtualResources.isEmpty() || 1 == virtualResources.size() ) {
\r
483 return virtualResources;
\r
486 List<VirtualResource> sortedVirtualResources = new ArrayList<VirtualResource>(virtualResources.size());
\r
487 sortedVirtualResources.addAll(virtualResources);
\r
489 for ( VirtualResource virtualResource : virtualResources ) {
\r
490 sortedVirtualResources.set(virtualResource.getOrder().intValue(), virtualResource);
\r
493 return sortedVirtualResources;
\r
499 * @param physicalHosts TODO
\r
500 * @param physicalHostId TODO
\r
503 private static PhysicalHost getPhysicalHost(List<PhysicalHost> physicalHosts,
\r
504 PhysicalHostId physicalHostId) {
\r
505 for ( PhysicalHost physicalHost : physicalHosts ) {
\r
506 if ( physicalHost.getHostId().equals(physicalHostId) ) {
\r
507 return physicalHost;
\r
517 * @param connections TODO
\r
518 * @param connectionId TODO
\r
521 private static Connection getConnection(List<Connection> connections, ConnectionId connectionId) {
\r
522 for ( Connection connection : connections ) {
\r
523 if ( connection.getConnectionId().equals(connectionId) ) {
\r
534 * @param flows TODO
\r
535 * @param flowId TODO
\r
538 private static Flow getFlow(List<Flow> flows, FlowId flowId) {
\r
539 for ( Flow flow : flows ) {
\r
540 if ( flow.getFlowId().equals(flowId) ) {
\r
551 * @param matchItems TODO
\r
552 * @param matchItemName TODO
\r
555 private static MatchItem getMatchItem(List<MatchItem> matchItems, MatchItemName matchItemName) {
\r
556 if ( null != matchItems ) {
\r
557 for ( MatchItem matchItem : matchItems ) {
\r
558 if ( matchItem.getMatchItemName().equals(matchItemName) ) {
\r
570 * @param virtualNodes TODO
\r
571 * @param ipPrefix TODO
\r
572 * @param nodes TODO
\r
573 * @param intentVnMappingResults TODO
\r
576 private static VirtualNode getVirtualRouterWithIpPrefix(List<VirtualNode> virtualNodes,
\r
577 String ipPrefix, List<Node> nodes,
\r
578 List<IntentVnMappingResult> intentVnMappingResults) {
\r
579 NodeType layer2GroupNodeType = new NodeType("l2-group");
\r
580 NodeType externalGroupNodeType = new NodeType("ext-group");
\r
581 PropertyName propertyName = new PropertyName("ip-prefix");
\r
583 List<StringValue> propertyValues;
\r
584 IntentVnMappingResult intentVnMappingResult;
\r
585 VirtualResource virtualResource;
\r
586 VirtualNodeId virtualNodeId;
\r
587 VirtualNode virtualNode;
\r
589 for ( Node node : nodes ) {
\r
590 if ( node.getNodeType().equals(layer2GroupNodeType)
\r
591 || node.getNodeType().equals(externalGroupNodeType) ) {
\r
592 property = getNodeProperty(node.getProperty(), propertyName);
\r
594 if ( null != property ) {
\r
595 propertyValues = property.getPropertyValues().getStringValue();
\r
597 if ( containPropertyValue(propertyValues, ipPrefix) ) {
\r
598 intentVnMappingResult = getIntentVnMappingResult(intentVnMappingResults,
\r
599 new IntentId(node.getNodeId().getValue()));
\r
601 if ( null == intentVnMappingResult ) {
\r
605 virtualResource = intentVnMappingResult.getVirtualResource().get(0);
\r
607 if ( VirtualResource.VirtualResourceType.Vport
\r
608 == virtualResource.getVirtualResourceType() ) {
\r
609 virtualNodeId = new VirtualNodeId(
\r
610 virtualResource.getParentVirtualResourceEntityId().getValue());
\r
611 } else if ( VirtualResource.VirtualResourceType.Vnode
\r
612 == virtualResource.getVirtualResourceType() ) {
\r
613 virtualNodeId = new VirtualNodeId(
\r
614 virtualResource.getVirtualResourceEntityId().getValue());
\r
619 virtualNode = getVirtualNode(virtualNodes, virtualNodeId);
\r
621 if ( null == virtualNode ) {
\r
625 if ( VirtualNode.NodeType.Vrouter == virtualNode.getNodeType() ) {
\r
626 return virtualNode;
\r
639 * @param propertyValues TODO
\r
640 * @param propertyValue TODO
\r
643 private static boolean containPropertyValue(List<StringValue> propertyValues, String propertyValue) {
\r
644 for ( StringValue stringValue : propertyValues ) {
\r
645 if ( stringValue.getValue().equals(propertyValue) ) {
\r