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