Upgrade ietf-{inet,yang}-types to 2013-07-15
[ovsdb.git] / utils / hwvtepsouthbound-utils / src / main / java / org / opendaylight / ovsdb / utils / hwvtepsouthbound / utils / HwvtepSouthboundUtils.java
1 /*
2  * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. 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
9 package org.opendaylight.ovsdb.utils.hwvtepsouthbound.utils;
10
11 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundConstants;
12 import org.opendaylight.ovsdb.hwvtepsouthbound.HwvtepSouthboundMapper;
13 import org.opendaylight.ovsdb.utils.mdsal.utils.MdsalUtils;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
15 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentation;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepGlobalAugmentationBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.HwvtepNodeName;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.hwvtep.rev150901.hwvtep.global.attributes.ConnectionInfo;
21 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
22 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
23 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
24 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
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.NodeBuilder;
27 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class HwvtepSouthboundUtils {
33     private static final Logger LOG = LoggerFactory.getLogger(HwvtepSouthboundUtils.class);
34     private static final int HWVTEP_UPDATE_TIMEOUT = 1000;
35     private final MdsalUtils mdsalUtils;
36
37     public HwvtepSouthboundUtils(MdsalUtils mdsalUtils) {
38         this.mdsalUtils = mdsalUtils;
39     }
40
41     public static NodeId createNodeId(IpAddress ip, PortNumber port) {
42         String uriString = HwvtepSouthboundConstants.HWVTEP_URI_PREFIX + "://"
43                 + String.valueOf(ip.getValue()) + ":" + port.getValue();
44         Uri uri = new Uri(uriString);
45         return new NodeId(uri);
46     }
47
48     public static Node createNode(ConnectionInfo key) {
49         NodeBuilder nodeBuilder = new NodeBuilder();
50         nodeBuilder.setNodeId(createNodeId(key.getRemoteIp(), key.getRemotePort()));
51         nodeBuilder.addAugmentation(HwvtepGlobalAugmentation.class, createHwvtepAugmentation(key));
52         return nodeBuilder.build();
53     }
54
55     public static HwvtepGlobalAugmentation createHwvtepAugmentation(ConnectionInfo key) {
56         HwvtepGlobalAugmentationBuilder hwvtepGlobalBuilder = new HwvtepGlobalAugmentationBuilder();
57         hwvtepGlobalBuilder.setConnectionInfo(key);
58         return hwvtepGlobalBuilder.build();
59     }
60
61     public static InstanceIdentifier<Node> createInstanceIdentifier(ConnectionInfo key) {
62         return createInstanceIdentifier(key.getRemoteIp(), key.getRemotePort());
63     }
64
65     public static InstanceIdentifier<Node> createInstanceIdentifier(IpAddress ip, PortNumber port) {
66         InstanceIdentifier<Node> path = InstanceIdentifier
67                 .create(NetworkTopology.class)
68                 .child(Topology.class, new TopologyKey(HwvtepSouthboundConstants.HWVTEP_TOPOLOGY_ID))
69                 .child(Node.class,createNodeKey(ip,port));
70         LOG.debug("Created hwvtep path: {}",path);
71         return path;
72     }
73
74     public static InstanceIdentifier<Node> createInstanceIdentifier(ConnectionInfo key, HwvtepNodeName name) {
75         return HwvtepSouthboundMapper.createInstanceIdentifier(
76                         createManagedNodeId(key, name));
77     }
78
79     private static NodeId createManagedNodeId(ConnectionInfo key, HwvtepNodeName nodeName) {
80         return createManagedNodeId(key.getRemoteIp(), key.getRemotePort(), nodeName);
81     }
82
83     private static NodeId createManagedNodeId(IpAddress remoteIp, PortNumber remotePort, HwvtepNodeName nodeName) {
84         //This assumes that HwvtepNode can only be Physical switch
85         return new NodeId(createNodeId(remoteIp,remotePort).getValue()
86                         + "/" + HwvtepSouthboundConstants.PSWITCH_URI_PREFIX + "/" + nodeName.getValue());
87     }
88
89     public static NodeKey createNodeKey(IpAddress ip, PortNumber port) {
90         return new NodeKey(createNodeId(ip, port));
91     }
92
93     public static Object connectionInfoToString(ConnectionInfo connectionInfo) {
94         return String.valueOf(
95                         connectionInfo.getRemoteIp().getValue()) + ":" + connectionInfo.getRemotePort().getValue();
96     }
97
98 }