2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.tosca.legacy.concepts;
23 import java.lang.reflect.Field;
24 import java.util.HashMap;
27 import javax.ws.rs.core.Response;
31 import org.onap.policy.common.utils.coder.StandardCoder;
32 import org.onap.policy.models.base.PfModelRuntimeException;
33 import org.onap.policy.models.tosca.legacy.mapping.LegacyGuardPolicyMapper;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * Content object of a Legacy Guard Policy.
40 * @author Liam Fallon (liam.fallon@est.tech)
43 public class LegacyGuardPolicyContent {
44 private static final Logger LOGGER = LoggerFactory.getLogger(LegacyGuardPolicyMapper.class);
47 private String recipe;
48 private String targets;
49 private String clname;
51 private String timeWindow;
52 private String timeUnits;
55 private String guardActiveStart;
56 private String guardActiveEnd;
59 * Get contents as a property map.
61 * @return the contents as a map.
63 public Map<String, String> getAsPropertyMap() {
64 final Map<String, String> propertyMap = new HashMap<>();
66 final StandardCoder coder = new StandardCoder();
69 for (Field field : this.getClass().getDeclaredFields()) {
70 if (field.get(this) != null && field.getType().equals(String.class)) {
71 propertyMap.put(field.getName(), coder.encode(field.get(this)));
74 } catch (Exception exc) {
75 String errorMessage = "could not convert content to a property map";
76 LOGGER.warn(errorMessage, exc);
77 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage, exc);
85 * Set the contents from a property map.
87 * @param propertyMap the incoming property map
89 public void setContent(final Map<String, String> propertyMap) {
90 final StandardCoder coder = new StandardCoder();
94 setActor( coder.decode(propertyMap.get("actor"), String.class));
95 setClname( coder.decode(propertyMap.get("clname"), String.class));
96 setGuardActiveEnd( coder.decode(propertyMap.get("guardActiveEnd"), String.class));
97 setGuardActiveStart(coder.decode(propertyMap.get("guardActiveStart"), String.class));
98 setLimit( coder.decode(propertyMap.get("limit"), String.class));
99 setMax( coder.decode(propertyMap.get("max"), String.class));
100 setMin( coder.decode(propertyMap.get("min"), String.class));
101 setRecipe( coder.decode(propertyMap.get("recipe"), String.class));
102 setTargets( coder.decode(propertyMap.get("targets"), String.class));
103 setTimeUnits( coder.decode(propertyMap.get("timeUnits"), String.class));
104 setTimeWindow( coder.decode(propertyMap.get("timeWindow"), String.class));
106 } catch (Exception exc) {
107 String errorMessage = "could not convert content to a property map";
108 LOGGER.warn(errorMessage, exc);
109 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, errorMessage, exc);