|
| 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.List; |
| 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.DomainRouterResponse; |
| 29 | +import org.apache.cloudstack.api.response.RouterHealthCheckResultResponse; |
| 30 | +import org.apache.cloudstack.api.response.RouterHealthCheckResultsListResponse; |
| 31 | +import org.apache.cloudstack.context.CallContext; |
| 32 | +import org.apache.commons.lang.BooleanUtils; |
| 33 | +import org.apache.log4j.Logger; |
| 34 | + |
| 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.utils.exception.CloudRuntimeException; |
| 40 | +import com.cloud.vm.VirtualMachine; |
| 41 | + |
| 42 | +@APICommand(name = GetRouterHealthCheckResultsCmd.APINAME, |
| 43 | + responseObject = RouterHealthCheckResultsListResponse.class, |
| 44 | + description = "Starts a router.", |
| 45 | + entityType = {VirtualMachine.class}, |
| 46 | + requestHasSensitiveInfo = false, |
| 47 | + responseHasSensitiveInfo = false, |
| 48 | + since = "4.14.0") |
| 49 | +public class GetRouterHealthCheckResultsCmd extends BaseCmd { |
| 50 | + public static final Logger s_logger = Logger.getLogger(GetRouterHealthCheckResultsCmd.class.getName()); |
| 51 | + public static final String APINAME = "getRouterHealthCheckResults"; |
| 52 | + |
| 53 | + ///////////////////////////////////////////////////// |
| 54 | + //////////////// API parameters ///////////////////// |
| 55 | + ///////////////////////////////////////////////////// |
| 56 | + |
| 57 | + @Parameter(name = ApiConstants.ROUTER_ID, type = CommandType.UUID, entityType = DomainRouterResponse.class, |
| 58 | + required = true, description = "the ID of the router") |
| 59 | + private Long routerId; |
| 60 | + |
| 61 | + @Parameter(name = ApiConstants.PERFORM_FRESH_CHECKS, type = CommandType.BOOLEAN, description = "if true is passed for this parameter, " + |
| 62 | + "health checks are performed on the fly. Else last performed checks data is fetched") |
| 63 | + private Boolean performFreshChecks; |
| 64 | + |
| 65 | + ///////////////////////////////////////////////////// |
| 66 | + /////////////////// Accessors /////////////////////// |
| 67 | + ///////////////////////////////////////////////////// |
| 68 | + |
| 69 | + public Long getRouterId() { |
| 70 | + return routerId; |
| 71 | + } |
| 72 | + |
| 73 | + public boolean shouldPerformFreshChecks() { |
| 74 | + return BooleanUtils.isTrue(performFreshChecks); |
| 75 | + } |
| 76 | + |
| 77 | + ///////////////////////////////////////////////////// |
| 78 | + /////////////// API Implementation/////////////////// |
| 79 | + ///////////////////////////////////////////////////// |
| 80 | + |
| 81 | + @Override |
| 82 | + public String getCommandName() { |
| 83 | + return APINAME.toLowerCase() + BaseCmd.RESPONSE_SUFFIX; |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public long getEntityOwnerId() { |
| 88 | + VirtualRouter router = _entityMgr.findById(VirtualRouter.class, getRouterId()); |
| 89 | + if (router != null) { |
| 90 | + return router.getAccountId(); |
| 91 | + } |
| 92 | + |
| 93 | + return Account.ACCOUNT_ID_SYSTEM; |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public void execute() throws ResourceUnavailableException, InvalidParameterValueException, ServerApiException { |
| 98 | + CallContext.current().setEventDetails("Router Id: " + this._uuidMgr.getUuid(VirtualMachine.class, getRouterId())); |
| 99 | + VirtualRouter router = _routerService.findRouter(getRouterId()); |
| 100 | + if (router == null || router.getRole() != VirtualRouter.Role.VIRTUAL_ROUTER) { |
| 101 | + throw new InvalidParameterValueException("Can't find router by routerId"); |
| 102 | + } |
| 103 | + |
| 104 | + try { |
| 105 | + List<RouterHealthCheckResultResponse> healthChecks = _queryService.listRouterHealthChecks(this); |
| 106 | + RouterHealthCheckResultsListResponse routerResponse = new RouterHealthCheckResultsListResponse(); |
| 107 | + routerResponse.setRouterId(router.getUuid()); |
| 108 | + routerResponse.setHealthChecks(healthChecks); |
| 109 | + routerResponse.setObjectName("routerhealthchecks"); |
| 110 | + routerResponse.setResponseName(getCommandName()); |
| 111 | + setResponseObject(routerResponse); |
| 112 | + } catch (CloudRuntimeException ex){ |
| 113 | + ex.printStackTrace(); |
| 114 | + throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to execute command due to exception: " + ex.getLocalizedMessage()); |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments