Merge "Bug 1029: Remove dead code: sal-schema-repository-api"
[controller.git] / opendaylight / md-sal / samples / l2switch / implementation / src / main / java / org / opendaylight / controller / sample / l2switch / md / 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.controller.sample.l2switch.md.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 /* InstanceIdentifierUtils provides utility functions related to InstanceIdentifiers.
30  */
31 public final class InstanceIdentifierUtils {
32
33     private InstanceIdentifierUtils() {
34         throw new UnsupportedOperationException("Utility class should never be instantiated");
35     }
36
37     /**
38      * Creates an Instance Identifier (path) for node with specified id
39      *
40      * @param nodeId
41      * @return
42      */
43     public static final InstanceIdentifier<Node> createNodePath(final NodeId nodeId) {
44         return InstanceIdentifier.builder(Nodes.class) //
45                 .child(Node.class, new NodeKey(nodeId)) //
46                 .build();
47     }
48
49     /**
50      * Shorten's node child path to node path.
51      *
52      * @param nodeChild child of node, from which we want node path.
53      * @return
54      */
55     public static final InstanceIdentifier<Node> getNodePath(final InstanceIdentifier<?> nodeChild) {
56         return nodeChild.firstIdentifierOf(Node.class);
57     }
58
59
60     /**
61      * Creates a table path by appending table specific location to node path
62      *
63      * @param nodePath
64      * @param tableKey
65      * @return
66      */
67     public static final InstanceIdentifier<Table> createTablePath(final InstanceIdentifier<Node> nodePath, final TableKey tableKey) {
68         return nodePath.builder()
69                 .augmentation(FlowCapableNode.class)
70                 .child(Table.class, tableKey)
71                 .build();
72     }
73
74     /**
75      * Creates a path for particular flow, by appending flow-specific information
76      * to table path.
77      *
78      * @param table
79      * @param flowKey
80      * @return
81      */
82     public static InstanceIdentifier<Flow> createFlowPath(final InstanceIdentifier<Table> table, final FlowKey flowKey) {
83         return table.child(Flow.class, flowKey);
84     }
85
86     /**
87      * Extract table id from table path.
88      *
89      * @param tablePath
90      * @return
91      */
92     public static Short getTableId(final InstanceIdentifier<Table> tablePath) {
93         return tablePath.firstKeyOf(Table.class, TableKey.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, NodeConnectorKey.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, NodeKey.class);
108     }
109
110
111     //
112     public static final InstanceIdentifier<NodeConnector> createNodeConnectorIdentifier(final String nodeIdValue,
113             final String nodeConnectorIdValue) {
114         return createNodePath(new NodeId(nodeIdValue))
115                 .child(NodeConnector.class, new NodeConnectorKey(new NodeConnectorId(nodeConnectorIdValue)));
116     }
117
118     /**
119      * @param nodeConnectorRef
120      * @return
121      */
122     public static InstanceIdentifier<Node> generateNodeInstanceIdentifier(final NodeConnectorRef nodeConnectorRef) {
123         return nodeConnectorRef.getValue().firstIdentifierOf(Node.class);
124     }
125
126     /**
127      * @param nodeConnectorRef
128      * @param flowTableKey
129      * @return
130      */
131     public static InstanceIdentifier<Table> generateFlowTableInstanceIdentifier(final NodeConnectorRef nodeConnectorRef, final TableKey flowTableKey) {
132         return generateNodeInstanceIdentifier(nodeConnectorRef).builder()
133                 .augmentation(FlowCapableNode.class)
134                 .child(Table.class, flowTableKey)
135                 .build();
136     }
137
138     /**
139      * @param nodeConnectorRef
140      * @param flowTableKey
141      * @param flowKey
142      * @return
143      */
144     public static InstanceIdentifier<Flow> generateFlowInstanceIdentifier(final NodeConnectorRef nodeConnectorRef,
145             final TableKey flowTableKey,
146             final FlowKey flowKey) {
147         return generateFlowTableInstanceIdentifier(nodeConnectorRef, flowTableKey).child(Flow.class, flowKey);
148     }
149
150     public static InstanceIdentifier<Topology> generateTopologyInstanceIdentifier(final String topologyId) {
151         return InstanceIdentifier.builder(NetworkTopology.class)
152                 .child(Topology.class, new TopologyKey(new TopologyId(topologyId)))
153                 .build();
154     }
155 }
156