blob: 016ea2ce590fa182a77128d52499d80be05f27d1 [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.
Thomas Vachuskaa01ef782018-07-25 14:07:11 -070035 *
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 * }
Thomas Vachuskaa01ef782018-07-25 14:07:11 -070046 * </p>
hirokif4ed5212018-05-26 22:39:38 -070047 * <p>
hirokiec18d3a2018-05-16 15:27:37 -070048 * grouping name-and-value {
hirokif4ed5212018-05-26 22:39:38 -070049 * leaf value-name {
50 * type string;
51 * description "The name of the value. The value need not have a name.";
52 * }
53 * leaf value {
54 * type string;
55 * description "The value";
56 * }
57 * description "A scoped name-value pair";
hirokiec18d3a2018-05-16 15:27:37 -070058 * }
59 * </p>
60 */
61public final class TapiGlobalClassUtil {
62
63 private static final Logger log = getLogger(TapiGlobalClassUtil.class);
64
65 private TapiGlobalClassUtil() {
66 }
67
68 /**
hirokif4ed5212018-05-26 22:39:38 -070069 * Set uuid for the ModelObject.
70 *
71 * @param obj ModelObject
72 * @param uuid Uuid
73 * @param <T> Type of ModelObject
hirokiec18d3a2018-05-16 15:27:37 -070074 */
hirokif4ed5212018-05-26 22:39:38 -070075 public static <T extends ModelObject> void setUuid(T obj, Uuid uuid) {
hirokiec18d3a2018-05-16 15:27:37 -070076 @SuppressWarnings("unchecked")
77 Class<T> cls = (Class<T>) obj.getClass();
78 try {
79 Method method = cls.getMethod("uuid", Uuid.class);
80 method.invoke(obj, uuid);
81 } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
82 log.error("Exception thrown", e);
83 }
hirokif4ed5212018-05-26 22:39:38 -070084 }
85
86 /**
87 * Get uuid for the ModelObject.
88 *
89 * @param obj ModelObject
90 * @param <T> Type of ModelObject
91 * @return Uuid
92 */
93 public static <T extends ModelObject> Uuid getUuid(T obj) {
94 Uuid uuid = null;
95 @SuppressWarnings("unchecked")
96 Class<T> cls = (Class<T>) obj.getClass();
97 try {
98 Method method = cls.getMethod("uuid");
99 uuid = (Uuid) method.invoke(obj);
100 } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
101 log.error("Exception thrown", e);
102 }
hirokiec18d3a2018-05-16 15:27:37 -0700103
104 return uuid;
105 }
106
107 /**
108 * Add key-value to the ModelObject as "name-and-value" list.
hirokif4ed5212018-05-26 22:39:38 -0700109 *
hirokiec18d3a2018-05-16 15:27:37 -0700110 * @param obj ModelObject
hirokif4ed5212018-05-26 22:39:38 -0700111 * @param kvs Key-value map
112 * @param <T> Type of ModelObject
hirokiec18d3a2018-05-16 15:27:37 -0700113 */
hiroki684aa2f2018-05-19 20:48:49 -0700114 public static <T extends ModelObject> void addNameList(T obj, Map<String, String> kvs) {
hirokiec18d3a2018-05-16 15:27:37 -0700115
116 @SuppressWarnings("unchecked")
117 Class<T> cls = (Class<T>) obj.getClass();
118 try {
119 Method method = cls.getMethod("addToName", Name.class);
120
121 for (Entry<String, String> kv : kvs.entrySet()) {
122 DefaultName prop = new DefaultName();
123 prop.valueName(kv.getKey());
124 prop.value(kv.getValue());
125 method.invoke(obj, prop);
126 }
127 } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
128 log.error("Exception thrown", e);
129 }
130 }
131}