Luca Prete | dce16f8 | 2016-11-22 13:11:56 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | */ |
| 15 | package org.onosproject.vpls.cli; |
| 16 | |
| 17 | import java.util.ArrayList; |
| 18 | import java.util.Arrays; |
| 19 | import java.util.List; |
| 20 | import java.util.stream.Collectors; |
| 21 | |
| 22 | /** |
| 23 | * Enum representing the VPLS command type. |
| 24 | */ |
| 25 | public enum VplsCommandEnum { |
| 26 | ADD_IFACE("add-if"), |
| 27 | CLEAN("clean"), |
| 28 | CREATE("create"), |
| 29 | DELETE("delete"), |
| 30 | LIST("list"), |
| 31 | REMOVE_IFACE("rem-if"), |
| 32 | SET_ENCAP("set-encap"), |
| 33 | SHOW("show"); |
| 34 | |
| 35 | private final String command; |
| 36 | |
| 37 | /** |
| 38 | * Creates the enum from a string representing the command. |
| 39 | * |
| 40 | * @param command the text representing the command |
| 41 | */ |
| 42 | private VplsCommandEnum(final String command) { |
| 43 | this.command = command; |
| 44 | } |
| 45 | |
| 46 | @Override |
| 47 | public String toString() { |
| 48 | return command; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Returns a list of command string values. |
| 53 | * |
| 54 | * @return the list of string values corresponding to the enums |
| 55 | */ |
| 56 | public static List<String> toStringList() { |
| 57 | return Arrays.stream(values()) |
| 58 | .map(c -> c.toString()) |
| 59 | .collect(Collectors.toCollection(ArrayList::new)); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Alternative method to valueOf. It returns the command type |
| 64 | * corresponding to the given string. If the parameter does not match a |
| 65 | * constant name, or is null, null is returned. |
| 66 | * |
| 67 | * @param command the string representing the encapsulation type |
| 68 | * @return the EncapsulationType constant corresponding to the string given |
| 69 | */ |
| 70 | public static VplsCommandEnum enumFromString(String command) { |
| 71 | if (command != null && !command.isEmpty()) { |
| 72 | for (VplsCommandEnum c : values()) { |
| 73 | if (command.toString().equalsIgnoreCase(c.toString())) { |
| 74 | return c; |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | return null; |
| 79 | } |
| 80 | } |