blob: 16958b685e53b663a4a61ea1b3a327fc49d2ef78 [file] [log] [blame]
Ai Hamanobd51cdd2018-10-18 11:30:07 +09001/*
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 */
16package org.onosproject.odtn.utils.openconfig;
17
18import org.onosproject.yang.model.KeyLeaf;
19import org.onosproject.yang.model.ModelObject;
20import org.onosproject.yang.model.ResourceId;
21import org.onosproject.yang.runtime.AnnotatedNodeInfo;
22import org.onosproject.yang.runtime.Annotation;
23import org.onosproject.yang.runtime.DefaultAnnotatedNodeInfo;
24import org.onosproject.yang.runtime.DefaultAnnotation;
25import org.slf4j.Logger;
26import org.slf4j.LoggerFactory;
27import java.util.List;
28
29/**
30 * Utility abstract class to deal with OPENCONFIG ModelObject & Annotation.
31 *
32 * @param <O> modelOject to be dealt with
33 */
34public abstract class OpenConfigObjectHandler<O extends ModelObject> {
35 protected final Logger log = LoggerFactory.getLogger(getClass());
36 protected O modelObject;
37 protected ResourceId resourceId;
38 protected List<AnnotatedNodeInfo> annotatedNodeInfos;
39
40 /**
41 * Get modelObject instance.
42 *
43 * @return ModelObject of target OPENCONFIG
44 */
45 public O getModelObject() {
46 return modelObject;
47 }
48
49 /**
50 * Get resourceId instance.
51 *
52 * @return ResourceId of target OPENCONFIG
53 */
54 public ResourceId getResourceId() {
55 return resourceId;
56 }
57
58 /**
59 * Set resourceId instance.
60 *
61 * @param openconfigName String of target OPENCONFIG name
62 * @param openconfigNameSpace String of target OPENCONFIG namespace
63 * @param keyLeaf KeyLeaf of target OPENCONFIG
64 * @param pBuilder ResourceId.Builder of parent OPENCONFIG
65 */
66 protected void setResourceId(String openconfigName, String openconfigNameSpace,
67 KeyLeaf keyLeaf, ResourceId.Builder pBuilder) {
68 ResourceId.Builder ridBuilder = ResourceId.builder();
69 if (pBuilder != null) {
70 ridBuilder = pBuilder.addBranchPointSchema(openconfigName, openconfigNameSpace);
71 } else {
72 ridBuilder = ridBuilder.addBranchPointSchema(openconfigName,
73 openconfigNameSpace);
74 }
75
76 if (keyLeaf != null) {
77 ridBuilder = ridBuilder.addKeyLeaf(
78 keyLeaf.leafSchema().name(),
79 keyLeaf.leafSchema().namespace(),
80 keyLeaf.leafValue());
81 }
82
83 resourceId = ridBuilder.build();
84 }
85
86 /**
87 * Get ridBuilder instance.
88 *
89 * @return ResourceId.Builder of target OPENCONFIG
90 */
91 public ResourceId.Builder getResourceIdBuilder() {
92 try {
93 return resourceId.copyBuilder();
94 } catch (CloneNotSupportedException e) {
95 log.error("Exception thrown", e);
96 return null;
97 }
98 }
99
100 /**
101 * Add Annotation for annotaedNodeinfos.
102 *
103 * @param key String of Annotation's key
104 * @param value String of Annotation's value
105 */
106 public void addAnnotation(String key, String value) {
107 AnnotatedNodeInfo.Builder builder = DefaultAnnotatedNodeInfo.builder();
108
109 AnnotatedNodeInfo preAnnotate = annotatedNodeInfos.stream()
110 .filter(annotatedNodeInfo -> annotatedNodeInfo.resourceId()
111 .equals(resourceId))
112 .findFirst()
113 .orElse(null);
114
115 if (preAnnotate != null) {
116 annotatedNodeInfos.remove(preAnnotate);
117 for (Annotation annotation : preAnnotate.annotations()) {
118 builder.addAnnotation(annotation);
119 }
120 }
121
122 annotatedNodeInfos.add(builder.addAnnotation(new DefaultAnnotation(key, value))
123 .resourceId(resourceId)
124 .build());
125 }
126
127 /**
128 * Get annotatedNodeInfos instance.
129 *
130 * @return List<AnnotatedNodeInfo> of all OPENCONFIG
131 */
132 public List<AnnotatedNodeInfo> getAnnotatedNodeInfoList() {
133 return annotatedNodeInfos;
134 }
135}