Hostconfig for Openstack in VPP renderer
[groupbasedpolicy.git] / neutron-vpp-mapper / src / main / java / org / opendaylight / groupbasedpolicy / neutron / vpp / mapper / util / HostconfigUtil.java
1 /*\r
2  * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.\r
3  *\r
4  * This program and the accompanying materials are made available under the\r
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
6  * and is available at http://www.eclipse.org/legal/epl-v10.html\r
7  */\r
8 \r
9 package org.opendaylight.groupbasedpolicy.neutron.vpp.mapper.util;\r
10 \r
11 import java.util.List;\r
12 \r
13 import org.opendaylight.groupbasedpolicy.neutron.vpp.mapper.SocketInfo;\r
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.hostconfig.rev150712.hostconfig.attributes.hostconfigs.Hostconfig;\r
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.hostconfig.rev150712.hostconfig.attributes.hostconfigs.HostconfigBuilder;\r
16 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;\r
17 \r
18 import com.google.common.base.Preconditions;\r
19 import com.google.common.collect.Lists;\r
20 import com.google.gson.JsonArray;\r
21 import com.google.gson.JsonObject;\r
22 \r
23 \r
24 public class HostconfigUtil {\r
25 \r
26     public static final String L2_HOST_TYPE = "ODL L2";\r
27     private static final String VHOST_USER = "vhostuser";\r
28     private static final String VNIC_TYPE = "normal";\r
29     private static final String HAS_DATAPATH_TYPE_NETDEV = "False";\r
30     private static final String SUPPORT_VHOST_USER = "True";\r
31     private static final String VHOSTUSER_MODE = "server";\r
32     private static final List<String> supportedNetworkTypes = Lists.newArrayList("local", "vlan", "vxlan", "gre");\r
33 \r
34     public static Hostconfig createHostconfigsDataFor(NodeId nodeId, SocketInfo socketInfo) {\r
35         Preconditions.checkNotNull(nodeId);\r
36         Preconditions.checkNotNull(socketInfo);\r
37         JsonObject odlL2 = new JsonObject();\r
38         odlL2.add("allowed_network_types", buildSupportedNetworkTypes());\r
39         odlL2.add("supported_vnic_types", buildSupportedVnicTypes(socketInfo));\r
40         return new HostconfigBuilder().setHostId(nodeId.getValue())\r
41             .setHostType(L2_HOST_TYPE)\r
42             .setConfig(odlL2.toString())\r
43             .build();\r
44     }\r
45 \r
46     private static JsonArray buildSupportedNetworkTypes() {\r
47         JsonArray networkTypes = new JsonArray();\r
48         supportedNetworkTypes.forEach(networkTypes::add);\r
49         return networkTypes;\r
50     }\r
51 \r
52     private static JsonArray buildSupportedVnicTypes(SocketInfo socketInfo) {\r
53         JsonArray supportedVnicTypes = new JsonArray();\r
54         JsonObject supportedVnicType = new JsonObject();\r
55         supportedVnicType.addProperty("vnic_type", VNIC_TYPE);\r
56         supportedVnicType.addProperty("vif_type", VHOST_USER);\r
57         supportedVnicType.add("vif_details", buildVifDetails(socketInfo));\r
58         supportedVnicTypes.add(supportedVnicType);\r
59         return supportedVnicTypes;\r
60     }\r
61 \r
62     private static JsonObject buildVifDetails(SocketInfo socketInfo) {\r
63         JsonObject vifDetails = new JsonObject();\r
64         vifDetails.addProperty("has_datapath_type_netdev", HAS_DATAPATH_TYPE_NETDEV);\r
65         vifDetails.addProperty("support_vhost_user", SUPPORT_VHOST_USER);\r
66         vifDetails.addProperty("port_prefix", socketInfo.getSocketPrefix());\r
67         vifDetails.addProperty("vhostuser_socket_dir", socketInfo.getSocketPath());\r
68         vifDetails.addProperty("vhostuser_mode", VHOSTUSER_MODE);\r
69         vifDetails.addProperty("vhostuser_socket", socketInfo.getVhostUserSocket());\r
70         return vifDetails;\r
71     }\r
72 }\r