blob: edeffef3ef6eca0093f7b05211d0f086ab98c618 [file] [log] [blame]
janani bf7060cd2017-03-28 19:06:30 +05301/*
2 * Copyright 2017-present Open Networking Laboratory
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.drivers.huawei;
18
19import org.onosproject.yang.gen.v1.ne.l3vpn.api.rev20141225.nel3vpnapi.DefaultDevices;
20import org.onosproject.yang.model.AtomicPath;
21import org.onosproject.yang.model.DefaultModelObjectData;
22import org.onosproject.yang.model.InnerModelObject;
23import org.onosproject.yang.model.ModelObject;
24import org.onosproject.yang.model.ModelObjectData;
25import org.onosproject.yang.model.ModelObjectId;
26import org.onosproject.yang.model.MultiInstanceNode;
27
28import java.util.Iterator;
29import java.util.List;
30
31/**
32 * Representation of utility for huawei driver.
33 */
34public final class DriverUtil {
35
36 /**
37 * Static constant value for devices.
38 */
39 static final String CONS_DEVICES = "Devices";
40
41 /**
42 * Error message for YANG model registry service not found.
43 */
44 static final String SERVICE_NOT_FOUND = "Service required for huawei " +
45 "driver is not found.";
46
47 /**
48 * Error message for object from the standard device model being null.
49 */
50 static final String OBJECT_NULL = "Object from device model cannot be null";
51
52 /**
53 * Error message for device object under devices from the standard model
54 * being null.
55 */
56 static final String DEVICE_NULL = "Device object from the devices of " +
57 "standard device model cannot be null";
58
59 /**
60 * Error message for device object under devices from the standard model
61 * being null.
62 */
63 static final String INS_NULL = "Instance object from the instances of " +
64 "standard device model cannot be null";
65
66 /**
67 * Error message for unsupported model id level.
68 */
69 static final String UNSUPPORTED_MODEL_LVL = "The model id level is not " +
70 "supported";
71
72 /**
73 * Error message for failure of device info retrieval.
74 */
75 static final String DEV_INFO_FAILURE = "Failed to retrieve device info.";
76
77 /**
78 * Error message for failure of interface info retrieval.
79 */
80 static final String INT_INFO_FAILURE = "Failed to retrieve Interfaces";
81
82 /**
83 * RPC message header.
84 */
85 static final String RPC_MSG = "<rpc message-id=\"101\" xmlns=\"urn:ietf:" +
86 "params:xml:ns:netconf:base:1.0\">";
87
88 /**
89 * RPC get message.
90 */
91 static final String RPC_GET = "<get>";
92
93 /**
94 * RPC subtree filter message.
95 */
96 static final String RPC_FILTER = "<filter type=\"subtree\">";
97
98 /**
99 * RPC system message with namespace.
100 */
101 static final String RPC_SYS = "<system xmlns=\"http://www.huawei.com/" +
102 "netconf/vrp\" format-version=\"1.0\" content-version=\"1.0\"/>";
103
104 /**
105 * RPC ifm message with namespace.
106 */
107 static final String RPC_IFM = "<ifm xmlns=\"http://www.huawei.com" +
108 "/netconf/vrp\" format-version=\"1.0\" content-version=\"1.0\">";
109
110 /**
111 * RPC interfaces message.
112 */
Vidyashree Ramad89a1532017-03-30 15:13:52 +0530113 static final String RPC_IFS =
114 "<interfaces><interface><ifPhyType>Ethernet</ifPhyType" +
115 "></interface></interfaces>";
janani bf7060cd2017-03-28 19:06:30 +0530116
117 /**
118 * RPC ifm message.
119 */
120 static final String RPC_CLOSE_IFM = "</ifm>";
121
122 /**
123 * RPC filter close message.
124 */
125 static final String RPC_CLOSE_FILTER = "</filter>";
126
127 /**
128 * RPC close message.
129 */
130 static final String RPC_CLOSE = "</rpc>";
131
132 /**
133 * RPC get close message.
134 */
135 static final String RPC_CLOSE_GET = "</get>";
136
137 /**
138 * Static constant for slash.
139 */
140 static final String SLASH = "/";
141
142 /**
143 * Static constant for devices name.
144 */
145 static final String DEVICES = "devices";
146
147 /**
148 * Static constant for devices namespace.
149 */
150 static final String NAMESPACE = "ne-l3vpn-api";
151
152 /**
153 * Error message for model object id having more than two objects.
154 */
155 private static final String MODEL_OBJ_ID_LIMIT = "The model object id " +
156 "must not have more than two objects.";
157
158 // No instantiation.
159 private DriverUtil() {
160 }
161
162 /**
163 * Returns the device id from the model object id. If model object id is
164 * not present then null is returned. If only one path is available in
165 * the list then devices value is returned.
166 *
167 * @param id model object data
168 * @return device id
169 */
170 static String getIdFromModId(ModelObjectId id) {
171 if (id == null) {
172 return null;
173 }
174
175 List<AtomicPath> paths = id.atomicPaths();
176 int size = paths.size();
177 if (size == 1) {
178 return CONS_DEVICES;
179 } else if (size == 2) {
180 return ((MultiInstanceNode) paths.get(1)).key().toString();
181 } else {
182 throw new IllegalArgumentException(MODEL_OBJ_ID_LIMIT);
183 }
184 }
185
186 /**
187 * Returns the first object from the model object data. If no objects are
188 * present then it returns null.
189 *
190 * @param modData model object data
191 * @return object
192 */
193 static Object getObjFromModData(ModelObjectData modData) {
194 List<ModelObject> obj = modData.modelObjects();
195 Iterator<ModelObject> it = obj.iterator();
196 if (it.hasNext()) {
197 return it.next();
198 }
199 return null;
200 }
201
202 /**
203 * Returns the model object id for with the devices object added to it.
204 *
205 * @return model object id
206 */
207 static ModelObjectId getModObjIdDriDevices() {
208 return ModelObjectId.builder().addChild(DefaultDevices.class).build();
209 }
210
211 /**
212 * Returns model object data built from the object that has to be added
213 * and the model object id.
214 *
215 * @param id model object id
216 * @param obj object
217 * @return model object data
218 */
219 static ModelObjectData getData(ModelObjectId id, InnerModelObject obj) {
220 return DefaultModelObjectData.builder().addModelObject(obj)
221 .identifier(id).build();
222 }
223}