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 | |
| 68 | /** |
| 69 | * Service interface of createConnectivityService. |
| 70 | * |
| 71 | * @param inputVar input of service interface createConnectivityService |
| 72 | * @return output of service interface createConnectivityService |
| 73 | */ |
| 74 | @Override |
| 75 | public RpcOutput createConnectivityService(RpcInput inputVar) { |
| 76 | |
| 77 | try { |
| 78 | TapiCreateConnectivityInputHandler input = new TapiCreateConnectivityInputHandler(); |
| 79 | input.setRpcInput(inputVar); |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 80 | log.info("input SIPs: {}", input.getSips()); |
| 81 | |
Ai Hamano | be23914 | 2018-12-07 13:10:19 +0900 | [diff] [blame] | 82 | // check SIP validation |
| 83 | if (!disjoint(getUsedSips(), input.getSips())) { |
| 84 | log.error("check SIP validation : NG"); |
| 85 | return new RpcOutput(RpcOutput.Status.RPC_FAILURE, null); |
| 86 | } |
| 87 | log.debug("check SIP validation : OK"); |
| 88 | |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 89 | List<TapiNepRef> nepRefs = input.getSips().stream() |
| 90 | .map(sipId -> resolver.getNepRef(sipId)) |
| 91 | .collect(Collectors.toList()); |
| 92 | // for test |
| 93 | // Map<String, String> filter = new HashMap<>(); |
| 94 | // filter.put(ODTN_PORT_TYPE, OdtnDeviceDescriptionDiscovery.OdtnPortType.CLIENT.value()); |
| 95 | // List<TapiNepRef> nepRefs = resolver.getNepRefs(filter); |
| 96 | |
| 97 | // setup connections |
| 98 | TapiNepPair neps = TapiNepPair.create(nepRefs.get(0), nepRefs.get(1)); |
| 99 | DcsBasedTapiConnectionManager connectionManager = DcsBasedTapiConnectionManager.create(); |
| 100 | connectionManager.createConnection(neps); |
| 101 | |
| 102 | // setup connectivity service |
| 103 | TapiConnectivityServiceHandler connectivityServiceHandler = TapiConnectivityServiceHandler.create(); |
| 104 | connectivityServiceHandler.addConnection(connectionManager.getConnectionHandler().getModelObject().uuid()); |
| 105 | neps.stream() |
| 106 | .map(nepRef -> TapiSepHandler.create().setSip(nepRef.getSipId())) |
| 107 | .forEach(sepBuilder -> { |
| 108 | connectivityServiceHandler.addSep(sepBuilder.getModelObject()); |
| 109 | }); |
| 110 | |
| 111 | // build |
| 112 | connectionManager.apply(); |
| 113 | connectivityServiceHandler.add(); |
| 114 | |
| 115 | // output |
| 116 | TapiCreateConnectivityOutputHandler output = TapiCreateConnectivityOutputHandler.create() |
| 117 | .addService(connectivityServiceHandler.getModelObject()); |
| 118 | return new RpcOutput(RpcOutput.Status.RPC_SUCCESS, output.getDataNode()); |
| 119 | |
| 120 | } catch (Throwable e) { |
| 121 | log.error("Error:", e); |
| 122 | return new RpcOutput(RpcOutput.Status.RPC_FAILURE, null); |
| 123 | } |
| 124 | |
| 125 | } |
| 126 | |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 127 | /** |
| 128 | * Service interface of deleteConnectivityService. |
| 129 | * |
| 130 | * @param inputVar input of service interface deleteConnectivityService |
| 131 | * @return output of service interface deleteConnectivityService |
| 132 | */ |
| 133 | @Override |
| 134 | public RpcOutput deleteConnectivityService(RpcInput inputVar) { |
| 135 | |
| 136 | try { |
| 137 | TapiDeleteConnectivityInputHandler input = new TapiDeleteConnectivityInputHandler(); |
| 138 | input.setRpcInput(inputVar); |
| 139 | log.info("input serviceId: {}", input.getId()); |
| 140 | |
| 141 | TapiConnectivityServiceHandler serviceHandler = TapiConnectivityServiceHandler.create(); |
| 142 | serviceHandler.setId(input.getId()); |
| 143 | |
| 144 | DefaultConnectivityService service = serviceHandler.read(); |
| 145 | |
| 146 | service.connection().stream().forEach(connection -> { |
| 147 | TapiConnectionHandler connectionHandler = TapiConnectionHandler.create(); |
hiroki | d8fd786 | 2018-10-09 15:24:24 +0900 | [diff] [blame] | 148 | connectionHandler.setId(Uuid.fromString(connection.connectionUuid().toString())); |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 149 | DcsBasedTapiConnectionManager manager = DcsBasedTapiConnectionManager.create(); |
| 150 | manager.deleteConnection(connectionHandler); |
| 151 | manager.apply(); |
| 152 | }); |
| 153 | serviceHandler.remove(); |
| 154 | |
hiroki | d8fd786 | 2018-10-09 15:24:24 +0900 | [diff] [blame] | 155 | return new RpcOutput(RpcOutput.Status.RPC_SUCCESS, null); |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 156 | } catch (Throwable e) { |
| 157 | log.error("Error:", e); |
| 158 | return new RpcOutput(RpcOutput.Status.RPC_FAILURE, null); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Service interface of updateConnectivityService. |
| 164 | * |
| 165 | * @param inputVar input of service interface updateConnectivityService |
| 166 | * @return output of service interface updateConnectivityService |
| 167 | */ |
| 168 | @Override |
| 169 | public RpcOutput updateConnectivityService(RpcInput inputVar) { |
| 170 | log.error("Not implemented"); |
| 171 | return new RpcOutput(RpcOutput.Status.RPC_FAILURE, null); |
| 172 | |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Service interface of getConnectivityServiceList. |
| 177 | * |
| 178 | * @param inputVar input of service interface getConnectivityServiceList |
| 179 | * @return output of service interface getConnectivityServiceList |
| 180 | */ |
| 181 | @Override |
| 182 | public RpcOutput getConnectivityServiceList(RpcInput inputVar) { |
| 183 | |
| 184 | try { |
| 185 | TapiGetConnectivityListOutputHandler output = TapiGetConnectivityListOutputHandler.create(); |
| 186 | log.info("get list called"); |
| 187 | |
| 188 | TapiContextHandler handler = TapiContextHandler.create(); |
| 189 | handler.read(); |
| 190 | log.info("model : {}", handler.getModelObject()); |
| 191 | log.info("conserv : {}", handler.getConnectivityServices()); |
| 192 | |
| 193 | handler.getConnectivityServices().stream() |
| 194 | .map(TapiObjectHandler::getModelObject) |
| 195 | .forEach(output::addService); |
| 196 | |
| 197 | return new RpcOutput(RpcOutput.Status.RPC_SUCCESS, output.getDataNode()); |
| 198 | |
| 199 | } catch (Throwable e) { |
| 200 | log.error("Error:", e); |
| 201 | return new RpcOutput(RpcOutput.Status.RPC_FAILURE, null); |
| 202 | } |
| 203 | } |
| 204 | |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 205 | /** |
| 206 | * Service interface of getConnectivityServiceDetails. |
| 207 | * |
| 208 | * @param inputVar input of service interface getConnectivityServiceDetails |
| 209 | * @return output of service interface getConnectivityServiceDetails |
| 210 | */ |
| 211 | @Override |
| 212 | public RpcOutput getConnectivityServiceDetails(RpcInput inputVar) { |
| 213 | |
| 214 | try { |
| 215 | TapiGetConnectivityDetailsInputHandler input = new TapiGetConnectivityDetailsInputHandler(); |
| 216 | input.setRpcInput(inputVar); |
| 217 | log.info("input serviceId: {}", input.getId()); |
| 218 | |
| 219 | TapiConnectivityServiceHandler handler = TapiConnectivityServiceHandler.create(); |
| 220 | handler.setId(input.getId()); |
| 221 | handler.read(); |
| 222 | |
| 223 | TapiGetConnectivityDetailsOutputHandler output = TapiGetConnectivityDetailsOutputHandler.create() |
| 224 | .addService(handler.getModelObject()); |
| 225 | |
| 226 | return new RpcOutput(RpcOutput.Status.RPC_SUCCESS, output.getDataNode()); |
| 227 | |
| 228 | } catch (Throwable e) { |
| 229 | return new RpcOutput(RpcOutput.Status.RPC_FAILURE, null); |
| 230 | } |
| 231 | |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Service interface of getConnectionDetails. |
| 236 | * |
| 237 | * @param inputVar input of service interface getConnectionDetails |
| 238 | * @return output of service interface getConnectionDetails |
| 239 | */ |
| 240 | @Override |
| 241 | public RpcOutput getConnectionDetails(RpcInput inputVar) { |
| 242 | log.error("Not implemented"); |
| 243 | return new RpcOutput(RpcOutput.Status.RPC_FAILURE, null); |
| 244 | } |
| 245 | |
Ai Hamano | 933b0fe | 2018-11-30 11:22:37 +0900 | [diff] [blame] | 246 | /** |
| 247 | * Service interface of getConnectionEndPointDetails. |
| 248 | * |
| 249 | * @param inputVar input of service interface getConnectionEndPointDetails |
| 250 | * @return output of service interface getConnectionEndPointDetails |
| 251 | */ |
| 252 | @Override |
| 253 | public RpcOutput getConnectionEndPointDetails(RpcInput inputVar) { |
| 254 | log.error("Not implemented"); |
| 255 | return new RpcOutput(RpcOutput.Status.RPC_FAILURE, null); |
| 256 | |
| 257 | } |
Ai Hamano | be23914 | 2018-12-07 13:10:19 +0900 | [diff] [blame] | 258 | |
| 259 | /** |
| 260 | * Get used SIPs. |
| 261 | * |
| 262 | * @return list of used SIPs |
| 263 | */ |
| 264 | private List<String> getUsedSips() { |
| 265 | TapiContextHandler handler = TapiContextHandler.create(); |
| 266 | handler.read(); |
| 267 | |
| 268 | List<String> usedSips = new ArrayList(); |
| 269 | handler.getConnectivityServices().stream() |
| 270 | .forEach(connectivityService -> connectivityService.getEndPoint().stream() |
| 271 | .forEach(endPoint -> usedSips.add(endPoint.serviceInterfacePoint() |
| 272 | .serviceInterfacePointUuid().toString()))); |
| 273 | |
| 274 | log.debug("usedSips: {}", usedSips); |
| 275 | return usedSips; |
| 276 | } |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 277 | } |