Uni Add cmd CLI implmentation
[unimgr.git] / impl / src / main / java / org / opendaylight / unimgr / impl / UnimgrMapper.java
1 /*
2  * Copyright (c) 2015 CableLabs 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.unimgr.impl;
9
10 import java.util.List;
11
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentation;
17 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.LinkId;
18 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
19 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
20 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
21 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
22 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
24 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkKey;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
28 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30
31 public class UnimgrMapper {
32
33     /**
34      * Create an OVSDB Bridge node Instance Identifier by constructing
35      * a nodeId manually. This function is different from
36      * getOvsdbBridgeNodeIid
37      * @param ovsdbNode The OVSDB node that has the OvsdbNodeAugmentation
38      * @param bridgeName The bridge name appended to the URI.
39      * @return An Instance Identifier of a bridge
40      */
41     public static InstanceIdentifier<Node> createOvsdbBridgeNodeIid(Node ovsdbNode,
42                                                                     String bridgeName) {
43         String bridgeNodeName = ovsdbNode.getNodeId().getValue()
44                             + UnimgrConstants.DEFAULT_BRIDGE_NODE_ID_SUFFIX
45                             + bridgeName;
46         NodeId bridgeNodeId = new NodeId(bridgeNodeName);
47         InstanceIdentifier<Node> bridgeNodePath = InstanceIdentifier
48                                                       .create(NetworkTopology.class)
49                                                       .child(Topology.class,
50                                                               new TopologyKey(UnimgrConstants.OVSDB_TOPOLOGY_ID))
51                                                       .child(Node.class, new NodeKey(bridgeNodeId));
52         return bridgeNodePath;
53     }
54
55     /**
56      * Generates an Instance Identifier by using a Link ID. The Link Id
57      * is appended as a link key.
58      * @param id The LinkId of a given Link. Similiar to a nodeId.
59      * @return An Instance Identifier for a given Link
60      */
61     public static InstanceIdentifier<Link> getEvcLinkIid(LinkId id) {
62         InstanceIdentifier<Link> linkPath = InstanceIdentifier
63                                                 .create(NetworkTopology.class)
64                                                 .child(Topology.class,
65                                                         new TopologyKey(UnimgrConstants.EVC_TOPOLOGY_ID))
66                                                 .child(Link.class,
67                                                         new LinkKey(id));
68         return linkPath;
69     }
70
71     /**
72      * Generates an Instance Identifier for the unimgr:evc URI.
73      * @return An Instance Identifier for the EVC topology: unimgr:evc
74      */
75     public static InstanceIdentifier<Topology> getEvcTopologyIid() {
76         InstanceIdentifier<Topology> topoPath = InstanceIdentifier
77                                                     .create(NetworkTopology.class)
78                                                     .child(Topology.class,
79                                                             new TopologyKey(UnimgrConstants.EVC_TOPOLOGY_ID));
80         return topoPath;
81     }
82
83     /**
84      * Generates an Instance Identifier for the EVC nodes.
85      * @return An Instance Identifier for the EVC nodes.
86      */
87     public static InstanceIdentifier<Node> getEvcTopologyNodeIid() {
88         InstanceIdentifier<Node> nodePath = InstanceIdentifier
89                                                 .create(NetworkTopology.class)
90                                                 .child(Topology.class,
91                                                         new TopologyKey(UnimgrConstants.EVC_TOPOLOGY_ID))
92                                                 .child(Node.class);
93         return nodePath;
94     }
95
96     /**
97      * Generates an Instance Identifier for an OvsdbBridgeNode by retrieving the Iid
98      * via the OvsdbNodeAugmentation's BridgeRef.
99      * the same as createOvsdbBridgeNodeIid.
100      * @param ovsdbNode
101      * @return An Instance Identifier for a bridge associated with an OVSDB node.
102      */
103     public static InstanceIdentifier<Node> getOvsdbBridgeNodeIid(Node ovsdbNode) {
104         OvsdbNodeAugmentation ovsdbNodeAugmentation = ovsdbNode.getAugmentation(OvsdbNodeAugmentation.class);
105         InstanceIdentifier<Node> nodePath = ovsdbNodeAugmentation
106                                                 .getManagedNodeEntry()
107                                                 .iterator()
108                                                 .next()
109                                                 .getBridgeRef()
110                                                 .getValue()
111                                                 .firstIdentifierOf(Node.class);
112         return nodePath;
113     }
114
115     /**
116      * Generates an Instance Identifier for a specific OVSDB node
117      * by using its IP address.
118      * @param ipAddress The IP address of the OVSDB node. This should be the remote IP
119      * @return An Instance Identifier for a specific OVSDB node
120      */
121     public static InstanceIdentifier<Node> getOvsdbNodeIid(IpAddress ipAddress) {
122         String nodeId = UnimgrConstants.OVSDB_PREFIX
123                             + ipAddress.getIpv4Address().getValue().toString()
124                             + ":"
125                             + UnimgrConstants.OVSDB_PORT;
126         InstanceIdentifier<Node> nodePath = InstanceIdentifier
127                                                 .create(NetworkTopology.class)
128                                                 .child(Topology.class,
129                                                         new TopologyKey(UnimgrConstants.OVSDB_TOPOLOGY_ID))
130                                                 .child(Node.class,
131                                                         new NodeKey(new NodeId(nodeId)));
132         return nodePath;
133     }
134
135     /**
136      * Generates an Instance Identifier for a specific OVSDB node by
137      * using the node Id
138      * @param nodeId The node ID of a specific OVSDB node.
139      * @return An Instance Identifier for a specific OVSDB node.
140      */
141     public static InstanceIdentifier<Node> getOvsdbNodeIid(NodeId nodeId) {
142         InstanceIdentifier<Node> nodePath = InstanceIdentifier
143                                                 .create(NetworkTopology.class)
144                                                 .child(Topology.class,
145                                                         new TopologyKey(UnimgrConstants.OVSDB_TOPOLOGY_ID))
146                                                 .child(Node.class,
147                                                         new NodeKey(nodeId));
148         return nodePath;
149     }
150
151     /**
152      * Generates an Instance Identifier for the OVSDB topology ovsdb:1
153      * @return An Instance Identifier for the OVSDB topology ovsdb:1
154      */
155     public static InstanceIdentifier<Topology> getOvsdbTopologyIid() {
156         InstanceIdentifier<Topology> topoPath = InstanceIdentifier
157                                                     .create(NetworkTopology.class)
158                                                     .child(Topology.class,
159                                                             new TopologyKey(UnimgrConstants.OVSDB_TOPOLOGY_ID));
160         return topoPath;
161     }
162
163     /**
164      * Generates an Instance Identifier for a Termination Point by using
165      * the Bridge Node and the Port Name.
166      * @param bridgeNode The bridge where the port resides.
167      * @param portName The name of the port, example: eth0
168      * @return
169      */
170     public static InstanceIdentifier<TerminationPoint> getTerminationPointIid(
171                                                            Node bridgeNode,
172                                                            String portName) {
173         InstanceIdentifier<TerminationPoint> terminationPointPath =
174                                                  InstanceIdentifier
175                                                      .create(NetworkTopology.class)
176                                                      .child(Topology.class,
177                                                              new TopologyKey(UnimgrConstants.OVSDB_TOPOLOGY_ID))
178                                                      .child(Node.class, bridgeNode.getKey())
179                                                      .child(TerminationPoint.class,
180                                                              new TerminationPointKey(new TpId(portName)));
181         return terminationPointPath;
182     }
183
184     /**
185      * Generates an Instance Identifier for a Termination Point
186      * by using the bridge node and the Termination Point ID.
187      * @param bridgeNode The bridge Node to where the TP resides.
188      * @param tpId The termination point ID
189      * @return An Instance Identifier for a specific TP
190      */
191     public static InstanceIdentifier<TerminationPoint> getTerminationPointIid(
192                                                            Node bridgeNode,
193                                                            TpId tpId) {
194         InstanceIdentifier<TerminationPoint> terminationPointPath =
195                                                  InstanceIdentifier
196                                                      .create(NetworkTopology.class)
197                                                      .child(Topology.class,
198                                                              new TopologyKey(UnimgrConstants.OVSDB_TOPOLOGY_ID))
199                                                      .child(Node.class,
200                                                              bridgeNode.getKey())
201                                                      .child(TerminationPoint.class,
202                                                              new TerminationPointKey(tpId));
203         return terminationPointPath;
204     }
205
206     /**
207      * Generates an Instance Identifier for a UNI by querying the datastore.
208      * Query will ask the Operational store by default.
209      * @param dataBroker
210      * @param ip The IP of the UNI
211      * @return An Instance Identifier of a UNI by using its IP address.
212      */
213     public static InstanceIdentifier<Node> getUniIid(DataBroker dataBroker,
214                                                      IpAddress ip) {
215         List<Node> uniNodes = UnimgrUtils.getUniNodes(dataBroker,
216                                                       LogicalDatastoreType.OPERATIONAL);
217         for (Node node : uniNodes) {
218             UniAugmentation uniAugmentation = node.getAugmentation(UniAugmentation.class);
219             if (uniAugmentation.getIpAddress().equals(ip)) {
220                 InstanceIdentifier<Node> uniNode = InstanceIdentifier
221                                                        .create(NetworkTopology.class)
222                                                        .child(Topology.class,
223                                                                new TopologyKey(UnimgrConstants.UNI_TOPOLOGY_ID))
224                                                        .child(Node.class,
225                                                                new NodeKey(node.getKey()));
226                 return uniNode;
227             }
228         }
229         return null;
230     }
231
232     /**
233      * Generates an Instance Identifier for a UNI by querying the datastore
234      * with the IP address of the UNI.
235      * @param dataBroker
236      * @param ip The IP of the UNI
237      * @param store The store where the query should be sent
238      * @return An Instance Identifier of a UNI by using its IP address.
239      */
240     public static InstanceIdentifier<Node> getUniIid(DataBroker dataBroker,
241                                                      IpAddress ip,
242                                                      LogicalDatastoreType store) {
243         List<Node> uniNodes = UnimgrUtils.getUniNodes(dataBroker,
244                                                       store);
245         for (Node node : uniNodes) {
246             UniAugmentation uniAugmentation = node.getAugmentation(UniAugmentation.class);
247             if (uniAugmentation.getIpAddress().equals(ip)) {
248                 InstanceIdentifier<Node> uniNode = InstanceIdentifier
249                                                        .create(NetworkTopology.class)
250                                                        .child(Topology.class,
251                                                                new TopologyKey(UnimgrConstants.UNI_TOPOLOGY_ID))
252                                                        .child(Node.class,
253                                                                new NodeKey(node.getKey()));
254                 return uniNode;
255             }
256         }
257         return null;
258     }
259
260     /**
261      * Generates an Instance Identifier for the UNI topology: unimgr:uni
262      * @return An Instance Identifier for the UNI topology
263      */
264     public static InstanceIdentifier<Topology> getUniTopologyIid() {
265         InstanceIdentifier<Topology> topoPath = InstanceIdentifier
266                                                    .create(NetworkTopology.class)
267                                                    .child(Topology.class,
268                                                            new TopologyKey(UnimgrConstants.UNI_TOPOLOGY_ID));
269         return topoPath;
270     }
271
272     /**
273      * Generates an Instance Identifier for UNI nodes topology.
274      * @return An Instance Identifier for the UNI nodes topology.
275      */
276     public static InstanceIdentifier<Node> getUniTopologyNodeIid() {
277         InstanceIdentifier<Node> nodePath = InstanceIdentifier
278                                                 .create(NetworkTopology.class)
279                                                 .child(Topology.class,
280                                                         new TopologyKey(UnimgrConstants.UNI_TOPOLOGY_ID))
281                                                 .child(Node.class);
282         return nodePath;
283     }
284
285     /**
286      * Generates an Instance Identifier for a specific UNI node by
287      * using the node Id
288      * @param nodeId The node ID of a specific UNI node.
289      * @return An Instance Identifier for a specific UNI node.
290      */
291     public static InstanceIdentifier<Node> getUniNodeIid(NodeId nodeId) {
292     InstanceIdentifier<Node> nodePath = InstanceIdentifier
293                                             .create(NetworkTopology.class)
294                                             .child(Topology.class,
295                                                     new TopologyKey(UnimgrConstants.UNI_TOPOLOGY_ID))
296                                             .child(Node.class,
297                                                     new NodeKey(nodeId));
298     return nodePath;
299 }
300 }