From d2e3b238ed554542a579be696daa294ed7cc331b Mon Sep 17 00:00:00 2001 From: Hugo Trippaers Date: Tue, 12 Aug 2014 15:00:55 +0200 Subject: [PATCH] New style dnsmasq configuration --- .../resource/virtualnetwork/ConfigHelper.java | 39 +++++----- .../resource/virtualnetwork/VRScripts.java | 2 + .../virtualnetwork/model/ConfigBase.java | 1 + .../virtualnetwork/model/DhcpConfig.java | 45 ++++++++++++ .../virtualnetwork/model/DhcpConfigEntry.java | 72 +++++++++++++++++++ 5 files changed, 141 insertions(+), 18 deletions(-) create mode 100644 core/src/com/cloud/agent/resource/virtualnetwork/model/DhcpConfig.java create mode 100644 core/src/com/cloud/agent/resource/virtualnetwork/model/DhcpConfigEntry.java diff --git a/core/src/com/cloud/agent/resource/virtualnetwork/ConfigHelper.java b/core/src/com/cloud/agent/resource/virtualnetwork/ConfigHelper.java index 4f2c6a026a7..ba00e1f0bd6 100644 --- a/core/src/com/cloud/agent/resource/virtualnetwork/ConfigHelper.java +++ b/core/src/com/cloud/agent/resource/virtualnetwork/ConfigHelper.java @@ -23,6 +23,10 @@ import java.util.ArrayList; import java.util.LinkedList; import java.util.List; +import com.google.gson.FieldNamingPolicy; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + import com.cloud.agent.api.BumpUpPriorityCommand; import com.cloud.agent.api.SetupGuestNetworkCommand; import com.cloud.agent.api.routing.CreateIpAliasCommand; @@ -55,6 +59,8 @@ import com.cloud.agent.api.to.StaticNatRuleTO; import com.cloud.agent.resource.virtualnetwork.model.AclRule; import com.cloud.agent.resource.virtualnetwork.model.AllAclRule; import com.cloud.agent.resource.virtualnetwork.model.ConfigBase; +import com.cloud.agent.resource.virtualnetwork.model.DhcpConfig; +import com.cloud.agent.resource.virtualnetwork.model.DhcpConfigEntry; import com.cloud.agent.resource.virtualnetwork.model.ForwardingRule; import com.cloud.agent.resource.virtualnetwork.model.ForwardingRules; import com.cloud.agent.resource.virtualnetwork.model.GuestNetwork; @@ -85,9 +91,6 @@ import com.cloud.network.rules.FirewallRule; import com.cloud.network.vpc.StaticRouteProfile; import com.cloud.utils.exception.CloudRuntimeException; import com.cloud.utils.net.NetUtils; -import com.google.gson.FieldNamingPolicy; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; public class ConfigHelper { private final static Gson gson; @@ -115,7 +118,7 @@ public class ConfigHelper { } else if (cmd instanceof CreateIpAliasCommand) { cfg = generateConfig((CreateIpAliasCommand)cmd); // Migrated } else if (cmd instanceof DnsMasqConfigCommand) { - cfg = generateConfig((DnsMasqConfigCommand)cmd); + cfg = generateConfig((DnsMasqConfigCommand)cmd); // Migrated } else if (cmd instanceof DeleteIpAliasCommand) { cfg = generateConfig((DeleteIpAliasCommand)cmd); // Migrated } else if (cmd instanceof VmDataCommand) { @@ -336,22 +339,14 @@ public class ConfigHelper { } private static List generateConfig(DnsMasqConfigCommand cmd) { - LinkedList cfg = new LinkedList<>(); + LinkedList entries = new LinkedList(); - List dhcpTos = cmd.getIps(); - StringBuffer buff = new StringBuffer(); - for (DhcpTO dhcpTo : dhcpTos) { - buff.append(dhcpTo.getRouterIp()); - buff.append(":"); - buff.append(dhcpTo.getGateway()); - buff.append(":"); - buff.append(dhcpTo.getNetmask()); - buff.append(":"); - buff.append(dhcpTo.getStartIpOfSubnet()); - buff.append("-"); + for (DhcpTO dhcpTo : cmd.getIps()) { + DhcpConfigEntry entry = new DhcpConfigEntry(dhcpTo.getRouterIp(), dhcpTo.getGateway(), dhcpTo.getNetmask(), dhcpTo.getStartIpOfSubnet()); + entries.add(entry); } - cfg.add(new ScriptConfigItem(VRScripts.DNSMASQ_CONFIG, buff.toString())); - return cfg; + + return generateConfigItems(new DhcpConfig(entries)); } private static List generateConfig(BumpUpPriorityCommand cmd) { @@ -535,6 +530,14 @@ public class ConfigHelper { break; case ConfigBase.MONITORSERVICE: destinationFile = VRScripts.MONITOR_SERVICE_CONFIG; + case ConfigBase.STATIC_ROUTES: + destinationFile = VRScripts.STATIC_ROUTES_CONFIG; + break; + case ConfigBase.DHCP_CONFIG: + destinationFile = VRScripts.DHCP_CONFIG; + break; + case ConfigBase.IP_ALIAS_CONFIG: + destinationFile = VRScripts.IP_ALIAS_CONFIG; break; default: throw new CloudRuntimeException("Unable to process the configuration for " + configuration.getType()); diff --git a/core/src/com/cloud/agent/resource/virtualnetwork/VRScripts.java b/core/src/com/cloud/agent/resource/virtualnetwork/VRScripts.java index 1fdaa7f1bfa..d2d5f2ca120 100644 --- a/core/src/com/cloud/agent/resource/virtualnetwork/VRScripts.java +++ b/core/src/com/cloud/agent/resource/virtualnetwork/VRScripts.java @@ -34,6 +34,8 @@ public class VRScripts { protected static final String STATIC_ROUTES_CONFIG = "static_routes.json"; protected static final String REMOTE_ACCESS_VPN_CONFIG = "remote_access_vpn.json"; protected static final String MONITOR_SERVICE_CONFIG = "monitor_service.json"; + protected static final String DHCP_CONFIG = "dhcp.json"; + protected static final String IP_ALIAS_CONFIG = "ip_aliases.json"; protected final static String CONFIG_CACHE_LOCATION = "/var/cache/cloud/"; protected final static int DEFAULT_EXECUTEINVR_TIMEOUT = 120; //Seconds diff --git a/core/src/com/cloud/agent/resource/virtualnetwork/model/ConfigBase.java b/core/src/com/cloud/agent/resource/virtualnetwork/model/ConfigBase.java index f2701560b92..36803e90773 100644 --- a/core/src/com/cloud/agent/resource/virtualnetwork/model/ConfigBase.java +++ b/core/src/com/cloud/agent/resource/virtualnetwork/model/ConfigBase.java @@ -35,6 +35,7 @@ public abstract class ConfigBase { public static final String STATIC_ROUTES = "staticroutes"; public static final String REMOTEACCESSVPN = "remoteaccessvpn"; public static final String MONITORSERVICE = "monitorservice"; + public static final String DHCP_CONFIG = "dhcpconfig"; private String type = UNKNOWN; diff --git a/core/src/com/cloud/agent/resource/virtualnetwork/model/DhcpConfig.java b/core/src/com/cloud/agent/resource/virtualnetwork/model/DhcpConfig.java new file mode 100644 index 00000000000..b1a83a394df --- /dev/null +++ b/core/src/com/cloud/agent/resource/virtualnetwork/model/DhcpConfig.java @@ -0,0 +1,45 @@ +// +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +// + +package com.cloud.agent.resource.virtualnetwork.model; + +import java.util.LinkedList; +import java.util.List; + +public class DhcpConfig extends ConfigBase { + List entries = new LinkedList(); + + public DhcpConfig() { + super(ConfigBase.DHCP_CONFIG); + } + + public DhcpConfig(List entries) { + super(ConfigBase.DHCP_CONFIG); + this.entries = entries; + } + + public List getEntries() { + return entries; + } + + public void setEntries(List entries) { + this.entries = entries; + } + +} diff --git a/core/src/com/cloud/agent/resource/virtualnetwork/model/DhcpConfigEntry.java b/core/src/com/cloud/agent/resource/virtualnetwork/model/DhcpConfigEntry.java new file mode 100644 index 00000000000..6bd1521f0d4 --- /dev/null +++ b/core/src/com/cloud/agent/resource/virtualnetwork/model/DhcpConfigEntry.java @@ -0,0 +1,72 @@ +// +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +// + +package com.cloud.agent.resource.virtualnetwork.model; + +public class DhcpConfigEntry { + private String routerIpAddress; + private String gateway; + private String netmask; + private String firstIpOfSubnet; + + public DhcpConfigEntry() { + // Empty for (de)serialization + } + + public DhcpConfigEntry(String routerIpAddress, String gateway, String netmask, String firstIpOfSubnet) { + super(); + this.routerIpAddress = routerIpAddress; + this.gateway = gateway; + this.netmask = netmask; + this.firstIpOfSubnet = firstIpOfSubnet; + } + + public String getRouterIpAddress() { + return routerIpAddress; + } + + public void setRouterIpAddress(String routerIpAddress) { + this.routerIpAddress = routerIpAddress; + } + + public String getGateway() { + return gateway; + } + + public void setGateway(String gateway) { + this.gateway = gateway; + } + + public String getNetmask() { + return netmask; + } + + public void setNetmask(String netmask) { + this.netmask = netmask; + } + + public String getFirstIpOfSubnet() { + return firstIpOfSubnet; + } + + public void setFirstIpOfSubnet(String firstIpOfSubnet) { + this.firstIpOfSubnet = firstIpOfSubnet; + } + +}