janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [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 | |
| 17 | package org.onosproject.drivers.huawei; |
| 18 | |
| 19 | import org.onlab.osgi.ServiceNotFoundException; |
| 20 | import org.onosproject.config.DynamicConfigService; |
| 21 | import org.onosproject.config.FailedException; |
| 22 | import org.onosproject.l3vpn.netl3vpn.BgpDriverInfo; |
| 23 | import org.onosproject.l3vpn.netl3vpn.BgpInfo; |
janani b | 9ed76be | 2017-08-29 19:11:33 +0530 | [diff] [blame] | 24 | import org.onosproject.l3vpn.netl3vpn.L3VpnConfig; |
| 25 | import org.onosproject.l3vpn.netl3vpn.TunnelInfo; |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 26 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 27 | import org.onosproject.yang.model.DataNode; |
| 28 | import org.onosproject.yang.model.ModelObjectData; |
| 29 | import org.onosproject.yang.model.ResourceId; |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 30 | import org.onosproject.yang.runtime.YangModelRegistry; |
| 31 | |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 32 | import static org.onosproject.drivers.huawei.BgpConstructionUtil.getCreateBgp; |
janani b | d821b18 | 2017-03-30 16:34:49 +0530 | [diff] [blame] | 33 | import static org.onosproject.drivers.huawei.BgpConstructionUtil.getDeleteBgp; |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 34 | import static org.onosproject.drivers.huawei.DriverUtil.DEVICES; |
| 35 | import static org.onosproject.drivers.huawei.DriverUtil.NAMESPACE; |
| 36 | import static org.onosproject.drivers.huawei.DriverUtil.SERVICE_NOT_FOUND; |
| 37 | import static org.onosproject.drivers.huawei.DriverUtil.SLASH; |
| 38 | import static org.onosproject.drivers.huawei.InsConstructionUtil.getCreateVpnIns; |
janani b | d821b18 | 2017-03-30 16:34:49 +0530 | [diff] [blame] | 39 | import static org.onosproject.drivers.huawei.InsConstructionUtil.getDeleteVpnIns; |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 40 | import static org.onosproject.drivers.huawei.IntConstructionUtil.getCreateInt; |
janani b | 9ed76be | 2017-08-29 19:11:33 +0530 | [diff] [blame] | 41 | import static org.onosproject.drivers.huawei.TnlConstructionUtil.getBindTnl; |
| 42 | import static org.onosproject.drivers.huawei.TnlConstructionUtil.getCreateTnl; |
| 43 | import static org.onosproject.drivers.huawei.TnlConstructionUtil.getCreateTnlDev; |
| 44 | import static org.onosproject.drivers.huawei.TnlConstructionUtil.getCreateTnlPol; |
| 45 | import static org.onosproject.drivers.huawei.TnlConstructionUtil.getDeleteTnl; |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * Configures l3vpn on Huawei devices. |
| 49 | */ |
janani b | d821b18 | 2017-03-30 16:34:49 +0530 | [diff] [blame] | 50 | public class HuaweiL3VpnConfig extends AbstractHandlerBehaviour |
| 51 | implements L3VpnConfig { |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 52 | |
| 53 | /** |
| 54 | * YANG model registry. |
| 55 | */ |
| 56 | protected YangModelRegistry modelRegistry; |
| 57 | |
| 58 | /** |
| 59 | * Dynamic config service. |
| 60 | */ |
| 61 | protected DynamicConfigService configService; |
| 62 | |
| 63 | /** |
| 64 | * Constructs huawei L3VPN config. |
| 65 | */ |
janani b | d821b18 | 2017-03-30 16:34:49 +0530 | [diff] [blame] | 66 | public HuaweiL3VpnConfig() { |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Takes the YANG model registry service and registers the driver YANG. |
| 71 | * If service is not available it throws exception. |
| 72 | */ |
| 73 | private void init() { |
| 74 | try { |
| 75 | modelRegistry = handler().get(YangModelRegistry.class); |
| 76 | configService = handler().get(DynamicConfigService.class); |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 77 | } catch (ServiceNotFoundException e) { |
| 78 | throw new ServiceNotFoundException(SERVICE_NOT_FOUND); |
| 79 | } |
| 80 | } |
| 81 | |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 82 | @Override |
janani b | 9ed76be | 2017-08-29 19:11:33 +0530 | [diff] [blame] | 83 | public ModelObjectData createInstance(ModelObjectData objectData) { |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 84 | if (modelRegistry == null) { |
| 85 | init(); |
| 86 | } |
janani b | 9ed76be | 2017-08-29 19:11:33 +0530 | [diff] [blame] | 87 | return getCreateVpnIns(objectData, isDevicesPresent()); |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | @Override |
janani b | 9ed76be | 2017-08-29 19:11:33 +0530 | [diff] [blame] | 91 | public ModelObjectData bindInterface(ModelObjectData objectData) { |
| 92 | return getCreateInt(objectData); |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | @Override |
janani b | 9ed76be | 2017-08-29 19:11:33 +0530 | [diff] [blame] | 96 | public ModelObjectData createBgpInfo(BgpInfo bgpInfo, |
| 97 | BgpDriverInfo bgpConfig) { |
| 98 | return getCreateBgp(bgpInfo, bgpConfig); |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | @Override |
janani b | 9ed76be | 2017-08-29 19:11:33 +0530 | [diff] [blame] | 102 | public ModelObjectData createTnlDev(TunnelInfo tnlInfo) { |
| 103 | return getCreateTnlDev(tnlInfo); |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public ModelObjectData createTnlPol(TunnelInfo tnlInfo) { |
| 108 | return getCreateTnlPol(tnlInfo); |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public ModelObjectData createTnl(TunnelInfo tnlInfo) { |
| 113 | return getCreateTnl(tnlInfo); |
| 114 | } |
| 115 | |
| 116 | @Override |
| 117 | public ModelObjectData bindTnl(TunnelInfo tnlInfo) { |
| 118 | return getBindTnl(tnlInfo); |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public ModelObjectData deleteInstance(ModelObjectData objectData) { |
| 123 | return getDeleteVpnIns(objectData); |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public ModelObjectData unbindInterface(ModelObjectData objectData) { |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 128 | //TODO:To be committed. |
| 129 | return null; |
| 130 | } |
| 131 | |
| 132 | @Override |
janani b | 9ed76be | 2017-08-29 19:11:33 +0530 | [diff] [blame] | 133 | public ModelObjectData deleteTnl(TunnelInfo tnlInfo) { |
| 134 | return getDeleteTnl(tnlInfo); |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public ModelObjectData deleteBgpInfo(BgpInfo bgpInfo, |
| 139 | BgpDriverInfo bgpConfig) { |
| 140 | return getDeleteBgp(bgpInfo, bgpConfig); |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Returns true if devices, which is the root node present in store; |
| 145 | * false otherwise. |
| 146 | * |
| 147 | * @return true if devices available; false otherwise |
| 148 | */ |
| 149 | private boolean isDevicesPresent() { |
| 150 | ResourceId resId = ResourceId.builder() |
| 151 | .addBranchPointSchema(SLASH, null) |
| 152 | .addBranchPointSchema(DEVICES, NAMESPACE).build(); |
| 153 | try { |
| 154 | DataNode node = configService.readNode(resId, null); |
| 155 | if (node != null) { |
| 156 | return true; |
| 157 | } |
| 158 | } catch (FailedException e) { |
| 159 | return false; |
| 160 | } |
| 161 | return false; |
| 162 | } |
| 163 | } |