/** constructASGraph Task 2 */ private void constructASGraph() { for (Map.Entry<String, ASPath> entry : asPathMap.entrySet()) { ASPath path = entry.getValue(); AS[] ases = path.getAses(); if (ases.length == 1) continue; for (int i = 0; i < ases.length; i++) { AS as = ases[i]; if (i == 0) { AS next = ases[i + 1]; if (!as.getNeighbourMap().containsKey(next.getNumber())) { as.insertNeibour(next.getNumber(), next); } } else if (i == ases.length - 1) { AS previous = ases[i - 1]; if (!as.getNeighbourMap().containsKey(previous.getNumber())) { as.insertNeibour(previous.getNumber(), previous); } } else { AS previous = ases[i - 1]; AS next = ases[i + 1]; if (!as.getNeighbourMap().containsKey(next.getNumber())) { as.insertNeibour(next.getNumber(), next); } if (!as.getNeighbourMap().containsKey(previous.getNumber())) { as.insertNeibour(previous.getNumber(), previous); } } } } AS[] top10 = new AS[10]; int index = 0; for (Map.Entry<Integer, AS> entry : asMap.entrySet()) { AS as = entry.getValue(); if (index < 10) { top10[index] = as; // Arrays.sort(top10, new ByWeightComparator()); } else if (index == 10) { Arrays.sort(top10, new ByDegreeComparator()); if (as.getDegree() > top10[0].getDegree()) { top10[0] = as; } Arrays.sort(top10, new ByDegreeComparator()); } else { if (as.getDegree() > top10[0].getDegree()) { top10[0] = as; } Arrays.sort(top10, new ByDegreeComparator()); } index++; } for (int i = 0; i < 10; i++) { System.out.println("AS " + top10[i].getNumber() + " degree:" + top10[i].getDegree()); } // System.out.println("Neighbours of AS " + top10[0].getNumber()); // for (Map.Entry<Integer, AS> entry : top10[0].getNeighbourMap().entrySet()) { // System.out.println(entry.getValue().getNumber()); // } }
private void asSumary() { int totalPathLen = 0; for (Map.Entry<String, ASPath> entry : asPathMap.entrySet()) { ASPath path = entry.getValue(); AS[] ases = path.getAses(); totalPathLen += ases.length; } System.out.println("--------------------Summary--------------------------------"); System.out.println("number of ASes:" + originAsMap.size()); System.out.println("number of AS pathes:" + asPathMap.size()); System.out.println("average length of AS paths: " + totalPathLen / asPathMap.size()); System.out.println("number of dense cores:" + denseCores.size()); System.out.println("number of transit cores:" + transitCores.size()); System.out.println("number of outer cores:" + outerCores.size()); System.out.println("number of regional ISPs:" + regionISPs.size()); System.out.println("number of stubs:" + stubs.size()); }
/** constructASGraph Task 3 */ private void annotateASGraph() { // algorithm 1 // phase 2 for (Map.Entry<String, ASPath> entry : asPathMap.entrySet()) { ASPath path = entry.getValue(); AS[] ases = path.getAses(); if (ases.length == 1) { continue; } int j = 0; int maxdegree = 0; for (int i = 0; i < ases.length; i++) { if (ases[i].getDegree() > maxdegree) { maxdegree = ases[i].getDegree(); j = i; } } for (int i = 0; i < j; i++) { Map<Integer, Integer> transitMap = ases[i].getTransit(); if (transitMap.containsKey(ases[i + 1].getNumber())) { Integer transit = transitMap.get(ases[i + 1].getNumber()); transit = transit + 1; transitMap.replace(ases[i + 1].getNumber(), transit); } else { System.err.println( "AS " + ases[i].getNumber() + " do not contain transit of AS " + ases[i + 1].getNumber()); transitMap.put(ases[i + 1].getNumber(), 1); } } for (int i = j; i < ases.length - 1; i++) { Map<Integer, Integer> transitMap = ases[i + 1].getTransit(); if (transitMap.containsKey(ases[i].getNumber())) { Integer transit = transitMap.get(ases[i].getNumber()); transit = transit + 1; transitMap.replace(ases[i].getNumber(), transit); } else { System.err.println( "AS " + ases[i + 1].getNumber() + " do not contain transit of AS " + ases[i].getNumber()); transitMap.put(ases[i].getNumber(), 1); } } } // phase 3 for (Map.Entry<String, ASPath> entry : asPathMap.entrySet()) { ASPath path = entry.getValue(); AS[] ases = path.getAses(); if (ases.length == 1) { continue; } for (int i = 0; i < ases.length - 1; i++) { Map<Integer, Integer> transFor_ui = ases[i].getTransit(); Map<Integer, Integer> transFor_ui1 = ases[i + 1].getTransit(); Integer ui = ases[i].getNumber(); Integer ui1 = ases[i + 1].getNumber(); Map<Integer, Relationship> edge_ui = ases[i].getEdge(); Map<Integer, Relationship> edge_ui1 = ases[i + 1].getEdge(); if ((transFor_ui1.get(ui) > Constants.L && transFor_ui.get(ui1) > Constants.L) || (transFor_ui.get(ui1) <= Constants.L && transFor_ui.get(ui1) > 0 && transFor_ui1.get(ui) <= Constants.L && transFor_ui1.get(ui) > 0)) { edge_ui.replace(ui1, Relationship.S2S); edge_ui1.replace(ui, Relationship.S2S); } else if (transFor_ui1.get(ui) > Constants.L || transFor_ui.get(ui1) == 0) { edge_ui.replace(ui1, Relationship.P2C); edge_ui1.replace(ui, Relationship.C2P); } else if (transFor_ui.get(ui1) > Constants.L || transFor_ui1.get(ui) == 0) { edge_ui.replace(ui1, Relationship.C2P); edge_ui1.replace(ui, Relationship.P2C); } } } // algorithm 2, annotating p2p relationship // Phase 2 for (Map.Entry<String, ASPath> entry : asPathMap.entrySet()) { ASPath path = entry.getValue(); AS[] ases = path.getAses(); if (ases.length == 1) { continue; } int j = 0; int maxdegree = 0; for (int i = 0; i < ases.length; i++) { if (ases[i].getDegree() > maxdegree) { maxdegree = ases[i].getDegree(); j = i; } } for (int i = 0; i < j - 2; i++) { Map<Integer, Integer> notPeer_ui = ases[i].getNotpeering(); if (notPeer_ui.containsKey(ases[i + 1].getNumber())) { notPeer_ui.replace(ases[i + 1].getNumber(), 1); } else { System.err.println( "AS " + ases[i].getNumber() + " do not contain notpeer of AS " + ases[i + 1].getNumber()); notPeer_ui.put(ases[i + 1].getNumber(), 1); } } for (int i = j + 1; i < ases.length - 1; i++) { Map<Integer, Integer> notPeer_ui = ases[i].getNotpeering(); if (notPeer_ui.containsKey(ases[i + 1].getNumber())) { notPeer_ui.replace(ases[i + 1].getNumber(), 1); } else { System.err.println( "AS " + ases[i].getNumber() + " do not contain notpeer of AS " + ases[i + 1].getNumber()); notPeer_ui.put(ases[i + 1].getNumber(), 1); } } if (j > 0 && j < ases.length - 1) { Map<Integer, Relationship> edgeMap_uj = ases[j].getEdge(); Map<Integer, Relationship> edgeMap_uj_1 = ases[j - 1].getEdge(); if (edgeMap_uj_1.get(ases[j].getNumber()) != Relationship.S2S && edgeMap_uj.get(ases[j + 1].getNumber()) != Relationship.S2S) { if (ases[j - 1].getDegree() > ases[j + 1].getDegree()) { ases[j].getNotpeering().replace(ases[j + 1].getNumber(), 1); } else { ases[j - 1].getNotpeering().replace(ases[j].getNumber(), 1); } } } } // Phase 3 for (Map.Entry<String, ASPath> entry : asPathMap.entrySet()) { ASPath path = entry.getValue(); AS[] ases = path.getAses(); if (ases.length == 1) { continue; } for (int j = 0; j < ases.length - 1; j++) { Map<Integer, Integer> notPeering_uj = ases[j].getNotpeering(); Map<Integer, Integer> notPeering_uj1 = ases[j + 1].getNotpeering(); AS uj = ases[j]; AS uj1 = ases[j + 1]; if (notPeering_uj.get(uj1.getNumber()) != 1 && notPeering_uj1.get(uj.getNumber()) != 1 && (int) (uj.getDegree() / uj1.getDegree()) < Constants.R && (double) ((double) uj.getDegree() / (double) uj1.getDegree()) > (double) ((double) 1 / (double) Constants.R)) { uj.getEdge().replace(uj1.getNumber(), Relationship.P2P); uj1.getEdge().replace(uj.getNumber(), Relationship.P2P); } } } // output int counter = 0; for (Map.Entry<String, ASPath> entry : asPathMap.entrySet()) { ASPath path = entry.getValue(); AS[] ases = path.getAses(); if (ases.length == 1) continue; for (int i = 0; i < ases.length - 1; i++) { switch (ases[i].getEdge().get(ases[i + 1].getNumber())) { case P2P: System.out.println(ases[i].getNumber() + " " + ases[i + 1].getNumber() + " p2p"); break; case C2P: System.out.println(ases[i].getNumber() + " " + ases[i + 1].getNumber() + " c2p"); break; case S2S: System.out.println(ases[i].getNumber() + " " + ases[i + 1].getNumber() + " s2s"); break; case P2C: System.out.println(ases[i].getNumber() + " " + ases[i + 1].getNumber() + " p2c"); break; case UNKOWN: System.out.println(ases[i].getNumber() + " " + ases[i + 1].getNumber() + " unkown"); default: break; } // System.out.println(ases[i+1].getNumber() + " " + ases[i].getNumber() + // ases[i+1].getEdge().get(ases[i].getNumber())); } if (counter > 10) { break; } counter++; } }
/** preprocessDataset Task 1 */ private void preprocessDataset() { BufferedReader br = null; FileOutputStream fos = null; PrintStream ps = null; try { fos = new FileOutputStream(Constants.preprocessFile); ps = new PrintStream(fos); } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } String line = ""; try { br = new BufferedReader(new FileReader(Constants.bgpdata)); while ((line = br.readLine()) != null) { // && index < 5 if (line.contains("{") || line.contains("}")) { // System.out.println(line); } else { String ASString = line.substring(Constants.bpgdataPrefix.length()); String[] ASes = ASString.split(" "); if (ASes.length == 0 || ASString.equals("")) { continue; } List<Integer> asKeysInpath = new ArrayList<Integer>(); for (int i = 0; i < ASes.length; i++) { Integer asKey = Integer.parseInt(ASes[i]); if (asKeysInpath.indexOf(asKey) < 0) { asKeysInpath.add(asKey); } if (!asMap.containsKey(asKey)) { AS newAs = new AS(asKey.intValue()); asMap.put(asKey, newAs); originAsMap.put(asKey, newAs); } } AS[] asesInpath = new AS[asKeysInpath.size()]; for (int i = 0; i < asKeysInpath.size(); i++) { asesInpath[i] = asMap.get(asKeysInpath.get(i)); } ASPath path = new ASPath(asesInpath); // System.out.println("path:"+path.toString()); if (!asPathMap.containsKey(path.toString())) { asPathMap.put(path.toString(), path); ps.println(path.toString()); } } // String[] orderline = line.split(","); } System.out.println("Number of ASes: " + asMap.size()); ps.println("Number of ASes: " + asMap.size()); System.out.println("Number of AS paths:" + asPathMap.size()); ps.println("Number of AS paths:" + asPathMap.size()); // System.out.println("duplicated aspathes:"+index); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } }