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