blob: 536366725b097a57dbf79c2dae749900a38f61bc [file] [log] [blame]
Thomas Vachuska1eff3a62016-05-03 01:07:24 -07001/*
2 * Copyright 2016 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onlab.warden;
18
19import static com.google.common.base.Preconditions.checkState;
20
21/**
22 * Cell reservation record.
23 */
24final class Reservation {
25
26 final String cellName;
27 final String userName;
28 final long time;
29 final int duration;
Thomas Vachuska5420ba32016-05-13 14:45:25 -040030 final String cellSpec;
Thomas Vachuska1eff3a62016-05-03 01:07:24 -070031
32 // Creates a new reservation record
Thomas Vachuska5420ba32016-05-13 14:45:25 -040033 Reservation(String cellName, String userName, long time, int duration, String cellSpec) {
Thomas Vachuska1eff3a62016-05-03 01:07:24 -070034 this.cellName = cellName;
35 this.userName = userName;
36 this.time = time;
37 this.duration = duration;
Thomas Vachuska5420ba32016-05-13 14:45:25 -040038 this.cellSpec = cellSpec;
Thomas Vachuska1eff3a62016-05-03 01:07:24 -070039 }
40
41 /**
42 * Decodes reservation record from the specified line.
43 *
44 * @param line string line
45 */
46 Reservation(String line) {
47 String[] fields = line.trim().split("\t");
Thomas Vachuska5420ba32016-05-13 14:45:25 -040048 checkState(fields.length == 5, "Incorrect reservation encoding");
Thomas Vachuska1eff3a62016-05-03 01:07:24 -070049 this.cellName = fields[0];
50 this.userName = fields[1];
51 this.time = Long.parseLong(fields[2]);
52 this.duration = Integer.parseInt(fields[3]);
Thomas Vachuska5420ba32016-05-13 14:45:25 -040053 this.cellSpec = fields[4];
Thomas Vachuska1eff3a62016-05-03 01:07:24 -070054 }
55
56 /**
57 * Encodes reservation record into a string line.
58 *
59 * @return encoded string
60 */
61 String encode() {
Thomas Vachuska5420ba32016-05-13 14:45:25 -040062 return String.format("%s\t%s\t%s\t%s\t%s\n", cellName, userName, time, duration, cellSpec);
Thomas Vachuska1eff3a62016-05-03 01:07:24 -070063 }
64
65}