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; |
| 20 | import java.util.List; |
| 21 | |
| 22 | import org.onosproject.config.FailedException; |
| 23 | import org.onosproject.net.device.DeviceService; |
| 24 | import org.onosproject.odtn.utils.tapi.TapiConnection; |
| 25 | import org.onosproject.odtn.utils.tapi.TapiNepPair; |
| 26 | import org.onosproject.odtn.utils.tapi.TapiCepRefHandler; |
| 27 | import org.onosproject.odtn.utils.tapi.TapiConnectionHandler; |
| 28 | |
| 29 | import org.onosproject.odtn.utils.tapi.TapiRouteHandler; |
| 30 | import org.slf4j.Logger; |
| 31 | import org.slf4j.LoggerFactory; |
| 32 | |
| 33 | import static org.onlab.osgi.DefaultServiceDirectory.getService; |
| 34 | |
| 35 | /** |
| 36 | * DCS-dependent Tapi connection manager implementation. |
| 37 | */ |
| 38 | public class DcsBasedTapiConnectionManager implements TapiConnectionManager { |
| 39 | |
| 40 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 41 | protected TapiPathComputer connectionController; |
| 42 | private DeviceService deviceService; |
| 43 | |
| 44 | private List<DcsBasedTapiConnectionManager> connectionManagerList = new ArrayList<>(); |
| 45 | private TapiConnectionHandler connectionHandler = TapiConnectionHandler.create(); |
| 46 | private Operation op = null; |
| 47 | |
| 48 | |
| 49 | enum Operation { |
| 50 | CREATE, |
| 51 | DELETE |
| 52 | } |
| 53 | |
| 54 | public static DcsBasedTapiConnectionManager create() { |
| 55 | DcsBasedTapiConnectionManager self = new DcsBasedTapiConnectionManager(); |
| 56 | self.connectionController = DefaultTapiPathComputer.create(); |
| 57 | self.deviceService = getService(DeviceService.class); |
| 58 | return self; |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public TapiConnectionHandler createConnection(TapiNepPair neps) { |
| 63 | |
| 64 | // Calculate route |
| 65 | TapiConnection connection = connectionController.pathCompute(neps); |
| 66 | log.info("Calculated path: {}", connection); |
| 67 | |
| 68 | createConnectionRecursively(connection); |
| 69 | return connectionHandler; |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public void deleteConnection(TapiConnectionHandler connectionHandler) { |
| 74 | |
| 75 | // read target to be deleted |
| 76 | this.connectionHandler = connectionHandler; |
| 77 | this.connectionHandler.read(); |
| 78 | log.info("model: {}", connectionHandler.getModelObject()); |
| 79 | |
| 80 | deleteConnectionRecursively(connectionHandler); |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public void apply() { |
| 85 | connectionManagerList.forEach(DcsBasedTapiConnectionManager::apply); |
| 86 | switch (op) { |
| 87 | case CREATE: |
| 88 | connectionHandler.add(); |
| 89 | break; |
| 90 | case DELETE: |
| 91 | connectionHandler.remove(); |
| 92 | break; |
| 93 | default: |
| 94 | throw new FailedException("Unknown operation type."); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Generate TAPI connection and its under connections recursively |
| 100 | * and add them to creation queue. |
| 101 | * |
| 102 | * @param connection connection to be created |
| 103 | */ |
| 104 | private void createConnectionRecursively(TapiConnection connection) { |
| 105 | op = Operation.CREATE; |
| 106 | connectionManagerList.clear(); |
| 107 | |
| 108 | TapiRouteHandler routeBuilder = TapiRouteHandler.create(); |
| 109 | |
| 110 | // Create under connection, and set them into routeBuilder |
| 111 | connection.getLowerConnections().forEach(lowerConnection -> { |
| 112 | delegateConnectionCreation(lowerConnection); |
| 113 | routeBuilder.addCep(lowerConnection.getCeps().left()); |
| 114 | routeBuilder.addCep(lowerConnection.getCeps().right()); |
| 115 | }); |
| 116 | |
| 117 | connectionHandler.addRoute(routeBuilder.getModelObject()); |
| 118 | |
| 119 | connectionHandler.addCep(TapiCepRefHandler.create() |
| 120 | .setCep(connection.getCeps().left()).getModelObject()); |
| 121 | connectionHandler.addCep(TapiCepRefHandler.create() |
| 122 | .setCep(connection.getCeps().right()).getModelObject()); |
| 123 | |
| 124 | connectionManagerList.forEach(manager -> |
| 125 | connectionHandler.addLowerConnection(manager.getConnectionHandler().getModelObject())); |
| 126 | |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Generate TAPI connection and its under connections recursively |
| 131 | * and add them to deletion queue. |
| 132 | * |
| 133 | * @param connectionHandler connectionHandler of connection to be deleted |
| 134 | */ |
| 135 | private void deleteConnectionRecursively(TapiConnectionHandler connectionHandler) { |
| 136 | op = Operation.DELETE; |
| 137 | connectionManagerList.clear(); |
| 138 | |
| 139 | this.connectionHandler = connectionHandler; |
| 140 | this.connectionHandler.getLowerConnections().forEach(lowerConnectionHandler -> { |
| 141 | delegateConnectionDeletion(lowerConnectionHandler); |
| 142 | }); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Delegate lower-connection creation to other corresponding TapiConnectionManager of each Nodes. |
| 147 | * |
| 148 | * @param connection connection to be created |
| 149 | */ |
| 150 | private void delegateConnectionCreation(TapiConnection connection) { |
| 151 | log.info("ceps: {}", connection.getCeps()); |
| 152 | DcsBasedTapiConnectionManager manager = DcsBasedTapiConnectionManager.create(); |
| 153 | manager.createConnectionRecursively(connection); |
| 154 | connectionManagerList.add(manager); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Delegate lower-connection deletion to other corresponding TapiConnectionManager of each Nodes. |
| 159 | * |
| 160 | * @param connectionHandler connectionHandler of connection to be deleted |
| 161 | */ |
| 162 | private void delegateConnectionDeletion(TapiConnectionHandler connectionHandler) { |
| 163 | log.info("model: {}", connectionHandler.getModelObject()); |
| 164 | DcsBasedTapiConnectionManager manager = DcsBasedTapiConnectionManager.create(); |
| 165 | manager.deleteConnectionRecursively(connectionHandler); |
| 166 | connectionManagerList.add(manager); |
| 167 | } |
| 168 | |
| 169 | |
| 170 | public TapiConnectionHandler getConnectionHandler() { |
| 171 | return connectionHandler; |
| 172 | } |
| 173 | |
| 174 | } |