|
| 1 | +package oracle.kubernetes.operator.utils; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.JsonNode; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import com.fasterxml.jackson.databind.node.ArrayNode; |
| 6 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 7 | +import com.fasterxml.jackson.dataformat.yaml.YAMLMapper; |
| 8 | +import java.io.IOException; |
| 9 | +import java.nio.charset.Charset; |
| 10 | +import java.nio.charset.StandardCharsets; |
| 11 | +import java.nio.file.Files; |
| 12 | +import java.nio.file.Path; |
| 13 | +import java.nio.file.Paths; |
| 14 | + |
| 15 | +/** A Parser utility class to manipulate domain.yaml file */ |
| 16 | +public class DomainCRD { |
| 17 | + |
| 18 | + /** |
| 19 | + * A utility method to add restartVersion attribute to domain in domain.yaml |
| 20 | + * |
| 21 | + * @param domainCRD String representation of the domain.yaml as JSON tree |
| 22 | + * @param version Version value for the restartVersion attribute |
| 23 | + * @return Yaml String with added restartVersion attribute in domain |
| 24 | + * @throws IOException |
| 25 | + */ |
| 26 | + public String addRestartVersionToDomain(String domainCRD, String version) throws IOException { |
| 27 | + ObjectMapper objectMapper = new ObjectMapper(); |
| 28 | + JsonNode root = objectMapper.readTree(domainCRD); |
| 29 | + |
| 30 | + // modify admin server restartVersion |
| 31 | + JsonNode specNode = getSpecNode(root); |
| 32 | + ((ObjectNode) specNode).put("restartVersion", version); |
| 33 | + |
| 34 | + String jsonAsYaml = new YAMLMapper().writerWithDefaultPrettyPrinter().writeValueAsString(root); |
| 35 | + return jsonAsYaml; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * A utility method to add restartVersion attribute to administration server in domain.yaml |
| 40 | + * |
| 41 | + * @param domainCRD String representation of the domain.yaml as JSON tree |
| 42 | + * @param version Version value for the restartVersion attribute |
| 43 | + * @return Yaml String with added restartVersion attribute in administration server |
| 44 | + * @throws IOException |
| 45 | + */ |
| 46 | + public String addRestartVersionToAdminServer(String domainCRD, String version) |
| 47 | + throws IOException { |
| 48 | + ObjectMapper objectMapper = new ObjectMapper(); |
| 49 | + JsonNode root = objectMapper.readTree(domainCRD); |
| 50 | + |
| 51 | + // modify admin server restartVersion |
| 52 | + JsonNode adminServerNode = getAdminServerNode(root); |
| 53 | + ((ObjectNode) adminServerNode).put("restartVersion", version); |
| 54 | + |
| 55 | + String jsonAsYaml = new YAMLMapper().writerWithDefaultPrettyPrinter().writeValueAsString(root); |
| 56 | + return jsonAsYaml; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * A utility method to add restartVersion attribute to cluster in domain.yaml |
| 61 | + * |
| 62 | + * @param domainCRD String representation of the domain.yaml as JSON tree |
| 63 | + * @param ClusterName Name of the cluster in which to add the restartVersion attribute |
| 64 | + * @param version Version value for the restartVersion attribute |
| 65 | + * @return Yaml String with added restartVersion attribute in cluster |
| 66 | + * @throws IOException when the JSON tree cannot be read |
| 67 | + */ |
| 68 | + public String addRestartVersionToCluster(String domainCRD, String ClusterName, String version) |
| 69 | + throws IOException { |
| 70 | + ObjectMapper objectMapper = new ObjectMapper(); |
| 71 | + JsonNode root = objectMapper.readTree(domainCRD); |
| 72 | + |
| 73 | + // modify cluster restartVersion |
| 74 | + JsonNode clusterNode = getClusterNode(root, ClusterName); |
| 75 | + ((ObjectNode) clusterNode).put("restartVersion", version); |
| 76 | + |
| 77 | + String jsonAsYaml = new YAMLMapper().writerWithDefaultPrettyPrinter().writeValueAsString(root); |
| 78 | + return jsonAsYaml; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * A utility method to add restartVersion attribute to managed server in domain.yaml |
| 83 | + * |
| 84 | + * @param domainCRD domainCRD String representation of the domain.yaml as JSON tree |
| 85 | + * @param managedServerName Name of the managed server in which to add the restartVersion |
| 86 | + * attribute |
| 87 | + * @param version Version value for the restartVersion attribute |
| 88 | + * @return Yaml String with added restartVersion attribute in managed server |
| 89 | + * @throws IOException when the JSON tree cannot be read |
| 90 | + */ |
| 91 | + public String addRestartVersionToMS(String domainCRD, String managedServerName, String version) |
| 92 | + throws IOException { |
| 93 | + ObjectMapper objectMapper = new ObjectMapper(); |
| 94 | + JsonNode root = objectMapper.readTree(domainCRD); |
| 95 | + |
| 96 | + // modify managedserver restartVersion |
| 97 | + JsonNode managedServerNode = getManagedServerNode(objectMapper, root, managedServerName); |
| 98 | + ((ObjectNode) managedServerNode).put("restartVersion", version); |
| 99 | + |
| 100 | + String jsonAsYaml = new YAMLMapper().writerWithDefaultPrettyPrinter().writeValueAsString(root); |
| 101 | + return jsonAsYaml; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Gets the spec node entry from Domain CRD JSON tree |
| 106 | + * |
| 107 | + * @param root - Root JSON node of the Domain CRD JSON tree |
| 108 | + * @return - spec node entry from Domain CRD JSON tree |
| 109 | + */ |
| 110 | + private JsonNode getSpecNode(JsonNode root) { |
| 111 | + JsonNode items = root.get("items"); |
| 112 | + JsonNode spec = null; |
| 113 | + for (JsonNode item : items) { |
| 114 | + spec = item.path("spec"); |
| 115 | + } |
| 116 | + return spec; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Gets the administration server node entry from Domain CRD JSON tree |
| 121 | + * |
| 122 | + * @param root - Root JSON node of the Domain CRD JSON tree |
| 123 | + * @return - administration server node entry from Domain CRD JSON tree |
| 124 | + */ |
| 125 | + private JsonNode getAdminServerNode(JsonNode root) { |
| 126 | + JsonNode items = root.get("items"); |
| 127 | + JsonNode adminServer = null; |
| 128 | + for (JsonNode item : items) { |
| 129 | + adminServer = item.path("spec").path("adminServer"); |
| 130 | + } |
| 131 | + return adminServer; |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Gets the cluster node entry from Domain CRD JSON tree for the given cluster name |
| 136 | + * |
| 137 | + * @param root - Root JSON node of the Domain CRD JSON tree |
| 138 | + * @param clusterName - Name of the cluster |
| 139 | + * @return - cluster node entry from Domain CRD JSON tree |
| 140 | + */ |
| 141 | + private JsonNode getClusterNode(JsonNode root, String clusterName) { |
| 142 | + ArrayNode items = (ArrayNode) root.get("items"); |
| 143 | + ArrayNode clusters; |
| 144 | + JsonNode clusterNode = null; |
| 145 | + for (JsonNode item : items) { |
| 146 | + clusters = (ArrayNode) item.path("spec").path("clusters"); |
| 147 | + for (JsonNode cluster : clusters) { |
| 148 | + if (cluster.get("clusterName").asText().equals(clusterName)) { |
| 149 | + clusterNode = cluster; |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + return clusterNode; |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Gets the managed server node entry from Domain CRD JSON tree |
| 158 | + * |
| 159 | + * @param objectMapper - Instance of the ObjectMapper to use for creating a missing node |
| 160 | + * @param root - Root JSON node of the Domain CRD JSON tree |
| 161 | + * @param managedServerName - Name of the managed server for which to get the JSON node |
| 162 | + * @return administration server node entry from Domain CRD JSON tree |
| 163 | + */ |
| 164 | + private JsonNode getManagedServerNode( |
| 165 | + ObjectMapper objectMapper, JsonNode root, String managedServerName) { |
| 166 | + ArrayNode items = (ArrayNode) root.get("items"); |
| 167 | + ArrayNode managedservers = null; |
| 168 | + JsonNode managedserverNode = null; |
| 169 | + for (JsonNode item : items) { |
| 170 | + managedservers = (ArrayNode) item.path("spec").path("managedServers"); |
| 171 | + if (managedservers.size() != 0) { |
| 172 | + for (JsonNode managedserver : managedservers) { |
| 173 | + if (managedserver.get("serverName").equals(managedServerName)) { |
| 174 | + managedserverNode = managedserver; |
| 175 | + } |
| 176 | + } |
| 177 | + } else { |
| 178 | + ObjectNode managedserver = objectMapper.createObjectNode(); |
| 179 | + managedserver.put("serverName", managedServerName); |
| 180 | + managedservers.add(managedserver); |
| 181 | + managedserverNode = managedserver; |
| 182 | + } |
| 183 | + } |
| 184 | + return managedserverNode; |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * Utility method to create a file and write to it. |
| 189 | + * |
| 190 | + * @param dir - Directory in which to create the file |
| 191 | + * @param file - Name of the file to write the content to |
| 192 | + * @param content - String content to write to the file |
| 193 | + * @throws IOException - When file cannot opened or written |
| 194 | + */ |
| 195 | + public void writeToFile(String dir, String file, String content) throws IOException { |
| 196 | + Path path = Paths.get(dir, file); |
| 197 | + Charset charset = StandardCharsets.UTF_8; |
| 198 | + Files.write(path, content.getBytes(charset)); |
| 199 | + } |
| 200 | +} |
0 commit comments