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 | 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; |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 26 | import org.onosproject.yang.model.DataNode; |
| 27 | import org.onosproject.yang.model.ModelObjectData; |
| 28 | import org.onosproject.yang.model.ResourceId; |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 29 | import org.onosproject.yang.runtime.YangModelRegistry; |
| 30 | |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 31 | import static org.onosproject.drivers.huawei.BgpConstructionUtil.getCreateBgp; |
janani b | d821b18 | 2017-03-30 16:34:49 +0530 | [diff] [blame] | 32 | import static org.onosproject.drivers.huawei.BgpConstructionUtil.getDeleteBgp; |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 33 | import static org.onosproject.drivers.huawei.DriverUtil.DEVICES; |
| 34 | import static org.onosproject.drivers.huawei.DriverUtil.NAMESPACE; |
| 35 | import static org.onosproject.drivers.huawei.DriverUtil.SERVICE_NOT_FOUND; |
| 36 | import static org.onosproject.drivers.huawei.DriverUtil.SLASH; |
| 37 | import static org.onosproject.drivers.huawei.InsConstructionUtil.getCreateVpnIns; |
janani b | d821b18 | 2017-03-30 16:34:49 +0530 | [diff] [blame] | 38 | import static org.onosproject.drivers.huawei.InsConstructionUtil.getDeleteVpnIns; |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 39 | import static org.onosproject.drivers.huawei.IntConstructionUtil.getCreateInt; |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Configures l3vpn on Huawei devices. |
| 43 | */ |
janani b | d821b18 | 2017-03-30 16:34:49 +0530 | [diff] [blame] | 44 | public class HuaweiL3VpnConfig extends AbstractHandlerBehaviour |
| 45 | implements L3VpnConfig { |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * YANG model registry. |
| 49 | */ |
| 50 | protected YangModelRegistry modelRegistry; |
| 51 | |
| 52 | /** |
| 53 | * Dynamic config service. |
| 54 | */ |
| 55 | protected DynamicConfigService configService; |
| 56 | |
| 57 | /** |
| 58 | * Constructs huawei L3VPN config. |
| 59 | */ |
janani b | d821b18 | 2017-03-30 16:34:49 +0530 | [diff] [blame] | 60 | public HuaweiL3VpnConfig() { |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Takes the YANG model registry service and registers the driver YANG. |
| 65 | * If service is not available it throws exception. |
| 66 | */ |
| 67 | private void init() { |
| 68 | try { |
| 69 | modelRegistry = handler().get(YangModelRegistry.class); |
| 70 | configService = handler().get(DynamicConfigService.class); |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 71 | } catch (ServiceNotFoundException e) { |
| 72 | throw new ServiceNotFoundException(SERVICE_NOT_FOUND); |
| 73 | } |
| 74 | } |
| 75 | |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 76 | @Override |
| 77 | public Object createInstance(Object objectData) { |
| 78 | if (modelRegistry == null) { |
| 79 | init(); |
| 80 | } |
| 81 | return getCreateVpnIns((ModelObjectData) objectData, |
| 82 | isDevicesPresent()); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public Object bindInterface(Object objectData) { |
| 87 | return getCreateInt((ModelObjectData) objectData); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public Object createBgpInfo(Object bgpInfo, Object bgpConfig) { |
| 92 | return getCreateBgp((BgpInfo) bgpInfo, (BgpDriverInfo) bgpConfig); |
| 93 | } |
| 94 | |
| 95 | |
| 96 | @Override |
| 97 | public Object deleteInstance(Object objectData) { |
janani b | d821b18 | 2017-03-30 16:34:49 +0530 | [diff] [blame] | 98 | return getDeleteVpnIns((ModelObjectData) objectData); |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public Object unbindInterface(Object objectData) { |
| 103 | //TODO:To be committed. |
| 104 | return null; |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public Object deleteBgpInfo(Object bgpInfo, Object bgpConfig) { |
janani b | d821b18 | 2017-03-30 16:34:49 +0530 | [diff] [blame] | 109 | return getDeleteBgp((BgpInfo) bgpInfo, (BgpDriverInfo) bgpConfig); |
janani b | f7060cd | 2017-03-28 19:06:30 +0530 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Returns true if devices, which is the root node present in store; |
| 114 | * false otherwise. |
| 115 | * |
| 116 | * @return true if devices available; false otherwise |
| 117 | */ |
| 118 | private boolean isDevicesPresent() { |
| 119 | ResourceId resId = ResourceId.builder() |
| 120 | .addBranchPointSchema(SLASH, null) |
| 121 | .addBranchPointSchema(DEVICES, NAMESPACE).build(); |
| 122 | try { |
| 123 | DataNode node = configService.readNode(resId, null); |
| 124 | if (node != null) { |
| 125 | return true; |
| 126 | } |
| 127 | } catch (FailedException e) { |
| 128 | return false; |
| 129 | } |
| 130 | return false; |
| 131 | } |
| 132 | } |