Bharat saraswal | 2f1077d | 2016-04-19 19:50:32 +0530 | [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 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.yangutils.parser.impl.parserutils; |
| 18 | |
| 19 | import java.util.ArrayList; |
| 20 | import java.util.List; |
| 21 | |
| 22 | import org.onosproject.yangutils.datamodel.YangNodeIdentifier; |
| 23 | |
| 24 | import static org.onosproject.yangutils.translator.tojava.utils.JavaIdentifierSyntax.getCaptialCase; |
| 25 | |
| 26 | /** |
| 27 | * Represents a utility which provides valid name for generated java file for augment node. |
| 28 | */ |
| 29 | public final class AugmentJavaFileNameGenUtil { |
| 30 | |
| 31 | /** |
| 32 | * Prefix to be added to generated java file for augment node. |
| 33 | */ |
| 34 | private static final String AUGMENTED = "Augmented"; |
| 35 | |
| 36 | /** |
| 37 | * The number of time augment has updated the same target node in same module/submodule. |
| 38 | */ |
| 39 | private static int occurrenceCount = 1; |
| 40 | |
| 41 | /** |
| 42 | * List of names for generated augment java file. |
| 43 | */ |
| 44 | private static List<String> augmentJavaFileNameList = new ArrayList<>(); |
| 45 | |
| 46 | private static final int ONE = 1; |
| 47 | private static final int TWO = 2; |
| 48 | private static final int ZERO = 0; |
| 49 | |
| 50 | /** |
| 51 | * Creates an instance of augment java file name generator utility. |
| 52 | */ |
| 53 | private AugmentJavaFileNameGenUtil() { |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Sets the augment java file name list. |
| 58 | * |
| 59 | * @param nameList name list |
| 60 | */ |
| 61 | private static void setAugmentJavaFileNameList(List<String> nameList) { |
| 62 | augmentJavaFileNameList = nameList; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Returns augment java file name list. |
| 67 | * |
| 68 | * @return augment java file name list |
| 69 | */ |
| 70 | public static List<String> getAugmentJavaFileNameList() { |
| 71 | return augmentJavaFileNameList; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Sets occurrence count. |
| 76 | * |
| 77 | * @param occurrence occurrence count |
| 78 | */ |
| 79 | private static void setOccurrenceCount(int occurrence) { |
| 80 | occurrenceCount = occurrence; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Returns occurrence count. |
| 85 | * |
| 86 | * @return occurrence count |
| 87 | */ |
| 88 | private static int getOccurrenceCount() { |
| 89 | return occurrenceCount; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Creates a name identifier for augment. |
| 94 | * |
| 95 | * @param nodeId node identifier |
| 96 | * @param isPrefix if prefix is present or it is not equals to parent's prefix |
| 97 | * @return valid name for augment |
| 98 | */ |
| 99 | public static String createValidNameForAugment(YangNodeIdentifier nodeId, boolean isPrefix) { |
| 100 | getAugmentJavaFileNameList().add(createName(nodeId, isPrefix)); |
| 101 | setAugmentJavaFileNameList(getAugmentJavaFileNameList()); |
| 102 | return getAugmentJavaFileNameList().get(getAugmentJavaFileNameList().size() - 1); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Creates name for the current augment file. |
| 107 | * |
| 108 | * @param nodeId node identifier |
| 109 | * @param isPrefix if prefix is present or it is not equals to parent's prefix |
| 110 | */ |
| 111 | private static String createName(YangNodeIdentifier nodeId, boolean isPrefix) { |
| 112 | if (isPrefix) { |
| 113 | return AUGMENTED + getCaptialCase(nodeId.getPrefix()) + getCaptialCase(nodeId.getName()); |
| 114 | } else { |
| 115 | return AUGMENTED + getCaptialCase(nodeId.getName()); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Updates occurrence count of augment. |
| 121 | */ |
| 122 | public static void updateOccurenceCount() { |
| 123 | int count = getOccurrenceCount(); |
| 124 | count++; |
| 125 | setOccurrenceCount(count); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Updates the list of name when augment has occurred multiple times to update the same target node |
| 130 | * and returns a valid name for augment node's generated java file. |
| 131 | * |
| 132 | * @param nodeId YANG node identifier |
| 133 | * @param isPrefix true if a prefix is present and it is not equals to parents prefix |
| 134 | * @return valid name for augment node |
| 135 | */ |
| 136 | public static String updateNameWhenHasMultipleOuccrrence(YangNodeIdentifier nodeId, boolean isPrefix) { |
| 137 | String name = ""; |
| 138 | updateOccurenceCount(); |
| 139 | |
| 140 | if (getOccurrenceCount() == TWO) { |
| 141 | String previousAugmentsName = getAugmentJavaFileNameList().get(getAugmentJavaFileNameList().size() - ONE); |
| 142 | getAugmentJavaFileNameList().remove(ZERO); |
| 143 | getAugmentJavaFileNameList().add(previousAugmentsName + ONE); |
| 144 | //TODO: update when already contains the name. |
| 145 | name = createName(nodeId, isPrefix) + TWO; |
| 146 | } else { |
| 147 | name = createName(nodeId, isPrefix) + getOccurrenceCount(); |
| 148 | } |
| 149 | getAugmentJavaFileNameList().add(name); |
| 150 | return name; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Resets occurrence count to one. |
| 155 | */ |
| 156 | public static void clearOccurrenceCount() { |
| 157 | setOccurrenceCount(ONE); |
| 158 | } |
| 159 | |
| 160 | } |