Aaron Kruglikov | 309068e | 2017-03-17 15:25:54 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Aaron Kruglikov | 309068e | 2017-03-17 15:25:54 -0700 | [diff] [blame] | 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 | |
Thomas Vachuska | 59d24eb | 2017-03-22 10:57:34 -0700 | [diff] [blame] | 17 | package org.onosproject.netconf.client.impl; |
Aaron Kruglikov | 309068e | 2017-03-17 15:25:54 -0700 | [diff] [blame] | 18 | |
| 19 | import com.google.common.annotations.Beta; |
| 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
Aaron Kruglikov | 309068e | 2017-03-17 15:25:54 -0700 | [diff] [blame] | 22 | import org.apache.felix.scr.annotations.Deactivate; |
| 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 25 | import org.onosproject.config.DynamicConfigEvent; |
| 26 | import org.onosproject.config.DynamicConfigListener; |
| 27 | import org.onosproject.config.DynamicConfigService; |
Sithara Punnassery | 0b51d43 | 2017-03-28 01:09:28 -0700 | [diff] [blame] | 28 | import org.onosproject.config.ResourceIdParser; |
Aaron Kruglikov | 309068e | 2017-03-17 15:25:54 -0700 | [diff] [blame] | 29 | import org.onosproject.config.Filter; |
| 30 | import org.onosproject.mastership.MastershipService; |
| 31 | import org.onosproject.net.DeviceId; |
Thomas Vachuska | 59d24eb | 2017-03-22 10:57:34 -0700 | [diff] [blame] | 32 | import org.onosproject.netconf.NetconfController; |
| 33 | import org.onosproject.netconf.NetconfException; |
Aaron Kruglikov | 309068e | 2017-03-17 15:25:54 -0700 | [diff] [blame] | 34 | import org.onosproject.netconf.client.NetconfTranslator; |
| 35 | import org.onosproject.netconf.client.NetconfTranslator.OperationType; |
| 36 | import org.onosproject.yang.model.DataNode; |
Bharat saraswal | b0dbe6b | 2017-03-24 22:51:06 +0530 | [diff] [blame] | 37 | import org.onosproject.yang.model.InnerNode; |
Sithara Punnassery | 425837f | 2017-03-21 12:58:00 -0700 | [diff] [blame] | 38 | import org.onosproject.yang.model.LeafNode; |
Sithara Punnassery | bda8250 | 2017-03-22 19:08:19 -0700 | [diff] [blame] | 39 | import org.onosproject.yang.model.ListKey; |
Bharat saraswal | b0dbe6b | 2017-03-24 22:51:06 +0530 | [diff] [blame] | 40 | import org.onosproject.yang.model.NodeKey; |
Aaron Kruglikov | 309068e | 2017-03-17 15:25:54 -0700 | [diff] [blame] | 41 | import org.onosproject.yang.model.ResourceId; |
Thomas Vachuska | a4d5a49 | 2017-05-10 12:02:43 -0700 | [diff] [blame] | 42 | import org.onosproject.yang.model.DefaultResourceData; |
Sithara Punnassery | 326b61d | 2017-03-24 14:15:04 -0700 | [diff] [blame] | 43 | import org.onlab.util.AbstractAccumulator; |
| 44 | import org.onlab.util.Accumulator; |
Aaron Kruglikov | 309068e | 2017-03-17 15:25:54 -0700 | [diff] [blame] | 45 | import org.slf4j.Logger; |
| 46 | import org.slf4j.LoggerFactory; |
| 47 | |
| 48 | import java.io.IOException; |
Thomas Vachuska | 59d24eb | 2017-03-22 10:57:34 -0700 | [diff] [blame] | 49 | import java.net.URI; |
| 50 | import java.net.URISyntaxException; |
Sithara Punnassery | 0b51d43 | 2017-03-28 01:09:28 -0700 | [diff] [blame] | 51 | import java.util.ArrayList; |
Bharat saraswal | b0dbe6b | 2017-03-24 22:51:06 +0530 | [diff] [blame] | 52 | import java.util.Iterator; |
Sithara Punnassery | 0b51d43 | 2017-03-28 01:09:28 -0700 | [diff] [blame] | 53 | import java.util.LinkedHashMap; |
Bharat saraswal | b0dbe6b | 2017-03-24 22:51:06 +0530 | [diff] [blame] | 54 | import java.util.List; |
| 55 | import java.util.Map; |
Sithara Punnassery | 326b61d | 2017-03-24 14:15:04 -0700 | [diff] [blame] | 56 | import java.util.Timer; |
Bharat saraswal | b0dbe6b | 2017-03-24 22:51:06 +0530 | [diff] [blame] | 57 | |
| 58 | import static com.google.common.base.Preconditions.checkNotNull; |
Aaron Kruglikov | 309068e | 2017-03-17 15:25:54 -0700 | [diff] [blame] | 59 | |
Sithara Punnassery | 425837f | 2017-03-21 12:58:00 -0700 | [diff] [blame] | 60 | |
Aaron Kruglikov | 309068e | 2017-03-17 15:25:54 -0700 | [diff] [blame] | 61 | @Beta |
| 62 | @Component(immediate = true) |
Sithara Punnassery | 425837f | 2017-03-21 12:58:00 -0700 | [diff] [blame] | 63 | public class NetconfActiveComponent implements DynamicConfigListener { |
Aaron Kruglikov | 309068e | 2017-03-17 15:25:54 -0700 | [diff] [blame] | 64 | |
Sithara Punnassery | 425837f | 2017-03-21 12:58:00 -0700 | [diff] [blame] | 65 | private static final Logger log = LoggerFactory.getLogger(NetconfActiveComponent.class); |
Aaron Kruglikov | 309068e | 2017-03-17 15:25:54 -0700 | [diff] [blame] | 66 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Sithara Punnassery | 425837f | 2017-03-21 12:58:00 -0700 | [diff] [blame] | 67 | protected DynamicConfigService cfgService; |
Aaron Kruglikov | 309068e | 2017-03-17 15:25:54 -0700 | [diff] [blame] | 68 | |
| 69 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 70 | protected NetconfTranslator netconfTranslator; |
| 71 | |
| 72 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 73 | protected MastershipService mastershipService; |
| 74 | |
Sithara Punnassery | 425837f | 2017-03-21 12:58:00 -0700 | [diff] [blame] | 75 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 76 | protected NetconfController controller; |
| 77 | |
Sithara Punnassery | 326b61d | 2017-03-24 14:15:04 -0700 | [diff] [blame] | 78 | private final Accumulator<DynamicConfigEvent> accumulator = new InternalEventAccummulator(); |
| 79 | private static final String DEVNMSPACE = "ne-l3vpn-api"; |
| 80 | private static final String DEVICES = "devices"; |
| 81 | private static final String DEVICE = "device"; |
| 82 | private static final String DEVICE_ID = "deviceid"; |
Sithara Punnassery | dcc83b1 | 2017-03-28 18:19:14 -0700 | [diff] [blame] | 83 | private static final String DEF_IP = "0:0:0"; //hack, remove later |
Sithara Punnassery | 326b61d | 2017-03-24 14:15:04 -0700 | [diff] [blame] | 84 | |
| 85 | //Symbolic constants for use with the accumulator |
| 86 | private static final int MAX_EVENTS = 1000; |
| 87 | private static final int MAX_BATCH_MS = 5000; |
| 88 | private static final int MAX_IDLE_MS = 1000; |
Bharat saraswal | b0dbe6b | 2017-03-24 22:51:06 +0530 | [diff] [blame] | 89 | |
Sithara Punnassery | dcc83b1 | 2017-03-28 18:19:14 -0700 | [diff] [blame] | 90 | |
Sithara Punnassery | 0b51d43 | 2017-03-28 01:09:28 -0700 | [diff] [blame] | 91 | private ResourceId defParent = new ResourceId.Builder() |
Bharat saraswal | b0dbe6b | 2017-03-24 22:51:06 +0530 | [diff] [blame] | 92 | .addBranchPointSchema(DEVICES, DEVNMSPACE) |
| 93 | .addBranchPointSchema(DEVICE, DEVNMSPACE) |
Sithara Punnassery | dcc83b1 | 2017-03-28 18:19:14 -0700 | [diff] [blame] | 94 | //.addBranchPointSchema(DEVICE_ID, DEVNMSPACE) |
| 95 | .addKeyLeaf(DEVICE_ID, DEVNMSPACE, DEF_IP) |
Aaron Kruglikov | 309068e | 2017-03-17 15:25:54 -0700 | [diff] [blame] | 96 | .build(); |
Thomas Vachuska | 59d24eb | 2017-03-22 10:57:34 -0700 | [diff] [blame] | 97 | |
Sithara Punnassery | 0b51d43 | 2017-03-28 01:09:28 -0700 | [diff] [blame] | 98 | //TODO remove this hack after store ordering is fixed |
| 99 | private static final String EXCPATH = "root|devices#ne-l3vpn-api|" + |
| 100 | "device#ne-l3vpn-api$deviceid#ne-l3vpn-api#netconf:172.16.5.11:22|" + |
| 101 | "l3vpn#ne-l3vpn-api|l3vpncomm#ne-l3vpn-api|l3vpnInstances#ne-l3vpn-api|" + |
| 102 | "l3vpnInstance#ne-l3vpn-api$vrfName#ne-l3vpn-api#vrf2|l3vpnIfs#ne-l3vpn-api"; |
| 103 | //end of hack |
| 104 | |
Aaron Kruglikov | 309068e | 2017-03-17 15:25:54 -0700 | [diff] [blame] | 105 | @Activate |
| 106 | protected void activate() { |
Sithara Punnassery | 425837f | 2017-03-21 12:58:00 -0700 | [diff] [blame] | 107 | cfgService.addListener(this); |
Aaron Kruglikov | 309068e | 2017-03-17 15:25:54 -0700 | [diff] [blame] | 108 | log.info("Started"); |
| 109 | } |
| 110 | |
| 111 | @Deactivate |
| 112 | protected void deactivate() { |
Sithara Punnassery | 425837f | 2017-03-21 12:58:00 -0700 | [diff] [blame] | 113 | cfgService.removeListener(this); |
Aaron Kruglikov | 309068e | 2017-03-17 15:25:54 -0700 | [diff] [blame] | 114 | log.info("Stopped"); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public boolean isRelevant(DynamicConfigEvent event) { |
Sithara Punnassery | 0b51d43 | 2017-03-28 01:09:28 -0700 | [diff] [blame] | 119 | String resId = ResourceIdParser.parseResId(event.subject()); |
| 120 | String refId = ResourceIdParser.parseResId(defParent); |
Sithara Punnassery | dcc83b1 | 2017-03-28 18:19:14 -0700 | [diff] [blame] | 121 | refId = refId.substring(0, refId.length() - (DEF_IP.length() + 1)); |
| 122 | if (!resId.contains(refId)) { |
| 123 | return false; |
| 124 | } |
| 125 | if (resId.length() < refId.length()) { |
| 126 | return false; |
| 127 | } |
| 128 | return (resId.substring(0, (refId.length())).compareTo(refId) == 0); |
Aaron Kruglikov | 309068e | 2017-03-17 15:25:54 -0700 | [diff] [blame] | 129 | } |
Sithara Punnassery | 425837f | 2017-03-21 12:58:00 -0700 | [diff] [blame] | 130 | |
| 131 | public boolean isMaster(DeviceId deviceId) { |
Thomas Vachuska | 59d24eb | 2017-03-22 10:57:34 -0700 | [diff] [blame] | 132 | return mastershipService.isLocalMaster(deviceId); |
Sithara Punnassery | 425837f | 2017-03-21 12:58:00 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Sithara Punnassery | 326b61d | 2017-03-24 14:15:04 -0700 | [diff] [blame] | 135 | |
Aaron Kruglikov | 309068e | 2017-03-17 15:25:54 -0700 | [diff] [blame] | 136 | @Override |
| 137 | public void event(DynamicConfigEvent event) { |
Sithara Punnassery | 326b61d | 2017-03-24 14:15:04 -0700 | [diff] [blame] | 138 | accumulator.add(event); |
Aaron Kruglikov | 309068e | 2017-03-17 15:25:54 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Performs the delete operation corresponding to the passed event. |
Thomas Vachuska | 59d24eb | 2017-03-22 10:57:34 -0700 | [diff] [blame] | 143 | * |
| 144 | * @param node a relevant dataNode |
| 145 | * @param deviceId the deviceId of the device to be updated |
Aaron Kruglikov | 309068e | 2017-03-17 15:25:54 -0700 | [diff] [blame] | 146 | * @param resourceId the resourceId of the root of the subtree to be edited |
| 147 | * @return true if the update succeeds false otherwise |
| 148 | */ |
Sithara Punnassery | 0b51d43 | 2017-03-28 01:09:28 -0700 | [diff] [blame] | 149 | private boolean configDelete(DataNode node, DeviceId deviceId, ResourceId resourceId) { |
Aaron Kruglikov | 309068e | 2017-03-17 15:25:54 -0700 | [diff] [blame] | 150 | return parseAndEdit(node, deviceId, resourceId, OperationType.DELETE); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Performs the update operation corresponding to the passed event. |
Thomas Vachuska | 59d24eb | 2017-03-22 10:57:34 -0700 | [diff] [blame] | 155 | * |
| 156 | * @param node a relevant dataNode |
| 157 | * @param deviceId the deviceId of the device to be updated |
Aaron Kruglikov | 309068e | 2017-03-17 15:25:54 -0700 | [diff] [blame] | 158 | * @param resourceId the resourceId of the root of the subtree to be edited |
| 159 | * @return true if the update succeeds false otherwise |
| 160 | */ |
| 161 | private boolean configUpdate(DataNode node, DeviceId deviceId, ResourceId resourceId) { |
| 162 | return parseAndEdit(node, deviceId, resourceId, OperationType.REPLACE); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Parses the incoming event and pushes configuration to the effected |
| 167 | * device. |
Thomas Vachuska | 59d24eb | 2017-03-22 10:57:34 -0700 | [diff] [blame] | 168 | * |
| 169 | * @param node the dataNode effecting a particular device of which this node |
| 170 | * is master |
| 171 | * @param deviceId the deviceId of the device to be modified |
| 172 | * @param resourceId the resourceId of the root of the subtree to be edited |
Aaron Kruglikov | 309068e | 2017-03-17 15:25:54 -0700 | [diff] [blame] | 173 | * @param operationType the type of editing to be performed |
| 174 | * @return true if the operation succeeds, false otherwise |
| 175 | */ |
| 176 | private boolean parseAndEdit(DataNode node, DeviceId deviceId, |
| 177 | ResourceId resourceId, |
| 178 | NetconfTranslator.OperationType operationType) { |
Sithara Punnassery | dcc83b1 | 2017-03-28 18:19:14 -0700 | [diff] [blame] | 179 | //FIXME Separate edit and delete, delete can proceed with a null node |
Bharat saraswal | b0dbe6b | 2017-03-24 22:51:06 +0530 | [diff] [blame] | 180 | DefaultResourceData.Builder builder = DefaultResourceData.builder(); |
Sithara Punnassery | 0b51d43 | 2017-03-28 01:09:28 -0700 | [diff] [blame] | 181 | if (node != null) { |
| 182 | //add all low level nodes of devices |
| 183 | Iterator<Map.Entry<NodeKey, DataNode>> it = ((InnerNode) node) |
| 184 | .childNodes().entrySet().iterator(); |
| 185 | while (it.hasNext()) { |
| 186 | DataNode n = it.next().getValue(); |
| 187 | if (!n.key().schemaId().name().equals("deviceid")) { |
| 188 | builder.addDataNode(n); |
| 189 | } |
Bharat saraswal | b0dbe6b | 2017-03-24 22:51:06 +0530 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | //add resouce id //TODO: check if it is correct |
Aaron Kruglikov | 309068e | 2017-03-17 15:25:54 -0700 | [diff] [blame] | 193 | try { |
| 194 | return netconfTranslator.editDeviceConfig( |
Bharat saraswal | b0dbe6b | 2017-03-24 22:51:06 +0530 | [diff] [blame] | 195 | deviceId, builder.build(), operationType); |
Aaron Kruglikov | 309068e | 2017-03-17 15:25:54 -0700 | [diff] [blame] | 196 | } catch (IOException e) { |
Ray Milkey | ba547f9 | 2018-02-01 15:22:31 -0800 | [diff] [blame] | 197 | log.debug("parseAndEdit()", e); |
Aaron Kruglikov | 309068e | 2017-03-17 15:25:54 -0700 | [diff] [blame] | 198 | return false; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | /** |
Sithara Punnassery | 425837f | 2017-03-21 12:58:00 -0700 | [diff] [blame] | 203 | * Retrieves device id from Data node. |
| 204 | * |
| 205 | * @param node the node associated with the event |
Aaron Kruglikov | 309068e | 2017-03-17 15:25:54 -0700 | [diff] [blame] | 206 | * @return the deviceId of the effected device |
| 207 | */ |
Sithara Punnassery | bda8250 | 2017-03-22 19:08:19 -0700 | [diff] [blame] | 208 | @Beta |
Sithara Punnassery | 425837f | 2017-03-21 12:58:00 -0700 | [diff] [blame] | 209 | public DeviceId getDeviceId(DataNode node) { |
| 210 | String[] temp; |
| 211 | String ip, port; |
| 212 | if (node.type() == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) { |
| 213 | temp = ((LeafNode) node).asString().split("\\:"); |
| 214 | if (temp.length != 3) { |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame] | 215 | throw new IllegalStateException(new NetconfException("Invalid device id form, cannot apply")); |
Sithara Punnassery | 425837f | 2017-03-21 12:58:00 -0700 | [diff] [blame] | 216 | } |
| 217 | ip = temp[1]; |
| 218 | port = temp[2]; |
Sithara Punnassery | bda8250 | 2017-03-22 19:08:19 -0700 | [diff] [blame] | 219 | } else if (node.type() == DataNode.Type.MULTI_INSTANCE_NODE) { |
| 220 | ListKey key = (ListKey) node.key(); |
| 221 | temp = key.keyLeafs().get(0).leafValAsString().split("\\:"); |
| 222 | if (temp.length != 3) { |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame] | 223 | throw new IllegalStateException(new NetconfException("Invalid device id form, cannot apply")); |
Sithara Punnassery | bda8250 | 2017-03-22 19:08:19 -0700 | [diff] [blame] | 224 | } |
| 225 | ip = temp[1]; |
| 226 | port = temp[2]; |
Sithara Punnassery | 425837f | 2017-03-21 12:58:00 -0700 | [diff] [blame] | 227 | } else { |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame] | 228 | throw new IllegalStateException(new NetconfException("Invalid device id type, cannot apply")); |
Sithara Punnassery | 425837f | 2017-03-21 12:58:00 -0700 | [diff] [blame] | 229 | } |
| 230 | try { |
| 231 | return DeviceId.deviceId(new URI("netconf", ip + ":" + port, (String) null)); |
| 232 | } catch (URISyntaxException var4) { |
| 233 | throw new IllegalArgumentException("Unable to build deviceID for device " + ip + ":" + port, var4); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Inititates a Netconf connection to the device. |
| 239 | * |
| 240 | * @param deviceId of the added device |
| 241 | */ |
| 242 | private void initiateConnection(DeviceId deviceId) { |
| 243 | if (controller.getNetconfDevice(deviceId) == null) { |
| 244 | try { |
| 245 | //if (this.isReachable(deviceId)) { |
Thomas Vachuska | 59d24eb | 2017-03-22 10:57:34 -0700 | [diff] [blame] | 246 | this.controller.connectDevice(deviceId); |
Sithara Punnassery | 425837f | 2017-03-21 12:58:00 -0700 | [diff] [blame] | 247 | //} |
| 248 | } catch (Exception ex) { |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame] | 249 | throw new IllegalStateException(new NetconfException("Unable to connect to NETCONF device on " + |
Sithara Punnassery | 326b61d | 2017-03-24 14:15:04 -0700 | [diff] [blame] | 250 | deviceId, ex)); |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
Sithara Punnassery | 0b51d43 | 2017-03-28 01:09:28 -0700 | [diff] [blame] | 255 | /** |
| 256 | * Retrieves device key from Resource id. |
| 257 | * |
| 258 | * @param path associated with the event |
| 259 | * @return the deviceId of the effected device |
| 260 | */ |
| 261 | @Beta |
| 262 | public ResourceId getDeviceKey(ResourceId path) { |
| 263 | String resId = ResourceIdParser.parseResId(path); |
| 264 | String[] el = resId.split(ResourceIdParser.EL_CHK); |
Sithara Punnassery | dcc83b1 | 2017-03-28 18:19:14 -0700 | [diff] [blame] | 265 | if (el.length < 3) { |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame] | 266 | throw new IllegalStateException(new NetconfException("Invalid resource id, cannot apply")); |
Sithara Punnassery | 0b51d43 | 2017-03-28 01:09:28 -0700 | [diff] [blame] | 267 | } |
Sithara Punnassery | dcc83b1 | 2017-03-28 18:19:14 -0700 | [diff] [blame] | 268 | if (!el[2].contains((ResourceIdParser.KEY_SEP))) { |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame] | 269 | throw new IllegalStateException(new NetconfException("Invalid device id key, cannot apply")); |
Sithara Punnassery | 0b51d43 | 2017-03-28 01:09:28 -0700 | [diff] [blame] | 270 | } |
Sithara Punnassery | dcc83b1 | 2017-03-28 18:19:14 -0700 | [diff] [blame] | 271 | String[] keys = el[2].split(ResourceIdParser.KEY_CHK); |
Sithara Punnassery | 0b51d43 | 2017-03-28 01:09:28 -0700 | [diff] [blame] | 272 | if (keys.length < 2) { |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame] | 273 | throw new IllegalStateException(new NetconfException("Invalid device id key, cannot apply")); |
Sithara Punnassery | 0b51d43 | 2017-03-28 01:09:28 -0700 | [diff] [blame] | 274 | } |
| 275 | String[] parts = keys[1].split(ResourceIdParser.NM_CHK); |
| 276 | if (parts.length < 3) { |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame] | 277 | throw new IllegalStateException(new NetconfException("Invalid device id key, cannot apply")); |
Sithara Punnassery | 0b51d43 | 2017-03-28 01:09:28 -0700 | [diff] [blame] | 278 | } |
| 279 | if (parts[2].split("\\:").length != 3) { |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame] | 280 | throw new IllegalStateException(new NetconfException("Invalid device id form, cannot apply")); |
Sithara Punnassery | 0b51d43 | 2017-03-28 01:09:28 -0700 | [diff] [blame] | 281 | } |
| 282 | return (new ResourceId.Builder() |
Sithara Punnassery | dcc83b1 | 2017-03-28 18:19:14 -0700 | [diff] [blame] | 283 | .addBranchPointSchema(el[1].split(ResourceIdParser.NM_CHK)[0], |
| 284 | el[1].split(ResourceIdParser.NM_CHK)[1]) |
Sithara Punnassery | 0b51d43 | 2017-03-28 01:09:28 -0700 | [diff] [blame] | 285 | .addBranchPointSchema(keys[0].split(ResourceIdParser.NM_CHK)[0], |
| 286 | keys[0].split(ResourceIdParser.NM_CHK)[1]) |
| 287 | .addKeyLeaf(parts[0], parts[1], parts[2]) |
| 288 | .build()); |
Sithara Punnassery | 0b51d43 | 2017-03-28 01:09:28 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Retrieves device id from Resource id. |
| 293 | * |
| 294 | * @param path associated with the event |
| 295 | * @return the deviceId of the effected device |
| 296 | */ |
| 297 | @Beta |
| 298 | public DeviceId getDeviceId(ResourceId path) { |
| 299 | String resId = ResourceIdParser.parseResId(path); |
| 300 | String[] el = resId.split(ResourceIdParser.EL_CHK); |
Sithara Punnassery | dcc83b1 | 2017-03-28 18:19:14 -0700 | [diff] [blame] | 301 | if (el.length < 3) { |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame] | 302 | throw new IllegalStateException(new NetconfException("Invalid resource id, cannot apply")); |
Sithara Punnassery | 0b51d43 | 2017-03-28 01:09:28 -0700 | [diff] [blame] | 303 | } |
| 304 | if (!el[2].contains((ResourceIdParser.KEY_SEP))) { |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame] | 305 | throw new IllegalStateException(new NetconfException("Invalid device id key, cannot apply")); |
Sithara Punnassery | 0b51d43 | 2017-03-28 01:09:28 -0700 | [diff] [blame] | 306 | } |
| 307 | String[] keys = el[2].split(ResourceIdParser.KEY_CHK); |
| 308 | if (keys.length < 2) { |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame] | 309 | throw new IllegalStateException(new NetconfException("Invalid device id key, cannot apply")); |
Sithara Punnassery | 0b51d43 | 2017-03-28 01:09:28 -0700 | [diff] [blame] | 310 | } |
| 311 | String[] parts = keys[1].split(ResourceIdParser.NM_CHK); |
| 312 | if (parts.length < 3) { |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame] | 313 | throw new IllegalStateException(new NetconfException("Invalid device id key, cannot apply")); |
Sithara Punnassery | 0b51d43 | 2017-03-28 01:09:28 -0700 | [diff] [blame] | 314 | } |
| 315 | String[] temp = parts[2].split("\\:"); |
| 316 | String ip, port; |
| 317 | if (temp.length != 3) { |
Ray Milkey | 986a47a | 2018-01-25 11:38:51 -0800 | [diff] [blame] | 318 | throw new IllegalStateException(new NetconfException("Invalid device id form, cannot apply")); |
Sithara Punnassery | 0b51d43 | 2017-03-28 01:09:28 -0700 | [diff] [blame] | 319 | } |
| 320 | ip = temp[1]; |
| 321 | port = temp[2]; |
| 322 | try { |
| 323 | return DeviceId.deviceId(new URI("netconf", ip + ":" + port, (String) null)); |
| 324 | } catch (URISyntaxException ex) { |
| 325 | throw new IllegalArgumentException("Unable to build deviceID for device " + ip + ":" + port, ex); |
| 326 | } |
| 327 | } |
| 328 | |
Sithara Punnassery | 326b61d | 2017-03-24 14:15:04 -0700 | [diff] [blame] | 329 | /* Accumulates events to allow processing after a desired number of events were accumulated. |
| 330 | */ |
| 331 | private class InternalEventAccummulator extends AbstractAccumulator<DynamicConfigEvent> { |
| 332 | protected InternalEventAccummulator() { |
| 333 | super(new Timer("dyncfgevt-timer"), MAX_EVENTS, MAX_BATCH_MS, MAX_IDLE_MS); |
| 334 | } |
| 335 | @Override |
| 336 | public void processItems(List<DynamicConfigEvent> events) { |
Sithara Punnassery | 0b51d43 | 2017-03-28 01:09:28 -0700 | [diff] [blame] | 337 | Map<ResourceId, List<DynamicConfigEvent>> evtmap = new LinkedHashMap<>(); |
| 338 | Boolean handleEx = false; //hack |
| 339 | ResourceId exId = null; //hack |
Sithara Punnassery | 326b61d | 2017-03-24 14:15:04 -0700 | [diff] [blame] | 340 | for (DynamicConfigEvent e : events) { |
Sithara Punnassery | 0b51d43 | 2017-03-28 01:09:28 -0700 | [diff] [blame] | 341 | checkNotNull(e, "process:Event cannot be null"); |
| 342 | ResourceId cur = e.subject(); |
| 343 | //TODO remove this hack after store ordering is fixed |
Sithara Punnassery | dcc83b1 | 2017-03-28 18:19:14 -0700 | [diff] [blame] | 344 | String expat = ResourceIdParser.parseResId(cur); |
| 345 | if ((expat.compareTo(EXCPATH) == 0) && (e.type() == DynamicConfigEvent.Type.NODE_ADDED)) { |
Sithara Punnassery | 0b51d43 | 2017-03-28 01:09:28 -0700 | [diff] [blame] | 346 | handleEx = true; |
| 347 | exId = cur; |
Sithara Punnassery | 0b51d43 | 2017-03-28 01:09:28 -0700 | [diff] [blame] | 348 | } else { //actual code |
| 349 | ResourceId key = getDeviceKey(e.subject()); |
| 350 | List<DynamicConfigEvent> el = evtmap.get(key); |
| 351 | if (el == null) { |
| 352 | el = new ArrayList<>(); |
| 353 | } |
| 354 | el.add(e); |
| 355 | evtmap.put(key, el); |
Sithara Punnassery | 326b61d | 2017-03-24 14:15:04 -0700 | [diff] [blame] | 356 | } |
Sithara Punnassery | 425837f | 2017-03-21 12:58:00 -0700 | [diff] [blame] | 357 | } |
Sithara Punnassery | 0b51d43 | 2017-03-28 01:09:28 -0700 | [diff] [blame] | 358 | evtmap.forEach((k, v) -> { |
Sithara Punnassery | 0b51d43 | 2017-03-28 01:09:28 -0700 | [diff] [blame] | 359 | DeviceId curDevice = getDeviceId(k); |
| 360 | if (!isMaster(curDevice)) { |
| 361 | log.info("NetConfListener: not master, ignoring config for device {}", k); |
| 362 | return; |
| 363 | } |
| 364 | initiateConnection(curDevice); |
Sithara Punnassery | dcc83b1 | 2017-03-28 18:19:14 -0700 | [diff] [blame] | 365 | for (DynamicConfigEvent curEvt : v) { |
Sithara Punnassery | 0b51d43 | 2017-03-28 01:09:28 -0700 | [diff] [blame] | 366 | switch (curEvt.type()) { |
| 367 | case NODE_ADDED: |
| 368 | case NODE_UPDATED: |
| 369 | case NODE_REPLACED: |
Yuta HIGUCHI | ac85ee1 | 2017-08-03 20:07:35 -0700 | [diff] [blame] | 370 | Filter filt = Filter.builder().build(); |
Sithara Punnassery | 0b51d43 | 2017-03-28 01:09:28 -0700 | [diff] [blame] | 371 | DataNode node = cfgService.readNode(k, filt); |
| 372 | configUpdate(node, curDevice, k); |
| 373 | break; |
| 374 | case NODE_DELETED: |
Sithara Punnassery | dcc83b1 | 2017-03-28 18:19:14 -0700 | [diff] [blame] | 375 | configDelete(null, curDevice, k); //TODO curEvt.subject())?? |
Sithara Punnassery | 0b51d43 | 2017-03-28 01:09:28 -0700 | [diff] [blame] | 376 | break; |
| 377 | case UNKNOWN_OPRN: |
| 378 | default: |
| 379 | log.warn("NetConfListener: unknown event: {}", curEvt.type()); |
| 380 | break; |
| 381 | } |
| 382 | } |
| 383 | }); |
| 384 | //TODO remove this hack after store ordering is fixed |
| 385 | if (handleEx) { |
Sithara Punnassery | 0b51d43 | 2017-03-28 01:09:28 -0700 | [diff] [blame] | 386 | DeviceId exDevice = getDeviceId(exId); |
| 387 | if (!isMaster(exDevice)) { |
| 388 | log.info("NetConfListener: not master, ignoring config for expath {}", exId); |
| 389 | return; |
| 390 | } |
| 391 | initiateConnection(exDevice); |
Yuta HIGUCHI | ac85ee1 | 2017-08-03 20:07:35 -0700 | [diff] [blame] | 392 | Filter filt = Filter.builder().build(); |
Sithara Punnassery | 0b51d43 | 2017-03-28 01:09:28 -0700 | [diff] [blame] | 393 | DataNode exnode = cfgService.readNode(exId, filt); |
| 394 | configUpdate(exnode, exDevice, exId); |
| 395 | } //end of hack |
Sithara Punnassery | 425837f | 2017-03-21 12:58:00 -0700 | [diff] [blame] | 396 | } |
Aaron Kruglikov | 309068e | 2017-03-17 15:25:54 -0700 | [diff] [blame] | 397 | } |
| 398 | } |