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