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