2 * Copyright (c) 2016 Brocade Communications Systems, Inc. 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
8 package org.opendaylight.netvirt.openstack.sfc.translator.portchain;
10 import com.google.common.base.Preconditions;
11 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.common.rev151017.SfcName;
12 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.common.rev151017.SfpName;
13 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.rsp.rev140701.CreateRenderedPathInput;
14 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.rsp.rev140701.CreateRenderedPathInputBuilder;
15 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.rsp.rev140701.DeleteRenderedPathInput;
16 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.rsp.rev140701.DeleteRenderedPathInputBuilder;
17 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sf.rev140701.service.functions.ServiceFunction;
18 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfc.rev140701.service.function.chain.grouping.ServiceFunctionChain;
19 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfc.rev140701.service.function.chain.grouping.ServiceFunctionChainBuilder;
20 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfc.rev140701.service.function.chain.grouping.ServiceFunctionChainKey;
21 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfc.rev140701.service.function.chain.grouping.service.function.chain.SfcServiceFunction;
22 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfc.rev140701.service.function.chain.grouping.service.function.chain.SfcServiceFunctionBuilder;
23 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfc.rev140701.service.function.chain.grouping.service.function.chain.SfcServiceFunctionKey;
24 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfp.rev140701.service.function.paths.ServiceFunctionPath;
25 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfp.rev140701.service.function.paths.ServiceFunctionPathBuilder;
26 import org.opendaylight.yang.gen.v1.urn.cisco.params.xml.ns.yang.sfc.sfp.rev140701.service.function.paths.ServiceFunctionPathKey;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.sfc.rev160511.port.chain.attributes.ChainParameters;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.sfc.rev160511.sfc.attributes.port.chains.PortChain;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 import java.util.ArrayList;
33 import java.util.List;
36 * Class will convert OpenStack Port Chain API yang models present in
37 * neutron northbound project to OpenDaylight SFC yang models.
39 public class PortChainTranslator {
40 private static final Logger LOG = LoggerFactory.getLogger(PortChainTranslator.class);
41 private static final String SYMMETRIC_PARAM = "symmetric";
42 private static final String SFP_NAME_PREFIX = "Path-";
44 public static ServiceFunctionChain buildServiceFunctionChain(
45 PortChain portChain, List<ServiceFunction> sfList) {
46 ServiceFunctionChainBuilder sfcBuilder = new ServiceFunctionChainBuilder();
47 sfcBuilder.setName(new SfcName(portChain.getName()));
48 sfcBuilder.setKey(new ServiceFunctionChainKey(sfcBuilder.getName()));
50 //By default set it to false. If user specify it in chain parameters, it
52 sfcBuilder.setSymmetric(false);
54 //Set service functions
55 List<SfcServiceFunction> sfcSfList = new ArrayList<>();
56 for(ServiceFunction sf : sfList) {
57 SfcServiceFunctionBuilder sfcSfBuilder = new SfcServiceFunctionBuilder();
58 sfcSfBuilder.setName(sf.getName().getValue());
59 sfcSfBuilder.setType(sf.getType());
60 sfcSfBuilder.setKey(new SfcServiceFunctionKey(sfcSfBuilder.getName()));
62 //NOTE: no explicit order is set.
63 sfcSfList.add(sfcSfBuilder.build());
65 List<ChainParameters> cpList = portChain.getChainParameters();
66 if (cpList != null && !cpList.isEmpty()) {
67 for (ChainParameters cp : cpList) {
68 if(cp.getChainParameter().equals(SYMMETRIC_PARAM)) {
69 //Override the symmetric default value.
70 sfcBuilder.setSymmetric(new Boolean(cp.getChainParameterValue()));
75 sfcBuilder.setSfcServiceFunction(sfcSfList);
76 return sfcBuilder.build();
79 public static ServiceFunctionPath buildServiceFunctionPath(ServiceFunctionChain sfc) {
80 Preconditions.checkNotNull(sfc, "Service Function Chain must not be null");
81 ServiceFunctionPathBuilder sfpBuilder = new ServiceFunctionPathBuilder();
84 sfpBuilder.setName(new SfpName(SFP_NAME_PREFIX + sfc.getName().getValue()));
86 sfpBuilder.setSymmetric(sfc.isSymmetric());
87 //Set related SFC name
88 sfpBuilder.setServiceChainName(sfc.getName());
89 return sfpBuilder.build();
92 public static CreateRenderedPathInput buildCreateRenderedServicePathInput(ServiceFunctionPath sfp) {
93 CreateRenderedPathInputBuilder rpInputBuilder = new CreateRenderedPathInputBuilder();
94 rpInputBuilder.setName(sfp.getName().getValue());
95 rpInputBuilder.setSymmetric(sfp.isSymmetric());
96 rpInputBuilder.setParentServiceFunctionPath(sfp.getName().getValue());
97 return rpInputBuilder.build();
100 public static DeleteRenderedPathInput buildDeleteRenderedServicePathInput(ServiceFunctionPathKey sfpKey) {
101 DeleteRenderedPathInputBuilder rpInputBuilder = new DeleteRenderedPathInputBuilder();
102 rpInputBuilder.setName(sfpKey.getName().getValue());
103 return rpInputBuilder.build();
105 public static ServiceFunctionChainKey getSFCKey(PortChain portChain) {
106 return new ServiceFunctionChainKey(new SfcName(portChain.getName()));
109 public static ServiceFunctionPathKey getSFPKey(PortChain portChain) {
110 return new ServiceFunctionPathKey(new SfpName(SFP_NAME_PREFIX + portChain.getName()));