Search in sources :

Example 1 with FlowCacheSyncResults

use of org.openkilda.messaging.payload.flow.FlowCacheSyncResults in project open-kilda by telstra.

the class CrudBolt method handleCacheSyncRequest.

private void handleCacheSyncRequest(CommandMessage message, Tuple tuple) throws IOException {
    logger.info("CACHE SYNCE: {}", message);
    // NB: This is going to be a "bulky" operation - get all flows from DB, and synchronize
    // with the cache.
    List<String> droppedFlows = new ArrayList<>();
    List<String> addedFlows = new ArrayList<>();
    List<String> modifiedFlows = new ArrayList<>();
    List<String> unchangedFlows = new ArrayList<>();
    List<FlowInfo> flowInfos = pathComputer.getFlowInfo();
    // Instead of determining left/right .. store based on flowid_& cookie
    HashMap<String, FlowInfo> flowToInfo = new HashMap<>();
    for (FlowInfo fi : flowInfos) {
        flowToInfo.put(fi.getFlowId() + fi.getCookie(), fi);
    }
    // We first look at comparing what is in the DB to what is in the Cache
    for (FlowInfo fi : flowInfos) {
        String flowid = fi.getFlowId();
        if (flowCache.cacheContainsFlow(flowid)) {
            // TODO: better, more holistic comparison
            // TODO: if the flow is modified, then just leverage drop / add primitives.
            // TODO: Ensure that the DB is always the source of truth - cache and db ops part of transaction.
            // Need to compare both sides
            ImmutablePair<Flow, Flow> fc = flowCache.getFlow(flowid);
            int count = modifiedFlows.size();
            if (fi.getCookie() != fc.left.getCookie() && fi.getCookie() != fc.right.getCookie())
                modifiedFlows.add("cookie: " + flowid + ":" + fi.getCookie() + ":" + fc.left.getCookie() + ":" + fc.right.getCookie());
            if (fi.getMeterId() != fc.left.getMeterId() && fi.getMeterId() != fc.right.getMeterId())
                modifiedFlows.add("meter: " + flowid + ":" + fi.getMeterId() + ":" + fc.left.getMeterId() + ":" + fc.right.getMeterId());
            if (fi.getTransitVlanId() != fc.left.getTransitVlan() && fi.getTransitVlanId() != fc.right.getTransitVlan())
                modifiedFlows.add("transit: " + flowid + ":" + fi.getTransitVlanId() + ":" + fc.left.getTransitVlan() + ":" + fc.right.getTransitVlan());
            if (!fi.getSrcSwitchId().equals(fc.left.getSourceSwitch()) && !fi.getSrcSwitchId().equals(fc.right.getSourceSwitch()))
                modifiedFlows.add("switch: " + flowid + "|" + fi.getSrcSwitchId() + "|" + fc.left.getSourceSwitch() + "|" + fc.right.getSourceSwitch());
            if (count == modifiedFlows.size())
                unchangedFlows.add(flowid);
        } else {
            // TODO: need to get the flow from the DB and add it properly
            addedFlows.add(flowid);
        }
    }
    // Now we see if the cache holds things not in the DB
    for (ImmutablePair<Flow, Flow> flow : flowCache.dumpFlows()) {
        String key = flow.left.getFlowId() + flow.left.getCookie();
        // compare the left .. if it is in, then check the right .. o/w remove it (no need to check right
        if (!flowToInfo.containsKey(key)) {
            /* (carmine) - This code is to drop the flow from the cache since it isn't in the DB
 *  But - the user can just as easily call delete in the NB API .. which should do the right thing.
 *  So, for now, just add the flow id.
 */
            // String removedFlow = flowCache.removeFlow(flow.left.getFlowId()).toString();
            // String asJson = MAPPER.writeValueAsString(removedFlow);
            // droppedFlows.add(asJson);
            droppedFlows.add(flow.left.getFlowId());
        } else {
            key = flow.right.getFlowId() + flow.right.getCookie();
            if (!flowToInfo.containsKey(key)) {
                // (carmine) - same comment..
                // String removedFlow = flowCache.removeFlow(flow.left.getFlowId()).toString();
                // String asJson = MAPPER.writeValueAsString(removedFlow);
                // droppedFlows.add(asJson);
                droppedFlows.add(flow.right.getFlowId());
            }
        }
    }
    FlowCacheSyncResults results = new FlowCacheSyncResults(droppedFlows.toArray(new String[0]), addedFlows.toArray(new String[0]), modifiedFlows.toArray(new String[0]), unchangedFlows.toArray(new String[0]));
    Values northbound = new Values(new InfoMessage(new FlowCacheSyncResponse(results), message.getTimestamp(), message.getCorrelationId(), Destination.NORTHBOUND));
    outputCollector.emit(StreamType.RESPONSE.toString(), tuple, northbound);
}
Also used : Values(org.apache.storm.tuple.Values) Flow(org.openkilda.messaging.model.Flow) FlowInfo(org.openkilda.pce.provider.FlowInfo) InfoMessage(org.openkilda.messaging.info.InfoMessage) FlowCacheSyncResults(org.openkilda.messaging.payload.flow.FlowCacheSyncResults)

Aggregations

Values (org.apache.storm.tuple.Values)1 InfoMessage (org.openkilda.messaging.info.InfoMessage)1 Flow (org.openkilda.messaging.model.Flow)1 FlowCacheSyncResults (org.openkilda.messaging.payload.flow.FlowCacheSyncResults)1 FlowInfo (org.openkilda.pce.provider.FlowInfo)1