blob: c24134758e2f549683aa6555c4d33fefbc84354d [file] [log] [blame]
lishuai6c56f5e2015-11-17 16:38:19 +08001package org.onosproject.vtn.util;
2
3import static org.onlab.util.Tools.toHex;
4
5import java.net.URI;
6import java.net.URISyntaxException;
7import java.util.Calendar;
8
9import org.onosproject.core.IdGenerator;
10import org.onosproject.net.DeviceId;
11
12public final class DataPathIdGenerator implements IdGenerator {
13 private static final String SCHEME = "of";
14 private String ipAddress;
15 private String timeStamp;
16
17 private DataPathIdGenerator(Builder builder) {
18 this.ipAddress = builder.ipAddress;
19 Calendar cal = Calendar.getInstance();
20 this.timeStamp = String.valueOf(cal.get(Calendar.SECOND))
21 + String.valueOf(cal.get(Calendar.MILLISECOND));
22 }
23
24 @Override
25 public long getNewId() {
26 String dpid = ipAddress.replace(".", "") + timeStamp;
27 return Long.parseLong(dpid);
28 }
29
30 public String getDpId() {
31 return toHex(getNewId());
32 }
33
34 public DeviceId getDeviceId() {
35 try {
36 URI uri = new URI(SCHEME, toHex(getNewId()), null);
37 return DeviceId.deviceId(uri);
38 } catch (URISyntaxException e) {
39 return null;
40 }
41 }
42
43 /**
44 * Returns a new builder.
45 *
46 * @return new builder
47 */
48 public static Builder builder() {
49 return new Builder();
50 }
51
52 public static final class Builder {
53 private String ipAddress;
54
55 public Builder addIpAddress(String ipAddress) {
56 this.ipAddress = ipAddress;
57 return this;
58 }
59
60 public DataPathIdGenerator build() {
61 return new DataPathIdGenerator(this);
62 }
63 }
64}