Skip to content

Commit 3a85f87

Browse files
Add support to add and delete and update static routes on Netris (#37)
* Add support to add static routes in Netris * support to delete static routes on netris * add defensive check for nextHop * Add support to update static routes * add state * pass empty list for switched to avoid timeout * Netris: search static route by name and next hop if exists --------- Co-authored-by: Wei Zhou <[email protected]>
1 parent f88b498 commit 3a85f87

File tree

11 files changed

+335
-2
lines changed

11 files changed

+335
-2
lines changed

api/src/main/java/com/cloud/network/netris/NetrisService.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,28 @@
2222

2323
public interface NetrisService {
2424
boolean createIPAMAllocationsForZoneLevelPublicRanges(long zoneId);
25+
2526
boolean createVpcResource(long zoneId, long accountId, long domainId, Long vpcId, String vpcName, boolean sourceNatEnabled, String cidr, boolean isVpcNetwork);
27+
2628
boolean deleteVpcResource(long zoneId, long accountId, long domainId, Vpc vpc);
29+
2730
boolean createVnetResource(Long zoneId, long accountId, long domainId, String vpcName, Long vpcId, String networkName, Long networkId, String cidr);
31+
2832
boolean deleteVnetResource(long zoneId, long accountId, long domainId, String vpcName, Long vpcId, String networkName, Long networkId, String cidr);
33+
2934
boolean createSnatRule(long zoneId, long accountId, long domainId, String vpcName, long vpcId, String networkName, long networkId, boolean isForVpc, String vpcCidr, String sourceNatIp);
35+
3036
boolean createPortForwardingRule(long zoneId, long accountId, long domainId, String vpcName, long vpcId, String networkName, Long networkId, boolean isForVpc, String vpcCidr, SDNProviderNetworkRule networkRule);
37+
3138
boolean deletePortForwardingRule(long zoneId, long accountId, long domainId, String vpcName, Long vpcId, String networkName, Long networkId, boolean isForVpc, String vpcCidr, SDNProviderNetworkRule networkRule);
39+
3240
boolean updateVpcSourceNatIp(Vpc vpc, IpAddress address);
41+
3342
boolean createStaticNatRule(long zoneId, long accountId, long domainId, String networkResourceName, Long networkResourceId, boolean isForVpc, String vpcCidr, String staticNatIp, String vmIp);
43+
3444
boolean deleteStaticNatRule(long zoneId, long accountId, long domainId, String networkResourceName, Long networkResourceId, boolean isForVpc, String staticNatIp);
45+
46+
boolean addOrUpdateStaticRoute(long zoneId, long accountId, long domainId, String networkResourceName, Long networkResourceId, boolean isForVpc, String prefix, String nextHop, Long routeId, boolean updateRoute);
47+
48+
boolean deleteStaticRoute(long zoneId, long accountId, long domainId, String networkResourceName, Long networkResourceId, boolean isForVpc, String prefix, String nextHop, Long routeId);
3549
}

api/src/main/java/com/cloud/network/vpc/StaticRoute.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ enum State {
2525
Staged, // route been created but has never got through network rule conflict detection. Routes in this state can not be sent to VPC virtual router.
2626
Add, // Add means the route has been created and has gone through network rule conflict detection.
2727
Active, // Route has been sent to the VPC router and reported to be active.
28+
Update,
2829
Revoke, // Revoke means this route has been revoked. If this route has been sent to the VPC router, the route will be deleted from database.
2930
Deleting // rule has been revoked and is scheduled for deletion
3031
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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 org.apache.cloudstack.agent.api;
18+
19+
public class AddOrUpdateNetrisStaticRouteCommand extends NetrisCommand {
20+
private String prefix;
21+
private String nextHop;
22+
private Long routeId;
23+
private boolean updateRoute;
24+
25+
public AddOrUpdateNetrisStaticRouteCommand(long zoneId, Long accountId, Long domainId, String name, Long id, boolean isVpc, String prefix, String nextHop, Long routeId, boolean updateRoute) {
26+
super(zoneId, accountId, domainId, name, id, isVpc);
27+
this.prefix = prefix;
28+
this.nextHop = nextHop;
29+
this.routeId = routeId;
30+
this.updateRoute = updateRoute;
31+
}
32+
33+
public String getPrefix() {
34+
return prefix;
35+
}
36+
37+
public String getNextHop() {
38+
return nextHop;
39+
}
40+
41+
public Long getRouteId() {
42+
return routeId;
43+
}
44+
45+
public boolean isUpdateRoute() {
46+
return updateRoute;
47+
}
48+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 org.apache.cloudstack.agent.api;
18+
19+
public class DeleteNetrisStaticRouteCommand extends AddOrUpdateNetrisStaticRouteCommand {
20+
public DeleteNetrisStaticRouteCommand(long zoneId, Long accountId, Long domainId, String name, Long id, boolean isVpc, String prefix, String nextHop, Long routeId) {
21+
super(zoneId, accountId, domainId, name, id, isVpc, prefix, nextHop, routeId, false);
22+
}
23+
}

plugins/network-elements/netris/src/main/java/org/apache/cloudstack/resource/NetrisResource.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@
2828
import com.cloud.host.Host;
2929
import com.cloud.resource.ServerResource;
3030
import com.cloud.utils.exception.CloudRuntimeException;
31+
import org.apache.cloudstack.agent.api.AddOrUpdateNetrisStaticRouteCommand;
3132
import org.apache.cloudstack.agent.api.CreateNetrisVnetCommand;
3233
import org.apache.cloudstack.agent.api.CreateNetrisVpcCommand;
3334
import org.apache.cloudstack.agent.api.CreateOrUpdateNetrisNatCommand;
3435
import org.apache.cloudstack.agent.api.DeleteNetrisNatRuleCommand;
36+
import org.apache.cloudstack.agent.api.DeleteNetrisStaticRouteCommand;
3537
import org.apache.cloudstack.agent.api.DeleteNetrisVnetCommand;
3638
import org.apache.cloudstack.agent.api.DeleteNetrisVpcCommand;
3739
import org.apache.cloudstack.agent.api.NetrisAnswer;
@@ -102,7 +104,11 @@ public Answer executeRequest(Command cmd) {
102104
} else if (cmd instanceof DeleteNetrisNatRuleCommand) {
103105
return executeRequest((DeleteNetrisNatRuleCommand) cmd);
104106
} else if (cmd instanceof CreateOrUpdateNetrisNatCommand) {
105-
return executeRequest((CreateOrUpdateNetrisNatCommand) cmd);
107+
return executeRequest((CreateOrUpdateNetrisNatCommand) cmd);
108+
} else if (cmd instanceof DeleteNetrisStaticRouteCommand) {
109+
return executeRequest((DeleteNetrisStaticRouteCommand) cmd);
110+
} else if (cmd instanceof AddOrUpdateNetrisStaticRouteCommand) {
111+
return executeRequest((AddOrUpdateNetrisStaticRouteCommand) cmd);
106112
} else {
107113
return Answer.createUnsupportedCommandAnswer(cmd);
108114
}
@@ -297,6 +303,22 @@ private Answer executeRequest(DeleteNetrisNatRuleCommand cmd) {
297303
return new NetrisAnswer(cmd, true, "OK");
298304
}
299305

306+
private Answer executeRequest(AddOrUpdateNetrisStaticRouteCommand cmd) {
307+
boolean result = netrisApiClient.addOrUpdateStaticRoute(cmd);
308+
if (!result) {
309+
return new NetrisAnswer(cmd, false, String.format("Failed to add static route for VPC: %s, prefix: %s, nextHop: %s ", cmd.getName(), cmd.getPrefix(), cmd.getNextHop()));
310+
}
311+
return new NetrisAnswer(cmd, true, "OK");
312+
}
313+
314+
private Answer executeRequest(DeleteNetrisStaticRouteCommand cmd) {
315+
boolean result = netrisApiClient.deleteStaticRoute(cmd);
316+
if (!result) {
317+
return new NetrisAnswer(cmd, false, String.format("Failed to add static route for VPC: %s, prefix: %s, nextHop: %s ", cmd.getName(), cmd.getPrefix(), cmd.getNextHop()));
318+
}
319+
return new NetrisAnswer(cmd, true, "OK");
320+
}
321+
300322
@Override
301323
public boolean start() {
302324
return true;

plugins/network-elements/netris/src/main/java/org/apache/cloudstack/resource/NetrisResourceObjectUtils.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
public class NetrisResourceObjectUtils {
2323

2424
public enum NetrisObjectType {
25-
VPC, IPAM_ALLOCATION, IPAM_SUBNET, VNET, SNAT, STATICNAT, DNAT
25+
VPC, IPAM_ALLOCATION, IPAM_SUBNET, VNET, SNAT, STATICNAT, DNAT, STATICROUTE
2626
}
2727

2828
public static String retrieveNetrisResourceObjectName(NetrisCommand cmd, NetrisObjectType netrisObjectType, String... suffixes) {
@@ -72,6 +72,10 @@ public static String retrieveNetrisResourceObjectName(NetrisCommand cmd, NetrisO
7272
stringBuilder.append(String.format("%s%s-%s", prefix, suffixes[0], "DNAT"));
7373
suffixes = ArrayUtils.subarray(suffixes, 1, suffixes.length);
7474
break;
75+
case STATICROUTE:
76+
stringBuilder.append(String.format("%s%s-%s%s", prefix, suffixes[0], "ROUTE", suffixes[1]));
77+
suffixes = new String[0];
78+
break;
7579
case VNET:
7680
break;
7781
default:

plugins/network-elements/netris/src/main/java/org/apache/cloudstack/service/NetrisApiClient.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@
2020
import io.netris.model.GetSiteBody;
2121
import io.netris.model.VPCListing;
2222
import io.netris.model.response.TenantResponse;
23+
import org.apache.cloudstack.agent.api.AddOrUpdateNetrisStaticRouteCommand;
2324
import org.apache.cloudstack.agent.api.CreateNetrisVnetCommand;
2425
import org.apache.cloudstack.agent.api.CreateNetrisVpcCommand;
2526
import org.apache.cloudstack.agent.api.CreateOrUpdateNetrisNatCommand;
2627
import org.apache.cloudstack.agent.api.DeleteNetrisNatRuleCommand;
28+
import org.apache.cloudstack.agent.api.DeleteNetrisStaticRouteCommand;
2729
import org.apache.cloudstack.agent.api.DeleteNetrisVnetCommand;
2830
import org.apache.cloudstack.agent.api.DeleteNetrisVpcCommand;
2931
import org.apache.cloudstack.agent.api.SetupNetrisPublicRangeCommand;
@@ -74,4 +76,6 @@ public interface NetrisApiClient {
7476
boolean createOrUpdateDNATRule(CreateOrUpdateNetrisNatCommand cmd);
7577
boolean createStaticNatRule(CreateOrUpdateNetrisNatCommand cmd);
7678
boolean deleteNatRule(DeleteNetrisNatRuleCommand cmd);
79+
boolean addOrUpdateStaticRoute(AddOrUpdateNetrisStaticRouteCommand cmd);
80+
boolean deleteStaticRoute(DeleteNetrisStaticRouteCommand cmd);
7781
}

0 commit comments

Comments
 (0)