Fixed EVC create
[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     public static InstanceIdentifier<Node> createOvsdbBridgeNodeIid(Node ovsdbNode,
34                                                                     String bridgeName) {
35         String bridgeNodeName = ovsdbNode.getNodeId().getValue()
36                             + UnimgrConstants.DEFAULT_BRIDGE_NODE_ID_SUFFIX
37                             + bridgeName;
38         NodeId bridgeNodeId = new NodeId(bridgeNodeName);
39         InstanceIdentifier<Node> bridgeNodePath = InstanceIdentifier
40                                                       .create(NetworkTopology.class)
41                                                       .child(Topology.class,
42                                                               new TopologyKey(UnimgrConstants.OVSDB_TOPOLOGY_ID))
43                                                       .child(Node.class, new NodeKey(bridgeNodeId));
44         return bridgeNodePath;
45     }
46
47     public static InstanceIdentifier<Link> getEvcLinkIid(LinkId id) {
48         InstanceIdentifier<Link> linkPath = InstanceIdentifier
49                                                 .create(NetworkTopology.class)
50                                                 .child(Topology.class,
51                                                         new TopologyKey(UnimgrConstants.EVC_TOPOLOGY_ID))
52                                                 .child(Link.class,
53                                                         new LinkKey(id));
54         return linkPath;
55     }
56
57     public static InstanceIdentifier<Topology> getEvcTopologyIid() {
58         InstanceIdentifier<Topology> topoPath = InstanceIdentifier
59                                                     .create(NetworkTopology.class)
60                                                     .child(Topology.class,
61                                                             new TopologyKey(UnimgrConstants.EVC_TOPOLOGY_ID));
62         return topoPath;
63     }
64
65     public static InstanceIdentifier<Node> getEvcTopologyNodeIid() {
66         InstanceIdentifier<Node> nodePath = InstanceIdentifier
67                                                 .create(NetworkTopology.class)
68                                                 .child(Topology.class,
69                                                         new TopologyKey(UnimgrConstants.EVC_TOPOLOGY_ID))
70                                                 .child(Node.class);
71         return nodePath;
72     }
73
74     public static InstanceIdentifier<Node> getOvsdbBridgeNodeIid(Node ovsdbNode) {
75         OvsdbNodeAugmentation ovsdbNodeAugmentation = ovsdbNode.getAugmentation(OvsdbNodeAugmentation.class);
76         InstanceIdentifier<Node> nodePath = ovsdbNodeAugmentation
77                                                 .getManagedNodeEntry()
78                                                 .iterator()
79                                                 .next()
80                                                 .getBridgeRef()
81                                                 .getValue()
82                                                 .firstIdentifierOf(Node.class);
83         return nodePath;
84     }
85
86     public static InstanceIdentifier<Node> getOvsdbNodeIid(IpAddress ipAddress) {
87         String nodeId = UnimgrConstants.OVSDB_PREFIX
88                             + ipAddress.getIpv4Address().getValue().toString()
89                             + ":"
90                             + UnimgrConstants.OVSDB_PORT;
91         InstanceIdentifier<Node> nodePath = InstanceIdentifier
92                                                 .create(NetworkTopology.class)
93                                                 .child(Topology.class,
94                                                         new TopologyKey(UnimgrConstants.OVSDB_TOPOLOGY_ID))
95                                                 .child(Node.class,
96                                                         new NodeKey(new NodeId(nodeId)));
97         return nodePath;
98     }
99
100     public static InstanceIdentifier<Node> getOvsdbNodeIid(NodeId nodeId) {
101         InstanceIdentifier<Node> nodePath = InstanceIdentifier
102                                                 .create(NetworkTopology.class)
103                                                 .child(Topology.class,
104                                                         new TopologyKey(UnimgrConstants.OVSDB_TOPOLOGY_ID))
105                                                 .child(Node.class,
106                                                         new NodeKey(nodeId));
107         return nodePath;
108     }
109
110     public static InstanceIdentifier<Topology> getOvsdbTopologyIid() {
111         InstanceIdentifier<Topology> topoPath = InstanceIdentifier
112                                                     .create(NetworkTopology.class)
113                                                     .child(Topology.class,
114                                                             new TopologyKey(UnimgrConstants.OVSDB_TOPOLOGY_ID));
115         return topoPath;
116     }
117
118     public static InstanceIdentifier<TerminationPoint> getTerminationPointIid(
119                                                            Node bridgeNode,
120                                                            String portName) {
121         InstanceIdentifier<TerminationPoint> terminationPointPath =
122                                                  InstanceIdentifier
123                                                      .create(NetworkTopology.class)
124                                                      .child(Topology.class,
125                                                              new TopologyKey(UnimgrConstants.OVSDB_TOPOLOGY_ID))
126                                                      .child(Node.class, bridgeNode.getKey())
127                                                      .child(TerminationPoint.class,
128                                                              new TerminationPointKey(new TpId(portName)));
129         return terminationPointPath;
130     }
131
132     public static InstanceIdentifier<Node> getUniIid(DataBroker dataBroker,
133                                                      IpAddress ip) {
134         List<Node> uniNodes = UnimgrUtils.getUniNodes(dataBroker,
135                                                       LogicalDatastoreType.OPERATIONAL);
136         for (Node node : uniNodes) {
137             UniAugmentation uniAugmentation = node.getAugmentation(UniAugmentation.class);
138             if (uniAugmentation.getIpAddress().equals(ip)) {
139                 InstanceIdentifier<Node> uniNode = InstanceIdentifier
140                                                        .create(NetworkTopology.class)
141                                                        .child(Topology.class,
142                                                                new TopologyKey(UnimgrConstants.UNI_TOPOLOGY_ID))
143                                                        .child(Node.class,
144                                                                new NodeKey(node.getKey()));
145                 return uniNode;
146             }
147         }
148         return null;
149     }
150
151     public static InstanceIdentifier<Node> getUniIid(DataBroker dataBroker,
152                                                      IpAddress ip,
153                                                      LogicalDatastoreType store) {
154         List<Node> uniNodes = UnimgrUtils.getUniNodes(dataBroker,
155                                                       store);
156         for (Node node : uniNodes) {
157             UniAugmentation uniAugmentation = node.getAugmentation(UniAugmentation.class);
158             if (uniAugmentation.getIpAddress().equals(ip)) {
159                 InstanceIdentifier<Node> uniNode = InstanceIdentifier
160                                                        .create(NetworkTopology.class)
161                                                        .child(Topology.class,
162                                                                new TopologyKey(UnimgrConstants.UNI_TOPOLOGY_ID))
163                                                        .child(Node.class,
164                                                                new NodeKey(node.getKey()));
165                 return uniNode;
166             }
167         }
168         return null;
169     }
170
171     public static InstanceIdentifier<Topology> getUniTopologyIid() {
172         InstanceIdentifier<Topology> topoPath = InstanceIdentifier
173                                                    .create(NetworkTopology.class)
174                                                    .child(Topology.class,
175                                                            new TopologyKey(UnimgrConstants.UNI_TOPOLOGY_ID));
176         return topoPath;
177     }
178
179     public static InstanceIdentifier<Node> getUniTopologyNodeIid() {
180         InstanceIdentifier<Node> nodePath = InstanceIdentifier
181                                                 .create(NetworkTopology.class)
182                                                 .child(Topology.class,
183                                                         new TopologyKey(UnimgrConstants.UNI_TOPOLOGY_ID))
184                                                 .child(Node.class);
185         return nodePath;
186     }
187
188 }