Advertisement
Guest User

Untitled

a guest
Jul 30th, 2015
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.74 KB | None | 0 0
  1. /*
  2. * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  6. * and is available at http://www.eclipse.org/legal/epl-v10.html
  7. */
  8.  
  9. package org.opendaylight.sfc.provider;
  10.  
  11. import com.google.common.base.Optional;
  12. import com.google.common.util.concurrent.Futures;
  13. import org.opendaylight.controller.md.sal.binding.api.DataBroker;
  14. import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
  15. import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
  16. import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
  17. import org.opendaylight.controller.sal.common.util.Rpcs;
  18. import org.opendaylight.sfc.provider.api.*;
  19. import org.opendaylight.sfc.provider.util.SfcSftMapper;
  20. import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.rsp.rev140701.*;
  21. import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.rsp.rev140701.rendered.service.path.first.hop.info.RenderedServicePathFirstHop;
  22. import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.rsp.rev140701.rendered.service.paths.RenderedServicePath;
  23. import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sf.rev140701.*;
  24. import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sf.rev140701.service.function.entry.SfDataPlaneLocator;
  25. import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sf.rev140701.service.functions.ServiceFunction;
  26. import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sf.rev140701.service.functions.ServiceFunctionBuilder;
  27. import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sf.rev140701.service.functions.ServiceFunctionKey;
  28. import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfc.rev140701.*;
  29. import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfc.rev140701.service.function.chain.grouping.ServiceFunctionChain;
  30. import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfc.rev140701.service.function.chain.grouping.ServiceFunctionChainKey;
  31. import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfp.rev140701.service.function.paths.ServiceFunctionPath;
  32. import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfp.rev140701.service.function.paths.service.function.path.ServicePathHop;
  33. import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfp.rev140701.service.function.paths.service.function.path.ServicePathHopBuilder;
  34. import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sft.rev140701.ServiceFunctionTypeIdentity;
  35. import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
  36. import org.opendaylight.yangtools.yang.common.RpcError;
  37. import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
  38. import org.opendaylight.yangtools.yang.common.RpcResult;
  39. import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
  40. import org.slf4j.Logger;
  41. import org.slf4j.LoggerFactory;
  42.  
  43. import java.util.ArrayList;
  44. import java.util.Collections;
  45. import java.util.List;
  46. import java.util.concurrent.ExecutionException;
  47. import java.util.concurrent.Future;
  48.  
  49. import static org.opendaylight.sfc.provider.SfcProviderDebug.printTraceStart;
  50. import static org.opendaylight.sfc.provider.SfcProviderDebug.printTraceStop;
  51.  
  52.  
  53. /**
  54. * This class holds all RPCs methods for SFC Provider.
  55. *
  56. * <p>
  57. * @author Reinaldo Penno (rapenno@gmail.com)
  58. * @author Konstantin Blagov (blagov.sk@hotmail.com)
  59. * @version 0.1
  60. * @since 2014-06-30
  61. */
  62.  
  63. public class SfcProviderRpc implements ServiceFunctionService,
  64. ServiceFunctionChainService, RenderedServicePathService {
  65.  
  66. private static final Logger LOG = LoggerFactory
  67. .getLogger(SfcProviderRpc.class);
  68. private OpendaylightSfc odlSfc = OpendaylightSfc.getOpendaylightSfcObj();
  69. private DataBroker dataBroker = odlSfc.getDataProvider();
  70.  
  71.  
  72. public static SfcProviderRpc getSfcProviderRpc() {
  73. return new SfcProviderRpc();
  74. }
  75.  
  76.  
  77. @Override
  78. public Future<RpcResult<Void>> deleteAllServiceFunction() {
  79. return null;
  80. }
  81.  
  82. @Override
  83. public Future<RpcResult<Void>> deleteServiceFunction(DeleteServiceFunctionInput input) {
  84. return null;
  85. }
  86.  
  87. @Override
  88. public Future<RpcResult<Void>> putServiceFunction(PutServiceFunctionInput input) {
  89. printTraceStart(LOG);
  90. LOG.info("\n####### Input: " + input);
  91.  
  92. if (dataBroker != null) {
  93.  
  94.  
  95. // Data PLane Locator
  96. List<SfDataPlaneLocator> sfDataPlaneLocatorList = input.getSfDataPlaneLocator();
  97.  
  98. ServiceFunctionBuilder sfbuilder = new ServiceFunctionBuilder();
  99. ServiceFunctionKey sfkey = new ServiceFunctionKey(input.getName());
  100. ServiceFunction sf = sfbuilder.setName(input.getName()).setType(input.getType())
  101. .setKey(sfkey).setIpMgmtAddress(input.getIpMgmtAddress())
  102. .setSfDataPlaneLocator(sfDataPlaneLocatorList).build();
  103.  
  104. InstanceIdentifier<ServiceFunction> sfEntryIID =
  105. InstanceIdentifier.builder(ServiceFunctions.class).
  106. child(ServiceFunction.class, sf.getKey()).toInstance();
  107.  
  108. WriteTransaction writeTx = dataBroker.newWriteOnlyTransaction();
  109. writeTx.merge(LogicalDatastoreType.CONFIGURATION,
  110. sfEntryIID, sf, true);
  111. writeTx.commit();
  112.  
  113. } else {
  114. LOG.warn("\n####### Data Provider is NULL : {}", Thread.currentThread().getStackTrace()[1]);
  115. }
  116. printTraceStop(LOG);
  117. return Futures.immediateFuture(Rpcs.<Void>getRpcResult(true,
  118. Collections.<RpcError>emptySet()));
  119. }
  120.  
  121. @Override
  122. public Future<RpcResult<ReadServiceFunctionOutput>> readServiceFunction(ReadServiceFunctionInput input) {
  123. printTraceStart(LOG);
  124. LOG.info("Input: " + input);
  125.  
  126. if (dataBroker != null) {
  127. ServiceFunctionKey sfkey = new ServiceFunctionKey(input.getName());
  128. InstanceIdentifier<ServiceFunction> sfIID;
  129. sfIID = InstanceIdentifier.builder(ServiceFunctions.class).
  130. child(ServiceFunction.class, sfkey).toInstance();
  131.  
  132. ReadOnlyTransaction readTx = dataBroker.newReadOnlyTransaction();
  133. Optional<ServiceFunction> dataObject = null;
  134. try {
  135. dataObject = readTx.read(LogicalDatastoreType.CONFIGURATION, sfIID).get();
  136. } catch (InterruptedException | ExecutionException e) {
  137. LOG.debug("Failed to readServiceFunction : {}",
  138. e.getMessage());
  139. }
  140. if (dataObject != null && dataObject.isPresent()) {
  141. ServiceFunction serviceFunction = dataObject.get();
  142. LOG.debug("readServiceFunction Success: {}", serviceFunction.getName());
  143. ReadServiceFunctionOutput readServiceFunctionOutput = null;
  144. ReadServiceFunctionOutputBuilder outputBuilder = new ReadServiceFunctionOutputBuilder();
  145. outputBuilder.setName(serviceFunction.getName())
  146. .setType(serviceFunction.getType())
  147. .setIpMgmtAddress(serviceFunction.getIpMgmtAddress())
  148. .setSfDataPlaneLocator(serviceFunction.getSfDataPlaneLocator());
  149. readServiceFunctionOutput = outputBuilder.build();
  150. printTraceStop(LOG);
  151. return Futures.immediateFuture(Rpcs.<ReadServiceFunctionOutput>
  152. getRpcResult(true, readServiceFunctionOutput, Collections.<RpcError>emptySet()));
  153. }
  154. printTraceStop(LOG);
  155. return Futures.immediateFuture(Rpcs.<ReadServiceFunctionOutput>getRpcResult(true, null, Collections.<RpcError>emptySet()));
  156. } else {
  157. LOG.warn("\n####### Data Provider is NULL : {}", Thread.currentThread().getStackTrace()[1]);
  158. printTraceStop(LOG);
  159. return Futures.immediateFuture(Rpcs.<ReadServiceFunctionOutput>getRpcResult(true, null, Collections.<RpcError>emptySet()));
  160. }
  161. }
  162.  
  163. @Override
  164. public Future<RpcResult<InstantiateServiceFunctionChainOutput>> instantiateServiceFunctionChain(InstantiateServiceFunctionChainInput input) {
  165. return null;
  166. }
  167.  
  168. @Override
  169. public Future<RpcResult<Void>> putServiceFunctionChains(PutServiceFunctionChainsInput input) {
  170. printTraceStart(LOG);
  171. ServiceFunctionChainsBuilder serviceFunctionChainsBuilder = new ServiceFunctionChainsBuilder();
  172. serviceFunctionChainsBuilder = serviceFunctionChainsBuilder
  173. .setServiceFunctionChain(input.getServiceFunctionChain());
  174. ServiceFunctionChains sfcs = serviceFunctionChainsBuilder.build();
  175.  
  176.  
  177. if (!SfcDataStoreAPI.writeMergeTransactionAPI(OpendaylightSfc.SFC_IID, sfcs,
  178. LogicalDatastoreType.CONFIGURATION)) {
  179. LOG.error("Failed to create service function chain: {}", input.getServiceFunctionChain().toString());
  180. }
  181. return Futures.immediateFuture(Rpcs.<Void>getRpcResult(true,
  182. Collections.<RpcError>emptySet()));
  183. }
  184.  
  185. @SuppressWarnings("unused")
  186. private ServiceFunctionChain findServiceFunctionChain(String name) {
  187. ServiceFunctionChainKey key = new ServiceFunctionChainKey(name);
  188. InstanceIdentifier<ServiceFunctionChain> serviceFunctionChainInstanceIdentifier =
  189. InstanceIdentifier.builder(ServiceFunctionChains.class)
  190. .child(ServiceFunctionChain.class, key)
  191. .build();
  192.  
  193. ServiceFunctionChain serviceFunctionChain = SfcDataStoreAPI
  194. .readTransactionAPI(serviceFunctionChainInstanceIdentifier, LogicalDatastoreType.CONFIGURATION);
  195. if (serviceFunctionChain == null) {
  196. LOG.error("Failed to find Service Function Chain: {}", name);
  197. }
  198. return serviceFunctionChain;
  199. }
  200.  
  201. private List<ServicePathHop> findInstancesByType(Class<? extends ServiceFunctionTypeIdentity> sfType) {
  202.  
  203. List<ServicePathHop> ret = new ArrayList<>();
  204. SfcSftMapper mapper = new SfcSftMapper(odlSfc);
  205. List<ServiceFunction> sfList = mapper.getSfList(sfType);
  206. short hopCount = 0;
  207. for (ServiceFunction sf : sfList) {
  208. ServicePathHopBuilder builder = new ServicePathHopBuilder();
  209. ret.add(builder.setHopNumber(hopCount)
  210. .setServiceFunctionName(sf.getName())
  211. .setServiceFunctionForwarder(sf.getSfDataPlaneLocator()
  212. .get(0).getServiceFunctionForwarder())
  213. .build());
  214. hopCount++;
  215. }
  216. return ret;
  217. }
  218.  
  219. @Override
  220. public Future<RpcResult<CreateRenderedPathOutput>> createRenderedPath(CreateRenderedPathInput createRenderedPathInput) {
  221.  
  222. ServiceFunctionPath createdServiceFunctionPath;
  223. RenderedServicePath renderedServicePath;
  224. RenderedServicePath revRenderedServicePath;
  225. CreateRenderedPathOutputBuilder createRenderedPathOutputBuilder = new CreateRenderedPathOutputBuilder();
  226. RpcResult <CreateRenderedPathOutput> rpcResult;
  227. String retRspName = null;
  228.  
  229. createdServiceFunctionPath = SfcProviderServicePathAPI.readServiceFunctionPathExecutor
  230. (createRenderedPathInput.getParentServiceFunctionPath());
  231.  
  232. if (createdServiceFunctionPath != null) {
  233. renderedServicePath = SfcProviderRenderedPathAPI.createRenderedServicePathAndState
  234. (createdServiceFunctionPath, createRenderedPathInput);
  235. if (renderedServicePath != null) {
  236. retRspName = renderedServicePath.getName();
  237. createRenderedPathOutputBuilder.setName(retRspName);
  238. rpcResult = RpcResultBuilder.success(createRenderedPathOutputBuilder.build()).build();
  239.  
  240. if ((createdServiceFunctionPath.getClassifier() != null) &&
  241. SfcProviderServiceClassifierAPI.readServiceClassifierExecutor
  242. (createdServiceFunctionPath.getClassifier()) != null) {
  243. SfcProviderServiceClassifierAPI.addRenderedPathToServiceClassifierStateExecutor
  244. (createdServiceFunctionPath.getClassifier(), renderedServicePath.getName());
  245. } else {
  246. LOG.warn("Classifier not provided or does not exist");
  247. }
  248.  
  249. if ((createdServiceFunctionPath.isSymmetric() != null) && createdServiceFunctionPath.isSymmetric()) {
  250.  
  251. revRenderedServicePath = SfcProviderRenderedPathAPI.
  252. createSymmetricRenderedServicePathAndState(renderedServicePath);
  253. if (revRenderedServicePath == null) {
  254. LOG.error("Failed to create symmetric service path: {}");
  255. } else if ((createdServiceFunctionPath.getSymmetricClassifier() != null) &&
  256. SfcProviderServiceClassifierAPI
  257. .readServiceClassifierExecutor
  258. (createdServiceFunctionPath.getSymmetricClassifier()) != null) {
  259. SfcProviderServiceClassifierAPI.addRenderedPathToServiceClassifierStateExecutor
  260. (createdServiceFunctionPath.getSymmetricClassifier(), revRenderedServicePath.getName());
  261.  
  262. } else {
  263. LOG.warn("Symmetric Classifier not provided or does not exist");
  264. }
  265. }
  266. } else {
  267. rpcResult = RpcResultBuilder.<CreateRenderedPathOutput>failed()
  268. .withError(ErrorType.APPLICATION, "Failed to create RSP").build();
  269. }
  270.  
  271. } else {
  272. rpcResult = RpcResultBuilder.<CreateRenderedPathOutput>failed()
  273. .withError(ErrorType.APPLICATION, "Service Function Path does not exist").build();
  274. }
  275. return Futures.immediateFuture(rpcResult);
  276. }
  277.  
  278.  
  279. @Override
  280. public Future<RpcResult<DeleteRenderedPathOutput>> deleteRenderedPath(DeleteRenderedPathInput input) {
  281.  
  282.  
  283. boolean ret;
  284. RpcResultBuilder<DeleteRenderedPathOutput> rpcResultBuilder;
  285. // If a RSP is deleted we delete both SF and SFF operational states.
  286. SfcProviderServiceForwarderAPI
  287. .deletePathFromServiceForwarderStateExecutor(input.getName());
  288. SfcProviderServiceFunctionAPI
  289. .deleteServicePathFromServiceFunctionStateExecutor(input.getName());
  290.  
  291. ret = SfcProviderRenderedPathAPI.deleteRenderedServicePathExecutor(input.getName());
  292. DeleteRenderedPathOutputBuilder deleteRenderedPathOutputBuilder = new DeleteRenderedPathOutputBuilder();
  293. deleteRenderedPathOutputBuilder.setResult(ret);
  294. if (ret) {
  295. rpcResultBuilder =
  296. RpcResultBuilder.success(deleteRenderedPathOutputBuilder.build());
  297. } else {
  298. String message = "Error Deleting Rendered Service Path: " + input.getName();
  299. rpcResultBuilder =
  300. RpcResultBuilder.<DeleteRenderedPathOutput>failed()
  301. .withError(ErrorType.APPLICATION, message);
  302. }
  303.  
  304. return Futures.immediateFuture(rpcResultBuilder.build());
  305. }
  306.  
  307. /**
  308. * This method gets all necessary information for a system to construct
  309. * a NSH header and associated overlay packet to target the first
  310. * service hop of a Rendered Service Path
  311. * <p>
  312. * @param input RPC input including a Rendered Service Path name
  313. * @return RPC output including a renderedServicePathFirstHop.
  314. */
  315. @Override
  316. public Future<RpcResult<ReadRenderedServicePathFirstHopOutput>> readRenderedServicePathFirstHop(ReadRenderedServicePathFirstHopInput input) {
  317.  
  318. RenderedServicePathFirstHop renderedServicePathFirstHop = null;
  319. RpcResultBuilder<ReadRenderedServicePathFirstHopOutput> rpcResultBuilder;
  320.  
  321. renderedServicePathFirstHop =
  322. SfcProviderRenderedPathAPI.readRenderedServicePathFirstHop(input.getName());
  323.  
  324. ReadRenderedServicePathFirstHopOutput renderedServicePathFirstHopOutput = null;
  325. if (renderedServicePathFirstHop != null) {
  326. ReadRenderedServicePathFirstHopOutputBuilder renderedServicePathFirstHopOutputBuilder =
  327. new ReadRenderedServicePathFirstHopOutputBuilder();
  328. renderedServicePathFirstHopOutputBuilder.setRenderedServicePathFirstHop(renderedServicePathFirstHop);
  329. renderedServicePathFirstHopOutput = renderedServicePathFirstHopOutputBuilder.build();
  330.  
  331. rpcResultBuilder =
  332. RpcResultBuilder.success(renderedServicePathFirstHopOutput);
  333. } else {
  334. String message = "Error Reading RSP First Hop from DataStore: " + input.getName();
  335. rpcResultBuilder =
  336. RpcResultBuilder.<ReadRenderedServicePathFirstHopOutput>failed()
  337. .withError(ErrorType.APPLICATION, message);
  338. }
  339.  
  340.  
  341. return Futures.immediateFuture(rpcResultBuilder.build());
  342. }
  343.  
  344. /**
  345. * This method reads all the necessary information for the first hop of a
  346. * Rendered Service Path by ServiceFunctionTypeIdentity list.
  347. * <p>
  348. * @param input RPC input including a ServiceFunctionTypeIdentity list
  349. * @return RPC output including a renderedServicePathFirstHop.
  350. */
  351. @Override
  352. public Future<RpcResult<ReadRspFirstHopBySftListOutput>> readRspFirstHopBySftList(ReadRspFirstHopBySftListInput input) {
  353. RenderedServicePathFirstHop renderedServicePathFirstHop = null;
  354. renderedServicePathFirstHop = SfcProviderRenderedPathAPI.readRspFirstHopBySftList(input.getSfst(), input.getSftList());
  355. ReadRspFirstHopBySftListOutput readRspFirstHopBySftListOutput = null;
  356. if (renderedServicePathFirstHop != null) {
  357. ReadRspFirstHopBySftListOutputBuilder readRspFirstHopBySftListOutputBuilder = new ReadRspFirstHopBySftListOutputBuilder();
  358. readRspFirstHopBySftListOutputBuilder.setRenderedServicePathFirstHop(renderedServicePathFirstHop);
  359. readRspFirstHopBySftListOutput = readRspFirstHopBySftListOutputBuilder.build();
  360. }
  361.  
  362. RpcResultBuilder<ReadRspFirstHopBySftListOutput> rpcResultBuilder = RpcResultBuilder.success(readRspFirstHopBySftListOutput);
  363. return Futures.immediateFuture(rpcResultBuilder.build());
  364. }
  365.  
  366. @Override
  367. public Future<RpcResult<TraceRenderedServicePathOutput>> traceRenderedServicePath(TraceRenderedServicePathInput input) {
  368. return null;
  369. }
  370.  
  371. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement