OPNFLWPLUG-1032: Neon-MRI: Bump odlparent, yangtools, mdsal
[openflowplugin.git] / samples / learning-switch / src / main / java / org / opendaylight / openflowplugin / learningswitch / InstanceIdentifierUtils.java
1 /*
2  * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.openflowplugin.learningswitch;
10
11 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
22 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
23
24 public final class InstanceIdentifierUtils {
25
26     private InstanceIdentifierUtils() {
27         //hiding constructor for util class
28     }
29
30     /**
31      * Creates an Instance Identifier (path) for node with specified id.
32      *
33      * @param nodeId the NodeId
34      */
35     public static InstanceIdentifier<Node> createNodePath(final NodeId nodeId) {
36         return InstanceIdentifier.builder(Nodes.class)
37                 .child(Node.class, new NodeKey(nodeId))
38                 .build();
39     }
40
41     /**
42      * Shorten's node child path to node path.
43      *
44      * @param nodeChild child of node, from which we want node path.
45      */
46     public static InstanceIdentifier<Node> getNodePath(final InstanceIdentifier<?> nodeChild) {
47         return nodeChild.firstIdentifierOf(Node.class);
48     }
49
50     /**
51      * Creates a table path by appending table specific location to node path.
52      *
53      * @param nodePath the node path
54      * @param tableKey the table yey
55      */
56     public static InstanceIdentifier<Table> createTablePath(final InstanceIdentifier<Node> nodePath,
57             final TableKey tableKey) {
58         return nodePath.augmentation(FlowCapableNode.class).child(Table.class, tableKey);
59     }
60
61     /**
62      * Creates a path for particular flow, by appending flow-specific information
63      * to table path.
64      *
65      * @param tablePath the table path
66      * @param flowKey the flow key
67      * @return path to flow
68      */
69     public static InstanceIdentifier<Flow> createFlowPath(final InstanceIdentifier<Table> tablePath,
70             final FlowKey flowKey) {
71         return tablePath.child(Flow.class, flowKey);
72     }
73
74     /**
75      * Extract table id from table path.
76      *
77      * @param tablePath the table path
78      */
79     public static Short getTableId(final InstanceIdentifier<Table> tablePath) {
80         return tablePath.firstKeyOf(Table.class).getId();
81     }
82
83     /**
84      * Extracts NodeConnectorKey from node connector path.
85      */
86     public static NodeConnectorKey getNodeConnectorKey(final InstanceIdentifier<?> nodeConnectorPath) {
87         return nodeConnectorPath.firstKeyOf(NodeConnector.class);
88     }
89
90     public static InstanceIdentifier<NodeConnector> createNodeConnectorPath(final InstanceIdentifier<Node> nodeKey,
91             final NodeConnectorKey nodeConnectorKey) {
92         return nodeKey.child(NodeConnector.class,nodeConnectorKey);
93     }
94 }