blob: 5dd0dbf49fc6f9c59fbd1fd2d3084f14e26671ea [file] [log] [blame]
Andrea Campanella59b549d2017-04-14 21:58:16 +02001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Andrea Campanella59b549d2017-04-14 21:58:16 +02003 *
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
Yuta HIGUCHIc6358352017-06-23 11:56:27 -070017package org.onosproject.tl1.device;
Andrea Campanella59b549d2017-04-14 21:58:16 +020018
19import com.google.common.annotations.Beta;
20import org.apache.commons.lang3.tuple.Pair;
21import org.onlab.packet.IpAddress;
22import org.onosproject.net.DeviceId;
23import org.onosproject.net.config.Config;
24
25/**
26 * Configuration to push devices to the TL1 provider.
27 */
28@Beta
29public class Tl1DeviceConfig extends Config<DeviceId> {
30
Yuta HIGUCHIc6358352017-06-23 11:56:27 -070031 /**
32 * TL1 provider scheme and ConfigKey.
33 */
34 public static final String TL1 = "tl1";
35
Andrea Campanella59b549d2017-04-14 21:58:16 +020036 private static final String IP = "ip";
37 private static final String PORT = "port";
38 private static final String USERNAME = "username";
39 private static final String PASSWORD = "password";
40
41 @Override
42 public boolean isValid() {
43 return hasOnlyFields(IP, PORT, USERNAME, PASSWORD) &&
44 ip() != null;
45 }
46
47 /**
48 * Gets the Ip of the TL1 device.
49 *
50 * @return ip
51 */
52 public IpAddress ip() {
53 return IpAddress.valueOf(get(IP, extractIpPort().getKey()));
54 }
55
56 /**
57 * Gets the port of the TL1 device.
58 *
59 * @return port
60 */
61 public int port() {
62 return get(PORT, extractIpPort().getValue());
63 }
64
65 /**
66 * Gets the username of the TL1 device.
67 *
68 * @return username
69 */
70 public String username() {
71 return get(USERNAME, "");
72 }
73
74 /**
75 * Gets the password of the TL1 device.
76 *
77 * @return password
78 */
79 public String password() {
80 return get(PASSWORD, "");
81 }
82
83
84 private Pair<String, Integer> extractIpPort() {
85 String info = subject.toString();
Yuta HIGUCHIc6358352017-06-23 11:56:27 -070086 if (info.startsWith(TL1)) {
Andrea Campanella59b549d2017-04-14 21:58:16 +020087 //+1 is due to length of colon separator
88 String ip = info.substring(info.indexOf(":") + 1, info.lastIndexOf(":"));
89 int port = Integer.parseInt(info.substring(info.lastIndexOf(":") + 1));
90 return Pair.of(ip, port);
91 }
92 return null;
93 }
94}