blob: 9a465302f790d2574e516121192dba02b73fd514 [file] [log] [blame]
Brian O'Connor7f8e3012014-02-15 23:59:29 -08001package net.onrc.onos.intent.runtime;
2
3import java.util.ArrayList;
4import java.util.Collection;
5import java.util.Collections;
6import java.util.HashMap;
7import java.util.HashSet;
8import java.util.List;
9import java.util.Map;
10import java.util.Set;
11
12import net.floodlightcontroller.util.MACAddress;
13import net.onrc.onos.intent.FlowEntry;
14import net.onrc.onos.intent.Intent;
15import net.onrc.onos.intent.PathIntent;
16import net.onrc.onos.intent.PathIntents;
17import net.onrc.onos.intent.ShortestPathIntent;
18import net.onrc.onos.ofcontroller.networkgraph.Link;
19import net.onrc.onos.ofcontroller.networkgraph.NetworkGraph;
20import net.onrc.onos.ofcontroller.networkgraph.Port;
21import net.onrc.onos.ofcontroller.networkgraph.Switch;
22
23/**
24 *
25 * @author Brian O'Connor <bocon@onlab.us>
26 *
27 */
28
29public class PlanCalcRuntime {
30 NetworkGraph graph;
31 protected PathIntents intents;
32 protected Set<Collection<FlowEntry>> flowEntries;
33 protected List<Set<FlowEntry>> plan;
34
35 public PlanCalcRuntime(NetworkGraph graph) {
36 this.graph = graph;
37 this.flowEntries = new HashSet<>();
38 this.plan = new ArrayList<>();
39 }
40
41 public void addIntents(PathIntents intents) {
42 this.intents = intents;
43 computeFlowEntries();
44 constructPlan();
45 }
46
47 public List<Set<FlowEntry>> getPlan() {
48 return plan;
49 }
50
51 public void computeFlowEntries() {
52 for(PathIntent intent : intents.getIntents()) {
53 Intent parent = intent.getParentIntent();
54 Port srcPort, dstPort, lastDstPort = null;
55 MACAddress srcMac, dstMac;
56 if(parent instanceof ShortestPathIntent) {
57 ShortestPathIntent pathIntent = (ShortestPathIntent) parent;
58 Switch srcSwitch = graph.getSwitch(pathIntent.getSrcSwitchDpid());
59 srcPort = srcSwitch.getPort(pathIntent.getSrcPortNumber());
60 srcMac = MACAddress.valueOf(pathIntent.getSrcMac());
61 dstMac = MACAddress.valueOf(pathIntent.getDstMac());
62 Switch dstSwitch = graph.getSwitch(pathIntent.getDstSwitchDpid());
63 lastDstPort = dstSwitch.getPort(pathIntent.getDstPortNumber());
64 }
65 else {
66 // TODO: log this error
67 continue;
68 }
69 List<FlowEntry> entries = new ArrayList<>();
70 for(Link link : intent.getPath(graph)) {
71 Switch sw = link.getSourceSwitch();
72 dstPort = link.getSourcePort();
73 FlowEntry fe = new FlowEntry(sw, srcPort, dstPort, srcMac, dstMac);
74 entries.add(fe);
75 srcPort = link.getDestinationPort();
76 }
77 if(lastDstPort != null) {
78 Switch sw = lastDstPort.getSwitch();
79 dstPort = lastDstPort;
80 FlowEntry fe = new FlowEntry(sw, srcPort, dstPort, srcMac, dstMac);
81 entries.add(fe);
82 }
83 // install flow entries in reverse order
84 Collections.reverse(entries);
85 flowEntries.add(entries);
86 }
87 }
88
89 public void constructPlan() {
90 Map<FlowEntry, Integer> map = new HashMap<>();
91 for(Collection<FlowEntry> c : flowEntries) {
92 for(FlowEntry e: c) {
93 Integer i = map.get(e);
94 if(i == null) {
95 map.put(e, 1);
96 }
97 else {
98 i += 1;
99 }
100
101 }
102 }
103
104 // really simple first iteration of plan
105 //TODO: optimize the map in phases
106 plan.add(map.keySet());
107 }
108}