4b588fd9b8395f8fc0451b5f76934a6a20d58261
[sfc.git] /
1 /*
2  * Copyright (c) 2015 Intel Ltd. 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.api;
10
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;
19
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Random;
24
25
26 /**
27  * This class implements a random SF scheduling mode.
28  * <p>
29  *
30  * @author Johnson Li ([email protected])
31  * @version 0.1
32  * <p>
33  * @since 2015-03-04
34  */
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);
39     }
40
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());
48
49         while (maxTries > 0) {
50             serviceFunctionName = sftServiceFunctionNameList.get(start).getName();
51             serviceFunction = SfcProviderServiceFunctionAPI.readServiceFunctionExecutor(serviceFunctionName);
52             if (serviceFunction != null) {
53                 break;
54             } else {
55                 LOG.debug("ServiceFunction {} doesn't exist", serviceFunctionName);
56                 maxTries--;
57                 serviceFunctionName = null;
58                 start = (start + 1) % sftServiceFunctionNameList.size();
59             }
60         }
61         if (serviceFunctionName == null) {
62             LOG.error("Could not find an existing ServiceFunction for {}", serviceFunctionType.getType().getSimpleName());
63         }
64         return serviceFunctionName;
65     }
66
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());
71         short index = 0;
72         Map<Short, String> sfpMapping = getSFPHopSfMapping(sfp);
73
74         /*
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.
77          */
78         for (SfcServiceFunction sfcServiceFunction : sfcServiceFunctionList) {
79             LOG.info("ServiceFunction name: {}", sfcServiceFunction.getName());
80             String hopSf = sfpMapping.get(index++);
81             if(hopSf != null){
82                 sfNameList.add(hopSf);
83                 continue;
84             }
85
86             /*
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
90              */
91
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);
100                 } else {
101                     LOG.error("Could not create path because there are no configured SFs of type: {}",
102                             sfcServiceFunction.getType());
103                     return null;
104                 }
105             } else {
106                 LOG.error("Could not create path because there are no configured SFs of type: {}",
107                         sfcServiceFunction.getType());
108                 return null;
109             }
110         }
111
112         return sfNameList;
113     }
114 }