blob: 76d7932d309961116f31f6cf06d49c65fda0434b [file] [log] [blame]
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -08001/*
2 * Copyright 2015 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 */
16package org.onosproject.net.driver;
17
18import com.google.common.collect.ImmutableSet;
Thomas Vachuska80b0a802015-07-17 08:43:30 -070019import org.onosproject.net.DeviceId;
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080020import org.onosproject.net.MutableAnnotations;
21
22import java.util.HashMap;
23import java.util.Map;
24import java.util.Set;
25
26import static com.google.common.base.MoreObjects.toStringHelper;
27
28/**
29 * Default implementation of driver data descriptor.
30 */
31public class DefaultDriverData implements DriverData {
32
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070033 private final Driver driver;
Thomas Vachuska80b0a802015-07-17 08:43:30 -070034 private final DeviceId deviceId;
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080035 private final Map<String, String> properties;
36
37 /**
38 * Creates new driver data.
39 *
Thomas Vachuskad894b5d2015-07-30 11:59:07 -070040 * @param driver parent driver type
41 * @param deviceId device identifier
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080042 */
Thomas Vachuska80b0a802015-07-17 08:43:30 -070043 public DefaultDriverData(Driver driver, DeviceId deviceId) {
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070044 this.driver = driver;
Thomas Vachuska80b0a802015-07-17 08:43:30 -070045 this.deviceId = deviceId;
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080046 this.properties = new HashMap<>();
47 }
48
49 @Override
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070050 public Driver driver() {
51 return driver;
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080052 }
53
54 @Override
Thomas Vachuska80b0a802015-07-17 08:43:30 -070055 public DeviceId deviceId() {
56 return deviceId;
57 }
58
59 @Override
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080060 public <T extends Behaviour> T behaviour(Class<T> behaviourClass) {
Thomas Vachuskafacc3f52015-04-10 08:58:36 -070061 return driver.createBehaviour(this, behaviourClass);
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080062 }
63
64 @Override
65 public MutableAnnotations set(String key, String value) {
66 properties.put(key, value);
67 return this;
68 }
69
70 @Override
71 public MutableAnnotations clear(String... keys) {
72 if (keys.length == 0) {
73 properties.clear();
74 } else {
75 for (String key : keys) {
76 properties.remove(key);
77 }
78 }
79 return this;
80 }
81
82 @Override
83 public Set<String> keys() {
84 return ImmutableSet.copyOf(properties.keySet());
85 }
86
87 @Override
88 public String value(String key) {
89 return properties.get(key);
90 }
91
92 @Override
93 public String toString() {
94 return toStringHelper(this)
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070095 .add("type", driver)
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080096 .add("properties", properties)
97 .toString();
98 }
99
100}