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