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