blob: 7063c03885762cb17e0d2be60fb07ebe5135a234 [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 Vachuskaca88bb72015-04-08 19:38:02 -070040 * @param driver parent driver type
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080041 */
Thomas Vachuska80b0a802015-07-17 08:43:30 -070042 public DefaultDriverData(Driver driver, DeviceId deviceId) {
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070043 this.driver = driver;
Thomas Vachuska80b0a802015-07-17 08:43:30 -070044 this.deviceId = deviceId;
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080045 this.properties = new HashMap<>();
46 }
47
48 @Override
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070049 public Driver driver() {
50 return driver;
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080051 }
52
53 @Override
Thomas Vachuska80b0a802015-07-17 08:43:30 -070054 public DeviceId deviceId() {
55 return deviceId;
56 }
57
58 @Override
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080059 public <T extends Behaviour> T behaviour(Class<T> behaviourClass) {
Thomas Vachuskafacc3f52015-04-10 08:58:36 -070060 return driver.createBehaviour(this, behaviourClass);
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080061 }
62
63 @Override
64 public MutableAnnotations set(String key, String value) {
65 properties.put(key, value);
66 return this;
67 }
68
69 @Override
70 public MutableAnnotations clear(String... keys) {
71 if (keys.length == 0) {
72 properties.clear();
73 } else {
74 for (String key : keys) {
75 properties.remove(key);
76 }
77 }
78 return this;
79 }
80
81 @Override
82 public Set<String> keys() {
83 return ImmutableSet.copyOf(properties.keySet());
84 }
85
86 @Override
87 public String value(String key) {
88 return properties.get(key);
89 }
90
91 @Override
92 public String toString() {
93 return toStringHelper(this)
Thomas Vachuskaca88bb72015-04-08 19:38:02 -070094 .add("type", driver)
Thomas Vachuskaa8f4e7d2015-01-08 17:31:55 -080095 .add("properties", properties)
96 .toString();
97 }
98
99}