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.List; |
| 20 | import java.util.stream.Collectors; |
| 21 | import org.onosproject.config.DynamicConfigService; |
| 22 | import org.onosproject.odtn.TapiResolver; |
| 23 | import org.onosproject.odtn.utils.tapi.TapiNepPair; |
| 24 | import org.onosproject.odtn.utils.tapi.TapiConnectionHandler; |
| 25 | import org.onosproject.odtn.utils.tapi.TapiConnectivityServiceHandler; |
| 26 | import org.onosproject.odtn.utils.tapi.TapiContextHandler; |
| 27 | import org.onosproject.odtn.utils.tapi.TapiCreateConnectivityInputHandler; |
| 28 | import org.onosproject.odtn.utils.tapi.TapiCreateConnectivityOutputHandler; |
| 29 | import org.onosproject.odtn.utils.tapi.TapiDeleteConnectivityInputHandler; |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 30 | import org.onosproject.odtn.utils.tapi.TapiGetConnectivityDetailsInputHandler; |
| 31 | import org.onosproject.odtn.utils.tapi.TapiGetConnectivityDetailsOutputHandler; |
| 32 | import org.onosproject.odtn.utils.tapi.TapiGetConnectivityListOutputHandler; |
| 33 | import org.onosproject.odtn.utils.tapi.TapiNepRef; |
| 34 | import org.onosproject.odtn.utils.tapi.TapiObjectHandler; |
| 35 | import org.onosproject.odtn.utils.tapi.TapiSepHandler; |
hiroki | d8fd786 | 2018-10-09 15:24:24 +0900 | [diff] [blame] | 36 | import org.onosproject.yang.gen.v1.tapicommon.rev20181016.tapicommon.Uuid; |
| 37 | import org.onosproject.yang.gen.v1.tapiconnectivity.rev20181016.TapiConnectivityService; |
| 38 | import org.onosproject.yang.gen.v1.tapiconnectivity.rev20181016.tapiconnectivity.connectivitycontext.DefaultConnectivityService; |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 39 | import org.onosproject.yang.model.ModelConverter; |
| 40 | import org.onosproject.yang.model.RpcInput; |
| 41 | import org.onosproject.yang.model.RpcOutput; |
| 42 | |
| 43 | import org.slf4j.Logger; |
| 44 | import org.slf4j.LoggerFactory; |
| 45 | |
| 46 | import static org.onlab.osgi.DefaultServiceDirectory.getService; |
| 47 | |
| 48 | |
| 49 | /** |
| 50 | * DCS-dependent tapi-connectivity yang RPCs implementation. |
| 51 | */ |
| 52 | public class DcsBasedTapiConnectivityRpc implements TapiConnectivityService { |
| 53 | |
| 54 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 55 | |
| 56 | protected DynamicConfigService dcs; |
| 57 | protected ModelConverter modelConverter; |
| 58 | protected TapiResolver resolver; |
| 59 | |
| 60 | public void init() { |
| 61 | dcs = getService(DynamicConfigService.class); |
| 62 | modelConverter = getService(ModelConverter.class); |
| 63 | resolver = getService(TapiResolver.class); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Service interface of createConnectivityService. |
| 68 | * |
| 69 | * @param inputVar input of service interface createConnectivityService |
| 70 | * @return output of service interface createConnectivityService |
| 71 | */ |
| 72 | @Override |
| 73 | public RpcOutput createConnectivityService(RpcInput inputVar) { |
| 74 | |
| 75 | try { |
| 76 | TapiCreateConnectivityInputHandler input = new TapiCreateConnectivityInputHandler(); |
| 77 | input.setRpcInput(inputVar); |
| 78 | // TODO validation check |
| 79 | log.info("input SIPs: {}", input.getSips()); |
| 80 | |
| 81 | List<TapiNepRef> nepRefs = input.getSips().stream() |
| 82 | .map(sipId -> resolver.getNepRef(sipId)) |
| 83 | .collect(Collectors.toList()); |
| 84 | // for test |
| 85 | // Map<String, String> filter = new HashMap<>(); |
| 86 | // filter.put(ODTN_PORT_TYPE, OdtnDeviceDescriptionDiscovery.OdtnPortType.CLIENT.value()); |
| 87 | // List<TapiNepRef> nepRefs = resolver.getNepRefs(filter); |
| 88 | |
| 89 | // setup connections |
| 90 | TapiNepPair neps = TapiNepPair.create(nepRefs.get(0), nepRefs.get(1)); |
| 91 | DcsBasedTapiConnectionManager connectionManager = DcsBasedTapiConnectionManager.create(); |
| 92 | connectionManager.createConnection(neps); |
| 93 | |
| 94 | // setup connectivity service |
| 95 | TapiConnectivityServiceHandler connectivityServiceHandler = TapiConnectivityServiceHandler.create(); |
| 96 | connectivityServiceHandler.addConnection(connectionManager.getConnectionHandler().getModelObject().uuid()); |
| 97 | neps.stream() |
| 98 | .map(nepRef -> TapiSepHandler.create().setSip(nepRef.getSipId())) |
| 99 | .forEach(sepBuilder -> { |
| 100 | connectivityServiceHandler.addSep(sepBuilder.getModelObject()); |
| 101 | }); |
| 102 | |
| 103 | // build |
| 104 | connectionManager.apply(); |
| 105 | connectivityServiceHandler.add(); |
| 106 | |
| 107 | // output |
| 108 | TapiCreateConnectivityOutputHandler output = TapiCreateConnectivityOutputHandler.create() |
| 109 | .addService(connectivityServiceHandler.getModelObject()); |
| 110 | return new RpcOutput(RpcOutput.Status.RPC_SUCCESS, output.getDataNode()); |
| 111 | |
| 112 | } catch (Throwable e) { |
| 113 | log.error("Error:", e); |
| 114 | return new RpcOutput(RpcOutput.Status.RPC_FAILURE, null); |
| 115 | } |
| 116 | |
| 117 | } |
| 118 | |
| 119 | |
| 120 | /** |
| 121 | * Service interface of deleteConnectivityService. |
| 122 | * |
| 123 | * @param inputVar input of service interface deleteConnectivityService |
| 124 | * @return output of service interface deleteConnectivityService |
| 125 | */ |
| 126 | @Override |
| 127 | public RpcOutput deleteConnectivityService(RpcInput inputVar) { |
| 128 | |
| 129 | try { |
| 130 | TapiDeleteConnectivityInputHandler input = new TapiDeleteConnectivityInputHandler(); |
| 131 | input.setRpcInput(inputVar); |
| 132 | log.info("input serviceId: {}", input.getId()); |
| 133 | |
| 134 | TapiConnectivityServiceHandler serviceHandler = TapiConnectivityServiceHandler.create(); |
| 135 | serviceHandler.setId(input.getId()); |
| 136 | |
| 137 | DefaultConnectivityService service = serviceHandler.read(); |
| 138 | |
| 139 | service.connection().stream().forEach(connection -> { |
| 140 | TapiConnectionHandler connectionHandler = TapiConnectionHandler.create(); |
hiroki | d8fd786 | 2018-10-09 15:24:24 +0900 | [diff] [blame] | 141 | connectionHandler.setId(Uuid.fromString(connection.connectionUuid().toString())); |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 142 | DcsBasedTapiConnectionManager manager = DcsBasedTapiConnectionManager.create(); |
| 143 | manager.deleteConnection(connectionHandler); |
| 144 | manager.apply(); |
| 145 | }); |
| 146 | serviceHandler.remove(); |
| 147 | |
hiroki | d8fd786 | 2018-10-09 15:24:24 +0900 | [diff] [blame] | 148 | return new RpcOutput(RpcOutput.Status.RPC_SUCCESS, null); |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 149 | } catch (Throwable e) { |
| 150 | log.error("Error:", e); |
| 151 | return new RpcOutput(RpcOutput.Status.RPC_FAILURE, null); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Service interface of updateConnectivityService. |
| 157 | * |
| 158 | * @param inputVar input of service interface updateConnectivityService |
| 159 | * @return output of service interface updateConnectivityService |
| 160 | */ |
| 161 | @Override |
| 162 | public RpcOutput updateConnectivityService(RpcInput inputVar) { |
| 163 | log.error("Not implemented"); |
| 164 | return new RpcOutput(RpcOutput.Status.RPC_FAILURE, null); |
| 165 | |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Service interface of getConnectivityServiceList. |
| 170 | * |
| 171 | * @param inputVar input of service interface getConnectivityServiceList |
| 172 | * @return output of service interface getConnectivityServiceList |
| 173 | */ |
| 174 | @Override |
| 175 | public RpcOutput getConnectivityServiceList(RpcInput inputVar) { |
| 176 | |
| 177 | try { |
| 178 | TapiGetConnectivityListOutputHandler output = TapiGetConnectivityListOutputHandler.create(); |
| 179 | log.info("get list called"); |
| 180 | |
| 181 | TapiContextHandler handler = TapiContextHandler.create(); |
| 182 | handler.read(); |
| 183 | log.info("model : {}", handler.getModelObject()); |
| 184 | log.info("conserv : {}", handler.getConnectivityServices()); |
| 185 | |
| 186 | handler.getConnectivityServices().stream() |
| 187 | .map(TapiObjectHandler::getModelObject) |
| 188 | .forEach(output::addService); |
| 189 | |
| 190 | return new RpcOutput(RpcOutput.Status.RPC_SUCCESS, output.getDataNode()); |
| 191 | |
| 192 | } catch (Throwable e) { |
| 193 | log.error("Error:", e); |
| 194 | return new RpcOutput(RpcOutput.Status.RPC_FAILURE, null); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | |
| 199 | /** |
| 200 | * Service interface of getConnectivityServiceDetails. |
| 201 | * |
| 202 | * @param inputVar input of service interface getConnectivityServiceDetails |
| 203 | * @return output of service interface getConnectivityServiceDetails |
| 204 | */ |
| 205 | @Override |
| 206 | public RpcOutput getConnectivityServiceDetails(RpcInput inputVar) { |
| 207 | |
| 208 | try { |
| 209 | TapiGetConnectivityDetailsInputHandler input = new TapiGetConnectivityDetailsInputHandler(); |
| 210 | input.setRpcInput(inputVar); |
| 211 | log.info("input serviceId: {}", input.getId()); |
| 212 | |
| 213 | TapiConnectivityServiceHandler handler = TapiConnectivityServiceHandler.create(); |
| 214 | handler.setId(input.getId()); |
| 215 | handler.read(); |
| 216 | |
| 217 | TapiGetConnectivityDetailsOutputHandler output = TapiGetConnectivityDetailsOutputHandler.create() |
| 218 | .addService(handler.getModelObject()); |
| 219 | |
| 220 | return new RpcOutput(RpcOutput.Status.RPC_SUCCESS, output.getDataNode()); |
| 221 | |
| 222 | } catch (Throwable e) { |
| 223 | return new RpcOutput(RpcOutput.Status.RPC_FAILURE, null); |
| 224 | } |
| 225 | |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Service interface of getConnectionDetails. |
| 230 | * |
| 231 | * @param inputVar input of service interface getConnectionDetails |
| 232 | * @return output of service interface getConnectionDetails |
| 233 | */ |
| 234 | @Override |
| 235 | public RpcOutput getConnectionDetails(RpcInput inputVar) { |
| 236 | log.error("Not implemented"); |
| 237 | return new RpcOutput(RpcOutput.Status.RPC_FAILURE, null); |
| 238 | } |
| 239 | |
Ai Hamano | 933b0fe | 2018-11-30 11:22:37 +0900 | [diff] [blame^] | 240 | /** |
| 241 | * Service interface of getConnectionEndPointDetails. |
| 242 | * |
| 243 | * @param inputVar input of service interface getConnectionEndPointDetails |
| 244 | * @return output of service interface getConnectionEndPointDetails |
| 245 | */ |
| 246 | @Override |
| 247 | public RpcOutput getConnectionEndPointDetails(RpcInput inputVar) { |
| 248 | log.error("Not implemented"); |
| 249 | return new RpcOutput(RpcOutput.Status.RPC_FAILURE, null); |
| 250 | |
| 251 | } |
hiroki | f4ed521 | 2018-05-26 22:39:38 -0700 | [diff] [blame] | 252 | } |