Streamlined UnimgrMapper, cleaned up the code
[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.unimgr.rev151012.UniAugmentation;
16 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.LinkId;
17 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
18 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
19 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
20 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
21 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
22 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkKey;
24 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29
30 public class UnimgrMapper {
31
32     public static InstanceIdentifier<Link> getEvcLinkIID(LinkId id) {
33         InstanceIdentifier<Link> linkPath = InstanceIdentifier
34                                                 .create(NetworkTopology.class)
35                                                 .child(Topology.class,
36                                                         new TopologyKey(UnimgrConstants.EVC_TOPOLOGY_ID))
37                                                 .child(Link.class,
38                                                         new LinkKey(id));
39         return linkPath;
40     }
41
42     public static InstanceIdentifier<Topology> getEvcTopologyIid() {
43         InstanceIdentifier<Topology> topoPath = InstanceIdentifier
44                                                     .create(NetworkTopology.class)
45                                                     .child(Topology.class,
46                                                             new TopologyKey(UnimgrConstants.EVC_TOPOLOGY_ID));
47         return topoPath;
48     }
49
50     public static InstanceIdentifier<Node> getEvcTopologyNodeIid() {
51         InstanceIdentifier<Node> nodePath = InstanceIdentifier
52                                                 .create(NetworkTopology.class)
53                                                 .child(Topology.class,
54                                                         new TopologyKey(UnimgrConstants.EVC_TOPOLOGY_ID))
55                                                 .child(Node.class);
56         return nodePath;
57     }
58
59     public static InstanceIdentifier<Node> getOvsdbBridgeNodeIid(NodeId ovsdbNode,
60                                                                  String bridgeName) {
61         String ovsdbNodeId = ovsdbNode.getValue();
62         NodeId bridgeNodeId = new NodeId(ovsdbNodeId
63                                        + UnimgrConstants.DEFAULT_BRIDGE_NODE_ID_SUFFIX
64                                        + bridgeName);
65         InstanceIdentifier<Node> nodePath = InstanceIdentifier
66                                                 .create(NetworkTopology.class)
67                                                 .child(Topology.class,
68                                                         new TopologyKey(UnimgrConstants.OVSDB_TOPOLOGY_ID))
69                                                 .child(Node.class,
70                                                         new NodeKey(bridgeNodeId));
71         return nodePath;
72     }
73
74     public static InstanceIdentifier<Node> getOvsdbNodeIid(IpAddress ipAddress) {
75         String nodeId = UnimgrConstants.OVSDB_PREFIX
76                             + ipAddress.getIpv4Address().getValue().toString()
77                             + ":"
78                             + UnimgrConstants.OVSDB_PORT;
79         InstanceIdentifier<Node> nodePath = InstanceIdentifier
80                                                 .create(NetworkTopology.class)
81                                                 .child(Topology.class,
82                                                         new TopologyKey(UnimgrConstants.OVSDB_TOPOLOGY_ID))
83                                                 .child(Node.class,
84                                                         new NodeKey(new NodeId(nodeId)));
85         return nodePath;
86     }
87
88     public static InstanceIdentifier<Node> getOvsdbNodeIid(NodeId nodeId) {
89         InstanceIdentifier<Node> nodePath = InstanceIdentifier
90                                                 .create(NetworkTopology.class)
91                                                 .child(Topology.class,
92                                                         new TopologyKey(UnimgrConstants.OVSDB_TOPOLOGY_ID))
93                                                 .child(Node.class,
94                                                         new NodeKey(nodeId));
95         return nodePath;
96     }
97
98     public static InstanceIdentifier<Topology> getOvsdbTopologyIid() {
99         InstanceIdentifier<Topology> topoPath = InstanceIdentifier
100                                                     .create(NetworkTopology.class)
101                                                     .child(Topology.class,
102                                                             new TopologyKey(UnimgrConstants.OVSDB_TOPOLOGY_ID));
103         return topoPath;
104     }
105
106     public static InstanceIdentifier<TerminationPoint> getTerminationPointIid(
107                                                            Node bridgeNode,
108                                                            String portName) {
109         InstanceIdentifier<TerminationPoint> terminationPointPath =
110                                                  InstanceIdentifier
111                                                      .create(NetworkTopology.class)
112                                                      .child(Topology.class,
113                                                              new TopologyKey(UnimgrConstants.OVSDB_TOPOLOGY_ID))
114                                                      .child(Node.class, bridgeNode.getKey())
115                                                      .child(TerminationPoint.class,
116                                                              new TerminationPointKey(new TpId(portName)));
117         return terminationPointPath;
118     }
119
120     public static InstanceIdentifier<Node> getUniIid(DataBroker dataBroker,
121                                                      IpAddress ip) {
122         List<Node> uniNodes = UnimgrUtils.getUniNodes(dataBroker,
123                                                       LogicalDatastoreType.OPERATIONAL);
124         for (Node node : uniNodes) {
125             UniAugmentation uniAugmentation = node.getAugmentation(UniAugmentation.class);
126             if (uniAugmentation.getIpAddress().equals(ip)) {
127                 InstanceIdentifier<Node> uniNode = InstanceIdentifier
128                                                        .create(NetworkTopology.class)
129                                                        .child(Topology.class,
130                                                                new TopologyKey(UnimgrConstants.UNI_TOPOLOGY_ID))
131                                                        .child(Node.class,
132                                                                new NodeKey(node.getKey()));
133                 return uniNode;
134             }
135         }
136         return null;
137     }
138
139     public static InstanceIdentifier<Node> getUniIid(DataBroker dataBroker,
140                                                      IpAddress ip,
141                                                      LogicalDatastoreType store) {
142         List<Node> uniNodes = UnimgrUtils.getUniNodes(dataBroker,
143                                                       store);
144         for (Node node : uniNodes) {
145             UniAugmentation uniAugmentation = node.getAugmentation(UniAugmentation.class);
146             if (uniAugmentation.getIpAddress().equals(ip)) {
147                 InstanceIdentifier<Node> uniNode = InstanceIdentifier
148                                                        .create(NetworkTopology.class)
149                                                        .child(Topology.class,
150                                                                new TopologyKey(UnimgrConstants.UNI_TOPOLOGY_ID))
151                                                        .child(Node.class,
152                                                                new NodeKey(node.getKey()));
153                 return uniNode;
154             }
155         }
156         return null;
157     }
158
159     public static InstanceIdentifier<Topology> getUniTopologyIid() {
160         InstanceIdentifier<Topology> topoPath = InstanceIdentifier
161                                                    .create(NetworkTopology.class)
162                                                    .child(Topology.class,
163                                                            new TopologyKey(UnimgrConstants.UNI_TOPOLOGY_ID));
164         return topoPath;
165     }
166
167     public static InstanceIdentifier<Node> getUniTopologyNodeIid() {
168         InstanceIdentifier<Node> nodePath = InstanceIdentifier
169                                                 .create(NetworkTopology.class)
170                                                 .child(Topology.class,
171                                                         new TopologyKey(UnimgrConstants.UNI_TOPOLOGY_ID))
172                                                 .child(Node.class);
173         return nodePath;
174     }
175
176 }