blob: 300e48051829d82a3a16adea3d24a184ca76aec3 [file] [log] [blame]
janani bf7060cd2017-03-28 19:06:30 +05301/*
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
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;
26import org.onosproject.yang.gen.v1.l3vpn.comm.type.rev20141225.NeL3VpncommType;
27import org.onosproject.yang.gen.v1.ne.bgpcomm.rev20141225.NeBgpcomm;
28import org.onosproject.yang.gen.v1.ne.bgpcomm.type.rev20141225.NeBgpcommType;
29import org.onosproject.yang.gen.v1.ne.l3vpn.api.rev20141225.NeL3VpnApi;
30import org.onosproject.yang.gen.v1.ne.l3vpn.comm.rev20141225.NeL3Vpncomm;
31import org.onosproject.yang.model.DataNode;
32import org.onosproject.yang.model.ModelObjectData;
33import org.onosproject.yang.model.ResourceId;
34import org.onosproject.yang.model.YangModel;
35import org.onosproject.yang.model.YangModuleId;
36import org.onosproject.yang.runtime.DefaultAppModuleInfo;
37import org.onosproject.yang.runtime.ModelRegistrationParam;
38import org.onosproject.yang.runtime.YangModelRegistry;
39
40import java.util.Iterator;
41
42import static org.onosproject.drivers.huawei.BgpConstructionUtil.getCreateBgp;
janani bd821b182017-03-30 16:34:49 +053043import static org.onosproject.drivers.huawei.BgpConstructionUtil.getDeleteBgp;
janani bf7060cd2017-03-28 19:06:30 +053044import static org.onosproject.drivers.huawei.DriverUtil.DEVICES;
45import static org.onosproject.drivers.huawei.DriverUtil.NAMESPACE;
46import static org.onosproject.drivers.huawei.DriverUtil.SERVICE_NOT_FOUND;
47import static org.onosproject.drivers.huawei.DriverUtil.SLASH;
48import static org.onosproject.drivers.huawei.InsConstructionUtil.getCreateVpnIns;
janani bd821b182017-03-30 16:34:49 +053049import static org.onosproject.drivers.huawei.InsConstructionUtil.getDeleteVpnIns;
janani bf7060cd2017-03-28 19:06:30 +053050import static org.onosproject.drivers.huawei.IntConstructionUtil.getCreateInt;
51import static org.onosproject.yang.runtime.DefaultModelRegistrationParam.builder;
52import static org.onosproject.yang.runtime.helperutils.YangApacheUtils.getYangModel;
53
54/**
55 * Configures l3vpn on Huawei devices.
56 */
janani bd821b182017-03-30 16:34:49 +053057public class HuaweiL3VpnConfig extends AbstractHandlerBehaviour
58 implements L3VpnConfig {
janani bf7060cd2017-03-28 19:06:30 +053059
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 bd821b182017-03-30 16:34:49 +053073 public HuaweiL3VpnConfig() {
janani bf7060cd2017-03-28 19:06:30 +053074 }
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 bd821b182017-03-30 16:34:49 +0530153 return getDeleteVpnIns((ModelObjectData) objectData);
janani bf7060cd2017-03-28 19:06:30 +0530154 }
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 bd821b182017-03-30 16:34:49 +0530164 return getDeleteBgp((BgpInfo) bgpInfo, (BgpDriverInfo) bgpConfig);
janani bf7060cd2017-03-28 19:06:30 +0530165 }
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}