hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.odtn.utils.tapi; |
| 18 | |
hiroki | d8fd786 | 2018-10-09 15:24:24 +0900 | [diff] [blame^] | 19 | import org.onosproject.yang.gen.v1.tapicommon.rev20181016.tapicommon.Uuid; |
| 20 | import org.onosproject.yang.gen.v1.tapicommon.rev20181016.tapicommon.globalclass.DefaultName; |
| 21 | import org.onosproject.yang.gen.v1.tapicommon.rev20181016.tapicommon.globalclass.Name; |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 22 | import org.onosproject.yang.model.ModelObject; |
| 23 | |
| 24 | import java.lang.reflect.InvocationTargetException; |
| 25 | import java.lang.reflect.Method; |
| 26 | import java.util.Map; |
| 27 | import java.util.Map.Entry; |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 28 | |
| 29 | import org.slf4j.Logger; |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 30 | |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 31 | import static org.slf4j.LoggerFactory.getLogger; |
| 32 | |
| 33 | /** |
| 34 | * Utility methods dealing with TAPI modelObject which includes global class grouping. |
Thomas Vachuska | a01ef78 | 2018-07-25 14:07:11 -0700 | [diff] [blame] | 35 | * |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 36 | * <p> tapi-common@2018-03-07.yang |
| 37 | * grouping global-class { |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 38 | * leaf uuid { |
| 39 | * type uuid; |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 40 | * } |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 41 | * list name { |
| 42 | * key 'value-name'; |
| 43 | * uses name-and-value; |
| 44 | * } |
| 45 | * } |
Thomas Vachuska | a01ef78 | 2018-07-25 14:07:11 -0700 | [diff] [blame] | 46 | * </p> |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 47 | * <p> |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 48 | * grouping name-and-value { |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 49 | * 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"; |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 58 | * } |
| 59 | * </p> |
| 60 | */ |
| 61 | public final class TapiGlobalClassUtil { |
| 62 | |
| 63 | private static final Logger log = getLogger(TapiGlobalClassUtil.class); |
| 64 | |
| 65 | private TapiGlobalClassUtil() { |
| 66 | } |
| 67 | |
| 68 | /** |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 69 | * Set uuid for the ModelObject. |
| 70 | * |
| 71 | * @param obj ModelObject |
| 72 | * @param uuid Uuid |
| 73 | * @param <T> Type of ModelObject |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 74 | */ |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 75 | public static <T extends ModelObject> void setUuid(T obj, Uuid uuid) { |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 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 | } |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 84 | } |
| 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 | } |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 103 | |
| 104 | return uuid; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Add key-value to the ModelObject as "name-and-value" list. |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 109 | * |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 110 | * @param obj ModelObject |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 111 | * @param kvs Key-value map |
| 112 | * @param <T> Type of ModelObject |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 113 | */ |
hiroki | 684aa2f | 2018-05-19 20:48:49 -0700 | [diff] [blame] | 114 | public static <T extends ModelObject> void addNameList(T obj, Map<String, String> kvs) { |
hiroki | ec18d3a | 2018-05-16 15:27:37 -0700 | [diff] [blame] | 115 | |
| 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 | } |
hiroki | d8fd786 | 2018-10-09 15:24:24 +0900 | [diff] [blame^] | 131 | } |