|
| 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.api.command.user.network; |
| 18 | + |
| 19 | +import com.cloud.utils.net.NetworkProtocols; |
| 20 | +import org.apache.cloudstack.acl.RoleType; |
| 21 | +import org.apache.cloudstack.api.APICommand; |
| 22 | +import org.apache.cloudstack.api.ApiConstants; |
| 23 | +import org.apache.cloudstack.api.BaseCmd; |
| 24 | +import org.apache.cloudstack.api.Parameter; |
| 25 | +import org.apache.cloudstack.api.response.ListResponse; |
| 26 | +import org.apache.cloudstack.api.response.NetworkProtocolResponse; |
| 27 | +import org.apache.cloudstack.context.CallContext; |
| 28 | +import org.apache.log4j.Logger; |
| 29 | + |
| 30 | +import java.util.ArrayList; |
| 31 | +import java.util.List; |
| 32 | + |
| 33 | +@APICommand(name = "listNetworkProtocols", description = "Lists details of network protocols", responseObject = NetworkProtocolResponse.class, |
| 34 | + requestHasSensitiveInfo = false, responseHasSensitiveInfo = false, |
| 35 | + authorized = { RoleType.Admin, RoleType.DomainAdmin, RoleType.ResourceAdmin, RoleType.User}, since = "4.19.0") |
| 36 | +public class ListNetworkProtocolsCmd extends BaseCmd { |
| 37 | + public static final Logger s_logger = Logger.getLogger(ListNetworkProtocolsCmd.class.getName()); |
| 38 | + |
| 39 | + |
| 40 | + ///////////////////////////////////////////////////// |
| 41 | + //////////////// API parameters ///////////////////// |
| 42 | + ///////////////////////////////////////////////////// |
| 43 | + |
| 44 | + @Parameter(name = ApiConstants.OPTION, type = CommandType.STRING, required = true, |
| 45 | + description = "The option of network protocols. Supported values are: protocolnumber, icmptype.") |
| 46 | + private String option; |
| 47 | + |
| 48 | + |
| 49 | + ///////////////////////////////////////////////////// |
| 50 | + /////////////////// Accessors /////////////////////// |
| 51 | + ///////////////////////////////////////////////////// |
| 52 | + |
| 53 | + |
| 54 | + public String getOption() { |
| 55 | + return option; |
| 56 | + } |
| 57 | + |
| 58 | + ///////////////////////////////////////////////////// |
| 59 | + /////////////// API Implementation/////////////////// |
| 60 | + ///////////////////////////////////////////////////// |
| 61 | + |
| 62 | + @Override |
| 63 | + public void execute() { |
| 64 | + ListResponse<NetworkProtocolResponse> response = new ListResponse<>(); |
| 65 | + List<NetworkProtocolResponse> networkProtocolResponses = new ArrayList<>(); |
| 66 | + |
| 67 | + NetworkProtocols.Option option = NetworkProtocols.Option.getOption(getOption()); |
| 68 | + switch (option) { |
| 69 | + case ProtocolNumber: |
| 70 | + updateResponseWithProtocolNumbers(networkProtocolResponses); |
| 71 | + break; |
| 72 | + case IcmpType: |
| 73 | + updateResponseWithIcmpTypes(networkProtocolResponses); |
| 74 | + break; |
| 75 | + default: |
| 76 | + break; |
| 77 | + } |
| 78 | + |
| 79 | + response.setResponses(networkProtocolResponses); |
| 80 | + response.setResponseName(getCommandName()); |
| 81 | + setResponseObject(response); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public long getEntityOwnerId() { |
| 86 | + return CallContext.current().getCallingAccount().getId(); |
| 87 | + } |
| 88 | + |
| 89 | + private void updateResponseWithProtocolNumbers(List<NetworkProtocolResponse> responses) { |
| 90 | + for (NetworkProtocols.ProtocolNumber protocolNumber : NetworkProtocols.ProtocolNumbers) { |
| 91 | + NetworkProtocolResponse networkProtocolResponse = new NetworkProtocolResponse(protocolNumber.getNumber(), |
| 92 | + protocolNumber.getKeyword(), protocolNumber.getProtocol()); |
| 93 | + networkProtocolResponse.setObjectName("networkprotocol"); |
| 94 | + responses.add(networkProtocolResponse); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private void updateResponseWithIcmpTypes(List<NetworkProtocolResponse> responses) { |
| 99 | + for (NetworkProtocols.IcmpType icmpType : NetworkProtocols.IcmpTypes) { |
| 100 | + NetworkProtocolResponse networkProtocolResponse = new NetworkProtocolResponse(icmpType.getType(), |
| 101 | + null, icmpType.getDescription()); |
| 102 | + for (NetworkProtocols.IcmpCode code : icmpType.getIcmpCodes()) { |
| 103 | + networkProtocolResponse.addDetail(String.valueOf(code.getCode()), code.getDescription()); |
| 104 | + } |
| 105 | + networkProtocolResponse.setObjectName("networkprotocol"); |
| 106 | + responses.add(networkProtocolResponse); |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments