Merge "correct default prootcol handling in stateless ACL"
[netvirt.git] / openstack / sfc-translator / impl / src / main / java / org / opendaylight / netvirt / openstack / sfc / translator / portchain / PortChainTranslator.java
1 /*
2  * Copyright (c) 2016 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 package org.opendaylight.netvirt.openstack.sfc.translator.portchain;
9
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;
31
32 import java.util.ArrayList;
33 import java.util.List;
34
35 /**
36  * Class will convert OpenStack Port Chain API yang models present in
37  * neutron northbound project to OpenDaylight SFC yang models.
38  */
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-";
43
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()));
49
50         //By default set it to false. If user specify it in chain parameters, it
51         //will be overridden.
52         sfcBuilder.setSymmetric(false);
53
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()));
61
62             //NOTE: no explicit order is set.
63             sfcSfList.add(sfcSfBuilder.build());
64         }
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()));
71                     break;
72                 }
73             }
74         }
75         sfcBuilder.setSfcServiceFunction(sfcSfList);
76         return sfcBuilder.build();
77     }
78
79     public static ServiceFunctionPath buildServiceFunctionPath(ServiceFunctionChain sfc) {
80         Preconditions.checkNotNull(sfc, "Service Function Chain must not be null");
81         ServiceFunctionPathBuilder sfpBuilder = new ServiceFunctionPathBuilder();
82
83         //Set the name
84         sfpBuilder.setName(new SfpName(SFP_NAME_PREFIX + sfc.getName().getValue()));
85
86         sfpBuilder.setSymmetric(sfc.isSymmetric());
87         //Set related SFC name
88         sfpBuilder.setServiceChainName(sfc.getName());
89         return sfpBuilder.build();
90     }
91
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();
98     }
99
100     public static DeleteRenderedPathInput buildDeleteRenderedServicePathInput(ServiceFunctionPathKey sfpKey) {
101         DeleteRenderedPathInputBuilder rpInputBuilder = new DeleteRenderedPathInputBuilder();
102         rpInputBuilder.setName(sfpKey.getName().getValue());
103         return rpInputBuilder.build();
104     }
105     public static ServiceFunctionChainKey getSFCKey(PortChain portChain) {
106         return new ServiceFunctionChainKey(new SfcName(portChain.getName()));
107     }
108
109     public static ServiceFunctionPathKey getSFPKey(PortChain portChain) {
110         return new ServiceFunctionPathKey(new SfpName(SFP_NAME_PREFIX + portChain.getName()));
111     }
112 }