Skip to content

Commit 76cd32f

Browse files
author
Anurag Awasthi
committed
Health check feature for virtual router
1 parent c01ce7b commit 76cd32f

24 files changed

Lines changed: 928 additions & 71 deletions

File tree

api/src/main/java/com/cloud/event/EventTypes.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@
1919
import java.util.HashMap;
2020
import java.util.Map;
2121

22+
import org.apache.cloudstack.acl.Role;
23+
import org.apache.cloudstack.acl.RolePermission;
24+
import org.apache.cloudstack.annotation.Annotation;
25+
import org.apache.cloudstack.config.Configuration;
26+
import org.apache.cloudstack.ha.HAConfig;
27+
import org.apache.cloudstack.usage.Usage;
28+
2229
import com.cloud.dc.DataCenter;
2330
import com.cloud.dc.Pod;
2431
import com.cloud.dc.StorageNetworkIpRange;
@@ -69,12 +76,6 @@
6976
import com.cloud.vm.Nic;
7077
import com.cloud.vm.NicSecondaryIp;
7178
import com.cloud.vm.VirtualMachine;
72-
import org.apache.cloudstack.acl.Role;
73-
import org.apache.cloudstack.acl.RolePermission;
74-
import org.apache.cloudstack.annotation.Annotation;
75-
import org.apache.cloudstack.config.Configuration;
76-
import org.apache.cloudstack.ha.HAConfig;
77-
import org.apache.cloudstack.usage.Usage;
7879

7980
public class EventTypes {
8081

@@ -106,6 +107,7 @@ public class EventTypes {
106107
public static final String EVENT_ROUTER_HA = "ROUTER.HA";
107108
public static final String EVENT_ROUTER_UPGRADE = "ROUTER.UPGRADE";
108109
public static final String EVENT_ROUTER_DIAGNOSTICS = "ROUTER.DIAGNOSTICS";
110+
public static final String EVENT_ROUTER_HEALTH_CHECKS = "ROUTER.HEALTH.CHECKS";
109111

110112
// Console proxy
111113
public static final String EVENT_PROXY_CREATE = "PROXY.CREATE";
@@ -603,6 +605,7 @@ public class EventTypes {
603605
entityEventDetails.put(EVENT_ROUTER_HA, VirtualRouter.class);
604606
entityEventDetails.put(EVENT_ROUTER_UPGRADE, VirtualRouter.class);
605607
entityEventDetails.put(EVENT_ROUTER_DIAGNOSTICS, VirtualRouter.class);
608+
entityEventDetails.put(EVENT_ROUTER_HEALTH_CHECKS, VirtualRouter.class);
606609

607610
entityEventDetails.put(EVENT_PROXY_CREATE, VirtualMachine.class);
608611
entityEventDetails.put(EVENT_PROXY_DESTROY, VirtualMachine.class);

api/src/main/java/com/cloud/network/VirtualNetworkApplianceService.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.cloud.network;
1818

1919
import java.util.List;
20+
import java.util.Map;
2021

2122
import org.apache.cloudstack.api.command.admin.router.UpgradeRouterCmd;
2223
import org.apache.cloudstack.api.command.admin.router.UpgradeRouterTemplateCmd;
@@ -31,8 +32,7 @@ public interface VirtualNetworkApplianceService {
3132
/**
3233
* Starts domain router
3334
*
34-
* @param cmd
35-
* the command specifying router's id
35+
* @param cmd the command specifying router's id
3636
* @return DomainRouter object
3737
*/
3838
VirtualRouter startRouter(long routerId, boolean reprogramNetwork) throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException;
@@ -51,10 +51,8 @@ public interface VirtualNetworkApplianceService {
5151
/**
5252
* Stops domain router
5353
*
54-
* @param id
55-
* of the router
56-
* @param forced
57-
* just do it. caller knows best.
54+
* @param id of the router
55+
* @param forced just do it. caller knows best.
5856
* @return router if successful, null otherwise
5957
* @throws ResourceUnavailableException
6058
* @throws ConcurrentOperationException
@@ -68,4 +66,14 @@ public interface VirtualNetworkApplianceService {
6866
VirtualRouter findRouter(long routerId);
6967

7068
List<Long> upgradeRouterTemplate(UpgradeRouterTemplateCmd cmd);
69+
70+
/**
71+
* Returns the health check results for the router. It can run the health checks on demand if runChecks is true. Otherwise,
72+
* it fetches the previously executed health checks.
73+
*
74+
* @param routerId id of the router
75+
* @param runChecks
76+
* @return
77+
*/
78+
Map<String, String> getRouterHealthCheckResults(long routerId, boolean runChecks);
7179
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
18+
package org.apache.cloudstack.api.command.admin.router;
19+
20+
import java.util.Map;
21+
22+
import org.apache.cloudstack.api.APICommand;
23+
import org.apache.cloudstack.api.ApiConstants;
24+
import org.apache.cloudstack.api.ApiErrorCode;
25+
import org.apache.cloudstack.api.BaseCmd;
26+
import org.apache.cloudstack.api.Parameter;
27+
import org.apache.cloudstack.api.ServerApiException;
28+
import org.apache.cloudstack.api.response.DomainRouterHealthChecksDataResponse;
29+
import org.apache.cloudstack.api.response.DomainRouterResponse;
30+
import org.apache.cloudstack.context.CallContext;
31+
import org.apache.log4j.Logger;
32+
33+
import com.cloud.exception.ConcurrentOperationException;
34+
import com.cloud.exception.InsufficientCapacityException;
35+
import com.cloud.exception.InvalidParameterValueException;
36+
import com.cloud.exception.ResourceUnavailableException;
37+
import com.cloud.network.router.VirtualRouter;
38+
import com.cloud.user.Account;
39+
import com.cloud.vm.VirtualMachine;
40+
41+
@APICommand(name = GetRouterHealthChecksDataCmd.APINAME,
42+
responseObject = DomainRouterHealthChecksDataResponse.class,
43+
description = "Starts a router.",
44+
entityType = {VirtualMachine.class},
45+
requestHasSensitiveInfo = false,
46+
responseHasSensitiveInfo = false,
47+
since = "4.13.1")
48+
public class GetRouterHealthChecksDataCmd extends BaseCmd {
49+
public static final Logger s_logger = Logger.getLogger(GetRouterHealthChecksDataCmd.class.getName());
50+
public static final String APINAME = "getRouterHealthChecksData";
51+
52+
/////////////////////////////////////////////////////
53+
//////////////// API parameters /////////////////////
54+
/////////////////////////////////////////////////////
55+
56+
@Parameter(name = ApiConstants.ID, type = CommandType.UUID, entityType = DomainRouterResponse.class,
57+
required = true, description = "the ID of the router")
58+
private Long id;
59+
60+
@Parameter(name = "performFreshChecks", type = CommandType.BOOLEAN, description = "if true is passed for this parameter, " +
61+
"health checks are performed on the fly. Else last performed checks data is fetched")
62+
private Boolean performFreshChecks;
63+
64+
/////////////////////////////////////////////////////
65+
/////////////////// Accessors ///////////////////////
66+
/////////////////////////////////////////////////////
67+
68+
public Long getId() {
69+
return id;
70+
}
71+
72+
public boolean shouldPerformFreshChecks() {
73+
return performFreshChecks == null ? false : performFreshChecks.booleanValue();
74+
}
75+
76+
/////////////////////////////////////////////////////
77+
/////////////// API Implementation///////////////////
78+
/////////////////////////////////////////////////////
79+
80+
@Override
81+
public String getCommandName() {
82+
return APINAME.toLowerCase() + BaseCmd.RESPONSE_SUFFIX;
83+
}
84+
85+
@Override
86+
public long getEntityOwnerId() {
87+
VirtualRouter router = _entityMgr.findById(VirtualRouter.class, getId());
88+
if (router != null) {
89+
return router.getAccountId();
90+
}
91+
92+
return Account.ACCOUNT_ID_SYSTEM;
93+
}
94+
95+
@Override
96+
public void execute() throws ConcurrentOperationException, ResourceUnavailableException, InsufficientCapacityException {
97+
CallContext.current().setEventDetails("Router Id: " + this._uuidMgr.getUuid(VirtualMachine.class, getId()));
98+
VirtualRouter router = _routerService.findRouter(getId());
99+
Map<String, String> result = null;
100+
if (router == null || router.getRole() != VirtualRouter.Role.VIRTUAL_ROUTER) {
101+
throw new InvalidParameterValueException("Can't find router by id");
102+
} else {
103+
result = _routerService.getRouterHealthCheckResults(getId(), shouldPerformFreshChecks());
104+
}
105+
106+
if (result != null) {
107+
DomainRouterHealthChecksDataResponse routerResponse = new DomainRouterHealthChecksDataResponse("healthData");
108+
routerResponse.setId(router.getUuid());
109+
routerResponse.setName(router.getInstanceName());
110+
routerResponse.setResult(result.get("success"));
111+
routerResponse.setDetails(result.get("message"));
112+
routerResponse.setResponseName(getCommandName());
113+
setResponseObject(routerResponse);
114+
} else {
115+
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to start router");
116+
}
117+
}
118+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
18+
package org.apache.cloudstack.api.response;
19+
20+
import org.apache.cloudstack.api.ApiConstants;
21+
import org.apache.cloudstack.api.BaseResponse;
22+
23+
import com.cloud.serializer.Param;
24+
import com.google.gson.annotations.SerializedName;
25+
26+
public class DomainRouterHealthChecksDataResponse extends BaseResponse {
27+
@SerializedName(ApiConstants.ID)
28+
@Param(description = "the id of the router")
29+
private String id;
30+
31+
@SerializedName(ApiConstants.NAME)
32+
@Param(description = "the name of the router")
33+
private String name;
34+
35+
@SerializedName(ApiConstants.RESULT)
36+
@Param(description = "result of management server's attempt to fetch data.")
37+
private String result;
38+
39+
@SerializedName(ApiConstants.DETAILS)
40+
@Param(description = "detailed data fetched from management server")
41+
private String details;
42+
43+
public DomainRouterHealthChecksDataResponse(String objectName) {
44+
super(objectName);
45+
}
46+
47+
public String getId() {
48+
return id;
49+
}
50+
51+
public String getName() {
52+
return name;
53+
}
54+
55+
public String getResult() {
56+
return result;
57+
}
58+
59+
public String getDetails() {
60+
return details;
61+
}
62+
63+
public void setId(String id) {
64+
this.id = id;
65+
}
66+
67+
public void setName(String name) {
68+
this.name = name;
69+
}
70+
71+
public void setResult(String result) {
72+
this.result = result;
73+
}
74+
75+
public void setDetails(String details) {
76+
this.details = details;
77+
}
78+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
18+
package com.cloud.agent.api.routing;
19+
20+
import java.util.List;
21+
22+
import com.cloud.agent.api.Answer;
23+
import com.cloud.agent.api.Command;
24+
25+
public class GetRouterMonitorResultsAnswer extends Answer {
26+
private List<String> failingChecks;
27+
private String monitoringResults;
28+
29+
protected GetRouterMonitorResultsAnswer() {
30+
super();
31+
}
32+
33+
public GetRouterMonitorResultsAnswer(Command cmd, boolean success, List<String> failingChecks, String monitoringResults) {
34+
super(cmd, success, monitoringResults);
35+
this.failingChecks = failingChecks;
36+
this.monitoringResults = monitoringResults;
37+
}
38+
39+
public List<String> getFailingChecks() {
40+
return failingChecks;
41+
}
42+
43+
public String getMonitoringResults() {
44+
return monitoringResults;
45+
}
46+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
18+
package com.cloud.agent.api.routing;
19+
20+
public class GetRouterMonitorResultsCommand extends NetworkElementCommand {
21+
private boolean performFreshChecks;
22+
23+
protected GetRouterMonitorResultsCommand() {
24+
}
25+
26+
public GetRouterMonitorResultsCommand(boolean performFreshChecks) {
27+
this.performFreshChecks = performFreshChecks;
28+
}
29+
30+
@Override
31+
public boolean isQuery() {
32+
return true;
33+
}
34+
35+
public boolean shouldPerformFreshChecks() {
36+
return performFreshChecks;
37+
}
38+
}

0 commit comments

Comments
 (0)