1//
2// Copyright (C) 2015 The Android Open Source Project
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
17#ifndef SHILL_DHCP_PROPERTIES_H_
18#define SHILL_DHCP_PROPERTIES_H_
19
20#include <string>
21
22#include <gtest/gtest_prod.h>  // for FRIEND_TEST
23
24#include "shill/accessor_interface.h"
25#include "shill/key_value_store.h"
26
27namespace shill {
28
29class Error;
30class PropertyStore;
31class StoreInterface;
32
33class DhcpProperties {
34 public:
35  static const char kHostnameProperty[];
36  static const char kVendorClassProperty[];
37
38  DhcpProperties();
39
40  virtual ~DhcpProperties();
41
42  // Adds property accessors to the DhcpProperty parameters in |this|
43  // to |store|.
44  void InitPropertyStore(PropertyStore* store);
45
46  // Loads DHCP properties from |storage| in group |id|.
47  virtual void Load(StoreInterface* store, const std::string& id);
48
49  // Saves DHCP properties to |storage| in group |id|.
50  virtual void Save(StoreInterface* store, const std::string& id) const;
51
52  // Combines two DHCP property objects and returns a
53  // std::unique_ptr<DhcpProperties>.  The new DhcpProperties instance is the
54  // union of the key-value pairs in |base| and |to_merge|.  For keys which
55  // exist in both |base| and |to_merge|, the value is taken from |to_merge|.
56  // EX:  |base| stores {"VendorClass": "v1", "Hostname": "host1"}
57  //      |to_merge| stores {"Hostname": "differentname"}
58  //      returned DhcpProperties will store:
59  //          {"VendorClass": "v1", "Hostname": "differentname"}
60  // EX:  |base| stores {"Hostname": "host1"}
61  //      |to_merge| stores {"Hostname": "differentname", "VendorClass": "vc"}
62  //      returned DhcpProperties will store:
63  //          {"Hostname": "differentname", "VendorClass": "vc"}
64  static std::unique_ptr<DhcpProperties> Combine(
65      const DhcpProperties& base,
66      const DhcpProperties& to_merge);
67
68  // Retrieves the value for a property with |name| in |value| if it is set.
69  // Returns true if the property was found.
70  bool GetValueForProperty(const std::string& name, std::string* value) const;
71
72  const KeyValueStore& properties() const { return properties_; };
73
74 private:
75  FRIEND_TEST(DhcpPropertiesTest, ClearMappedStringPropertyNoExistingValue);
76  FRIEND_TEST(DhcpPropertiesTest, ClearMappedStringPropertyWithSetValue);
77  FRIEND_TEST(DhcpPropertiesTest, CombineIntoEmpty);
78  FRIEND_TEST(DhcpPropertiesTest, CombineEmptyIntoExisting);
79  FRIEND_TEST(DhcpPropertiesTest, CombineConflicting);
80  FRIEND_TEST(DhcpPropertiesTest, Ctor);
81  FRIEND_TEST(DhcpPropertiesTest, GetMappedStringPropertyNoExistingValue);
82  FRIEND_TEST(DhcpPropertiesTest, GetMappedStringPropertyWithSetValue);
83  FRIEND_TEST(DhcpPropertiesTest, GetValueForProperty);
84  FRIEND_TEST(DhcpPropertiesTest, Load);
85  FRIEND_TEST(DhcpPropertiesTest, LoadEmpty);
86  FRIEND_TEST(DhcpPropertiesTest, LoadWithValuesSetAndClearRequired);
87  FRIEND_TEST(DhcpPropertiesTest, SaveWithValuesSet);
88  FRIEND_TEST(DhcpPropertiesTest, SavePropertyNotSetShouldBeDeleted);
89  FRIEND_TEST(DhcpPropertiesTest, SetMappedStringPropertyNoExistingValue);
90  FRIEND_TEST(DhcpPropertiesTest, SetMappedStringPropertyOverrideExisting);
91  FRIEND_TEST(DhcpPropertiesTest, SetMappedStringPropertySameAsExistingValue);
92
93  void ClearMappedStringProperty(const size_t& index, Error* error);
94  std::string GetMappedStringProperty(const size_t& index, Error* error);
95  bool SetMappedStringProperty(
96      const size_t& index, const std::string& value, Error* error);
97
98  // KeyValueStore tracking values for DhcpProperties settings.
99  KeyValueStore properties_;
100
101  DISALLOW_COPY_AND_ASSIGN(DhcpProperties);
102};
103
104}  // namespace shill
105
106#endif  // SHILL_DHCP_PROPERTIES_H_
107