Skip to content

Commit a49261c

Browse files
JayapalUradiAbhinandan Prateek
authored andcommitted
CLOUDSTACK-24: mipn feature for basiczone
Signed-off-by: Abhinandan Prateek <aprateek@apache.org>
1 parent 630e755 commit a49261c

23 files changed

Lines changed: 701 additions & 71 deletions

File tree

api/src/com/cloud/agent/api/SecurityGroupRulesCmd.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.ByteArrayOutputStream;
2020
import java.io.IOException;
21+
import java.util.List;
2122
import java.util.zip.DeflaterOutputStream;
2223

2324
import org.apache.commons.codec.binary.Base64;
@@ -80,6 +81,7 @@ public int getEndPort() {
8081
Long msId;
8182
IpPortAndProto [] ingressRuleSet;
8283
IpPortAndProto [] egressRuleSet;
84+
private List<String> secIps;
8385

8486
public SecurityGroupRulesCmd() {
8587
super();
@@ -103,6 +105,23 @@ public SecurityGroupRulesCmd(String guestIp, String guestMac, String vmName, Lon
103105
}
104106

105107

108+
public SecurityGroupRulesCmd(String guestIp, String guestMac, String vmName, Long vmId, String signature, Long seqNum, IpPortAndProto[] ingressRuleSet, IpPortAndProto[] egressRuleSet, List<String> secIps) {
109+
super();
110+
this.guestIp = guestIp;
111+
this.vmName = vmName;
112+
this.ingressRuleSet = ingressRuleSet;
113+
this.egressRuleSet = egressRuleSet;
114+
this.guestMac = guestMac;
115+
this.signature = signature;
116+
this.seqNum = seqNum;
117+
this.vmId = vmId;
118+
if (signature == null) {
119+
String stringified = stringifyRules();
120+
this.signature = DigestUtils.md5Hex(stringified);
121+
}
122+
this.secIps = secIps;
123+
}
124+
106125
@Override
107126
public boolean executeInSequence() {
108127
return true;
@@ -131,6 +150,10 @@ public String getGuestIp() {
131150
return guestIp;
132151
}
133152

153+
public List<String> getSecIps() {
154+
return secIps;
155+
}
156+
134157

135158
public String getVmName() {
136159
return vmName;
@@ -165,6 +188,20 @@ private String compressCidr(String cidr) {
165188
}
166189

167190

191+
public String getSecIpsString() {
192+
StringBuilder sb = new StringBuilder();
193+
List<String> ips = getSecIps();
194+
if (ips == null) {
195+
return "0:";
196+
} else {
197+
for (String ip : ips) {
198+
sb.append(ip).append(":");
199+
}
200+
}
201+
return sb.toString();
202+
}
203+
204+
168205
public String stringifyCompressedRules() {
169206
StringBuilder ruleBuilder = new StringBuilder();
170207
for (SecurityGroupRulesCmd.IpPortAndProto ipPandP : getIngressRuleSet()) {

api/src/com/cloud/agent/api/to/NicTO.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@
1616
// under the License.
1717
package com.cloud.agent.api.to;
1818

19+
import java.util.List;
20+
1921
public class NicTO extends NetworkTO {
2022
int deviceId;
2123
Integer networkRateMbps;
2224
Integer networkRateMulticastMbps;
2325
boolean defaultNic;
2426
String uuid;
27+
List <String> nicSecIps;
2528

2629
public NicTO() {
2730
super();
@@ -69,4 +72,12 @@ public void setUuid(String uuid) {
6972
public String toString() {
7073
return new StringBuilder("[Nic:").append(type).append("-").append(ip).append("-").append(broadcastUri).append("]").toString();
7174
}
75+
76+
public void setNicSecIps(List<String> secIps) {
77+
this.nicSecIps = secIps;
78+
}
79+
80+
public List<String> getNicSecIps() {
81+
return nicSecIps;
82+
}
7283
}

api/src/com/cloud/network/security/SecurityGroupService.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.apache.cloudstack.api.command.user.securitygroup.DeleteSecurityGroupCmd;
2525
import org.apache.cloudstack.api.command.user.securitygroup.RevokeSecurityGroupEgressCmd;
2626
import org.apache.cloudstack.api.command.user.securitygroup.RevokeSecurityGroupIngressCmd;
27+
import org.apache.cloudstack.api.command.user.vm.AddIpToVmNicCmd;
2728

2829
import com.cloud.exception.InvalidParameterValueException;
2930
import com.cloud.exception.PermissionDeniedException;
@@ -45,5 +46,6 @@ public interface SecurityGroupService {
4546
public List<? extends SecurityRule> authorizeSecurityGroupIngress(AuthorizeSecurityGroupIngressCmd cmd);
4647

4748
public List<? extends SecurityRule> authorizeSecurityGroupEgress(AuthorizeSecurityGroupEgressCmd cmd);
48-
49+
public boolean securityGroupRulesForVmSecIp(Long nicId, Long networkId,
50+
String secondaryIp, boolean ruleAction);
4951
}

api/src/org/apache/cloudstack/api/command/user/vm/AddIpToVmNicCmd.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import org.apache.cloudstack.api.response.NicSecondaryIpResponse;
2929

3030
import com.cloud.async.AsyncJob;
31+
import com.cloud.dc.DataCenter;
32+
import com.cloud.dc.DataCenter.NetworkType;
3133
import com.cloud.event.EventTypes;
3234
import com.cloud.exception.ConcurrentOperationException;
3335
import com.cloud.exception.InsufficientAddressCapacityException;
@@ -83,6 +85,9 @@ private long getZoneId() {
8385

8486
public Long getNetworkId() {
8587
Nic nic = _entityMgr.findById(Nic.class, nicId);
88+
if (nic == null) {
89+
throw new InvalidParameterValueException("Can't find network id for specified nic");
90+
}
8691
Long networkId = nic.getNetworkId();
8792
return networkId;
8893
}
@@ -98,6 +103,13 @@ public String getIpaddress () {
98103
return null;
99104
}
100105
}
106+
107+
public NetworkType getNetworkType() {
108+
Network ntwk = _entityMgr.findById(Network.class, getNetworkId());
109+
DataCenter dc = _entityMgr.findById(DataCenter.class, ntwk.getDataCenterId());
110+
return dc.getNetworkType();
111+
}
112+
101113
@Override
102114
public long getEntityOwnerId() {
103115
Account caller = UserContext.current().getCaller();
@@ -134,21 +146,30 @@ public void execute() throws ResourceUnavailableException, ResourceAllocationExc
134146

135147
UserContext.current().setEventDetails("Nic Id: " + getNicId() );
136148
String ip;
137-
String SecondaryIp = null;
149+
String secondaryIp = null;
138150
if ((ip = getIpaddress()) != null) {
139151
if (!NetUtils.isValidIp(ip)) {
140152
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Invalid ip address " + ip);
141153
}
142154
}
143155

144156
try {
145-
SecondaryIp = _networkService.allocateSecondaryGuestIP(_accountService.getAccount(getEntityOwnerId()), getZoneId(), getNicId(), getNetworkId(), getIpaddress());
157+
secondaryIp = _networkService.allocateSecondaryGuestIP(_accountService.getAccount(getEntityOwnerId()), getZoneId(), getNicId(), getNetworkId(), getIpaddress());
146158
} catch (InsufficientAddressCapacityException e) {
147159
throw new InvalidParameterValueException("Allocating guest ip for nic failed");
148160
}
149161

150-
if (SecondaryIp != null) {
151-
s_logger.info("Associated ip address to NIC : " + SecondaryIp);
162+
if (secondaryIp != null) {
163+
if (getNetworkType() == NetworkType.Basic) {
164+
// add security group rules for the secondary ip addresses
165+
boolean success = false;
166+
success = _securityGroupService.securityGroupRulesForVmSecIp(getNicId(), getNetworkId(), secondaryIp, (boolean) true);
167+
if (success == false) {
168+
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to set security group rules for the secondary ip");
169+
}
170+
}
171+
172+
s_logger.info("Associated ip address to NIC : " + secondaryIp);
152173
NicSecondaryIpResponse response = new NicSecondaryIpResponse();
153174
response = _responseGenerator.createSecondaryIPToNicResponse(ip, getNicId(), getNetworkId());
154175
response.setResponseName(getCommandName());

api/src/org/apache/cloudstack/api/command/user/vm/RemoveIpFromVmNicCmd.java

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,15 @@
2727
import org.apache.cloudstack.api.response.NicSecondaryIpResponse;
2828
import org.apache.cloudstack.api.response.SuccessResponse;
2929
import com.cloud.async.AsyncJob;
30+
import com.cloud.dc.DataCenter;
31+
import com.cloud.dc.DataCenter.NetworkType;
3032
import com.cloud.event.EventTypes;
3133
import com.cloud.exception.InvalidParameterValueException;
34+
import com.cloud.network.Network;
3235
import com.cloud.user.Account;
3336
import com.cloud.user.UserContext;
37+
import com.cloud.vm.Nic;
38+
import com.cloud.vm.NicSecondaryIp;
3439

3540
@APICommand(name = "removeIpFromNic", description="Assigns secondary IP to NIC.", responseObject=SuccessResponse.class)
3641
public class RemoveIpFromVmNicCmd extends BaseAsyncCmd {
@@ -43,7 +48,7 @@ public class RemoveIpFromVmNicCmd extends BaseAsyncCmd {
4348

4449
@Parameter(name=ApiConstants.ID, type=CommandType.UUID, required = true, entityType = NicSecondaryIpResponse.class,
4550
description="the ID of the secondary ip address to nic")
46-
private long id;
51+
private Long id;
4752

4853
// unexposed parameter needed for events logging
4954
@Parameter(name=ApiConstants.ACCOUNT_ID, type=CommandType.UUID, expose=false)
@@ -57,7 +62,7 @@ public String getEntityTable() {
5762
return "nic_secondary_ips";
5863
}
5964

60-
public long getIpAddressId() {
65+
public Long getIpAddressId() {
6166
return id;
6267
}
6368

@@ -80,6 +85,11 @@ public String getEventType() {
8085
return EventTypes.EVENT_NET_IP_ASSIGN;
8186
}
8287

88+
public NicSecondaryIp getIpEntry() {
89+
NicSecondaryIp nicSecIp = _entityMgr.findById(NicSecondaryIp.class, getIpAddressId());
90+
return nicSecIp;
91+
}
92+
8393
@Override
8494
public String getEventDescription() {
8595
return ("Disassociating ip address with id=" + id);
@@ -98,9 +108,43 @@ public static String getResultObjectName() {
98108
return "addressinfo";
99109
}
100110

111+
public Long getNetworkId() {
112+
NicSecondaryIp nicSecIp = _entityMgr.findById(NicSecondaryIp.class, getIpAddressId());
113+
if (nicSecIp != null) {
114+
Long networkId = nicSecIp.getNetworkId();
115+
return networkId;
116+
} else {
117+
return null;
118+
}
119+
}
120+
121+
public NetworkType getNetworkType() {
122+
Network ntwk = _entityMgr.findById(Network.class, getNetworkId());
123+
if (ntwk != null) {
124+
DataCenter dc = _entityMgr.findById(DataCenter.class, ntwk.getDataCenterId());
125+
return dc.getNetworkType();
126+
}
127+
return null;
128+
}
129+
101130
@Override
102131
public void execute() throws InvalidParameterValueException {
103132
UserContext.current().setEventDetails("Ip Id: " + getIpAddressId());
133+
NicSecondaryIp nicSecIp = getIpEntry();
134+
135+
if (nicSecIp == null) {
136+
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Invalid IP id is passed");
137+
}
138+
139+
if (getNetworkType() == NetworkType.Basic) {
140+
//remove the security group rules for this secondary ip
141+
boolean success = false;
142+
success = _securityGroupService.securityGroupRulesForVmSecIp(nicSecIp.getNicId(), nicSecIp.getNetworkId(),nicSecIp.getIp4Address(), false);
143+
if (success == false) {
144+
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to set security group rules for the secondary ip");
145+
}
146+
}
147+
104148
boolean result = _networkService.releaseSecondaryIpFromNic(getIpAddressId());
105149
if (result) {
106150
SuccessResponse response = new SuccessResponse(getCommandName());
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package com.cloud.agent.api;
18+
19+
import com.cloud.vm.VirtualMachine;
20+
21+
public class NetworkRulesVmSecondaryIpCommand extends Command {
22+
23+
private String vmName;
24+
private VirtualMachine.Type type;
25+
private String vmSecIp;
26+
private String vmMac;
27+
private String action;
28+
29+
public NetworkRulesVmSecondaryIpCommand(String vmName, VirtualMachine.Type type) {
30+
this.vmName = vmName;
31+
this.type = type;
32+
}
33+
34+
35+
public NetworkRulesVmSecondaryIpCommand(String vmName, String vmMac,
36+
String secondaryIp, boolean action) {
37+
this.vmName = vmName;
38+
this.vmMac = vmMac;
39+
this.vmSecIp = secondaryIp;
40+
if (action) {
41+
this.action = "-A";
42+
} else {
43+
this.action = "-D";
44+
}
45+
}
46+
47+
public String getVmName() {
48+
return vmName;
49+
}
50+
51+
public VirtualMachine.Type getType() {
52+
return type;
53+
}
54+
55+
public String getVmSecIp() {
56+
return vmSecIp;
57+
}
58+
59+
public String getVmMac() {
60+
return vmMac;
61+
}
62+
63+
public String getAction() {
64+
return action;
65+
}
66+
67+
@Override
68+
public boolean executeInSequence() {
69+
return false;
70+
}
71+
}

0 commit comments

Comments
 (0)