2 * Copyright (c) 2015 Intel Ltd. and others. All rights reserved.
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
9 package org.opendaylight.sfc.provider.api;
11 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sf.rev140701.service.functions.ServiceFunction;
12 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfc.rev140701.service.function.chain.grouping.ServiceFunctionChain;
13 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfc.rev140701.service.function.chain.grouping.service.function.chain.SfcServiceFunction;
14 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfp.rev140701.service.function.paths.ServiceFunctionPath;
15 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sft.rev140701.service.function.types.ServiceFunctionType;
16 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sft.rev140701.service.function.types.service.function.type.SftServiceFunctionName;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
20 import java.util.ArrayList;
21 import java.util.List;
23 import java.util.Random;
27 * This class implements a random SF scheduling mode.
35 public class SfcServiceFunctionRandomSchedulerAPI extends SfcServiceFunctionSchedulerAPI {
36 private static final Logger LOG = LoggerFactory.getLogger(SfcServiceFunctionRandomSchedulerAPI.class);
37 SfcServiceFunctionRandomSchedulerAPI() {
38 super.setSfcServiceFunctionSchedulerType(org.opendaylight.yang.gen.v1.urn.intel.params.xml.ns.yang.sfc.sfst.rev150312.Random.class);
41 private String getServiceFunctionByType(ServiceFunctionType serviceFunctionType) {
42 List<SftServiceFunctionName> sftServiceFunctionNameList = serviceFunctionType.getSftServiceFunctionName();
43 int maxTries = sftServiceFunctionNameList.size();
44 Random rad = new Random();
45 ServiceFunction serviceFunction = null;
46 String serviceFunctionName = null;
47 int start = rad.nextInt(sftServiceFunctionNameList.size());
49 while (maxTries > 0) {
50 serviceFunctionName = sftServiceFunctionNameList.get(start).getName();
51 serviceFunction = SfcProviderServiceFunctionAPI.readServiceFunctionExecutor(serviceFunctionName);
52 if (serviceFunction != null) {
55 LOG.debug("ServiceFunction {} doesn't exist", serviceFunctionName);
57 serviceFunctionName = null;
58 start = (start + 1) % sftServiceFunctionNameList.size();
61 if (serviceFunctionName == null) {
62 LOG.error("Could not find an existing ServiceFunction for {}", serviceFunctionType.getType().getSimpleName());
64 return serviceFunctionName;
67 public List<String> scheduleServiceFunctions(ServiceFunctionChain chain, int serviceIndex, ServiceFunctionPath sfp) {
68 List<String> sfNameList = new ArrayList<>();
69 List<SfcServiceFunction> sfcServiceFunctionList = new ArrayList<>();
70 sfcServiceFunctionList.addAll(chain.getSfcServiceFunction());
72 Map<Short, String> sfpMapping = getSFPHopSfMapping(sfp);
75 * For each ServiceFunction type in the list of ServiceFunctions we select a specific
76 * service function from the list of service functions by type.
78 for (SfcServiceFunction sfcServiceFunction : sfcServiceFunctionList) {
79 LOG.info("ServiceFunction name: {}", sfcServiceFunction.getName());
80 String hopSf = sfpMapping.get(index++);
82 sfNameList.add(hopSf);
87 * We iterate thorough the list of service function types and for each one we try to get
88 * get a suitable Service Function. WE need to perform lots of checking to make sure
89 * we do not hit NULL Pointer exceptions
92 ServiceFunctionType serviceFunctionType;
93 serviceFunctionType = SfcProviderServiceTypeAPI.readServiceFunctionTypeExecutor(sfcServiceFunction.getType());
94 if (serviceFunctionType != null) {
95 List<SftServiceFunctionName> sftServiceFunctionNameList = serviceFunctionType.getSftServiceFunctionName();
96 if (!sftServiceFunctionNameList.isEmpty()) {
97 String sfName = getServiceFunctionByType(serviceFunctionType);
98 LOG.info("sfName {} for serviceFunctionType {}", sfName, serviceFunctionType.getType().getSimpleName());
99 sfNameList.add(sfName);
101 LOG.error("Could not create path because there are no configured SFs of type: {}",
102 sfcServiceFunction.getType());
106 LOG.error("Could not create path because there are no configured SFs of type: {}",
107 sfcServiceFunction.getType());