[SO] Create changes for SO-API and BPMN-INFRA to support CNF's through ASD
[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     cnf("cnf");
42
43
44     final String name;
45
46     private ModelType(String name) {
47         this.name = name;
48     }
49
50
51     public <T> T getId(Object obj) {
52         return this.get(obj, "Id");
53     }
54
55     public <T> T getName(Object obj) {
56         return this.get(obj, "Name");
57     }
58
59     public void setId(Object obj, Object value) {
60         this.set(obj, "Id", value);
61     }
62
63     public void setName(Object obj, Object value) {
64         this.set(obj, "Name", value);
65     }
66
67     protected <T> T get(Object obj, String field) {
68         T result = null;
69         if (obj != null) {
70             try {
71                 result = (T) obj.getClass().getMethod(String.format("%s%s%s", "get",
72                         CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, this.name), field)).invoke(obj);
73             } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException
74                     | NoSuchMethodException | SecurityException e) {
75                 // silent fail
76             }
77         }
78
79         return result;
80     }
81
82     protected void set(Object obj, String field, Object value) {
83         if (obj != null) {
84             try {
85                 obj.getClass()
86                         .getMethod(
87                                 String.format("%s%s%s", "set",
88                                         CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, this.name), field),
89                                 value.getClass())
90                         .invoke(obj, value);
91             } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
92                     | InvocationTargetException e) {
93                 // silent fail
94             }
95         }
96     }
97 }