|
| 1 | +/** |
| 2 | + * configSettings parse the EdgeServer XML file and set all the parameters of the edge server. |
| 3 | + * parse the MicroService XML file to set all the parameyters of the microservices |
| 4 | + * parse the inputGraph XML file to set the input graph |
| 5 | + */ |
| 6 | + |
| 7 | +package core; |
| 8 | + |
| 9 | +import java.io.File; |
| 10 | +import java.util.ArrayList; |
| 11 | + |
| 12 | +import javax.xml.parsers.DocumentBuilder; |
| 13 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 14 | + |
| 15 | +import org.w3c.dom.Document; |
| 16 | +import org.w3c.dom.Element; |
| 17 | +import org.w3c.dom.Node; |
| 18 | +import org.w3c.dom.NodeList; |
| 19 | + |
| 20 | +import utils.EdgeServer; |
| 21 | +import utils.InputGraph; |
| 22 | +import utils.MicroService; |
| 23 | + |
| 24 | + |
| 25 | +public class ConfigSettings { |
| 26 | + |
| 27 | + private static ConfigSettings instance = null; |
| 28 | + private Document edgeServerDoc = null; |
| 29 | + private Document microServiceDoc = null; |
| 30 | + private Document microServiceGraphInputDoc = null; |
| 31 | + |
| 32 | + private int NUM_OF_EDGE_SERVER; |
| 33 | + private int NUM_OF_MICRO_SERVICES; |
| 34 | + private int NUM_OF_VERTEX; |
| 35 | + private int NUM_OF_EDGES; |
| 36 | + |
| 37 | + private ConfigSettings() { |
| 38 | + NUM_OF_EDGE_SERVER = 0; |
| 39 | + NUM_OF_MICRO_SERVICES = 0; |
| 40 | + } |
| 41 | + |
| 42 | + public static ConfigSettings getInstance() { |
| 43 | + if(instance == null) { |
| 44 | + instance = new ConfigSettings(); |
| 45 | + } |
| 46 | + return instance; |
| 47 | + } |
| 48 | + |
| 49 | + /* |
| 50 | + * Reads configuration files and stores the information to local variables |
| 51 | + */ |
| 52 | + public boolean initialize(String edgeServerFile, String microServiceFile, String microServiceGraphInputFile) { |
| 53 | + boolean result = false; |
| 54 | + |
| 55 | + // parse details of Edge Servers |
| 56 | + result = parseEdgeServerXML(edgeServerFile); |
| 57 | + |
| 58 | + // parse details of Microservices |
| 59 | + result = parseMicroServicesXML(microServiceFile); |
| 60 | + |
| 61 | + // parse details of Microservice Graph Input File |
| 62 | + result = parseMicroServiceGraphInputXML(microServiceGraphInputFile); |
| 63 | + |
| 64 | + return result; |
| 65 | + |
| 66 | + } |
| 67 | + |
| 68 | + // parse the input graph XML file |
| 69 | + private boolean parseMicroServiceGraphInputXML(String microServiceGraphInputFilePath) { |
| 70 | + |
| 71 | + try { |
| 72 | + int destVertex; |
| 73 | + int edgeWeight; |
| 74 | + |
| 75 | + File microServiceGraphInputFile = new File(microServiceGraphInputFilePath); |
| 76 | + DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); |
| 77 | + DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); |
| 78 | + microServiceGraphInputDoc = dBuilder.parse(microServiceGraphInputFile); |
| 79 | + microServiceGraphInputDoc.getDocumentElement().normalize(); |
| 80 | + |
| 81 | + InputGraph graphInstance = InputGraph.getInstance(); |
| 82 | + NodeList vertexList = microServiceGraphInputDoc.getElementsByTagName("node"); |
| 83 | + for(int i = 0; i < vertexList.getLength(); ++i) { |
| 84 | + NUM_OF_VERTEX++; |
| 85 | + Node inputGraphVertex = vertexList.item(i); |
| 86 | + |
| 87 | + Element inputGraphVertexElement = (Element) inputGraphVertex; |
| 88 | + isAttributePresent(inputGraphVertexElement, "name"); |
| 89 | + |
| 90 | + NodeList edgeList = inputGraphVertexElement.getElementsByTagName("edge"); |
| 91 | + for(int j = 0; j < edgeList.getLength(); ++j) { |
| 92 | + NUM_OF_EDGES++; |
| 93 | + Node edge = edgeList.item(j); |
| 94 | + |
| 95 | + Element edgeElement = (Element) edge; |
| 96 | + destVertex = Integer.parseInt(isElementPresent(edgeElement, "vertex")); |
| 97 | + edgeWeight = Integer.parseInt(isElementPresent(edgeElement, "weight")); |
| 98 | + graphInstance.addEdge(i, destVertex, edgeWeight); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + } catch(Exception e) { |
| 103 | + System.out.println("Microservice Graph input XML cannot be parsed! Terminating..."); |
| 104 | + e.printStackTrace(); |
| 105 | + System.exit(1); |
| 106 | + } |
| 107 | + |
| 108 | + return true; |
| 109 | + } |
| 110 | + |
| 111 | + // parse the microservice XML file |
| 112 | + private boolean parseMicroServicesXML(String microServiceFilePath) { |
| 113 | + |
| 114 | + try { |
| 115 | + int microServiceCore; |
| 116 | + int microServiceMemory; |
| 117 | + double microServiceExecutionTime; |
| 118 | + |
| 119 | + File microServiceFile = new File(microServiceFilePath); |
| 120 | + DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); |
| 121 | + DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); |
| 122 | + microServiceDoc = dBuilder.parse(microServiceFile); |
| 123 | + microServiceDoc.getDocumentElement().normalize(); |
| 124 | + |
| 125 | + NodeList microServiceList = microServiceDoc.getElementsByTagName("microservice"); |
| 126 | + for(int i = 0; i < microServiceList.getLength(); ++i) { |
| 127 | + NUM_OF_MICRO_SERVICES++; |
| 128 | + Node microServiceNode = microServiceList.item(i); |
| 129 | + |
| 130 | + Element microServiceElement = (Element) microServiceNode; |
| 131 | + isAttributePresent(microServiceElement, "name"); |
| 132 | + microServiceCore = Integer.parseInt(isElementPresent(microServiceElement, "cores")); |
| 133 | + microServiceMemory = Integer.parseInt(isElementPresent(microServiceElement, "memory")); |
| 134 | + microServiceExecutionTime = Double.parseDouble(isElementPresent(microServiceElement, "execution_time")); |
| 135 | + MicroService.createInstance(i, microServiceCore, microServiceMemory, microServiceExecutionTime); |
| 136 | + } |
| 137 | + |
| 138 | + } catch(Exception e) { |
| 139 | + System.out.println("Microservice XML cannot be parsed! Terminating..."); |
| 140 | + e.printStackTrace(); |
| 141 | + System.exit(1); |
| 142 | + } |
| 143 | + |
| 144 | + return true; |
| 145 | + } |
| 146 | + |
| 147 | + // parse the edge server XML file |
| 148 | + private boolean parseEdgeServerXML(String edgeServerFilePath) { |
| 149 | + |
| 150 | + try { |
| 151 | + int edgeServerCore; |
| 152 | + int edgeServerMemory; |
| 153 | + |
| 154 | + File edgeServerFile = new File(edgeServerFilePath); |
| 155 | + DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); |
| 156 | + DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); |
| 157 | + edgeServerDoc = dBuilder.parse(edgeServerFile); |
| 158 | + edgeServerDoc.getDocumentElement().normalize(); |
| 159 | + |
| 160 | + NodeList edgeServerList = edgeServerDoc.getElementsByTagName("edgeserver"); |
| 161 | + for(int i = 0; i < edgeServerList.getLength(); ++i) { |
| 162 | + NUM_OF_EDGE_SERVER++; |
| 163 | + Node edgeServerNode = edgeServerList.item(i); |
| 164 | + |
| 165 | + Element edgeServerElement = (Element) edgeServerNode; |
| 166 | + isAttributePresent(edgeServerElement, "name"); |
| 167 | + edgeServerCore = Integer.parseInt(isElementPresent(edgeServerElement, "cores")); |
| 168 | + edgeServerMemory = Integer.parseInt(isElementPresent(edgeServerElement, "memory")); |
| 169 | + EdgeServer.createInstance(i, edgeServerCore, edgeServerMemory); |
| 170 | + |
| 171 | + } |
| 172 | + } catch(Exception e) { |
| 173 | + System.out.println("Edge server XML cannot be parsed! Terminating..."); |
| 174 | + e.printStackTrace(); |
| 175 | + System.exit(1); |
| 176 | + } |
| 177 | + |
| 178 | + return true; |
| 179 | + } |
| 180 | + |
| 181 | + private void isAttributePresent(Element element, String key) { |
| 182 | + String value = element.getAttribute(key); |
| 183 | + if(value.isEmpty() || value == null) { |
| 184 | + throw new IllegalArgumentException("Attribute '" + key + "' is not found in '" + element.getNodeName() + "'"); |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + private String isElementPresent(Element element, String key) { |
| 189 | + try { |
| 190 | + String value = element.getElementsByTagName(key).item(0).getTextContent(); |
| 191 | + if(value.isEmpty() || value == null) { |
| 192 | + throw new IllegalArgumentException("Element '" + key + "' is not found in '" + element.getNodeName() + "'"); |
| 193 | + } |
| 194 | + return value; |
| 195 | + } catch(Exception e) { |
| 196 | + throw new IllegalArgumentException("Element '" + key + "' is not found in '" + element.getNodeName() + "'"); |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | +} |
0 commit comments