blob: f474a5cb78f1a3f7fdf8d3c32722d9d5fbe3eeea [file] [log] [blame]
MaoLu819fde22017-04-20 17:17:49 -07001/*
2 * Copyright 2016 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.oplink;
18
19import org.apache.commons.configuration.HierarchicalConfiguration;
20import org.onosproject.drivers.utilities.XmlConfigParser;
21import org.onosproject.net.driver.DriverHandler;
22import org.onosproject.netconf.NetconfController;
23import org.onosproject.netconf.NetconfException;
24import org.onosproject.netconf.NetconfSession;
25
26import java.io.IOException;
27
28import static com.google.common.base.Preconditions.checkNotNull;
29
30/**
31 * Netconf utility for oplink netconf drivers.
32 */
33public final class OplinkNetconfUtility {
34
35 // public used nodes
36 public static final String CFG_TAR_RUNNING = "running";
37 public static final String CFG_MODE_MERGE = "merge";
38 public static final String CFG_MODE_REMOVE = "remove";
39 public static final String KEY_XMLNS = "xmlns=\"http://com/att/device\"";
40 public static final String KEY_DATA = "data";
41 public static final String KEY_OPENOPTICALDEV = "open-optical-device";
42 public static final String KEY_CONNS = "connections";
43 public static final String KEY_CONNID = "connection-id";
44 public static final String KEY_PORTS = "ports";
45 public static final String KEY_PORTID = "port-id";
46 public static final String KEY_PORT = "port";
47 public static final String KEY_PORTDIRECT = "port-direction";
48 public static final String KEY_CHATT = "attenuation";
49 public static final String KEY_DATA_CONNS = String.format("%s.%s.%s", KEY_DATA, KEY_OPENOPTICALDEV, KEY_CONNS);
50 public static final String KEY_DATA_PORTS = String.format("%s.%s.%s", KEY_DATA, KEY_OPENOPTICALDEV, KEY_PORTS);
51 public static final String KEY_OPENOPTICALDEV_XMLNS = String.format("%s %s", KEY_OPENOPTICALDEV, KEY_XMLNS);
52
53 private OplinkNetconfUtility() {
54 }
55
56 /**
57 * Retrieves session reply information for get config operation.
58 *
59 * @param handler parent driver handler
60 * @param filter the filter string of xml content
61 * @return the reply string
62 */
63 public static String netconfGetConfig(DriverHandler handler, String filter) {
64 NetconfController controller = checkNotNull(handler.get(NetconfController.class));
65 NetconfSession session = controller.getNetconfDevice(handler.data().deviceId()).getSession();
66 String reply;
67 try {
68 reply = session.getConfig(CFG_TAR_RUNNING, filter);
69 } catch (IOException e) {
70 throw new RuntimeException(new NetconfException("Failed to retrieve configuration.", e));
71 }
72 return reply;
73 }
74
75 /**
76 * Retrieves session reply information for edit config operation.
77 *
78 * @param handler parent driver handler
79 * @param mode selected mode to change the configuration
80 * @param cfg the new configuration to be set
81 * @return the reply string
82 */
83 public static boolean netconfEditConfig(DriverHandler handler, String mode, String cfg) {
84 NetconfController controller = checkNotNull(handler.get(NetconfController.class));
85 NetconfSession session = controller.getNetconfDevice(handler.data().deviceId()).getSession();
86 boolean reply = false;
87 try {
88 reply = session.editConfig(CFG_TAR_RUNNING, mode, cfg);
89 } catch (IOException e) {
90 throw new RuntimeException(new NetconfException("Failed to edit configuration.", e));
91 }
92 return reply;
93 }
94
95 /**
96 * Retrieves specified node hierarchical configuration from the xml information.
97 *
98 * @param content the xml information
99 * @param key the configuration key node
100 * @return the hierarchical configuration
101 */
102 public static HierarchicalConfiguration configAt(String content, String key) {
103 HierarchicalConfiguration cfg = XmlConfigParser.loadXmlString(content);
104 HierarchicalConfiguration info;
105 try {
106 info = cfg.configurationAt(key);
107 } catch (Exception e) {
108 // Accept null for information polling
109 return null;
110 }
111 return info;
112 }
113
114 /**
115 * Makes a xml format sentence.
116 *
117 * @param node the node name
118 * @param content the node content
119 * @return the xml format sentence
120 */
121 public static String xml(String node, String content) {
122 return String.format("<%s>%s</%s>", node, content, node);
123 }
124
125 /**
126 * Makes a xml format open tag.
127 *
128 * @param node the node name
129 * @return the xml head format string
130 */
131 public static String xmlOpen(String node) {
132 return String.format("<%s>", node);
133 }
134
135 /**
136 * Makes a xml format close tag.
137 *
138 * @param node the node name
139 * @return the xml end format string
140 */
141 public static String xmlClose(String node) {
142 return String.format("</%s>", node);
143 }
144
145 /**
146 * Makes a xml format empty tag.
147 *
148 * @param node the node name
149 * @return the xml format of empty tag
150 */
151 public static String xmlEmpty(String node) {
152 return String.format("<%s/>", node);
153 }
154}