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