blob: 834c9275f70aeb674205820776f9380c9378eedb [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;
28import java.util.UUID;
29
30import org.slf4j.Logger;
31import static org.slf4j.LoggerFactory.getLogger;
32
33/**
34 * Utility methods dealing with TAPI modelObject which includes global class grouping.
35 *
36 * <p> tapi-common@2018-03-07.yang
37 * grouping global-class {
38 * leaf uuid {
39 * type uuid;
40 * }
41 * list name {
42 * key 'value-name';
43 * uses name-and-value;
44 * }
45 * }
46 *
47 * grouping name-and-value {
48 * 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";
57 * }
58 * </p>
59 */
60public final class TapiGlobalClassUtil {
61
62 private static final Logger log = getLogger(TapiGlobalClassUtil.class);
63
64 private TapiGlobalClassUtil() {
65 }
66
67 /**
68 * Generate and set Uuid for the ModelObject.
69 * @param obj ModelObject
70 * @param <T> type of ModelObject
71 * @return generated uuid
72 */
73 public static <T extends ModelObject> Uuid setUuid(T obj) {
74 Uuid uuid = Uuid.of(UUID.randomUUID().toString());
75
76 @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 }
84
85 return uuid;
86 }
87
88 /**
89 * Add key-value to the ModelObject as "name-and-value" list.
90 * @param obj ModelObject
91 * @param kvs key-value map
92 * @param <T> type of ModelObject
93 */
94 public static <T extends ModelObject> void setNameList(T obj, Map<String, String> kvs) {
95
96 @SuppressWarnings("unchecked")
97 Class<T> cls = (Class<T>) obj.getClass();
98 try {
99 Method method = cls.getMethod("addToName", Name.class);
100
101 for (Entry<String, String> kv : kvs.entrySet()) {
102 DefaultName prop = new DefaultName();
103 prop.valueName(kv.getKey());
104 prop.value(kv.getValue());
105 method.invoke(obj, prop);
106 }
107 } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
108 log.error("Exception thrown", e);
109 }
110 }
111}