blob: 63519f2af84a40a53ba94a3e4caaf8009dd589d3 [file] [log] [blame]
janani bf7060cd2017-03-28 19:06:30 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
janani bf7060cd2017-03-28 19:06:30 +05303 *
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
17package org.onosproject.drivers.huawei;
18
19import org.onlab.osgi.ServiceNotFoundException;
20import org.onosproject.config.DynamicConfigService;
21import org.onosproject.config.FailedException;
22import org.onosproject.l3vpn.netl3vpn.BgpDriverInfo;
23import org.onosproject.l3vpn.netl3vpn.BgpInfo;
janani bd821b182017-03-30 16:34:49 +053024import org.onosproject.net.behaviour.L3VpnConfig;
janani bf7060cd2017-03-28 19:06:30 +053025import org.onosproject.net.driver.AbstractHandlerBehaviour;
janani bf7060cd2017-03-28 19:06:30 +053026import org.onosproject.yang.model.DataNode;
27import org.onosproject.yang.model.ModelObjectData;
28import org.onosproject.yang.model.ResourceId;
janani bf7060cd2017-03-28 19:06:30 +053029import org.onosproject.yang.runtime.YangModelRegistry;
30
janani bf7060cd2017-03-28 19:06:30 +053031import static org.onosproject.drivers.huawei.BgpConstructionUtil.getCreateBgp;
janani bd821b182017-03-30 16:34:49 +053032import static org.onosproject.drivers.huawei.BgpConstructionUtil.getDeleteBgp;
janani bf7060cd2017-03-28 19:06:30 +053033import static org.onosproject.drivers.huawei.DriverUtil.DEVICES;
34import static org.onosproject.drivers.huawei.DriverUtil.NAMESPACE;
35import static org.onosproject.drivers.huawei.DriverUtil.SERVICE_NOT_FOUND;
36import static org.onosproject.drivers.huawei.DriverUtil.SLASH;
37import static org.onosproject.drivers.huawei.InsConstructionUtil.getCreateVpnIns;
janani bd821b182017-03-30 16:34:49 +053038import static org.onosproject.drivers.huawei.InsConstructionUtil.getDeleteVpnIns;
janani bf7060cd2017-03-28 19:06:30 +053039import static org.onosproject.drivers.huawei.IntConstructionUtil.getCreateInt;
janani bf7060cd2017-03-28 19:06:30 +053040
41/**
42 * Configures l3vpn on Huawei devices.
43 */
janani bd821b182017-03-30 16:34:49 +053044public class HuaweiL3VpnConfig extends AbstractHandlerBehaviour
45 implements L3VpnConfig {
janani bf7060cd2017-03-28 19:06:30 +053046
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 bd821b182017-03-30 16:34:49 +053060 public HuaweiL3VpnConfig() {
janani bf7060cd2017-03-28 19:06:30 +053061 }
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 bf7060cd2017-03-28 19:06:30 +053071 } catch (ServiceNotFoundException e) {
72 throw new ServiceNotFoundException(SERVICE_NOT_FOUND);
73 }
74 }
75
janani bf7060cd2017-03-28 19:06:30 +053076 @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 bd821b182017-03-30 16:34:49 +053098 return getDeleteVpnIns((ModelObjectData) objectData);
janani bf7060cd2017-03-28 19:06:30 +053099 }
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 bd821b182017-03-30 16:34:49 +0530109 return getDeleteBgp((BgpInfo) bgpInfo, (BgpDriverInfo) bgpConfig);
janani bf7060cd2017-03-28 19:06:30 +0530110 }
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}