+--------------------------------------------------------------------------------
+-- Power control units (PCUs)
+--------------------------------------------------------------------------------
+
+CREATE TABLE pcus (
+ -- Mandatory
+ pcu_id serial PRIMARY KEY, -- PCU identifier
+ site_id integer REFERENCES sites NOT NULL, -- Site identifier
+ hostname text, -- Hostname, not necessarily unique (multiple logical sites could use the same PCU)
+ ip text NOT NULL, -- IP, not necessarily unique
+
+ -- Optional
+ protocol text, -- Protocol, e.g. ssh or https or telnet
+ username text, -- Username, if applicable
+ "password" text, -- Password, if applicable
+ model text, -- Model, e.g. BayTech or iPal
+ notes text -- Random notes
+) WITH OIDS;
+CREATE INDEX pcus_site_id_idx ON pcus (site_id);
+
+CREATE VIEW site_pcus AS
+SELECT site_id,
+array_to_string(array_accum(pcu_id), ',') AS pcu_ids
+FROM pcus
+GROUP BY site_id;
+
+CREATE TABLE pcu_node (
+ pcu_id integer REFERENCES pcus NOT NULL, -- PCU identifier
+ node_id integer REFERENCES nodes NOT NULL, -- Node identifier
+ port integer NOT NULL, -- Port number
+ PRIMARY KEY (pcu_id, node_id), -- The same node cannot be controlled by different ports
+ UNIQUE (pcu_id, port) -- The same port cannot control multiple nodes
+);
+CREATE INDEX pcu_node_pcu_id_idx ON pcu_node (pcu_id);
+CREATE INDEX pcu_node_node_id_idx ON pcu_node (node_id);
+
+CREATE VIEW node_pcus AS
+SELECT node_id,
+array_to_string(array_accum(pcu_id), ',') AS pcu_ids,
+array_to_string(array_accum(port), ',') AS ports
+FROM pcu_node
+GROUP BY node_id;
+
+CREATE VIEW pcu_nodes AS
+SELECT pcu_id,
+array_to_string(array_accum(node_id), ',') AS node_ids,
+array_to_string(array_accum(port), ',') AS ports
+FROM pcu_node
+GROUP BY pcu_id;
+
+--------------------------------------------------------------------------------
+-- Slices
+--------------------------------------------------------------------------------
+
+CREATE TABLE slice_instantiations (
+ instantiation text PRIMARY KEY
+) WITH OIDS;
+INSERT INTO slice_instantiations (instantiation) VALUES ('not-instantiated'); -- Placeholder slice
+INSERT INTO slice_instantiations (instantiation) VALUES ('plc-instantiated'); -- Instantiated by Node Manager
+INSERT INTO slice_instantiations (instantiation) VALUES ('delegated'); -- Manually instantiated
+
+-- Slices
+CREATE TABLE slices (
+ slice_id serial PRIMARY KEY, -- Slice identifier
+ site_id integer REFERENCES sites NOT NULL, -- Site identifier
+ name text NOT NULL, -- Slice name
+ instantiation text REFERENCES slice_instantiations NOT NULL DEFAULT 'plc-instantiated', -- Slice state, e.g. plc-instantiated
+ url text, -- Project URL
+ description text, -- Project description
+
+ max_nodes integer NOT NULL DEFAULT 100, -- Maximum number of nodes that can be assigned to this slice
+
+ creator_person_id integer REFERENCES persons NOT NULL, -- Creator
+ created timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP, -- Creation date
+ expires timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP + '2 weeks', -- Expiration date
+
+ is_deleted boolean NOT NULL DEFAULT false
+) WITH OIDS;
+CREATE INDEX slices_site_id_idx ON slices (site_id) WHERE is_deleted IS false;
+CREATE INDEX slices_name_idx ON slices (name) WHERE is_deleted IS false;
+
+-- Slivers
+CREATE TABLE slice_node (
+ slice_id integer REFERENCES slices NOT NULL, -- Slice identifier
+ node_id integer REFERENCES nodes NOT NULL, -- Node identifier
+ PRIMARY KEY (slice_id, node_id)
+) WITH OIDS;
+CREATE INDEX slice_node_slice_id_idx ON slice_node (slice_id);
+CREATE INDEX slice_node_node_id_idx ON slice_node (node_id);
+
+-- Synonym for slice_node
+CREATE VIEW slivers AS
+SELECT * FROM slice_node;
+
+-- Nodes in each slice
+CREATE VIEW slice_nodes AS
+SELECT slice_id,
+array_to_string(array_accum(node_id), ',') AS node_ids
+FROM slice_node
+GROUP BY slice_id;
+
+-- Slices on each node
+CREATE VIEW node_slices AS
+SELECT node_id,
+array_to_string(array_accum(slice_id), ',') AS slice_ids
+FROM slice_node
+GROUP BY node_id;
+
+-- Slices at each site
+CREATE VIEW site_slices AS
+SELECT site_id,
+array_to_string(array_accum(slice_id), ',') AS slice_ids
+FROM slices
+GROUP BY site_id;
+
+-- Slice membership
+CREATE TABLE slice_person (
+ slice_id integer REFERENCES slices NOT NULL, -- Slice identifier
+ person_id integer REFERENCES persons NOT NULL, -- Account identifier
+ PRIMARY KEY (slice_id, person_id)
+) WITH OIDS;
+CREATE INDEX slice_person_slice_id_idx ON slice_person (slice_id);
+CREATE INDEX slice_person_person_id_idx ON slice_person (person_id);
+
+-- Members of the slice
+CREATE VIEW slice_persons AS
+SELECT slice_id,
+array_to_string(array_accum(person_id), ',') AS person_ids
+FROM slice_person
+GROUP BY slice_id;
+
+-- Slices of which each person is a member
+CREATE VIEW person_slices AS
+SELECT person_id,
+array_to_string(array_accum(slice_id), ',') AS slice_ids
+FROM slice_person
+GROUP BY person_id;
+
+--------------------------------------------------------------------------------
+-- Slice attributes
+--------------------------------------------------------------------------------
+
+-- Slice attribute types
+CREATE TABLE slice_attribute_types (
+ attribute_type_id serial PRIMARY KEY, -- Attribute type identifier
+ name text UNIQUE NOT NULL, -- Attribute name
+ description text, -- Attribute description
+ min_role_id integer REFERENCES roles DEFAULT 10 -- If set, minimum (least powerful) role that can set or change this attribute
+) WITH OIDS;
+
+-- Slice/sliver attributes
+CREATE TABLE slice_attribute (
+ slice_attribute_id serial PRIMARY KEY, -- Slice attribute identifier
+ slice_id integer REFERENCES slices NOT NULL, -- Slice identifier
+ node_id integer REFERENCES nodes, -- Sliver attribute if set
+ attribute_type_id integer REFERENCES slice_attribute_types NOT NULL, -- Attribute type identifier
+ value text
+) WITH OIDS;
+CREATE INDEX slice_attribute_slice_id_idx ON slice_attribute (slice_id);
+CREATE INDEX slice_attribute_node_id_idx ON slice_attribute (node_id);
+
+CREATE VIEW slice_attributes AS
+SELECT slice_id,
+array_to_string(array_accum(slice_attribute_id), ',') AS slice_attribute_ids
+FROM slice_attribute
+GROUP BY slice_id;
+
+--------------------------------------------------------------------------------
+-- Authenticated sessions
+--------------------------------------------------------------------------------
+
+-- Authenticated sessions
+CREATE TABLE sessions (
+ session_id text PRIMARY KEY, -- Session identifier
+ expires timestamp without time zone
+) WITH OIDS;
+
+-- People can have multiple sessions
+CREATE TABLE person_session (
+ person_id integer REFERENCES persons NOT NULL, -- Account identifier
+ session_id text REFERENCES sessions NOT NULL, -- Session identifier
+ PRIMARY KEY (person_id, session_id),
+ UNIQUE (session_id) -- Sessions are unique
+) WITH OIDS;
+CREATE INDEX person_session_person_id_idx ON person_session (person_id);
+
+-- Nodes can have only one session
+CREATE TABLE node_session (
+ node_id integer REFERENCES nodes NOT NULL, -- Node identifier
+ session_id text REFERENCES sessions NOT NULL, -- Session identifier
+ UNIQUE (node_id), -- Nodes can have only one session
+ UNIQUE (session_id) -- Sessions are unique
+) WITH OIDS;
+
+--------------------------------------------------------------------------------
+-- Message templates
+--------------------------------------------------------------------------------
+
+CREATE TABLE messages (
+ message_id text PRIMARY KEY, -- Message name
+ template text, -- Message template
+ enabled bool NOT NULL DEFAULT true -- Whether message is enabled
+) WITH OIDS;
+
+--------------------------------------------------------------------------------
+-- Events
+--------------------------------------------------------------------------------
+
+-- Event types
+CREATE TABLE event_types (
+ event_type text PRIMARY KEY -- Event type
+) WITH OIDS;
+INSERT INTO event_types (event_type) VALUES ('Add');
+INSERT INTO event_types (event_type) VALUES ('AddTo');
+INSERT INTO event_types (event_type) VALUES ('Get');
+INSERT INTO event_types (event_type) VALUES ('Update');
+INSERT INTO event_types (event_type) VALUES ('Delete');
+INSERT INTO event_types (event_type) VALUES ('DeleteFrom');
+INSERT INTO event_types (event_type) VALUES ('Unknown');
+
+-- Object types
+CREATE TABLE object_types (
+ object_type text PRIMARY KEY -- Object type
+) WITH OIDS;
+INSERT INTO object_types (object_type) VALUES ('AddressType');
+INSERT INTO object_types (object_type) VALUES ('Address');
+INSERT INTO object_types (object_type) VALUES ('BootState');
+INSERT INTO object_types (object_type) VALUES ('ConfFile');
+INSERT INTO object_types (object_type) VALUES ('KeyType');
+INSERT INTO object_types (object_type) VALUES ('Key');
+INSERT INTO object_types (object_type) VALUES ('NetworkMethod');
+INSERT INTO object_types (object_type) VALUES ('NetworkType');
+INSERT INTO object_types (object_type) VALUES ('Network');
+INSERT INTO object_types (object_type) VALUES ('NodeGroup');
+INSERT INTO object_types (object_type) VALUES ('NodeNetwork');
+INSERT INTO object_types (object_type) VALUES ('Node');
+INSERT INTO object_types (object_type) VALUES ('PCU');
+INSERT INTO object_types (object_type) VALUES ('Person');
+INSERT INTO object_types (object_type) VALUES ('Role');
+INSERT INTO object_types (object_type) VALUES ('Session');
+INSERT INTO object_types (object_type) VALUES ('Site');
+INSERT INTO object_types (object_type) VALUES ('SliceAttributeType');
+INSERT INTO object_types (object_type) VALUES ('SliceAttribute');
+INSERT INTO object_types (object_type) VALUES ('Slice');
+INSERT INTO object_types (object_type) VALUES ('Unknown');
+
+-- Events
+CREATE TABLE events (
+ event_id serial PRIMARY KEY, -- Event identifier
+ person_id integer REFERENCES persons, -- Person responsible for event, if any
+ node_id integer REFERENCES nodes, -- Node responsible for event, if any
+ event_type text REFERENCES event_types NOT NULL DEFAULT 'Unknown', -- Event type
+ object_type text REFERENCES object_types NOT NULL DEFAULT 'Unknown', -- Object type associated with event
+ fault_code integer NOT NULL DEFAULT 0, -- Did this event result in error
+ call text NOT NULL, -- Call responsible for this event
+ runtime float, -- Event run time
+ time timestamp without time zone NOT NULL DEFAULT CURRENT_TIMESTAMP -- Event timestamp
+) WITH OIDS;
+
+-- Event objects
+CREATE TABLE event_object (
+ event_id integer REFERENCES events NOT NULL, -- Event identifier
+ object_id integer NOT NULL -- Object identifier
+) WITH OIDS;
+CREATE INDEX event_object_event_id_idx ON event_object (event_id);
+CREATE INDEX event_object_object_id_idx ON event_object (object_id);
+
+CREATE VIEW event_objects AS
+SELECT event_id,
+array_to_string(array_accum(object_id), ',') AS object_ids
+FROM event_object
+GROUP BY event_id;
+