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 | |
Ai Hamano | be23914 | 2018-12-07 13:10:19 +0900 | [diff] [blame] | 19 | import java.util.ArrayList; |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 20 | import java.util.List; |
| 21 | import java.util.stream.Collectors; |
| 22 | import org.onosproject.config.DynamicConfigService; |
| 23 | import org.onosproject.odtn.TapiResolver; |
| 24 | import org.onosproject.odtn.utils.tapi.TapiNepPair; |
| 25 | import org.onosproject.odtn.utils.tapi.TapiConnectionHandler; |
| 26 | import org.onosproject.odtn.utils.tapi.TapiConnectivityServiceHandler; |
| 27 | import org.onosproject.odtn.utils.tapi.TapiContextHandler; |
| 28 | import org.onosproject.odtn.utils.tapi.TapiCreateConnectivityInputHandler; |
| 29 | import org.onosproject.odtn.utils.tapi.TapiCreateConnectivityOutputHandler; |
| 30 | import org.onosproject.odtn.utils.tapi.TapiDeleteConnectivityInputHandler; |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 31 | import org.onosproject.odtn.utils.tapi.TapiGetConnectivityDetailsInputHandler; |
| 32 | import org.onosproject.odtn.utils.tapi.TapiGetConnectivityDetailsOutputHandler; |
| 33 | import org.onosproject.odtn.utils.tapi.TapiGetConnectivityListOutputHandler; |
| 34 | import org.onosproject.odtn.utils.tapi.TapiNepRef; |
| 35 | import org.onosproject.odtn.utils.tapi.TapiObjectHandler; |
| 36 | import org.onosproject.odtn.utils.tapi.TapiSepHandler; |
hiroki | 96ab3c2 | 2018-12-11 14:10:52 -0800 | [diff] [blame] | 37 | import org.onosproject.yang.gen.v1.tapicommon.rev20181210.tapicommon.Uuid; |
| 38 | import org.onosproject.yang.gen.v1.tapiconnectivity.rev20181210.TapiConnectivityService; |
| 39 | import org.onosproject.yang.gen.v1.tapiconnectivity.rev20181210.tapiconnectivity.connectivitycontext.DefaultConnectivityService; |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 40 | import org.onosproject.yang.model.ModelConverter; |
| 41 | import org.onosproject.yang.model.RpcInput; |
| 42 | import org.onosproject.yang.model.RpcOutput; |
| 43 | |
| 44 | import org.slf4j.Logger; |
| 45 | import org.slf4j.LoggerFactory; |
| 46 | |
Ai Hamano | be23914 | 2018-12-07 13:10:19 +0900 | [diff] [blame] | 47 | import static java.util.Collections.disjoint; |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 48 | import static org.onlab.osgi.DefaultServiceDirectory.getService; |
| 49 | |
| 50 | |
| 51 | /** |
| 52 | * DCS-dependent tapi-connectivity yang RPCs implementation. |
| 53 | */ |
| 54 | public class DcsBasedTapiConnectivityRpc implements TapiConnectivityService { |
| 55 | |
| 56 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 57 | |
| 58 | protected DynamicConfigService dcs; |
| 59 | protected ModelConverter modelConverter; |
| 60 | protected TapiResolver resolver; |
| 61 | |
| 62 | public void init() { |
| 63 | dcs = getService(DynamicConfigService.class); |
| 64 | modelConverter = getService(ModelConverter.class); |
| 65 | resolver = getService(TapiResolver.class); |
| 66 | } |
| 67 | |
Ramon Casellas | 247a68f | 2019-01-18 13:36:14 +0100 | [diff] [blame] | 68 | |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 69 | /** |
| 70 | * Service interface of createConnectivityService. |
| 71 | * |
| 72 | * @param inputVar input of service interface createConnectivityService |
| 73 | * @return output of service interface createConnectivityService |
| 74 | */ |
| 75 | @Override |
| 76 | public RpcOutput createConnectivityService(RpcInput inputVar) { |
| 77 | |
| 78 | try { |
| 79 | TapiCreateConnectivityInputHandler input = new TapiCreateConnectivityInputHandler(); |
| 80 | input.setRpcInput(inputVar); |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 81 | log.info("input SIPs: {}", input.getSips()); |
| 82 | |
Ai Hamano | be23914 | 2018-12-07 13:10:19 +0900 | [diff] [blame] | 83 | // check SIP validation |
| 84 | if (!disjoint(getUsedSips(), input.getSips())) { |
| 85 | log.error("check SIP validation : NG"); |
| 86 | return new RpcOutput(RpcOutput.Status.RPC_FAILURE, null); |
| 87 | } |
| 88 | log.debug("check SIP validation : OK"); |
| 89 | |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 90 | List<TapiNepRef> nepRefs = input.getSips().stream() |
| 91 | .map(sipId -> resolver.getNepRef(sipId)) |
| 92 | .collect(Collectors.toList()); |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 93 | |
| 94 | // setup connections |
| 95 | TapiNepPair neps = TapiNepPair.create(nepRefs.get(0), nepRefs.get(1)); |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 96 | |
Ramon Casellas | 247a68f | 2019-01-18 13:36:14 +0100 | [diff] [blame] | 97 | // Allocate a connectivity Service |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 98 | TapiConnectivityServiceHandler connectivityServiceHandler = TapiConnectivityServiceHandler.create(); |
Ramon Casellas | 247a68f | 2019-01-18 13:36:14 +0100 | [diff] [blame] | 99 | |
| 100 | // This connectivity service will be supported over a single end-to-end connection |
| 101 | // Allocate a manager for that connection |
| 102 | DcsBasedTapiConnectionManager connectionManager = DcsBasedTapiConnectionManager.create(); |
| 103 | TapiConnectionHandler connectionHandler = connectionManager.createConnection(neps); |
| 104 | |
| 105 | // Add the supporting connection uuid to the service |
| 106 | connectivityServiceHandler.addConnection(connectionHandler.getModelObject().uuid()); |
| 107 | |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 108 | neps.stream() |
| 109 | .map(nepRef -> TapiSepHandler.create().setSip(nepRef.getSipId())) |
| 110 | .forEach(sepBuilder -> { |
| 111 | connectivityServiceHandler.addSep(sepBuilder.getModelObject()); |
| 112 | }); |
| 113 | |
| 114 | // build |
| 115 | connectionManager.apply(); |
| 116 | connectivityServiceHandler.add(); |
| 117 | |
| 118 | // output |
Ramon Casellas | 247a68f | 2019-01-18 13:36:14 +0100 | [diff] [blame] | 119 | TapiCreateConnectivityOutputHandler output = |
| 120 | TapiCreateConnectivityOutputHandler |
| 121 | .create() |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 122 | .addService(connectivityServiceHandler.getModelObject()); |
| 123 | return new RpcOutput(RpcOutput.Status.RPC_SUCCESS, output.getDataNode()); |
| 124 | |
| 125 | } catch (Throwable e) { |
| 126 | log.error("Error:", e); |
| 127 | return new RpcOutput(RpcOutput.Status.RPC_FAILURE, null); |
| 128 | } |
| 129 | |
| 130 | } |
| 131 | |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 132 | /** |
| 133 | * Service interface of deleteConnectivityService. |
| 134 | * |
| 135 | * @param inputVar input of service interface deleteConnectivityService |
| 136 | * @return output of service interface deleteConnectivityService |
| 137 | */ |
| 138 | @Override |
| 139 | public RpcOutput deleteConnectivityService(RpcInput inputVar) { |
| 140 | |
| 141 | try { |
| 142 | TapiDeleteConnectivityInputHandler input = new TapiDeleteConnectivityInputHandler(); |
| 143 | input.setRpcInput(inputVar); |
Ramon Casellas | 247a68f | 2019-01-18 13:36:14 +0100 | [diff] [blame] | 144 | log.info("deleteConnectivityService - serviceId: {}", input.getId()); |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 145 | |
Ramon Casellas | 247a68f | 2019-01-18 13:36:14 +0100 | [diff] [blame] | 146 | // Retrieve the Connectivity Service from the DCS, based on Id |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 147 | TapiConnectivityServiceHandler serviceHandler = TapiConnectivityServiceHandler.create(); |
| 148 | serviceHandler.setId(input.getId()); |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 149 | DefaultConnectivityService service = serviceHandler.read(); |
| 150 | |
Ramon Casellas | 247a68f | 2019-01-18 13:36:14 +0100 | [diff] [blame] | 151 | // For each top-most connection of the service handler, delete that connection |
| 152 | // using a manager |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 153 | service.connection().stream().forEach(connection -> { |
| 154 | TapiConnectionHandler connectionHandler = TapiConnectionHandler.create(); |
hiroki | d8fd786 | 2018-10-09 15:24:24 +0900 | [diff] [blame] | 155 | connectionHandler.setId(Uuid.fromString(connection.connectionUuid().toString())); |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 156 | DcsBasedTapiConnectionManager manager = DcsBasedTapiConnectionManager.create(); |
Ramon Casellas | 247a68f | 2019-01-18 13:36:14 +0100 | [diff] [blame] | 157 | log.info("deleteConnectivityService - connectionId: {}", connectionHandler.getId()); |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 158 | manager.deleteConnection(connectionHandler); |
| 159 | manager.apply(); |
| 160 | }); |
| 161 | serviceHandler.remove(); |
| 162 | |
hiroki | d8fd786 | 2018-10-09 15:24:24 +0900 | [diff] [blame] | 163 | return new RpcOutput(RpcOutput.Status.RPC_SUCCESS, null); |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 164 | } catch (Throwable e) { |
| 165 | log.error("Error:", e); |
| 166 | return new RpcOutput(RpcOutput.Status.RPC_FAILURE, null); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Service interface of updateConnectivityService. |
| 172 | * |
| 173 | * @param inputVar input of service interface updateConnectivityService |
| 174 | * @return output of service interface updateConnectivityService |
| 175 | */ |
| 176 | @Override |
| 177 | public RpcOutput updateConnectivityService(RpcInput inputVar) { |
| 178 | log.error("Not implemented"); |
| 179 | return new RpcOutput(RpcOutput.Status.RPC_FAILURE, null); |
| 180 | |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Service interface of getConnectivityServiceList. |
| 185 | * |
| 186 | * @param inputVar input of service interface getConnectivityServiceList |
| 187 | * @return output of service interface getConnectivityServiceList |
| 188 | */ |
| 189 | @Override |
| 190 | public RpcOutput getConnectivityServiceList(RpcInput inputVar) { |
| 191 | |
| 192 | try { |
| 193 | TapiGetConnectivityListOutputHandler output = TapiGetConnectivityListOutputHandler.create(); |
| 194 | log.info("get list called"); |
| 195 | |
| 196 | TapiContextHandler handler = TapiContextHandler.create(); |
| 197 | handler.read(); |
| 198 | log.info("model : {}", handler.getModelObject()); |
| 199 | log.info("conserv : {}", handler.getConnectivityServices()); |
| 200 | |
| 201 | handler.getConnectivityServices().stream() |
| 202 | .map(TapiObjectHandler::getModelObject) |
| 203 | .forEach(output::addService); |
| 204 | |
| 205 | return new RpcOutput(RpcOutput.Status.RPC_SUCCESS, output.getDataNode()); |
| 206 | |
| 207 | } catch (Throwable e) { |
| 208 | log.error("Error:", e); |
| 209 | return new RpcOutput(RpcOutput.Status.RPC_FAILURE, null); |
| 210 | } |
| 211 | } |
| 212 | |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 213 | /** |
| 214 | * Service interface of getConnectivityServiceDetails. |
| 215 | * |
| 216 | * @param inputVar input of service interface getConnectivityServiceDetails |
| 217 | * @return output of service interface getConnectivityServiceDetails |
| 218 | */ |
| 219 | @Override |
| 220 | public RpcOutput getConnectivityServiceDetails(RpcInput inputVar) { |
| 221 | |
| 222 | try { |
| 223 | TapiGetConnectivityDetailsInputHandler input = new TapiGetConnectivityDetailsInputHandler(); |
| 224 | input.setRpcInput(inputVar); |
| 225 | log.info("input serviceId: {}", input.getId()); |
| 226 | |
| 227 | TapiConnectivityServiceHandler handler = TapiConnectivityServiceHandler.create(); |
| 228 | handler.setId(input.getId()); |
| 229 | handler.read(); |
| 230 | |
| 231 | TapiGetConnectivityDetailsOutputHandler output = TapiGetConnectivityDetailsOutputHandler.create() |
| 232 | .addService(handler.getModelObject()); |
| 233 | |
| 234 | return new RpcOutput(RpcOutput.Status.RPC_SUCCESS, output.getDataNode()); |
| 235 | |
| 236 | } catch (Throwable e) { |
| 237 | return new RpcOutput(RpcOutput.Status.RPC_FAILURE, null); |
| 238 | } |
| 239 | |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Service interface of getConnectionDetails. |
| 244 | * |
| 245 | * @param inputVar input of service interface getConnectionDetails |
| 246 | * @return output of service interface getConnectionDetails |
| 247 | */ |
| 248 | @Override |
| 249 | public RpcOutput getConnectionDetails(RpcInput inputVar) { |
| 250 | log.error("Not implemented"); |
| 251 | return new RpcOutput(RpcOutput.Status.RPC_FAILURE, null); |
| 252 | } |
| 253 | |
Ai Hamano | 933b0fe | 2018-11-30 11:22:37 +0900 | [diff] [blame] | 254 | /** |
| 255 | * Service interface of getConnectionEndPointDetails. |
| 256 | * |
| 257 | * @param inputVar input of service interface getConnectionEndPointDetails |
| 258 | * @return output of service interface getConnectionEndPointDetails |
| 259 | */ |
| 260 | @Override |
| 261 | public RpcOutput getConnectionEndPointDetails(RpcInput inputVar) { |
| 262 | log.error("Not implemented"); |
| 263 | return new RpcOutput(RpcOutput.Status.RPC_FAILURE, null); |
| 264 | |
| 265 | } |
Ai Hamano | be23914 | 2018-12-07 13:10:19 +0900 | [diff] [blame] | 266 | |
| 267 | /** |
| 268 | * Get used SIPs. |
| 269 | * |
| 270 | * @return list of used SIPs |
| 271 | */ |
| 272 | private List<String> getUsedSips() { |
| 273 | TapiContextHandler handler = TapiContextHandler.create(); |
| 274 | handler.read(); |
| 275 | |
| 276 | List<String> usedSips = new ArrayList(); |
| 277 | handler.getConnectivityServices().stream() |
| 278 | .forEach(connectivityService -> connectivityService.getEndPoint().stream() |
| 279 | .forEach(endPoint -> usedSips.add(endPoint.serviceInterfacePoint() |
| 280 | .serviceInterfacePointUuid().toString()))); |
| 281 | |
| 282 | log.debug("usedSips: {}", usedSips); |
| 283 | return usedSips; |
| 284 | } |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 285 | } |