hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.odtn.internal; |
| 18 | |
| 19 | import java.util.ArrayList; |
hiroki | fca084b | 2018-06-02 08:29:42 -0700 | [diff] [blame] | 20 | import java.util.Arrays; |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 21 | import java.util.List; |
| 22 | |
hiroki | fca084b | 2018-06-02 08:29:42 -0700 | [diff] [blame] | 23 | import java.util.concurrent.atomic.AtomicReference; |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 24 | import org.onosproject.config.FailedException; |
hiroki | fca084b | 2018-06-02 08:29:42 -0700 | [diff] [blame] | 25 | import org.onosproject.net.config.NetworkConfigService; |
| 26 | import org.onosproject.odtn.TapiResolver; |
| 27 | import org.onosproject.odtn.behaviour.OdtnDeviceDescriptionDiscovery; |
| 28 | import org.onosproject.odtn.utils.tapi.DcsBasedTapiObjectRefFactory; |
| 29 | import org.onosproject.odtn.utils.tapi.TapiCepPair; |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 30 | import org.onosproject.odtn.utils.tapi.TapiConnection; |
| 31 | import org.onosproject.odtn.utils.tapi.TapiNepPair; |
| 32 | import org.onosproject.odtn.utils.tapi.TapiCepRefHandler; |
| 33 | import org.onosproject.odtn.utils.tapi.TapiConnectionHandler; |
| 34 | |
hiroki | fca084b | 2018-06-02 08:29:42 -0700 | [diff] [blame] | 35 | import org.onosproject.odtn.utils.tapi.TapiNepRef; |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 36 | import org.onosproject.odtn.utils.tapi.TapiRouteHandler; |
| 37 | import org.slf4j.Logger; |
| 38 | import org.slf4j.LoggerFactory; |
| 39 | |
| 40 | import static org.onlab.osgi.DefaultServiceDirectory.getService; |
| 41 | |
| 42 | /** |
| 43 | * DCS-dependent Tapi connection manager implementation. |
| 44 | */ |
hiroki | fca084b | 2018-06-02 08:29:42 -0700 | [diff] [blame] | 45 | public final class DcsBasedTapiConnectionManager implements TapiConnectionManager { |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 46 | |
| 47 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 48 | protected TapiPathComputer connectionController; |
hiroki | fca084b | 2018-06-02 08:29:42 -0700 | [diff] [blame] | 49 | private TapiResolver resolver; |
| 50 | private NetworkConfigService netcfgService; |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 51 | |
| 52 | private List<DcsBasedTapiConnectionManager> connectionManagerList = new ArrayList<>(); |
hiroki | fca084b | 2018-06-02 08:29:42 -0700 | [diff] [blame] | 53 | private TapiConnection connection = null; |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 54 | private TapiConnectionHandler connectionHandler = TapiConnectionHandler.create(); |
| 55 | private Operation op = null; |
| 56 | |
| 57 | |
| 58 | enum Operation { |
| 59 | CREATE, |
| 60 | DELETE |
| 61 | } |
| 62 | |
hiroki | fca084b | 2018-06-02 08:29:42 -0700 | [diff] [blame] | 63 | private DcsBasedTapiConnectionManager() { |
| 64 | } |
| 65 | |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 66 | public static DcsBasedTapiConnectionManager create() { |
| 67 | DcsBasedTapiConnectionManager self = new DcsBasedTapiConnectionManager(); |
| 68 | self.connectionController = DefaultTapiPathComputer.create(); |
hiroki | fca084b | 2018-06-02 08:29:42 -0700 | [diff] [blame] | 69 | self.resolver = getService(TapiResolver.class); |
| 70 | self.netcfgService = getService(NetworkConfigService.class); |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 71 | 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 | |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 88 | deleteConnectionRecursively(connectionHandler); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | public void apply() { |
| 93 | connectionManagerList.forEach(DcsBasedTapiConnectionManager::apply); |
hiroki | fca084b | 2018-06-02 08:29:42 -0700 | [diff] [blame] | 94 | |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 95 | switch (op) { |
| 96 | case CREATE: |
hiroki | fca084b | 2018-06-02 08:29:42 -0700 | [diff] [blame] | 97 | notifyDeviceConfigChange(true); |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 98 | connectionHandler.add(); |
| 99 | break; |
| 100 | case DELETE: |
hiroki | fca084b | 2018-06-02 08:29:42 -0700 | [diff] [blame] | 101 | notifyDeviceConfigChange(false); |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 102 | connectionHandler.remove(); |
| 103 | break; |
| 104 | default: |
| 105 | throw new FailedException("Unknown operation type."); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | /** |
hiroki | fca084b | 2018-06-02 08:29:42 -0700 | [diff] [blame] | 110 | * 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 | /** |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 141 | * 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(); |
hiroki | fca084b | 2018-06-02 08:29:42 -0700 | [diff] [blame] | 149 | this.connection = connection; |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 150 | |
| 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 | |
hiroki | fca084b | 2018-06-02 08:29:42 -0700 | [diff] [blame] | 182 | // 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 | |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 192 | 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 | * |
hiroki | fca084b | 2018-06-02 08:29:42 -0700 | [diff] [blame] | 213 | * @param connectionHandler connectionHandler of connection to be deleted |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 214 | */ |
| 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 | } |