blob: c7053daff36c6e39f55cc7e4f581e281b91964bd [file] [log] [blame]
MaoLu819fde22017-04-20 17:17:49 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016 Open Networking Foundation
MaoLu819fde22017-04-20 17:17:49 -07003 *
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.oplink;
18
MaoLu3308ca42017-05-10 20:18:54 -070019import com.google.common.collect.ImmutableList;
MaoLu819fde22017-04-20 17:17:49 -070020import org.apache.commons.configuration.HierarchicalConfiguration;
21import org.onosproject.drivers.utilities.XmlConfigParser;
22import org.onosproject.net.driver.DriverHandler;
MaoLu3308ca42017-05-10 20:18:54 -070023import org.onosproject.netconf.DatastoreId;
MaoLu819fde22017-04-20 17:17:49 -070024import org.onosproject.netconf.NetconfController;
25import org.onosproject.netconf.NetconfException;
26import org.onosproject.netconf.NetconfSession;
27
MaoLu3308ca42017-05-10 20:18:54 -070028import java.util.List;
MaoLu819fde22017-04-20 17:17:49 -070029
30import static com.google.common.base.Preconditions.checkNotNull;
31
32/**
33 * Netconf utility for oplink netconf drivers.
34 */
35public final class OplinkNetconfUtility {
36
37 // public used nodes
MaoLu819fde22017-04-20 17:17:49 -070038 public static final String CFG_MODE_MERGE = "merge";
39 public static final String CFG_MODE_REMOVE = "remove";
40 public static final String KEY_XMLNS = "xmlns=\"http://com/att/device\"";
41 public static final String KEY_DATA = "data";
42 public static final String KEY_OPENOPTICALDEV = "open-optical-device";
43 public static final String KEY_CONNS = "connections";
44 public static final String KEY_CONNID = "connection-id";
45 public static final String KEY_PORTS = "ports";
46 public static final String KEY_PORTID = "port-id";
47 public static final String KEY_PORT = "port";
48 public static final String KEY_PORTDIRECT = "port-direction";
49 public static final String KEY_CHATT = "attenuation";
50 public static final String KEY_DATA_CONNS = String.format("%s.%s.%s", KEY_DATA, KEY_OPENOPTICALDEV, KEY_CONNS);
51 public static final String KEY_DATA_PORTS = String.format("%s.%s.%s", KEY_DATA, KEY_OPENOPTICALDEV, KEY_PORTS);
52 public static final String KEY_OPENOPTICALDEV_XMLNS = String.format("%s %s", KEY_OPENOPTICALDEV, KEY_XMLNS);
53
54 private OplinkNetconfUtility() {
55 }
56
57 /**
MaoLu3308ca42017-05-10 20:18:54 -070058 * Retrieves session reply information for get operation.
59 *
60 * @param handler parent driver handler
61 * @param filter the filter string of xml content
62 * @return the reply string
63 */
64 public static String netconfGet(DriverHandler handler, String filter) {
65 NetconfController controller = checkNotNull(handler.get(NetconfController.class));
66 NetconfSession session = controller.getNetconfDevice(handler.data().deviceId()).getSession();
67 String reply;
68 try {
69 reply = session.get(filter, null);
Yuta HIGUCHI234eaf32017-09-06 13:45:05 -070070 } catch (NetconfException e) {
MaoLu3308ca42017-05-10 20:18:54 -070071 throw new RuntimeException(new NetconfException("Failed to retrieve configuration.", e));
72 }
73 return reply;
74 }
75
76 /**
MaoLu819fde22017-04-20 17:17:49 -070077 * Retrieves session reply information for get config operation.
78 *
79 * @param handler parent driver handler
80 * @param filter the filter string of xml content
81 * @return the reply string
82 */
83 public static String netconfGetConfig(DriverHandler handler, String filter) {
84 NetconfController controller = checkNotNull(handler.get(NetconfController.class));
85 NetconfSession session = controller.getNetconfDevice(handler.data().deviceId()).getSession();
86 String reply;
87 try {
MaoLu3308ca42017-05-10 20:18:54 -070088 reply = session.getConfig(DatastoreId.RUNNING, filter);
Yuta HIGUCHI234eaf32017-09-06 13:45:05 -070089 } catch (NetconfException e) {
MaoLu819fde22017-04-20 17:17:49 -070090 throw new RuntimeException(new NetconfException("Failed to retrieve configuration.", e));
91 }
92 return reply;
93 }
94
95 /**
96 * Retrieves session reply information for edit config operation.
97 *
98 * @param handler parent driver handler
99 * @param mode selected mode to change the configuration
100 * @param cfg the new configuration to be set
101 * @return the reply string
102 */
103 public static boolean netconfEditConfig(DriverHandler handler, String mode, String cfg) {
104 NetconfController controller = checkNotNull(handler.get(NetconfController.class));
105 NetconfSession session = controller.getNetconfDevice(handler.data().deviceId()).getSession();
106 boolean reply = false;
107 try {
MaoLu3308ca42017-05-10 20:18:54 -0700108 reply = session.editConfig(DatastoreId.RUNNING, mode, cfg);
Yuta HIGUCHI234eaf32017-09-06 13:45:05 -0700109 } catch (NetconfException e) {
MaoLu819fde22017-04-20 17:17:49 -0700110 throw new RuntimeException(new NetconfException("Failed to edit configuration.", e));
111 }
112 return reply;
113 }
114
115 /**
116 * Retrieves specified node hierarchical configuration from the xml information.
117 *
118 * @param content the xml information
119 * @param key the configuration key node
MaoLu3308ca42017-05-10 20:18:54 -0700120 * @return the hierarchical configuration, null if exception happens
MaoLu819fde22017-04-20 17:17:49 -0700121 */
122 public static HierarchicalConfiguration configAt(String content, String key) {
MaoLu819fde22017-04-20 17:17:49 -0700123 HierarchicalConfiguration info;
124 try {
MaoLu3308ca42017-05-10 20:18:54 -0700125 HierarchicalConfiguration cfg = XmlConfigParser.loadXmlString(content);
MaoLu819fde22017-04-20 17:17:49 -0700126 info = cfg.configurationAt(key);
127 } catch (Exception e) {
128 // Accept null for information polling
129 return null;
130 }
131 return info;
132 }
133
134 /**
MaoLu3308ca42017-05-10 20:18:54 -0700135 * Retrieves specified node hierarchical configurations from the xml information.
136 *
137 * @param content the xml information
138 * @param key the configuration key node
139 * @return the hierarchical configurations, empty if exception happens
140 */
141 public static List<HierarchicalConfiguration> configsAt(String content, String key) {
142 List<HierarchicalConfiguration> info;
143 try {
144 HierarchicalConfiguration cfg = XmlConfigParser.loadXmlString(content);
145 info = cfg.configurationsAt(key);
146 } catch (Exception e) {
147 // Accept empty for information polling
148 return ImmutableList.of();
149 }
150 return info;
151 }
152
153 /**
MaoLu819fde22017-04-20 17:17:49 -0700154 * Makes a xml format sentence.
155 *
156 * @param node the node name
157 * @param content the node content
158 * @return the xml format sentence
159 */
160 public static String xml(String node, String content) {
161 return String.format("<%s>%s</%s>", node, content, node);
162 }
163
164 /**
165 * Makes a xml format open tag.
166 *
167 * @param node the node name
168 * @return the xml head format string
169 */
170 public static String xmlOpen(String node) {
171 return String.format("<%s>", node);
172 }
173
174 /**
175 * Makes a xml format close tag.
176 *
177 * @param node the node name
178 * @return the xml end format string
179 */
180 public static String xmlClose(String node) {
181 return String.format("</%s>", node);
182 }
183
184 /**
185 * Makes a xml format empty tag.
186 *
187 * @param node the node name
188 * @return the xml format of empty tag
189 */
190 public static String xmlEmpty(String node) {
191 return String.format("<%s/>", node);
192 }
193}