667e24410838898a60bcf7fe4d571bb4d6e597bd
[neutron.git] /
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.neutron.transcriber;
9
10 import com.google.common.collect.ImmutableBiMap;
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.List;
14
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.neutron.northbound.api.BadRequestException;
17 import org.opendaylight.neutron.spi.INeutronSFCFlowClassifierCRUD;
18 import org.opendaylight.neutron.spi.NeutronSFCFlowClassifier;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.EthertypeBase;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.EthertypeV4;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.EthertypeV6;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.ProtocolBase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.ProtocolIcmp;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.ProtocolTcp;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.ProtocolUdp;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.sfc.flow.classifier.rev160511.flow.classifier.match.attributes.L7Parameter;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.sfc.flow.classifier.rev160511.flow.classifier.match.attributes.L7ParameterBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.sfc.flow.classifier.rev160511.flow.classifier.match.attributes.L7ParameterKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.sfc.flow.classifier.rev160511.sfc.flow.classifiers.attributes.SfcFlowClassifiers;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.sfc.flow.classifier.rev160511.sfc.flow.classifiers.attributes.sfc.flow.classifiers.SfcFlowClassifier;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.sfc.flow.classifier.rev160511.sfc.flow.classifiers.attributes.sfc.flow.classifiers.SfcFlowClassifierBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.sfc.flow.classifier.rev160511.sfc.flow.classifiers.attributes.sfc.flow.classifiers.SfcFlowClassifierKey;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * Created by Anil Vishnoi (avishnoi@Brocade.com) on 6/24/16.
40  */
41 public final class NeutronSFCFlowClassifierInterface
42         extends AbstractNeutronInterface<SfcFlowClassifier, SfcFlowClassifiers, SfcFlowClassifierKey,
43                                          NeutronSFCFlowClassifier>
44         implements INeutronSFCFlowClassifierCRUD {
45
46     private static final Logger LOG = LoggerFactory.getLogger(NeutronSFCFlowClassifierInterface.class);
47
48     private static final ImmutableBiMap<Class<? extends EthertypeBase>,
49             String> ETHERTYPE_MAP = new ImmutableBiMap.Builder<Class<? extends EthertypeBase>, String>()
50                     .put(EthertypeV4.class, "IPv4").put(EthertypeV6.class, "IPv6").build();
51
52     private static final ImmutableBiMap<Class<? extends ProtocolBase>,
53             String> PROTOCOL_MAP = new ImmutableBiMap.Builder<Class<? extends ProtocolBase>, String>()
54                     .put(ProtocolTcp.class, "tcp").put(ProtocolUdp.class, "udp").put(ProtocolIcmp.class, "icmp")
55                     .build();
56
57     NeutronSFCFlowClassifierInterface(DataBroker db) {
58         super(SfcFlowClassifierBuilder.class, db);
59     }
60
61     @Override
62     protected List<SfcFlowClassifier> getDataObjectList(SfcFlowClassifiers dataObjects) {
63         return dataObjects.getSfcFlowClassifier();
64     }
65
66     @Override
67     protected SfcFlowClassifier toMd(NeutronSFCFlowClassifier neutronClassifier) {
68
69         LOG.trace("toMd: REST SFC Flow Classifier data : {}", neutronClassifier);
70
71         SfcFlowClassifierBuilder result = new SfcFlowClassifierBuilder();
72         toMdBaseAttributes(neutronClassifier, result);
73         if (neutronClassifier.getEthertype() != null) {
74             final ImmutableBiMap<String, Class<? extends EthertypeBase>> mapper = ETHERTYPE_MAP.inverse();
75
76             result.setEthertype(mapper.get(neutronClassifier.getEthertype()));
77         }
78         if (neutronClassifier.getProtocol() != null) {
79             final ImmutableBiMap<String, Class<? extends ProtocolBase>> mapper = PROTOCOL_MAP.inverse();
80             Class<? extends ProtocolBase> protocol = mapper.get(neutronClassifier.getProtocol());
81             if (protocol != null) {
82                 result.setProtocol(protocol);
83             } else {
84                 throw new BadRequestException("Protocol {" + neutronClassifier.getProtocol() + "} is not supported");
85             }
86         }
87         if (neutronClassifier.getSourcePortRangeMin() != null) {
88             result.setSourcePortRangeMin(neutronClassifier.getSourcePortRangeMin());
89         }
90         if (neutronClassifier.getSourcePortRangeMax() != null) {
91             result.setSourcePortRangeMax(neutronClassifier.getSourcePortRangeMax());
92         }
93         if (neutronClassifier.getDestinationPortRangeMin() != null) {
94             result.setDestinationPortRangeMin(neutronClassifier.getDestinationPortRangeMin());
95         }
96         if (neutronClassifier.getDestinationPortRangeMax() != null) {
97             result.setDestinationPortRangeMax(neutronClassifier.getDestinationPortRangeMax());
98         }
99         if (neutronClassifier.getSourceIpPrefix() != null) {
100             result.setSourceIpPrefix(new IpPrefix(neutronClassifier.getSourceIpPrefix().toCharArray()));
101         }
102         if (neutronClassifier.getDestinationIpPrefix() != null) {
103             result.setDestinationIpPrefix(new IpPrefix(neutronClassifier.getDestinationIpPrefix().toCharArray()));
104         }
105         if (neutronClassifier.getLogicalSourcePortUUID() != null) {
106             result.setLogicalSourcePort(new Uuid(neutronClassifier.getLogicalSourcePortUUID()));
107         }
108         if (neutronClassifier.getLogicalDestinationPortUUID() != null) {
109             result.setLogicalDestinationPort(new Uuid(neutronClassifier.getLogicalDestinationPortUUID()));
110         }
111         if (neutronClassifier.getL7Parameters() != null) {
112             List<L7Parameter> l7Params = new ArrayList<>();
113             for (String paramKey : neutronClassifier.getL7Parameters().keySet()) {
114                 L7ParameterBuilder param = new L7ParameterBuilder();
115                 param.setKey(new L7ParameterKey(paramKey));
116                 param.setMatchParameter(paramKey);
117                 param.setMatchParameterValue(neutronClassifier.getL7Parameters().get(paramKey));
118                 l7Params.add(param.build());
119             }
120             result.setL7Parameter(l7Params);
121         }
122         LOG.trace("toMd: Yang SFC Flow Classifier data : {}", result);
123         return result.build();
124     }
125
126     @Override
127     protected NeutronSFCFlowClassifier fromMd(SfcFlowClassifier mdClassifier) {
128         LOG.trace("fromMd: Yang SFC flow classifier data : {}", mdClassifier);
129         NeutronSFCFlowClassifier result = new NeutronSFCFlowClassifier();
130         fromMdBaseAttributes(mdClassifier, result);
131         if (mdClassifier.getEthertype() != null) {
132             result.setEthertype(ETHERTYPE_MAP.get(mdClassifier.getEthertype()));
133         }
134         if (mdClassifier.getProtocol() != null) {
135             result.setProtocol(PROTOCOL_MAP.get(mdClassifier.getProtocol()));
136         }
137         if (mdClassifier.getSourcePortRangeMin() != null) {
138             result.setSourcePortRangeMin(mdClassifier.getSourcePortRangeMin());
139         }
140         if (mdClassifier.getSourcePortRangeMax() != null) {
141             result.setSourcePortRangeMax(mdClassifier.getSourcePortRangeMax());
142         }
143         if (mdClassifier.getDestinationPortRangeMin() != null) {
144             result.setDestinationPortRangeMin(mdClassifier.getDestinationPortRangeMin());
145         }
146         if (mdClassifier.getDestinationPortRangeMax() != null) {
147             result.setDestinationPortRangeMax(mdClassifier.getDestinationPortRangeMax());
148         }
149         if (mdClassifier.getSourceIpPrefix() != null) {
150             result.setSourceIpPrefix(String.valueOf(mdClassifier.getSourceIpPrefix().getValue()));
151         }
152         if (mdClassifier.getDestinationIpPrefix() != null) {
153             result.setDestinationIpPrefix(String.valueOf(mdClassifier.getDestinationIpPrefix().getValue()));
154         }
155         if (mdClassifier.getLogicalSourcePort() != null) {
156             result.setLogicalSourcePortUUID(mdClassifier.getLogicalSourcePort().getValue());
157         }
158         if (mdClassifier.getLogicalDestinationPort() != null) {
159             result.setLogicalDestinationPortUUID(mdClassifier.getLogicalDestinationPort().getValue());
160         }
161         if (mdClassifier.getL7Parameter() != null) {
162             HashMap<String, String> l7Param = new HashMap<>();
163             for (L7Parameter param : mdClassifier.getL7Parameter()) {
164                 l7Param.put(param.getMatchParameter(), param.getMatchParameterValue());
165             }
166             result.setL7Parameters(l7Param);
167         }
168         LOG.trace("fromMd: REST SFC Flow Classifier data : {}", result);
169         return result;
170     }
171 }