Update to work on Sodium SR1
[l2switch.git] / loopremover / implementation / src / main / java / org / opendaylight / l2switch / loopremover / util / InstanceIdentifierUtils.java
1 /*
2  * Copyright (c) 2014 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 package org.opendaylight.l2switch.loopremover.util;
9
10 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
11 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
24 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28
29 /**
30  * InstanceIdentifierUtils provides utility functions related to InstanceIdentifiers.
31  */
32 public final class InstanceIdentifierUtils {
33
34     private InstanceIdentifierUtils() {
35         throw new UnsupportedOperationException("Utility class should never be instantiated");
36     }
37
38     /**
39      * Creates an Instance Identifier (path) for node with specified id.
40      *
41      * @param nodeId the node id
42      * @return the node InstanceIdentifier
43      */
44     public static InstanceIdentifier<Node> createNodePath(final NodeId nodeId) {
45         return InstanceIdentifier.builder(Nodes.class) //
46                 .child(Node.class, new NodeKey(nodeId)) //
47                 .build();
48     }
49
50     /**
51      * Shorten's node child path to node path.
52      *
53      * @param nodeChild
54      *            child of node, from which we want node path.
55      * @return the Node InstanceIdentifier
56      */
57     public static InstanceIdentifier<Node> getNodePath(final InstanceIdentifier<?> nodeChild) {
58         return nodeChild.firstIdentifierOf(Node.class);
59     }
60
61     /**
62      * Creates a table path by appending table specific location to node path.
63      *
64      * @param nodePath the node path
65      * @param tableKey the table key
66      * @return the table InstanceIdentifier
67      */
68     public static InstanceIdentifier<Table> createTablePath(final InstanceIdentifier<Node> nodePath,
69             final TableKey tableKey) {
70         return nodePath.builder().augmentation(FlowCapableNode.class).child(Table.class, tableKey).build();
71     }
72
73     /**
74      * Creates a path for particular flow, by appending flow-specific
75      * information to table path.
76      *
77      * @param table the table path
78      * @param flowKey the floe key
79      * @return the flow InstanceIdentifier
80      */
81     public static InstanceIdentifier<Flow> createFlowPath(final InstanceIdentifier<Table> table,
82             final FlowKey flowKey) {
83         return table.child(Flow.class, flowKey);
84     }
85
86     /**
87      * Extract table id from table path.
88      *
89      * @param tablePath the table path
90      * @return the table id
91      */
92     public static Short getTableId(final InstanceIdentifier<Table> tablePath) {
93         return tablePath.firstKeyOf(Table.class).getId();
94     }
95
96     /**
97      * Extracts NodeConnectorKey from node connector path.
98      */
99     public static NodeConnectorKey getNodeConnectorKey(final InstanceIdentifier<?> nodeConnectorPath) {
100         return nodeConnectorPath.firstKeyOf(NodeConnector.class);
101     }
102
103     /**
104      * Extracts NodeKey from node path.
105      */
106     public static NodeKey getNodeKey(final InstanceIdentifier<?> nodePath) {
107         return nodePath.firstKeyOf(Node.class);
108     }
109
110     public static InstanceIdentifier<NodeConnector> createNodeConnectorIdentifier(final String nodeIdValue,
111             final String nodeConnectorIdValue) {
112         return createNodePath(new NodeId(nodeIdValue)).child(NodeConnector.class,
113                 new NodeConnectorKey(new NodeConnectorId(nodeConnectorIdValue)));
114     }
115
116     public static InstanceIdentifier<Node> generateNodeInstanceIdentifier(final NodeConnectorRef nodeConnectorRef) {
117         return nodeConnectorRef.getValue().firstIdentifierOf(Node.class);
118     }
119
120     public static InstanceIdentifier<Table> generateFlowTableInstanceIdentifier(final NodeConnectorRef nodeConnectorRef,
121             final TableKey flowTableKey) {
122         return generateNodeInstanceIdentifier(nodeConnectorRef).builder().augmentation(FlowCapableNode.class)
123                 .child(Table.class, flowTableKey).build();
124     }
125
126     public static InstanceIdentifier<Flow> generateFlowInstanceIdentifier(final NodeConnectorRef nodeConnectorRef,
127             final TableKey flowTableKey, final FlowKey flowKey) {
128         return generateFlowTableInstanceIdentifier(nodeConnectorRef, flowTableKey).child(Flow.class, flowKey);
129     }
130
131     public static InstanceIdentifier<Topology> generateTopologyInstanceIdentifier(final String topologyId) {
132         return InstanceIdentifier.builder(NetworkTopology.class)
133                 .child(Topology.class, new TopologyKey(new TopologyId(topologyId))).build();
134     }
135 }