blob: 200c623f143a2049d7aaaed3c0823449b31a4654 [file] [log] [blame]
hirokiec18d3a2018-05-16 15:27:37 -07001/*
2 * Copyright 2018-present Open Networking Foundation
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
17package org.onosproject.odtn.utils.tapi;
18
19import org.onosproject.yang.gen.v1.tapicommon.rev20180307.tapicommon.Uuid;
20import org.onosproject.yang.gen.v1.tapicommon.rev20180307.tapicommon.globalclass.DefaultName;
21import org.onosproject.yang.gen.v1.tapicommon.rev20180307.tapicommon.globalclass.Name;
22import org.onosproject.yang.model.ModelObject;
23
24import java.lang.reflect.InvocationTargetException;
25import java.lang.reflect.Method;
26import java.util.Map;
27import java.util.Map.Entry;
hirokiec18d3a2018-05-16 15:27:37 -070028
29import org.slf4j.Logger;
hirokif4ed5212018-05-26 22:39:38 -070030
hirokiec18d3a2018-05-16 15:27:37 -070031import static org.slf4j.LoggerFactory.getLogger;
32
33/**
34 * Utility methods dealing with TAPI modelObject which includes global class grouping.
hirokif4ed5212018-05-26 22:39:38 -070035 * <p>
hirokiec18d3a2018-05-16 15:27:37 -070036 * <p> tapi-common@2018-03-07.yang
37 * grouping global-class {
hirokif4ed5212018-05-26 22:39:38 -070038 * leaf uuid {
39 * type uuid;
hirokiec18d3a2018-05-16 15:27:37 -070040 * }
hirokif4ed5212018-05-26 22:39:38 -070041 * list name {
42 * key 'value-name';
43 * uses name-and-value;
44 * }
45 * }
46 * <p>
hirokiec18d3a2018-05-16 15:27:37 -070047 * grouping name-and-value {
hirokif4ed5212018-05-26 22:39:38 -070048 * leaf value-name {
49 * type string;
50 * description "The name of the value. The value need not have a name.";
51 * }
52 * leaf value {
53 * type string;
54 * description "The value";
55 * }
56 * description "A scoped name-value pair";
hirokiec18d3a2018-05-16 15:27:37 -070057 * }
58 * </p>
59 */
60public final class TapiGlobalClassUtil {
61
62 private static final Logger log = getLogger(TapiGlobalClassUtil.class);
63
64 private TapiGlobalClassUtil() {
65 }
66
67 /**
hirokif4ed5212018-05-26 22:39:38 -070068 * Set uuid for the ModelObject.
69 *
70 * @param obj ModelObject
71 * @param uuid Uuid
72 * @param <T> Type of ModelObject
hirokiec18d3a2018-05-16 15:27:37 -070073 */
hirokif4ed5212018-05-26 22:39:38 -070074 public static <T extends ModelObject> void setUuid(T obj, Uuid uuid) {
hirokiec18d3a2018-05-16 15:27:37 -070075 @SuppressWarnings("unchecked")
76 Class<T> cls = (Class<T>) obj.getClass();
77 try {
78 Method method = cls.getMethod("uuid", Uuid.class);
79 method.invoke(obj, uuid);
80 } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
81 log.error("Exception thrown", e);
82 }
hirokif4ed5212018-05-26 22:39:38 -070083 }
84
85 /**
86 * Get uuid for the ModelObject.
87 *
88 * @param obj ModelObject
89 * @param <T> Type of ModelObject
90 * @return Uuid
91 */
92 public static <T extends ModelObject> Uuid getUuid(T obj) {
93 Uuid uuid = null;
94 @SuppressWarnings("unchecked")
95 Class<T> cls = (Class<T>) obj.getClass();
96 try {
97 Method method = cls.getMethod("uuid");
98 uuid = (Uuid) method.invoke(obj);
99 } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
100 log.error("Exception thrown", e);
101 }
hirokiec18d3a2018-05-16 15:27:37 -0700102
103 return uuid;
104 }
105
106 /**
107 * Add key-value to the ModelObject as "name-and-value" list.
hirokif4ed5212018-05-26 22:39:38 -0700108 *
hirokiec18d3a2018-05-16 15:27:37 -0700109 * @param obj ModelObject
hirokif4ed5212018-05-26 22:39:38 -0700110 * @param kvs Key-value map
111 * @param <T> Type of ModelObject
hirokiec18d3a2018-05-16 15:27:37 -0700112 */
hiroki684aa2f2018-05-19 20:48:49 -0700113 public static <T extends ModelObject> void addNameList(T obj, Map<String, String> kvs) {
hirokiec18d3a2018-05-16 15:27:37 -0700114
115 @SuppressWarnings("unchecked")
116 Class<T> cls = (Class<T>) obj.getClass();
117 try {
118 Method method = cls.getMethod("addToName", Name.class);
119
120 for (Entry<String, String> kv : kvs.entrySet()) {
121 DefaultName prop = new DefaultName();
122 prop.valueName(kv.getKey());
123 prop.value(kv.getValue());
124 method.invoke(obj, prop);
125 }
126 } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
127 log.error("Exception thrown", e);
128 }
129 }
130}