Merge "Add SO VF Module Delete Operation"
[policy/models.git] / models-tosca / src / main / java / org / onap / policy / models / tosca / legacy / concepts / LegacyGuardPolicyContent.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.tosca.legacy.concepts;
22
23 import java.lang.reflect.Field;
24 import java.util.HashMap;
25 import java.util.Map;
26
27 import javax.ws.rs.core.Response;
28
29 import lombok.Data;
30
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;
36
37 /**
38  * Content object of a Legacy Guard Policy.
39  *
40  * @author Liam Fallon (liam.fallon@est.tech)
41  */
42 @Data
43 public class LegacyGuardPolicyContent {
44     private static final Logger LOGGER = LoggerFactory.getLogger(LegacyGuardPolicyMapper.class);
45
46     private String actor;
47     private String recipe;
48     private String targets;
49     private String clname;
50     private String limit;
51     private String timeWindow;
52     private String timeUnits;
53     private String min;
54     private String max;
55     private String guardActiveStart;
56     private String guardActiveEnd;
57
58     /**
59      * Get contents as a property map.
60      *
61      * @return the contents as a map.
62      */
63     public Map<String, String> getAsPropertyMap() {
64         final Map<String, String> propertyMap = new HashMap<>();
65
66         final StandardCoder coder  = new StandardCoder();
67
68         try {
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)));
72                 }
73             }
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);
78
79         }
80
81         return propertyMap;
82     }
83
84     /**
85      * Set the contents from a property map.
86      *
87      * @param propertyMap the incoming property map
88      */
89     public void setContent(final Map<String, String> propertyMap) {
90         final StandardCoder coder  = new StandardCoder();
91
92         try {
93             // @formatter:off
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));
105             // @formatter:on
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);
110         }
111     }
112 }