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