blob: 691a656d601aedb94bf15f0fc7dc5db286bfe485 [file] [log] [blame]
Nick Karanatsios8abe7172014-02-19 20:31:48 -08001/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5package net.onrc.onos.intent.persist;
6
7import com.esotericsoftware.kryo.Kryo;
Nick Karanatsios8abe7172014-02-19 20:31:48 -08008import com.esotericsoftware.kryo.io.Output;
9import edu.stanford.ramcloud.JRamCloud;
10import java.io.ByteArrayOutputStream;
11import java.util.concurrent.atomic.AtomicLong;
Nick Karanatsios8abe7172014-02-19 20:31:48 -080012import net.onrc.onos.datagrid.web.IntentResource;
13import net.onrc.onos.datastore.RCTable;
14import net.onrc.onos.intent.IntentOperationList;
15import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphService;
16import net.onrc.onos.ofcontroller.networkgraph.NetworkGraph;
Nick Karanatsiosed645df2014-02-20 23:22:29 -080017import net.onrc.onos.ofcontroller.util.serializers.KryoFactory;
Nick Karanatsios8abe7172014-02-19 20:31:48 -080018import net.onrc.onos.registry.controller.IControllerRegistryService;
19import net.onrc.onos.registry.controller.IdBlock;
Nick Karanatsiosed645df2014-02-20 23:22:29 -080020import org.slf4j.Logger;
Nick Karanatsios8abe7172014-02-19 20:31:48 -080021import org.slf4j.LoggerFactory;
22
23/**
24 *
25 * @author nickkaranatsios
26 */
27public class PersistIntent {
Nick Karanatsiosed645df2014-02-20 23:22:29 -080028 private final static Logger log = LoggerFactory.getLogger(IntentResource.class);
Nick Karanatsios8abe7172014-02-19 20:31:48 -080029 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 Karanatsiosed645df2014-02-20 23:22:29 -080046 stream = new ByteArrayOutputStream(1024);
Nick Karanatsios8abe7172014-02-19 20:31:48 -080047 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 Karanatsiosed645df2014-02-20 23:22:29 -080076 System.out.println("persist operations to ramcloud size of operations: " + operations.size());
Nick Karanatsios8abe7172014-02-19 20:31:48 -080077 kryo.writeObject(output, operations);
78 output.close();
79 byte[] buffer = stream.toByteArray();
80 table.create(String.valueOf(key).getBytes(), buffer);
Nick Karanatsiosed645df2014-02-20 23:22:29 -080081 System.out.println("key is " + key + " value length is " + buffer.length);
Nick Karanatsios8abe7172014-02-19 20:31:48 -080082 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}