blob: b5855067c932054ff5408320880a77a5569ce663 [file] [log] [blame]
hirokif4ed5212018-05-26 22:39:38 -07001/*
2 * Copyright 2018-present Open Networking Foundation
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.odtn.internal;
18
19import java.util.ArrayList;
hirokifca084b2018-06-02 08:29:42 -070020import java.util.Arrays;
hirokif4ed5212018-05-26 22:39:38 -070021import java.util.List;
22
hirokifca084b2018-06-02 08:29:42 -070023import java.util.concurrent.atomic.AtomicReference;
hirokif4ed5212018-05-26 22:39:38 -070024import org.onosproject.config.FailedException;
hirokifca084b2018-06-02 08:29:42 -070025import org.onosproject.net.config.NetworkConfigService;
26import org.onosproject.odtn.TapiResolver;
27import org.onosproject.odtn.behaviour.OdtnDeviceDescriptionDiscovery;
28import org.onosproject.odtn.utils.tapi.DcsBasedTapiObjectRefFactory;
29import org.onosproject.odtn.utils.tapi.TapiCepPair;
hirokif4ed5212018-05-26 22:39:38 -070030import org.onosproject.odtn.utils.tapi.TapiConnection;
31import org.onosproject.odtn.utils.tapi.TapiNepPair;
32import org.onosproject.odtn.utils.tapi.TapiCepRefHandler;
33import org.onosproject.odtn.utils.tapi.TapiConnectionHandler;
34
hirokifca084b2018-06-02 08:29:42 -070035import org.onosproject.odtn.utils.tapi.TapiNepRef;
hirokif4ed5212018-05-26 22:39:38 -070036import org.onosproject.odtn.utils.tapi.TapiRouteHandler;
37import org.slf4j.Logger;
38import org.slf4j.LoggerFactory;
39
40import static org.onlab.osgi.DefaultServiceDirectory.getService;
41
42/**
43 * DCS-dependent Tapi connection manager implementation.
44 */
hirokifca084b2018-06-02 08:29:42 -070045public final class DcsBasedTapiConnectionManager implements TapiConnectionManager {
hirokif4ed5212018-05-26 22:39:38 -070046
47 private final Logger log = LoggerFactory.getLogger(getClass());
48 protected TapiPathComputer connectionController;
hirokifca084b2018-06-02 08:29:42 -070049 private TapiResolver resolver;
50 private NetworkConfigService netcfgService;
hirokif4ed5212018-05-26 22:39:38 -070051
52 private List<DcsBasedTapiConnectionManager> connectionManagerList = new ArrayList<>();
hirokifca084b2018-06-02 08:29:42 -070053 private TapiConnection connection = null;
hirokif4ed5212018-05-26 22:39:38 -070054 private TapiConnectionHandler connectionHandler = TapiConnectionHandler.create();
55 private Operation op = null;
56
57
58 enum Operation {
59 CREATE,
60 DELETE
61 }
62
hirokifca084b2018-06-02 08:29:42 -070063 private DcsBasedTapiConnectionManager() {
64 }
65
hirokif4ed5212018-05-26 22:39:38 -070066 public static DcsBasedTapiConnectionManager create() {
67 DcsBasedTapiConnectionManager self = new DcsBasedTapiConnectionManager();
68 self.connectionController = DefaultTapiPathComputer.create();
hirokifca084b2018-06-02 08:29:42 -070069 self.resolver = getService(TapiResolver.class);
70 self.netcfgService = getService(NetworkConfigService.class);
hirokif4ed5212018-05-26 22:39:38 -070071 return self;
72 }
73
74 @Override
75 public TapiConnectionHandler createConnection(TapiNepPair neps) {
76
77 // Calculate route
78 TapiConnection connection = connectionController.pathCompute(neps);
79 log.info("Calculated path: {}", connection);
80
81 createConnectionRecursively(connection);
82 return connectionHandler;
83 }
84
85 @Override
86 public void deleteConnection(TapiConnectionHandler connectionHandler) {
87
hirokif4ed5212018-05-26 22:39:38 -070088 deleteConnectionRecursively(connectionHandler);
89 }
90
91 @Override
92 public void apply() {
93 connectionManagerList.forEach(DcsBasedTapiConnectionManager::apply);
hirokifca084b2018-06-02 08:29:42 -070094
hirokif4ed5212018-05-26 22:39:38 -070095 switch (op) {
96 case CREATE:
hirokifca084b2018-06-02 08:29:42 -070097 notifyDeviceConfigChange(true);
hirokif4ed5212018-05-26 22:39:38 -070098 connectionHandler.add();
99 break;
100 case DELETE:
hirokifca084b2018-06-02 08:29:42 -0700101 notifyDeviceConfigChange(false);
hirokif4ed5212018-05-26 22:39:38 -0700102 connectionHandler.remove();
103 break;
104 default:
105 throw new FailedException("Unknown operation type.");
106 }
107 }
108
109 /**
hirokifca084b2018-06-02 08:29:42 -0700110 * Emit NetworkConfig event with parameters for device config,
111 * to notify configuration change to device drivers.
112 */
113 private void notifyDeviceConfigChange(boolean enable) {
114 if (!this.connection.getCeps().isSameNode()) {
115 return;
116 }
117
118 TapiNepRef left = this.connection.getCeps().left().getNepRef();
119 TapiNepRef right = this.connection.getCeps().right().getNepRef();
120
121 // update with latest data in DCS
122 left = resolver.getNepRef(left);
123 right = resolver.getNepRef(right);
124
125 AtomicReference<TapiNepRef> line = new AtomicReference<>();
126 AtomicReference<TapiNepRef> client = new AtomicReference<>();
127 Arrays.asList(left, right).forEach(nep -> {
128 if (nep.getPortType() == OdtnDeviceDescriptionDiscovery.OdtnPortType.LINE) {
129 line.set(nep);
130 }
131 if (nep.getPortType() == OdtnDeviceDescriptionDiscovery.OdtnPortType.CLIENT) {
132 client.set(nep);
133 }
134 });
135
136 DeviceConfigEventEmitter eventEmitter = DeviceConfigEventEmitter.create();
137 eventEmitter.emit(line.get(), client.get(), enable);
138 }
139
140 /**
hirokif4ed5212018-05-26 22:39:38 -0700141 * Generate TAPI connection and its under connections recursively
142 * and add them to creation queue.
143 *
144 * @param connection connection to be created
145 */
146 private void createConnectionRecursively(TapiConnection connection) {
147 op = Operation.CREATE;
148 connectionManagerList.clear();
hirokifca084b2018-06-02 08:29:42 -0700149 this.connection = connection;
hirokif4ed5212018-05-26 22:39:38 -0700150
151 TapiRouteHandler routeBuilder = TapiRouteHandler.create();
152
153 // Create under connection, and set them into routeBuilder
154 connection.getLowerConnections().forEach(lowerConnection -> {
155 delegateConnectionCreation(lowerConnection);
156 routeBuilder.addCep(lowerConnection.getCeps().left());
157 routeBuilder.addCep(lowerConnection.getCeps().right());
158 });
159
160 connectionHandler.addRoute(routeBuilder.getModelObject());
161
162 connectionHandler.addCep(TapiCepRefHandler.create()
163 .setCep(connection.getCeps().left()).getModelObject());
164 connectionHandler.addCep(TapiCepRefHandler.create()
165 .setCep(connection.getCeps().right()).getModelObject());
166
167 connectionManagerList.forEach(manager ->
168 connectionHandler.addLowerConnection(manager.getConnectionHandler().getModelObject()));
169
170 }
171
172 /**
173 * Generate TAPI connection and its under connections recursively
174 * and add them to deletion queue.
175 *
176 * @param connectionHandler connectionHandler of connection to be deleted
177 */
178 private void deleteConnectionRecursively(TapiConnectionHandler connectionHandler) {
179 op = Operation.DELETE;
180 connectionManagerList.clear();
181
hirokifca084b2018-06-02 08:29:42 -0700182 // read target to be deleted
183 connectionHandler.read();
184 log.info("model: {}", connectionHandler.getModelObject());
185
186 this.connection = TapiConnection.create(
187 TapiCepPair.create(
188 DcsBasedTapiObjectRefFactory.create(connectionHandler.getCeps().get(0)),
189 DcsBasedTapiObjectRefFactory.create(connectionHandler.getCeps().get(1)))
190 );
191
hirokif4ed5212018-05-26 22:39:38 -0700192 this.connectionHandler = connectionHandler;
193 this.connectionHandler.getLowerConnections().forEach(lowerConnectionHandler -> {
194 delegateConnectionDeletion(lowerConnectionHandler);
195 });
196 }
197
198 /**
199 * Delegate lower-connection creation to other corresponding TapiConnectionManager of each Nodes.
200 *
201 * @param connection connection to be created
202 */
203 private void delegateConnectionCreation(TapiConnection connection) {
204 log.info("ceps: {}", connection.getCeps());
205 DcsBasedTapiConnectionManager manager = DcsBasedTapiConnectionManager.create();
206 manager.createConnectionRecursively(connection);
207 connectionManagerList.add(manager);
208 }
209
210 /**
211 * Delegate lower-connection deletion to other corresponding TapiConnectionManager of each Nodes.
212 *
hirokifca084b2018-06-02 08:29:42 -0700213 * @param connectionHandler connectionHandler of connection to be deleted
hirokif4ed5212018-05-26 22:39:38 -0700214 */
215 private void delegateConnectionDeletion(TapiConnectionHandler connectionHandler) {
216 log.info("model: {}", connectionHandler.getModelObject());
217 DcsBasedTapiConnectionManager manager = DcsBasedTapiConnectionManager.create();
218 manager.deleteConnectionRecursively(connectionHandler);
219 connectionManagerList.add(manager);
220 }
221
222
223 public TapiConnectionHandler getConnectionHandler() {
224 return connectionHandler;
225 }
226
227}