6b9b39e5a99621270dd20dbbdb6eb0fa79acdff8
[so.git] / common / src / main / java / org / onap / so / serviceinstancebeans / ModelType.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.serviceinstancebeans;
22
23 import java.lang.reflect.InvocationTargetException;
24 import com.google.common.base.CaseFormat;
25
26 /*
27  * Enum for Model Type values returned by API Handler to BPMN
28  */
29 public enum ModelType {
30     service("serviceInstance"),
31     vnf("vnf"),
32     vfModule("vfModule"),
33     volumeGroup("volumeGroup"),
34     network("network"),
35     configuration("configuration"),
36     connectionPoint("connectionPoint"),
37     pnf("pnf"),
38     networkInstanceGroup("networkInstanceGroup"),
39     instanceGroup("instanceGroup"),
40     vpnBinding("vpnBinding");
41
42     final String name;
43
44     private ModelType(String name) {
45         this.name = name;
46     }
47
48
49     public <T> T getId(Object obj) {
50         return this.get(obj, "Id");
51     }
52
53     public <T> T getName(Object obj) {
54         return this.get(obj, "Name");
55     }
56
57     public void setId(Object obj, Object value) {
58         this.set(obj, "Id", value);
59     }
60
61     public void setName(Object obj, Object value) {
62         this.set(obj, "Name", value);
63     }
64
65     protected <T> T get(Object obj, String field) {
66         T result = null;
67         if (obj != null) {
68             try {
69                 result = (T) obj.getClass().getMethod(String.format("%s%s%s", "get",
70                         CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, this.name), field)).invoke(obj);
71             } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
72                     | NoSuchMethodException | SecurityException e) {
73                 // silent fail
74             }
75         }
76
77         return result;
78     }
79
80     protected void set(Object obj, String field, Object value) {
81         if (obj != null) {
82             try {
83                 obj.getClass()
84                         .getMethod(
85                                 String.format("%s%s%s", "set",
86                                         CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, this.name), field),
87                                 value.getClass())
88                         .invoke(obj, value);
89             } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
90                     | InvocationTargetException e) {
91                 // silent fail
92             }
93         }
94     }
95 }