c643d9ca36570902999f0705b09097ea54767b48
[neutron.git] / neutron-hostconfig / vpp / src / main / java / org / opendaylight / neutron / hostconfig / vpp / HostconfigUtil.java
1 /*
2  * Copyright (c) 2017 Cisco Systems, Inc. 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.neutron.hostconfig.vpp;
10
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.Lists;
13 import com.google.gson.JsonArray;
14 import com.google.gson.JsonObject;
15
16 import java.util.HashMap;
17 import java.util.List;
18 import java.util.Map;
19
20 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
21
22 public final class HostconfigUtil {
23
24     static final String L2_HOST_TYPE = "ODL L2";
25     private static final String VHOST_USER = "vhostuser";
26     private static final String VNIC_TYPE = "normal";
27     private static final String HAS_DATAPATH_TYPE_NETDEV = "False";
28     private static final String SUPPORT_VHOST_USER = "True";
29     private static final List<String> SUPPORTED_NET_TYPES = Lists.newArrayList("local", "vlan", "vxlan", "gre");
30
31     private HostconfigUtil() {
32     }
33
34     public static Map<String, String> createHostconfigsDataFor(NodeId nodeId, SocketInfo socketInfo) {
35         Preconditions.checkNotNull(nodeId);
36         Preconditions.checkNotNull(socketInfo);
37         JsonObject odlL2 = new JsonObject();
38         odlL2.add("allowed_network_types", buildSupportedNetworkTypes());
39         odlL2.add("supported_vnic_types", buildSupportedVnicTypes(socketInfo));
40         Map<String, String> hostConfigs = new HashMap<>();
41         hostConfigs.put(L2_HOST_TYPE, odlL2.toString());
42         return hostConfigs;
43     }
44
45     private static JsonArray buildSupportedNetworkTypes() {
46         JsonArray networkTypes = new JsonArray();
47         SUPPORTED_NET_TYPES.forEach(networkTypes::add);
48         return networkTypes;
49     }
50
51     private static JsonArray buildSupportedVnicTypes(SocketInfo socketInfo) {
52         JsonObject supportedVnicType = new JsonObject();
53         supportedVnicType.addProperty("vnic_type", VNIC_TYPE);
54         supportedVnicType.addProperty("vif_type", VHOST_USER);
55         supportedVnicType.add("vif_details", buildVifDetails(socketInfo));
56         JsonArray supportedVnicTypes = new JsonArray();
57         supportedVnicTypes.add(supportedVnicType);
58         return supportedVnicTypes;
59     }
60
61     private static JsonObject buildVifDetails(SocketInfo socketInfo) {
62         JsonObject vifDetails = new JsonObject();
63         vifDetails.addProperty("has_datapath_type_netdev", HAS_DATAPATH_TYPE_NETDEV);
64         vifDetails.addProperty("support_vhost_user", SUPPORT_VHOST_USER);
65         vifDetails.addProperty("port_prefix", socketInfo.getSocketPrefix());
66         vifDetails.addProperty("vhostuser_socket_dir", socketInfo.getSocketPath());
67         vifDetails.addProperty("vhostuser_mode", socketInfo.getVhostuserMode());
68         vifDetails.addProperty("vhostuser_socket", socketInfo.getVhostUserSocket());
69         return vifDetails;
70     }
71 }