janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [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 | |
| 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 | d821b18 | 2017-03-30 16:34:49 +0530 | [diff] [blame^] | 24 | import org.onosproject.net.behaviour.L3VpnConfig; |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 25 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 26 | import org.onosproject.yang.gen.v1.l3vpn.comm.type.rev20141225.NeL3VpncommType; |
| 27 | import org.onosproject.yang.gen.v1.ne.bgpcomm.rev20141225.NeBgpcomm; |
| 28 | import org.onosproject.yang.gen.v1.ne.bgpcomm.type.rev20141225.NeBgpcommType; |
| 29 | import org.onosproject.yang.gen.v1.ne.l3vpn.api.rev20141225.NeL3VpnApi; |
| 30 | import org.onosproject.yang.gen.v1.ne.l3vpn.comm.rev20141225.NeL3Vpncomm; |
| 31 | import org.onosproject.yang.model.DataNode; |
| 32 | import org.onosproject.yang.model.ModelObjectData; |
| 33 | import org.onosproject.yang.model.ResourceId; |
| 34 | import org.onosproject.yang.model.YangModel; |
| 35 | import org.onosproject.yang.model.YangModuleId; |
| 36 | import org.onosproject.yang.runtime.DefaultAppModuleInfo; |
| 37 | import org.onosproject.yang.runtime.ModelRegistrationParam; |
| 38 | import org.onosproject.yang.runtime.YangModelRegistry; |
| 39 | |
| 40 | import java.util.Iterator; |
| 41 | |
| 42 | import static org.onosproject.drivers.huawei.BgpConstructionUtil.getCreateBgp; |
janani b | d821b18 | 2017-03-30 16:34:49 +0530 | [diff] [blame^] | 43 | import static org.onosproject.drivers.huawei.BgpConstructionUtil.getDeleteBgp; |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 44 | import static org.onosproject.drivers.huawei.DriverUtil.DEVICES; |
| 45 | import static org.onosproject.drivers.huawei.DriverUtil.NAMESPACE; |
| 46 | import static org.onosproject.drivers.huawei.DriverUtil.SERVICE_NOT_FOUND; |
| 47 | import static org.onosproject.drivers.huawei.DriverUtil.SLASH; |
| 48 | import static org.onosproject.drivers.huawei.InsConstructionUtil.getCreateVpnIns; |
janani b | d821b18 | 2017-03-30 16:34:49 +0530 | [diff] [blame^] | 49 | import static org.onosproject.drivers.huawei.InsConstructionUtil.getDeleteVpnIns; |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 50 | import static org.onosproject.drivers.huawei.IntConstructionUtil.getCreateInt; |
| 51 | import static org.onosproject.yang.runtime.DefaultModelRegistrationParam.builder; |
| 52 | import static org.onosproject.yang.runtime.helperutils.YangApacheUtils.getYangModel; |
| 53 | |
| 54 | /** |
| 55 | * Configures l3vpn on Huawei devices. |
| 56 | */ |
janani b | d821b18 | 2017-03-30 16:34:49 +0530 | [diff] [blame^] | 57 | public class HuaweiL3VpnConfig extends AbstractHandlerBehaviour |
| 58 | implements L3VpnConfig { |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * YANG model registry. |
| 62 | */ |
| 63 | protected YangModelRegistry modelRegistry; |
| 64 | |
| 65 | /** |
| 66 | * Dynamic config service. |
| 67 | */ |
| 68 | protected DynamicConfigService configService; |
| 69 | |
| 70 | /** |
| 71 | * Constructs huawei L3VPN config. |
| 72 | */ |
janani b | d821b18 | 2017-03-30 16:34:49 +0530 | [diff] [blame^] | 73 | public HuaweiL3VpnConfig() { |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Takes the YANG model registry service and registers the driver YANG. |
| 78 | * If service is not available it throws exception. |
| 79 | */ |
| 80 | private void init() { |
| 81 | try { |
| 82 | modelRegistry = handler().get(YangModelRegistry.class); |
| 83 | configService = handler().get(DynamicConfigService.class); |
| 84 | registerModel(); |
| 85 | } catch (ServiceNotFoundException e) { |
| 86 | throw new ServiceNotFoundException(SERVICE_NOT_FOUND); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Registers the huawei generated classes to the YANG model. |
| 92 | */ |
| 93 | private void registerModel() { |
| 94 | YangModel model = getYangModel(NeBgpcomm.class); |
| 95 | Iterator<YangModuleId> it = model.getYangModulesId().iterator(); |
| 96 | |
| 97 | //Create model registration param. |
| 98 | ModelRegistrationParam.Builder b = builder().setYangModel(model); |
| 99 | YangModuleId id; |
| 100 | while (it.hasNext()) { |
| 101 | id = it.next(); |
| 102 | switch (id.moduleName()) { |
| 103 | case "ne-bgpcomm": |
| 104 | b.addAppModuleInfo(id, new DefaultAppModuleInfo( |
| 105 | NeBgpcomm.class, null)); |
| 106 | break; |
| 107 | case "ne-bgpcomm-type": |
| 108 | b.addAppModuleInfo(id, new DefaultAppModuleInfo( |
| 109 | NeBgpcommType.class, null)); |
| 110 | break; |
| 111 | case "ne-l3vpn-api": |
| 112 | b.addAppModuleInfo(id, new DefaultAppModuleInfo( |
| 113 | NeL3VpnApi.class, null)); |
| 114 | break; |
| 115 | case "ne-l3vpncomm": |
| 116 | b.addAppModuleInfo(id, new DefaultAppModuleInfo( |
| 117 | NeL3Vpncomm.class, null)); |
| 118 | break; |
| 119 | case "ne-l3vpncomm-type": |
| 120 | b.addAppModuleInfo(id, new DefaultAppModuleInfo( |
| 121 | NeL3VpncommType.class, null)); |
| 122 | break; |
| 123 | default: |
| 124 | break; |
| 125 | } |
| 126 | } |
| 127 | ModelRegistrationParam regParam = b.build(); |
| 128 | modelRegistry.registerModel(regParam); |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public Object createInstance(Object objectData) { |
| 133 | if (modelRegistry == null) { |
| 134 | init(); |
| 135 | } |
| 136 | return getCreateVpnIns((ModelObjectData) objectData, |
| 137 | isDevicesPresent()); |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public Object bindInterface(Object objectData) { |
| 142 | return getCreateInt((ModelObjectData) objectData); |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public Object createBgpInfo(Object bgpInfo, Object bgpConfig) { |
| 147 | return getCreateBgp((BgpInfo) bgpInfo, (BgpDriverInfo) bgpConfig); |
| 148 | } |
| 149 | |
| 150 | |
| 151 | @Override |
| 152 | public Object deleteInstance(Object objectData) { |
janani b | d821b18 | 2017-03-30 16:34:49 +0530 | [diff] [blame^] | 153 | return getDeleteVpnIns((ModelObjectData) objectData); |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public Object unbindInterface(Object objectData) { |
| 158 | //TODO:To be committed. |
| 159 | return null; |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public Object deleteBgpInfo(Object bgpInfo, Object bgpConfig) { |
janani b | d821b18 | 2017-03-30 16:34:49 +0530 | [diff] [blame^] | 164 | return getDeleteBgp((BgpInfo) bgpInfo, (BgpDriverInfo) bgpConfig); |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Returns true if devices, which is the root node present in store; |
| 169 | * false otherwise. |
| 170 | * |
| 171 | * @return true if devices available; false otherwise |
| 172 | */ |
| 173 | private boolean isDevicesPresent() { |
| 174 | ResourceId resId = ResourceId.builder() |
| 175 | .addBranchPointSchema(SLASH, null) |
| 176 | .addBranchPointSchema(DEVICES, NAMESPACE).build(); |
| 177 | try { |
| 178 | DataNode node = configService.readNode(resId, null); |
| 179 | if (node != null) { |
| 180 | return true; |
| 181 | } |
| 182 | } catch (FailedException e) { |
| 183 | return false; |
| 184 | } |
| 185 | return false; |
| 186 | } |
| 187 | } |