Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 1 | /* |
| 2 | * To change this template, choose Tools | Templates |
| 3 | * and open the template in the editor. |
| 4 | */ |
| 5 | package net.onrc.onos.intent.persist; |
| 6 | |
| 7 | import com.esotericsoftware.kryo.Kryo; |
| 8 | import com.esotericsoftware.kryo.Serializer; |
| 9 | import com.esotericsoftware.kryo.io.Input; |
| 10 | import com.esotericsoftware.kryo.io.Output; |
| 11 | import edu.stanford.ramcloud.JRamCloud; |
| 12 | import java.io.ByteArrayOutputStream; |
| 13 | import java.util.concurrent.atomic.AtomicLong; |
| 14 | import java.util.logging.Level; |
| 15 | import java.util.logging.Logger; |
| 16 | import net.onrc.onos.datagrid.web.IntentResource; |
| 17 | import net.onrc.onos.datastore.RCTable; |
| 18 | import net.onrc.onos.intent.IntentOperationList; |
| 19 | import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphService; |
| 20 | import net.onrc.onos.ofcontroller.networkgraph.NetworkGraph; |
| 21 | import net.onrc.onos.registry.controller.IControllerRegistryService; |
| 22 | import net.onrc.onos.registry.controller.IdBlock; |
| 23 | import org.slf4j.LoggerFactory; |
| 24 | |
| 25 | /** |
| 26 | * |
| 27 | * @author nickkaranatsios |
| 28 | */ |
| 29 | public class PersistIntent { |
| 30 | private final static org.slf4j.Logger log = LoggerFactory.getLogger(IntentResource.class); |
| 31 | private final static long range = 10000; |
| 32 | private final IControllerRegistryService controllerRegistry; |
| 33 | NetworkGraph graph = null; |
| 34 | private final String intentJournal = "G:IntentJournal"; |
| 35 | private RCTable table; |
| 36 | private Kryo kryo = new Kryo(); |
| 37 | private ByteArrayOutputStream stream; |
| 38 | private Output output = null; |
| 39 | private AtomicLong nextId = null; |
| 40 | private long rangeEnd; |
| 41 | private IdBlock idBlock = null; |
| 42 | |
| 43 | |
| 44 | public PersistIntent(final IControllerRegistryService controllerRegistry, INetworkGraphService ng) { |
| 45 | this.controllerRegistry = controllerRegistry; |
| 46 | this.graph = ng.getNetworkGraph(); |
| 47 | table = RCTable.getTable(intentJournal); |
| 48 | stream = new ByteArrayOutputStream(); |
| 49 | output = new Output(stream); |
| 50 | } |
| 51 | |
| 52 | public long getKey() { |
| 53 | long key; |
| 54 | if (idBlock == null) { |
| 55 | key = getNextBlock(); |
| 56 | } else { |
| 57 | key = nextId.incrementAndGet(); |
| 58 | if (key >= rangeEnd) { |
| 59 | key = getNextBlock(); |
| 60 | } |
| 61 | } |
| 62 | return key; |
| 63 | } |
| 64 | |
| 65 | private long getNextBlock() { |
| 66 | idBlock = controllerRegistry.allocateUniqueIdBlock(range); |
| 67 | nextId = new AtomicLong(idBlock.getStart()); |
| 68 | rangeEnd = idBlock.getEnd(); |
| 69 | return nextId.get(); |
| 70 | } |
| 71 | |
| 72 | public boolean persistIfLeader(long key, IntentOperationList operations) { |
| 73 | boolean leader = true; |
| 74 | boolean ret = false; |
| 75 | // TODO call controllerRegistry.isClusterLeader() |
| 76 | if (leader) { |
| 77 | try { |
| 78 | System.out.println("persist operations to ramcloud"); |
| 79 | kryo.writeObject(output, operations); |
| 80 | output.close(); |
| 81 | byte[] buffer = stream.toByteArray(); |
| 82 | table.create(String.valueOf(key).getBytes(), buffer); |
| 83 | System.out.println("key is " + key); |
| 84 | ret = true; |
| 85 | } catch (JRamCloud.ObjectExistsException ex) { |
| 86 | log.warn("Failed to store intent journal with key " + key); |
| 87 | } |
| 88 | } |
| 89 | return ret; |
| 90 | } |
| 91 | } |