Pavlin Radoslavov | 1366905 | 2014-05-13 10:33:39 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.intent.runtime.web; |
| 2 | |
| 3 | import java.io.IOException; |
| 4 | import java.util.Collection; |
| 5 | |
| 6 | import net.floodlightcontroller.util.MACAddress; |
| 7 | import net.onrc.onos.api.intent.ApplicationIntent; |
| 8 | import net.onrc.onos.core.intent.ConstrainedShortestPathIntent; |
| 9 | import net.onrc.onos.core.intent.Intent; |
| 10 | import net.onrc.onos.core.intent.IntentMap; |
| 11 | import net.onrc.onos.core.intent.IntentOperation; |
| 12 | import net.onrc.onos.core.intent.IntentOperationList; |
| 13 | import net.onrc.onos.core.intent.ShortestPathIntent; |
| 14 | import net.onrc.onos.core.intent.runtime.IPathCalcRuntimeService; |
| 15 | import net.onrc.onos.core.util.Dpid; |
| 16 | |
| 17 | import org.codehaus.jackson.JsonParseException; |
| 18 | import org.codehaus.jackson.map.JsonMappingException; |
| 19 | import org.codehaus.jackson.map.ObjectMapper; |
| 20 | import org.restlet.resource.Delete; |
| 21 | import org.restlet.resource.Get; |
| 22 | import org.restlet.resource.Post; |
| 23 | import org.restlet.resource.ServerResource; |
| 24 | import org.slf4j.Logger; |
| 25 | import org.slf4j.LoggerFactory; |
| 26 | |
| 27 | /** |
| 28 | * A class to access the high-level intents. |
| 29 | */ |
| 30 | public class IntentHighResource extends ServerResource { |
| 31 | private static final Logger log = LoggerFactory.getLogger(IntentHighResource.class); |
| 32 | // TODO need to assign proper application id. |
| 33 | private static final String APPLN_ID = "1"; |
| 34 | |
| 35 | /** |
| 36 | * Gets all high-level intents. |
| 37 | * |
| 38 | * @return a collection with all high-level intents. |
| 39 | */ |
| 40 | @Get("json") |
| 41 | public Collection<Intent> retrieve() throws IOException { |
| 42 | IPathCalcRuntimeService pathRuntime = (IPathCalcRuntimeService) getContext(). |
| 43 | getAttributes().get(IPathCalcRuntimeService.class.getCanonicalName()); |
| 44 | |
| 45 | IntentMap intentMap = pathRuntime.getHighLevelIntents(); |
| 46 | Collection<Intent> intents = intentMap.getAllIntents(); |
| 47 | |
| 48 | return intents; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Adds a collection of high-level intents. |
| 53 | * |
| 54 | * @return the status of the operation (TBD). |
| 55 | */ |
| 56 | @Post("json") |
| 57 | public String store(String jsonIntent) throws IOException { |
| 58 | IPathCalcRuntimeService pathRuntime = (IPathCalcRuntimeService) getContext() |
| 59 | .getAttributes().get(IPathCalcRuntimeService.class.getCanonicalName()); |
| 60 | if (pathRuntime == null) { |
| 61 | log.warn("Failed to get path calc runtime"); |
| 62 | return ""; |
| 63 | } |
| 64 | |
| 65 | String reply = ""; |
| 66 | ObjectMapper mapper = new ObjectMapper(); |
| 67 | ApplicationIntent[] addOperations = null; |
| 68 | try { |
| 69 | addOperations = mapper.readValue(jsonIntent, ApplicationIntent[].class); |
| 70 | } catch (JsonParseException ex) { |
| 71 | log.error("JsonParseException occurred", ex); |
| 72 | } catch (JsonMappingException ex) { |
| 73 | log.error("JsonMappingException occurred", ex); |
| 74 | } catch (IOException ex) { |
| 75 | log.error("IOException occurred", ex); |
| 76 | } |
| 77 | if (addOperations == null) { |
| 78 | return ""; |
| 79 | } |
| 80 | |
| 81 | // |
| 82 | // Process all intents one-by-one |
| 83 | // |
| 84 | // TODO: The Intent Type should be enum instead of a string, |
| 85 | // and we should use a switch statement below to process the |
| 86 | // different type of intents. |
| 87 | // |
| 88 | IntentOperationList intentOperations = new IntentOperationList(); |
| 89 | for (ApplicationIntent oper : addOperations) { |
| 90 | String applnIntentId = APPLN_ID + ":" + oper.intentId(); |
| 91 | |
| 92 | IntentOperation.Operator operator = IntentOperation.Operator.ADD; |
| 93 | Dpid srcSwitchDpid = new Dpid(oper.srcSwitchDpid()); |
| 94 | Dpid dstSwitchDpid = new Dpid(oper.dstSwitchDpid()); |
| 95 | |
| 96 | if (oper.intentType().equals("SHORTEST_PATH")) { |
| 97 | // |
| 98 | // Process Shortest-Path Intent |
| 99 | // |
| 100 | ShortestPathIntent spi = |
| 101 | new ShortestPathIntent(applnIntentId, |
| 102 | srcSwitchDpid.value(), |
| 103 | oper.srcSwitchPort(), |
| 104 | MACAddress.valueOf(oper.matchSrcMac()).toLong(), |
| 105 | dstSwitchDpid.value(), |
| 106 | oper.dstSwitchPort(), |
| 107 | MACAddress.valueOf(oper.matchDstMac()).toLong()); |
| 108 | spi.setPathFrozen(oper.isStaticPath()); |
| 109 | intentOperations.add(operator, spi); |
| 110 | } else { |
| 111 | // |
| 112 | // Process Constrained Shortest-Path Intent |
| 113 | // |
| 114 | ConstrainedShortestPathIntent cspi = |
| 115 | new ConstrainedShortestPathIntent(applnIntentId, |
| 116 | srcSwitchDpid.value(), |
| 117 | oper.srcSwitchPort(), |
| 118 | MACAddress.valueOf(oper.matchSrcMac()).toLong(), |
| 119 | dstSwitchDpid.value(), |
| 120 | oper.dstSwitchPort(), |
| 121 | MACAddress.valueOf(oper.matchDstMac()).toLong(), |
| 122 | oper.bandwidth()); |
| 123 | cspi.setPathFrozen(oper.isStaticPath()); |
| 124 | intentOperations.add(operator, cspi); |
| 125 | } |
| 126 | } |
| 127 | // Apply the Intent Operations |
| 128 | pathRuntime.executeIntentOperations(intentOperations); |
| 129 | |
| 130 | return reply; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Deletes all high-level intents. |
| 135 | * |
| 136 | * @return the status of the operation (TBD). |
| 137 | */ |
| 138 | @Delete("json") |
| 139 | public String store() { |
| 140 | IPathCalcRuntimeService pathRuntime = (IPathCalcRuntimeService) getContext(). |
| 141 | getAttributes().get(IPathCalcRuntimeService.class.getCanonicalName()); |
| 142 | |
| 143 | // Delete all intents |
| 144 | // TODO: The implementation below is broken - waiting for the Java API |
| 145 | // TODO: The deletion should use synchronous Java API? |
| 146 | pathRuntime.purgeIntents(); |
| 147 | return ""; // TODO no reply yet from the purge intents call |
| 148 | } |
| 149 | } |