Bug 639, Bug 641, Bug 642: This is MD-SAL based sample implementation of a learning...
[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 class InstanceIdentifierUtils {
32
33   /**
34    * Creates an Instance Identifier (path) for node with specified id
35    *
36    * @param nodeId
37    * @return
38    */
39   public static final InstanceIdentifier<Node> createNodePath(NodeId nodeId) {
40     return InstanceIdentifier.builder(Nodes.class) //
41         .child(Node.class, new NodeKey(nodeId)) //
42         .build();
43   }
44
45   /**
46    * Shorten's node child path to node path.
47    *
48    * @param nodeChild child of node, from which we want node path.
49    * @return
50    */
51   public static final InstanceIdentifier<Node> getNodePath(InstanceIdentifier<?> nodeChild) {
52     return nodeChild.firstIdentifierOf(Node.class);
53   }
54
55
56   /**
57    * Creates a table path by appending table specific location to node path
58    *
59    * @param nodePath
60    * @param tableKey
61    * @return
62    */
63   public static final InstanceIdentifier<Table> createTablePath(InstanceIdentifier<Node> nodePath, TableKey tableKey) {
64     return InstanceIdentifier.builder(nodePath)
65         .augmentation(FlowCapableNode.class)
66         .child(Table.class, tableKey)
67         .build();
68   }
69
70   /**
71    * Creates a path for particular flow, by appending flow-specific information
72    * to table path.
73    *
74    * @param table
75    * @param flowKey
76    * @return
77    */
78   public static InstanceIdentifier<Flow> createFlowPath(InstanceIdentifier<Table> table, FlowKey flowKey) {
79     return InstanceIdentifier.builder(table)
80         .child(Flow.class, flowKey)
81         .build();
82   }
83
84   /**
85    * Extract table id from table path.
86    *
87    * @param tablePath
88    * @return
89    */
90   public static Short getTableId(InstanceIdentifier<Table> tablePath) {
91     return tablePath.firstKeyOf(Table.class, TableKey.class).getId();
92   }
93
94   /**
95    * Extracts NodeConnectorKey from node connector path.
96    */
97   public static NodeConnectorKey getNodeConnectorKey(InstanceIdentifier<?> nodeConnectorPath) {
98     return nodeConnectorPath.firstKeyOf(NodeConnector.class, NodeConnectorKey.class);
99   }
100
101   /**
102    * Extracts NodeKey from node path.
103    */
104   public static NodeKey getNodeKey(InstanceIdentifier<?> nodePath) {
105     return nodePath.firstKeyOf(Node.class, NodeKey.class);
106   }
107
108
109   //
110   public static final InstanceIdentifier<NodeConnector> createNodeConnectorIdentifier(String nodeIdValue,
111                                                                                       String nodeConnectorIdValue) {
112     return InstanceIdentifier.builder(createNodePath(new NodeId(nodeIdValue))) //
113         .child(NodeConnector.class, new NodeConnectorKey(new NodeConnectorId(nodeConnectorIdValue))) //
114         .build();
115   }
116
117   /**
118    * @param nodeConnectorRef
119    * @return
120    */
121   public static InstanceIdentifier<Node> generateNodeInstanceIdentifier(NodeConnectorRef nodeConnectorRef) {
122     return nodeConnectorRef.getValue().firstIdentifierOf(Node.class);
123   }
124
125   /**
126    * @param nodeConnectorRef
127    * @param flowTableKey
128    * @return
129    */
130   public static InstanceIdentifier<Table> generateFlowTableInstanceIdentifier(NodeConnectorRef nodeConnectorRef, TableKey flowTableKey) {
131     return InstanceIdentifier.builder(generateNodeInstanceIdentifier(nodeConnectorRef))
132         .augmentation(FlowCapableNode.class)
133         .child(Table.class, flowTableKey)
134         .build();
135   }
136
137   /**
138    * @param nodeConnectorRef
139    * @param flowTableKey
140    * @param flowKey
141    * @return
142    */
143   public static InstanceIdentifier<Flow> generateFlowInstanceIdentifier(NodeConnectorRef nodeConnectorRef,
144                                                                         TableKey flowTableKey,
145                                                                         FlowKey flowKey) {
146     return InstanceIdentifier.builder(generateFlowTableInstanceIdentifier(nodeConnectorRef, flowTableKey))
147         .child(Flow.class, flowKey)
148         .build();
149   }
150
151   public static InstanceIdentifier<Topology> generateTopologyInstanceIdentifier(String topologyId) {
152     return InstanceIdentifier.builder(NetworkTopology.class)
153         .child(Topology.class, new TopologyKey(new TopologyId(topologyId)))
154         .build();
155   }
156 }
157