cac59f8f68e9fe67671394256a14b288e7637479
[platform/upstream/iotivity.git] / service / simulator / java / sdk / src / org / oic / simulator / SimulatorResourceModel.java
1 /*
2  * Copyright 2015 Samsung Electronics All Rights Reserved.
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 package org.oic.simulator;
18
19 import java.util.HashMap;
20 import java.util.Map;
21
22 /**
23  * This class represents the resource model of a resource.
24  */
25 public class SimulatorResourceModel {
26
27     private Map<String, AttributeValue>    mValues     = null;
28     private Map<String, AttributeProperty> mProperties = null;
29
30     /**
31      * Constructs new {@SimulatorResourceModel} object.
32      */
33     public SimulatorResourceModel() {
34         mValues = new HashMap<>();
35         mProperties = new HashMap<>();
36     }
37
38     /**
39      * API to add an attribute to resource model.
40      *
41      * @param attrName
42      *            Name of the attribute.
43      * @param value
44      *            Value of the attribute.
45      *
46      * @throws InvalidArgsException
47      *             This exception will be thrown on invalid input.
48      */
49     public void addAttribute(String attrName, AttributeValue value)
50             throws InvalidArgsException {
51         if (null == attrName || attrName.isEmpty())
52             throw new InvalidArgsException("Invalid attribute name!");
53
54         if (null == value)
55             throw new InvalidArgsException("Attribute value is null!");
56
57         mValues.put(attrName, value);
58     }
59
60     /**
61      * API to add an attribute to resource model.
62      *
63      * @param attribute
64      *            {@link SimulatorResourceAttribute} to be add to resource
65      *            model.
66      *
67      * @throws InvalidArgsException
68      *             This exception will be thrown on invalid input.
69      */
70     public void addAttribute(SimulatorResourceAttribute attribute)
71             throws InvalidArgsException {
72         if (null == attribute || null == attribute.name()
73                 || attribute.name().isEmpty() || null == attribute.value())
74             throw new InvalidArgsException("Invalid attribute!");
75
76         mValues.put(attribute.name(), attribute.value());
77     }
78
79     /**
80      * API to set attribute's property.
81      *
82      * @param attrName
83      *            Name of the attribute.
84      * @param property
85      *            {@link AttributeProperty} to be set for attribute.
86      *
87      * @throws InvalidArgsException
88      *             This exception will be thrown on invalid input.
89      */
90     public void setAttributeProperty(String attrName, AttributeProperty property)
91             throws InvalidArgsException {
92         if (null == attrName || attrName.isEmpty())
93             throw new InvalidArgsException("Invalid attribute!");
94
95         if (null == property)
96             throw new InvalidArgsException("Invalid attribute property!");
97
98         mProperties.put(attrName, property);
99     }
100
101     /**
102      * API to set attribute's value.
103      *
104      * @param attrName
105      *            Name of the attribute.
106      * @param value
107      *            {@link AttributeValue} to be set for attribute.
108      *
109      * @throws InvalidArgsException
110      *             This exception will be thrown on invalid input.
111      */
112     public void setAttributeValue(String attrName, AttributeValue value)
113             throws InvalidArgsException {
114         if (null == attrName || attrName.isEmpty())
115             throw new InvalidArgsException("Invalid attribute name!");
116
117         if (null == value)
118             throw new InvalidArgsException("Attribute value is null!");
119
120         mValues.put(attrName, value);
121     }
122
123     /**
124      * API to get all the attributes of resource model.
125      *
126      * @return Map of attributes with attribute name as key and its
127      *         corresponding {@link SimulatorResourceAttribute} as value.
128      */
129     public Map<String, SimulatorResourceAttribute> getAttributes() {
130         Map<String, SimulatorResourceAttribute> attributes = new HashMap<>();
131         for (Map.Entry<String, AttributeValue> entry : mValues.entrySet()) {
132             SimulatorResourceAttribute attribute = new SimulatorResourceAttribute(
133                     entry.getKey(), entry.getValue(), mProperties.get(entry
134                             .getKey()));
135             attributes.put(entry.getKey(), attribute);
136         }
137
138         return attributes;
139     }
140
141     /**
142      * API to get attribute by name.
143      *
144      * @param attrName
145      *            Name of the attribute.
146      *
147      * @return {@link SimulatorResourceAttribute}.
148      */
149     public SimulatorResourceAttribute getAttribute(String attrName) {
150         if (mValues.containsKey(attrName)) {
151             SimulatorResourceAttribute attribute = new SimulatorResourceAttribute(
152                     attrName, mValues.get(attrName), mProperties.get(attrName));
153             return attribute;
154         }
155
156         return null;
157     }
158
159     /**
160      * API to check whether resource model has attribute.
161      *
162      * @param attrName
163      *            Name of the attribute.
164      *
165      * @return true if resource model has an attribute with given name,
166      *         otherwise false.
167      */
168     public boolean containsAttribute(String attrName) {
169         if (mValues.containsKey(attrName))
170             return true;
171         return false;
172     }
173
174     /**
175      * API to get value type information of attribute.
176      *
177      * @param attrName
178      *            Name of the attribute.
179      *
180      * @return Attribute value type information {@AttributeValue.TypeInfo}.
181      */
182     public AttributeValue.TypeInfo getAttributeType(String attrName) {
183         if (mValues.containsKey(attrName))
184             return mValues.get(attrName).typeInfo();
185         return null;
186     }
187
188     /**
189      * API to remove attribute from resource model.
190      *
191      * @param attrName
192      *            Name of the attribute.
193      *
194      * @return true if resource model has attribute it is removed, otherwise
195      *         false.
196      */
197     public boolean removeAttribute(String attrName) {
198         if (mValues.containsKey(attrName)) {
199             mValues.remove(attrName);
200             if (mProperties.containsKey(attrName))
201                 mProperties.remove(attrName);
202             return true;
203         }
204
205         return false;
206     }
207
208     /**
209      * API to get number of attributes present in resource model.
210      *
211      * @return Number of attributes present in resource model.
212      */
213     public int size() {
214         return mValues.size();
215     }
216
217     /**
218      * API to update the attribute values from given
219      * {@link SimulatorResourceModel}.
220      */
221     public void update(SimulatorResourceModel resourceModel) {
222         if (null == resourceModel || 0 == resourceModel.size())
223             return;
224
225         for (Map.Entry<String, SimulatorResourceAttribute> entry : resourceModel
226                 .getAttributes().entrySet()) {
227             SimulatorResourceAttribute newAttribute = entry.getValue();
228             SimulatorResourceAttribute attribute = getAttribute(entry.getKey());
229             if (null != newAttribute && null != attribute) {
230                 if (null != attribute.property()) {
231                     AttributeValueValidation validation = new AttributeValueValidation(
232                             attribute.property());
233                     if (!validation.validate(newAttribute.value())) {
234                         mValues.put(entry.getKey(), newAttribute.value());
235                     }
236                 } else {
237                     mValues.put(entry.getKey(), newAttribute.value());
238                 }
239             }
240         }
241     }
242
243     // Methods used in native code
244     private SimulatorResourceModel(Map<String, AttributeValue> values,
245             Map<String, AttributeProperty> properties) {
246         mValues = values;
247         mProperties = properties;
248     }
249 }