Integrate the new yang model with Network Topology augmentation
[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.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentation;
15 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.LinkId;
16 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
17 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
18 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
19 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
20 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
21 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
22 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.LinkKey;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
24 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
25 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
26 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 public class UnimgrMapper {
32
33     private static final Logger LOG = LoggerFactory.getLogger(UnimgrMapper.class);
34
35     public static InstanceIdentifier<Node> getOvsdbNodeIID(NodeId nodeId) {
36         InstanceIdentifier<Node> nodePath = InstanceIdentifier
37                 .create(NetworkTopology.class)
38                 .child(Topology.class, new TopologyKey(UnimgrConstants.OVSDB_TOPOLOGY_ID))
39                 .child(Node.class,new NodeKey(nodeId));
40         return nodePath;
41     }
42
43     public static InstanceIdentifier<Node> getOvsdbBridgeNodeIID(NodeId ovsdbNode, String bridgeName) {
44         NodeId bridgeNodeId = new NodeId(ovsdbNode + UnimgrConstants.DEFAULT_BRIDGE_NODE_ID_SUFFIX + bridgeName);
45         InstanceIdentifier<Node> nodePath = InstanceIdentifier
46                 .create(NetworkTopology.class)
47                 .child(Topology.class, new TopologyKey(UnimgrConstants.OVSDB_TOPOLOGY_ID))
48                 .child(Node.class,new NodeKey(bridgeNodeId));
49         return nodePath;
50     }
51
52     public static InstanceIdentifier<Node> getOvsdbNodeIID(IpAddress ipAddress) {
53         String nodeId = UnimgrConstants.OVSDB_PREFIX
54                 + ipAddress.getIpv4Address().getValue().toString()
55                 + ":"
56                 + UnimgrConstants.OVSDB_PORT;
57         InstanceIdentifier<Node> nodePath = InstanceIdentifier
58                 .create(NetworkTopology.class)
59                 .child(Topology.class, new TopologyKey(UnimgrConstants.OVSDB_TOPOLOGY_ID))
60                 .child(Node.class,new NodeKey(new NodeId(nodeId)));
61         return nodePath;
62     }
63
64     public static InstanceIdentifier<Node> getOvsdbTopologyIdentifier() {
65         InstanceIdentifier<Node> path = InstanceIdentifier
66                 .create(NetworkTopology.class)
67                 .child(Topology.class,
68                         new TopologyKey(UnimgrConstants.OVSDB_TOPOLOGY_ID))
69                 .child(Node.class);
70         return path;
71     }
72
73     public static InstanceIdentifier<TerminationPoint> createTerminationPointInstanceIdentifier(
74             Node bridgeNode, String portName) {
75         InstanceIdentifier<TerminationPoint> terminationPointPath = InstanceIdentifier
76                 .create(NetworkTopology.class)
77                 .child(Topology.class, new TopologyKey(UnimgrConstants.OVSDB_TOPOLOGY_ID))
78                 .child(Node.class,bridgeNode.getKey())
79                 .child(TerminationPoint.class, new TerminationPointKey(new TpId(portName)));
80
81         LOG.debug("Termination point InstanceIdentifier generated : {}",terminationPointPath);
82         return terminationPointPath;
83     }
84
85     public static InstanceIdentifier<Node> createUniIid() {
86         InstanceIdentifier<Node> iid = InstanceIdentifier.create(NetworkTopology.class)
87                 .child(Topology.class, new TopologyKey(UnimgrConstants.UNI_TOPOLOGY_ID))
88                 .child(Node.class);
89         return iid;
90     }
91
92     public static InstanceIdentifier<Node> createUniIid(DataBroker dataBroker, IpAddress ip) {
93         List<Node> uniNodes = UnimgrUtils.getUniNodes(dataBroker);
94         for (Node node: uniNodes) {
95             UniAugmentation uniAugmentation = node.getAugmentation(UniAugmentation.class);
96             if (uniAugmentation.getIpAddress().equals(ip)) {
97                 InstanceIdentifier<Node> uniNode = InstanceIdentifier
98                         .create(NetworkTopology.class)
99                         .child(Topology.class, new TopologyKey(UnimgrConstants.UNI_TOPOLOGY_ID))
100                         .child(Node.class, new NodeKey(node.getKey()));
101                 return uniNode;
102             }
103         }
104         return null;
105     }
106
107     public static InstanceIdentifier<Node> createEvcIid() {
108         InstanceIdentifier<Node> iid = InstanceIdentifier.create(NetworkTopology.class)
109                 .child(Topology.class, new TopologyKey(UnimgrConstants.EVC_TOPOLOGY_ID))
110                 .child(Node.class);
111         return iid;
112     }
113
114     public static InstanceIdentifier<Topology> createTopologyIid() {
115         InstanceIdentifier<Topology> iid = InstanceIdentifier.create(NetworkTopology.class)
116                 .child(Topology.class, new TopologyKey(UnimgrConstants.UNI_TOPOLOGY_ID));
117         return iid;
118     }
119
120     public static InstanceIdentifier<Link> getEvcLinkIID(LinkId id) {
121         InstanceIdentifier<Link> linkPath = InstanceIdentifier
122                 .create(NetworkTopology.class)
123                 .child(Topology.class, new TopologyKey(UnimgrConstants.EVC_TOPOLOGY_ID))
124                 .child(Link.class,new LinkKey(id));
125         return linkPath;
126     }
127 }