blob: 4582021c2c5238bf3b8f0e124386fd5a3986c43d [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;
Toshio Koide103ccb22014-02-18 21:51:25 -080015import net.onrc.onos.intent.IntentOperationList;
Brian O'Connor7f8e3012014-02-15 23:59:29 -080016import net.onrc.onos.intent.PathIntent;
Toshio Koide4f308732014-02-18 15:19:48 -080017import net.onrc.onos.intent.PathIntentMap;
Brian O'Connor7f8e3012014-02-15 23:59:29 -080018import net.onrc.onos.intent.ShortestPathIntent;
19import net.onrc.onos.ofcontroller.networkgraph.Link;
20import net.onrc.onos.ofcontroller.networkgraph.NetworkGraph;
21import net.onrc.onos.ofcontroller.networkgraph.Port;
22import net.onrc.onos.ofcontroller.networkgraph.Switch;
23
24/**
25 *
26 * @author Brian O'Connor <bocon@onlab.us>
27 *
28 */
29
30public class PlanCalcRuntime {
31 NetworkGraph graph;
Toshio Koide4f308732014-02-18 15:19:48 -080032 protected PathIntentMap intents;
Brian O'Connor7f8e3012014-02-15 23:59:29 -080033 protected Set<Collection<FlowEntry>> flowEntries;
34 protected List<Set<FlowEntry>> plan;
35
36 public PlanCalcRuntime(NetworkGraph graph) {
37 this.graph = graph;
38 this.flowEntries = new HashSet<>();
39 this.plan = new ArrayList<>();
Toshio Koide103ccb22014-02-18 21:51:25 -080040 this.intents = new PathIntentMap(this.graph);
Brian O'Connor7f8e3012014-02-15 23:59:29 -080041 }
42
Toshio Koide103ccb22014-02-18 21:51:25 -080043 public void addIntents(IntentOperationList intentOpList) {
44 intents.executeOperations(intentOpList);
Brian O'Connor7f8e3012014-02-15 23:59:29 -080045 computeFlowEntries();
46 constructPlan();
47 }
48
49 public List<Set<FlowEntry>> getPlan() {
50 return plan;
51 }
52
53 public void computeFlowEntries() {
Toshio Koide4f308732014-02-18 15:19:48 -080054 for(Intent i : intents.getAllIntents()) {
55 PathIntent intent = (PathIntent)i;
Brian O'Connor7f8e3012014-02-15 23:59:29 -080056 Intent parent = intent.getParentIntent();
57 Port srcPort, dstPort, lastDstPort = null;
58 MACAddress srcMac, dstMac;
59 if(parent instanceof ShortestPathIntent) {
60 ShortestPathIntent pathIntent = (ShortestPathIntent) parent;
61 Switch srcSwitch = graph.getSwitch(pathIntent.getSrcSwitchDpid());
62 srcPort = srcSwitch.getPort(pathIntent.getSrcPortNumber());
63 srcMac = MACAddress.valueOf(pathIntent.getSrcMac());
64 dstMac = MACAddress.valueOf(pathIntent.getDstMac());
65 Switch dstSwitch = graph.getSwitch(pathIntent.getDstSwitchDpid());
66 lastDstPort = dstSwitch.getPort(pathIntent.getDstPortNumber());
67 }
68 else {
69 // TODO: log this error
70 continue;
71 }
72 List<FlowEntry> entries = new ArrayList<>();
73 for(Link link : intent.getPath(graph)) {
74 Switch sw = link.getSourceSwitch();
75 dstPort = link.getSourcePort();
76 FlowEntry fe = new FlowEntry(sw, srcPort, dstPort, srcMac, dstMac);
77 entries.add(fe);
78 srcPort = link.getDestinationPort();
79 }
80 if(lastDstPort != null) {
81 Switch sw = lastDstPort.getSwitch();
82 dstPort = lastDstPort;
83 FlowEntry fe = new FlowEntry(sw, srcPort, dstPort, srcMac, dstMac);
84 entries.add(fe);
85 }
86 // install flow entries in reverse order
87 Collections.reverse(entries);
88 flowEntries.add(entries);
89 }
90 }
91
92 public void constructPlan() {
93 Map<FlowEntry, Integer> map = new HashMap<>();
94 for(Collection<FlowEntry> c : flowEntries) {
95 for(FlowEntry e: c) {
96 Integer i = map.get(e);
97 if(i == null) {
98 map.put(e, 1);
99 }
100 else {
101 i += 1;
102 }
103
104 }
105 }
106
107 // really simple first iteration of plan
108 //TODO: optimize the map in phases
109 plan.add(map.keySet());
110 }
111}