blob: 515b0bff3eee153348bf80830353b8ba951a0c45 [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;
Nick Karanatsiosf9336002014-02-24 11:26:58 -080011import java.io.IOException;
Nick Karanatsios8abe7172014-02-19 20:31:48 -080012import java.util.concurrent.atomic.AtomicLong;
Nick Karanatsios8abe7172014-02-19 20:31:48 -080013import net.onrc.onos.datagrid.web.IntentResource;
14import net.onrc.onos.datastore.RCTable;
15import net.onrc.onos.intent.IntentOperationList;
16import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphService;
17import net.onrc.onos.ofcontroller.networkgraph.NetworkGraph;
18import 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 Karanatsiosf9336002014-02-24 11:26:58 -080029 private long range = 10000L;
Nick Karanatsios8abe7172014-02-19 20:31:48 -080030 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 Karanatsios8abe7172014-02-19 20:31:48 -080076 kryo.writeObject(output, operations);
77 output.close();
78 byte[] buffer = stream.toByteArray();
79 table.create(String.valueOf(key).getBytes(), buffer);
Nick Karanatsiosf9336002014-02-24 11:26:58 -080080 log.debug("key is {} value length is {}", key, buffer.length);
81 stream.reset();
82 stream.close();
Nick Karanatsiosa1bad352014-02-22 14:16:34 -080083 log.debug("persist operations to ramcloud size of operations: {}", operations.size());
Nick Karanatsiosf9336002014-02-24 11:26:58 -080084 if (buffer.length > 921600 ) log.error("oversize key {} value length is {}", key, buffer.length);
Nick Karanatsios8abe7172014-02-19 20:31:48 -080085 ret = true;
86 } catch (JRamCloud.ObjectExistsException ex) {
87 log.warn("Failed to store intent journal with key " + key);
Nick Karanatsiosf9336002014-02-24 11:26:58 -080088 } catch (IOException ex) {
89 log.error("Failed to close the stream");
Nick Karanatsios8abe7172014-02-19 20:31:48 -080090 }
91 }
92 return ret;
93 }
94}