Aaron Kruglikov | 309068e | 2017-03-17 15:25:54 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
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; |
| 28 | import org.onosproject.config.Filter; |
| 29 | import org.onosproject.mastership.MastershipService; |
| 30 | import org.onosproject.net.DeviceId; |
Thomas Vachuska | 59d24eb | 2017-03-22 10:57:34 -0700 | [diff] [blame] | 31 | import org.onosproject.netconf.NetconfController; |
| 32 | import org.onosproject.netconf.NetconfException; |
Aaron Kruglikov | 309068e | 2017-03-17 15:25:54 -0700 | [diff] [blame] | 33 | import org.onosproject.netconf.client.NetconfTranslator; |
| 34 | import org.onosproject.netconf.client.NetconfTranslator.OperationType; |
| 35 | import org.onosproject.yang.model.DataNode; |
Sithara Punnassery | 425837f | 2017-03-21 12:58:00 -0700 | [diff] [blame] | 36 | import org.onosproject.yang.model.LeafNode; |
Sithara Punnassery | bda8250 | 2017-03-22 19:08:19 -0700 | [diff] [blame^] | 37 | import org.onosproject.yang.model.ListKey; |
Aaron Kruglikov | 309068e | 2017-03-17 15:25:54 -0700 | [diff] [blame] | 38 | import org.onosproject.yang.model.ResourceId; |
| 39 | import org.onosproject.yang.runtime.DefaultResourceData; |
| 40 | import org.slf4j.Logger; |
| 41 | import org.slf4j.LoggerFactory; |
| 42 | |
| 43 | import java.io.IOException; |
Thomas Vachuska | 59d24eb | 2017-03-22 10:57:34 -0700 | [diff] [blame] | 44 | import java.net.URI; |
| 45 | import java.net.URISyntaxException; |
Aaron Kruglikov | 309068e | 2017-03-17 15:25:54 -0700 | [diff] [blame] | 46 | |
Sithara Punnassery | 425837f | 2017-03-21 12:58:00 -0700 | [diff] [blame] | 47 | |
Aaron Kruglikov | 309068e | 2017-03-17 15:25:54 -0700 | [diff] [blame] | 48 | @Beta |
| 49 | @Component(immediate = true) |
Sithara Punnassery | 425837f | 2017-03-21 12:58:00 -0700 | [diff] [blame] | 50 | public class NetconfActiveComponent implements DynamicConfigListener { |
Aaron Kruglikov | 309068e | 2017-03-17 15:25:54 -0700 | [diff] [blame] | 51 | |
Sithara Punnassery | 425837f | 2017-03-21 12:58:00 -0700 | [diff] [blame] | 52 | private static final Logger log = LoggerFactory.getLogger(NetconfActiveComponent.class); |
Aaron Kruglikov | 309068e | 2017-03-17 15:25:54 -0700 | [diff] [blame] | 53 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Sithara Punnassery | 425837f | 2017-03-21 12:58:00 -0700 | [diff] [blame] | 54 | protected DynamicConfigService cfgService; |
Sithara Punnassery | bda8250 | 2017-03-22 19:08:19 -0700 | [diff] [blame^] | 55 | public static final String DEVNMSPACE = "ne-l3vpn-api"; |
Aaron Kruglikov | 309068e | 2017-03-17 15:25:54 -0700 | [diff] [blame] | 56 | |
| 57 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 58 | protected NetconfTranslator netconfTranslator; |
| 59 | |
| 60 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 61 | protected MastershipService mastershipService; |
| 62 | |
Sithara Punnassery | 425837f | 2017-03-21 12:58:00 -0700 | [diff] [blame] | 63 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 64 | protected NetconfController controller; |
| 65 | |
Aaron Kruglikov | 309068e | 2017-03-17 15:25:54 -0700 | [diff] [blame] | 66 | private ResourceId resId = new ResourceId.Builder() |
Thomas Vachuska | 59d24eb | 2017-03-22 10:57:34 -0700 | [diff] [blame] | 67 | .addBranchPointSchema("device", DEVNMSPACE) |
Sithara Punnassery | bda8250 | 2017-03-22 19:08:19 -0700 | [diff] [blame^] | 68 | .addBranchPointSchema("device", DEVNMSPACE) |
| 69 | .addKeyLeaf("deviceid", DEVNMSPACE, "netconf:172.16.5.11:22") |
Aaron Kruglikov | 309068e | 2017-03-17 15:25:54 -0700 | [diff] [blame] | 70 | .build(); |
Thomas Vachuska | 59d24eb | 2017-03-22 10:57:34 -0700 | [diff] [blame] | 71 | |
Aaron Kruglikov | 309068e | 2017-03-17 15:25:54 -0700 | [diff] [blame] | 72 | @Activate |
| 73 | protected void activate() { |
Sithara Punnassery | 425837f | 2017-03-21 12:58:00 -0700 | [diff] [blame] | 74 | cfgService.addListener(this); |
Aaron Kruglikov | 309068e | 2017-03-17 15:25:54 -0700 | [diff] [blame] | 75 | log.info("Started"); |
| 76 | } |
| 77 | |
| 78 | @Deactivate |
| 79 | protected void deactivate() { |
Sithara Punnassery | 425837f | 2017-03-21 12:58:00 -0700 | [diff] [blame] | 80 | cfgService.removeListener(this); |
Aaron Kruglikov | 309068e | 2017-03-17 15:25:54 -0700 | [diff] [blame] | 81 | log.info("Stopped"); |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public boolean isRelevant(DynamicConfigEvent event) { |
Thomas Vachuska | 59d24eb | 2017-03-22 10:57:34 -0700 | [diff] [blame] | 86 | return event.subject().equals(resId); |
Aaron Kruglikov | 309068e | 2017-03-17 15:25:54 -0700 | [diff] [blame] | 87 | } |
Sithara Punnassery | 425837f | 2017-03-21 12:58:00 -0700 | [diff] [blame] | 88 | |
| 89 | public boolean isMaster(DeviceId deviceId) { |
Thomas Vachuska | 59d24eb | 2017-03-22 10:57:34 -0700 | [diff] [blame] | 90 | return mastershipService.isLocalMaster(deviceId); |
Sithara Punnassery | 425837f | 2017-03-21 12:58:00 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Aaron Kruglikov | 309068e | 2017-03-17 15:25:54 -0700 | [diff] [blame] | 93 | @Override |
| 94 | public void event(DynamicConfigEvent event) { |
| 95 | Filter filt = new Filter(); |
Sithara Punnassery | 425837f | 2017-03-21 12:58:00 -0700 | [diff] [blame] | 96 | DataNode node = cfgService.readNode(event.subject(), filt); |
| 97 | DeviceId deviceId = getDeviceId(node); |
| 98 | if (!isMaster(deviceId)) { |
| 99 | log.info("NetConfListener: not master, ignoring config for {}", event.type()); |
| 100 | return; |
| 101 | } |
| 102 | initiateConnection(deviceId); |
Aaron Kruglikov | 309068e | 2017-03-17 15:25:54 -0700 | [diff] [blame] | 103 | switch (event.type()) { |
| 104 | case NODE_ADDED: |
| 105 | case NODE_UPDATED: |
| 106 | case NODE_REPLACED: |
| 107 | configUpdate(node, deviceId, event.subject()); |
| 108 | break; |
| 109 | case NODE_DELETED: |
| 110 | configDelete(node, deviceId, event.subject()); |
| 111 | break; |
| 112 | case UNKNOWN_OPRN: |
| 113 | default: |
| 114 | log.warn("NetConfListener: unknown event: {}", event.type()); |
| 115 | break; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Performs the delete operation corresponding to the passed event. |
Thomas Vachuska | 59d24eb | 2017-03-22 10:57:34 -0700 | [diff] [blame] | 121 | * |
| 122 | * @param node a relevant dataNode |
| 123 | * @param deviceId the deviceId of the device to be updated |
Aaron Kruglikov | 309068e | 2017-03-17 15:25:54 -0700 | [diff] [blame] | 124 | * @param resourceId the resourceId of the root of the subtree to be edited |
| 125 | * @return true if the update succeeds false otherwise |
| 126 | */ |
| 127 | private boolean configDelete(DataNode node, DeviceId deviceId, ResourceId resourceId) { |
| 128 | return parseAndEdit(node, deviceId, resourceId, OperationType.DELETE); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Performs the update operation corresponding to the passed event. |
Thomas Vachuska | 59d24eb | 2017-03-22 10:57:34 -0700 | [diff] [blame] | 133 | * |
| 134 | * @param node a relevant dataNode |
| 135 | * @param deviceId the deviceId of the device to be updated |
Aaron Kruglikov | 309068e | 2017-03-17 15:25:54 -0700 | [diff] [blame] | 136 | * @param resourceId the resourceId of the root of the subtree to be edited |
| 137 | * @return true if the update succeeds false otherwise |
| 138 | */ |
| 139 | private boolean configUpdate(DataNode node, DeviceId deviceId, ResourceId resourceId) { |
| 140 | return parseAndEdit(node, deviceId, resourceId, OperationType.REPLACE); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Parses the incoming event and pushes configuration to the effected |
| 145 | * device. |
Thomas Vachuska | 59d24eb | 2017-03-22 10:57:34 -0700 | [diff] [blame] | 146 | * |
| 147 | * @param node the dataNode effecting a particular device of which this node |
| 148 | * is master |
| 149 | * @param deviceId the deviceId of the device to be modified |
| 150 | * @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] | 151 | * @param operationType the type of editing to be performed |
| 152 | * @return true if the operation succeeds, false otherwise |
| 153 | */ |
| 154 | private boolean parseAndEdit(DataNode node, DeviceId deviceId, |
| 155 | ResourceId resourceId, |
| 156 | NetconfTranslator.OperationType operationType) { |
| 157 | try { |
| 158 | return netconfTranslator.editDeviceConfig( |
| 159 | deviceId, |
| 160 | DefaultResourceData.builder() |
| 161 | .addDataNode(node) |
| 162 | .resourceId(resourceId) |
| 163 | .build(), |
| 164 | operationType); |
| 165 | } catch (IOException e) { |
| 166 | e.printStackTrace(); |
| 167 | return false; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /** |
Sithara Punnassery | 425837f | 2017-03-21 12:58:00 -0700 | [diff] [blame] | 172 | * Retrieves device id from Data node. |
| 173 | * |
| 174 | * @param node the node associated with the event |
Aaron Kruglikov | 309068e | 2017-03-17 15:25:54 -0700 | [diff] [blame] | 175 | * @return the deviceId of the effected device |
| 176 | */ |
Sithara Punnassery | bda8250 | 2017-03-22 19:08:19 -0700 | [diff] [blame^] | 177 | @Beta |
Sithara Punnassery | 425837f | 2017-03-21 12:58:00 -0700 | [diff] [blame] | 178 | public DeviceId getDeviceId(DataNode node) { |
| 179 | String[] temp; |
| 180 | String ip, port; |
| 181 | if (node.type() == DataNode.Type.SINGLE_INSTANCE_LEAF_VALUE_NODE) { |
| 182 | temp = ((LeafNode) node).asString().split("\\:"); |
| 183 | if (temp.length != 3) { |
| 184 | throw new RuntimeException(new NetconfException("Invalid device id form, cannot apply")); |
| 185 | } |
| 186 | ip = temp[1]; |
| 187 | port = temp[2]; |
Sithara Punnassery | bda8250 | 2017-03-22 19:08:19 -0700 | [diff] [blame^] | 188 | } else if (node.type() == DataNode.Type.MULTI_INSTANCE_NODE) { |
| 189 | ListKey key = (ListKey) node.key(); |
| 190 | temp = key.keyLeafs().get(0).leafValAsString().split("\\:"); |
| 191 | if (temp.length != 3) { |
| 192 | throw new RuntimeException(new NetconfException("Invalid device id form, cannot apply")); |
| 193 | } |
| 194 | ip = temp[1]; |
| 195 | port = temp[2]; |
Sithara Punnassery | 425837f | 2017-03-21 12:58:00 -0700 | [diff] [blame] | 196 | } else { |
| 197 | throw new RuntimeException(new NetconfException("Invalid device id type, cannot apply")); |
| 198 | } |
| 199 | try { |
| 200 | return DeviceId.deviceId(new URI("netconf", ip + ":" + port, (String) null)); |
| 201 | } catch (URISyntaxException var4) { |
| 202 | throw new IllegalArgumentException("Unable to build deviceID for device " + ip + ":" + port, var4); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Inititates a Netconf connection to the device. |
| 208 | * |
| 209 | * @param deviceId of the added device |
| 210 | */ |
| 211 | private void initiateConnection(DeviceId deviceId) { |
| 212 | if (controller.getNetconfDevice(deviceId) == null) { |
| 213 | try { |
| 214 | //if (this.isReachable(deviceId)) { |
Thomas Vachuska | 59d24eb | 2017-03-22 10:57:34 -0700 | [diff] [blame] | 215 | this.controller.connectDevice(deviceId); |
Sithara Punnassery | 425837f | 2017-03-21 12:58:00 -0700 | [diff] [blame] | 216 | //} |
| 217 | } catch (Exception ex) { |
Thomas Vachuska | 59d24eb | 2017-03-22 10:57:34 -0700 | [diff] [blame] | 218 | throw new RuntimeException(new NetconfException("Unable to connect to NETCONF device on " + |
| 219 | deviceId, ex)); |
Sithara Punnassery | 425837f | 2017-03-21 12:58:00 -0700 | [diff] [blame] | 220 | } |
| 221 | } |
Aaron Kruglikov | 309068e | 2017-03-17 15:25:54 -0700 | [diff] [blame] | 222 | } |
| 223 | } |