blob: deb6871de4976ca2b26f58e06da2e75eac69ff5c [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;
17import net.onrc.onos.registry.controller.IControllerRegistryService;
18import net.onrc.onos.registry.controller.IdBlock;
Nick Karanatsiosed645df2014-02-20 23:22:29 -080019import org.slf4j.Logger;
Nick Karanatsios8abe7172014-02-19 20:31:48 -080020import org.slf4j.LoggerFactory;
21
22/**
23 *
24 * @author nickkaranatsios
25 */
26public class PersistIntent {
Nick Karanatsiosed645df2014-02-20 23:22:29 -080027 private final static Logger log = LoggerFactory.getLogger(IntentResource.class);
Nick Karanatsios8abe7172014-02-19 20:31:48 -080028 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 Karanatsiosed645df2014-02-20 23:22:29 -080045 stream = new ByteArrayOutputStream(1024);
Nick Karanatsios8abe7172014-02-19 20:31:48 -080046 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 Karanatsios8abe7172014-02-19 20:31:48 -080075 kryo.writeObject(output, operations);
76 output.close();
77 byte[] buffer = stream.toByteArray();
78 table.create(String.valueOf(key).getBytes(), buffer);
Nick Karanatsiosa1bad352014-02-22 14:16:34 -080079 log.debug("persist operations to ramcloud size of operations: {}", operations.size());
80 log.debug("key is {} ", key, " value length is {}", buffer.length);
Nick Karanatsios8abe7172014-02-19 20:31:48 -080081 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}