|
| 1 | +package core; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | + |
| 5 | +import javax.xml.parsers.DocumentBuilder; |
| 6 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 7 | + |
| 8 | +import org.w3c.dom.Document; |
| 9 | +import org.w3c.dom.Element; |
| 10 | +import org.w3c.dom.Node; |
| 11 | +import org.w3c.dom.NodeList; |
| 12 | + |
| 13 | +import utils.Application; |
| 14 | +import utils.EdgeServer; |
| 15 | + |
| 16 | +public class ConfigSettings { |
| 17 | + |
| 18 | + private static ConfigSettings instance = null; |
| 19 | + private Document edgeServerDoc = null; |
| 20 | + private Document applicationDoc = null; |
| 21 | + |
| 22 | + private int NUM_OF_EDGE_SERVER; |
| 23 | + private int NUM_OF_APPLICATION; |
| 24 | + |
| 25 | + private ConfigSettings() { |
| 26 | + NUM_OF_EDGE_SERVER = 0; |
| 27 | + NUM_OF_APPLICATION = 0; |
| 28 | + } |
| 29 | + |
| 30 | + public static ConfigSettings getInstance() { |
| 31 | + if(instance == null) { |
| 32 | + instance = new ConfigSettings(); |
| 33 | + } |
| 34 | + return instance; |
| 35 | + } |
| 36 | + |
| 37 | + /* |
| 38 | + * Reads configuration files and stores the information to local variables |
| 39 | + */ |
| 40 | + public boolean initialize(String edgeServerFile, String applicationFile) { |
| 41 | + boolean result = false; |
| 42 | + |
| 43 | + // parse details of edge servers |
| 44 | + result = parseEdgeServerXML(edgeServerFile); |
| 45 | + |
| 46 | + // parse details of applications |
| 47 | + result = parseApplicationXML(applicationFile); |
| 48 | + |
| 49 | + return result; |
| 50 | + |
| 51 | + } |
| 52 | + |
| 53 | + // parse edgeservers.xml file |
| 54 | + private boolean parseEdgeServerXML(String edgeServerFilePath) { |
| 55 | + |
| 56 | + try { |
| 57 | + int edgeServerMemoryCapacity; |
| 58 | + int edgeServerComputationalCapacity; |
| 59 | + int edgeServerDownlinkDatarate; |
| 60 | + int edgeServerUplinkDatarate; |
| 61 | + |
| 62 | + File edgeServerFile = new File(edgeServerFilePath); |
| 63 | + DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); |
| 64 | + DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); |
| 65 | + edgeServerDoc = dBuilder.parse(edgeServerFile); |
| 66 | + edgeServerDoc.getDocumentElement().normalize(); |
| 67 | + |
| 68 | + NodeList edgeServerList = edgeServerDoc.getElementsByTagName("edgeserver"); |
| 69 | + for(int i = 0; i < edgeServerList.getLength(); ++i) { |
| 70 | + NUM_OF_EDGE_SERVER++; |
| 71 | + Node EdgeServerNode = edgeServerList.item(i); |
| 72 | + |
| 73 | + Element edgeServerElement = (Element) EdgeServerNode; |
| 74 | + isAttributePresent(edgeServerElement, "name"); |
| 75 | + edgeServerMemoryCapacity = Integer.parseInt(isElementPresent(edgeServerElement, "memory_capacity")); |
| 76 | + edgeServerComputationalCapacity = Integer.parseInt(isElementPresent(edgeServerElement, "computational_capacity")); |
| 77 | + edgeServerDownlinkDatarate = Integer.parseInt(isElementPresent(edgeServerElement, "downlink_datarate")); |
| 78 | + edgeServerUplinkDatarate = Integer.parseInt(isElementPresent(edgeServerElement, "uplink_datarate")); |
| 79 | + EdgeServer.createInstance(i+1, edgeServerMemoryCapacity, edgeServerComputationalCapacity, edgeServerDownlinkDatarate, edgeServerUplinkDatarate); |
| 80 | + } |
| 81 | + |
| 82 | + } catch(Exception e) { |
| 83 | + System.out.println("Edge server XML cannot be parsed! Terminating..."); |
| 84 | + e.printStackTrace(); |
| 85 | + System.exit(1); |
| 86 | + } |
| 87 | + |
| 88 | + return true; |
| 89 | + } |
| 90 | + |
| 91 | + // parse application.xml file |
| 92 | + private boolean parseApplicationXML(String applicationFilePath) { |
| 93 | + |
| 94 | + try { |
| 95 | + double applicationExecutionTimeLimit; |
| 96 | + double applicationComputationalComplexity; |
| 97 | + int taskDataSize; |
| 98 | + int taskOutputDataSize; |
| 99 | + |
| 100 | + File applicationFile = new File(applicationFilePath); |
| 101 | + DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); |
| 102 | + DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); |
| 103 | + applicationDoc = dBuilder.parse(applicationFile); |
| 104 | + applicationDoc.getDocumentElement().normalize(); |
| 105 | + |
| 106 | + NodeList applicationList = applicationDoc.getElementsByTagName("application"); |
| 107 | + for(int i = 0; i < applicationList.getLength(); ++i) { |
| 108 | + NUM_OF_APPLICATION++; |
| 109 | + Node applicationNode = applicationList.item(i); |
| 110 | + |
| 111 | + Element applicationElement = (Element) applicationNode; |
| 112 | + isAttributePresent(applicationElement, "name"); |
| 113 | + applicationExecutionTimeLimit = Double.parseDouble(isElementPresent(applicationElement, "execution_time_limit")); |
| 114 | + applicationComputationalComplexity = Double.parseDouble(isElementPresent(applicationElement, "computational_complexity")); |
| 115 | + Application applicationInstance = Application.createInstance(i+1, applicationExecutionTimeLimit, applicationComputationalComplexity); |
| 116 | + |
| 117 | + NodeList taskList = applicationElement.getElementsByTagName("task"); |
| 118 | + for(int j = 0; j < taskList.getLength(); ++j) { |
| 119 | + Node taskNode = taskList.item(j); |
| 120 | + |
| 121 | + Element taskElement = (Element) taskNode; |
| 122 | + isAttributePresent(taskElement, "name"); |
| 123 | + taskDataSize = Integer.parseInt(isElementPresent(taskElement, "datasize")); |
| 124 | + taskOutputDataSize = Integer.parseInt(isElementPresent(taskElement, "output_datasize")); |
| 125 | + applicationInstance.addTask(j+1, taskDataSize, taskOutputDataSize); |
| 126 | + } |
| 127 | + |
| 128 | + Application.addToApplicationList(applicationInstance); |
| 129 | + } |
| 130 | + |
| 131 | + } catch(Exception e) { |
| 132 | + System.out.println("Application XML cannot be parsed! Terminating..."); |
| 133 | + e.printStackTrace(); |
| 134 | + System.exit(1); |
| 135 | + } |
| 136 | + |
| 137 | + return true; |
| 138 | + } |
| 139 | + |
| 140 | + private void isAttributePresent(Element element, String key) { |
| 141 | + String value = element.getAttribute(key); |
| 142 | + if(value.isEmpty() || value == null) { |
| 143 | + throw new IllegalArgumentException("Attribute '" + key + "' is not found in '" + element.getNodeName() + "'"); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + private String isElementPresent(Element element, String key) { |
| 148 | + try { |
| 149 | + String value = element.getElementsByTagName(key).item(0).getTextContent(); |
| 150 | + if(value.isEmpty() || value == null) { |
| 151 | + throw new IllegalArgumentException("Element '" + key + "' is not found in '" + element.getNodeName() + "'"); |
| 152 | + } |
| 153 | + return value; |
| 154 | + } catch(Exception e) { |
| 155 | + throw new IllegalArgumentException("Element '" + key + "' is not found in '" + element.getNodeName() + "'"); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | +} |
0 commit comments