Migrate to stringValue()
[neutron.git] / transcriber / src / main / java / org / opendaylight / neutron / transcriber / NeutronSFCFlowClassifierInterface.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.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 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.IpPrefixBuilder;
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;
39
40 /**
41  * Created by Anil Vishnoi (avishnoi@Brocade.com) on 6/24/16.
42  */
43 @Singleton
44 @OsgiServiceProvider(classes = INeutronSFCFlowClassifierCRUD.class)
45 public final class NeutronSFCFlowClassifierInterface
46         extends AbstractNeutronInterface<SfcFlowClassifier, SfcFlowClassifiers, SfcFlowClassifierKey,
47                                          NeutronSFCFlowClassifier>
48         implements INeutronSFCFlowClassifierCRUD {
49
50     private static final Logger LOG = LoggerFactory.getLogger(NeutronSFCFlowClassifierInterface.class);
51
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();
55
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")
59                     .build();
60
61     @Inject
62     public NeutronSFCFlowClassifierInterface(DataBroker db) {
63         super(SfcFlowClassifierBuilder.class, db);
64     }
65
66     @Override
67     protected List<SfcFlowClassifier> getDataObjectList(SfcFlowClassifiers dataObjects) {
68         return dataObjects.getSfcFlowClassifier();
69     }
70
71     @Override
72     protected SfcFlowClassifier toMd(NeutronSFCFlowClassifier neutronClassifier) {
73         LOG.trace("toMd: REST SFC Flow Classifier data : {}", neutronClassifier);
74
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();
79
80             result.setEthertype(mapper.get(neutronClassifier.getEthertype()));
81         }
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);
87             } else {
88                 throw new BadRequestException("Protocol {" + neutronClassifier.getProtocol() + "} is not supported");
89             }
90         }
91         if (neutronClassifier.getSourcePortRangeMin() != null) {
92             result.setSourcePortRangeMin(neutronClassifier.getSourcePortRangeMin());
93         }
94         if (neutronClassifier.getSourcePortRangeMax() != null) {
95             result.setSourcePortRangeMax(neutronClassifier.getSourcePortRangeMax());
96         }
97         if (neutronClassifier.getDestinationPortRangeMin() != null) {
98             result.setDestinationPortRangeMin(neutronClassifier.getDestinationPortRangeMin());
99         }
100         if (neutronClassifier.getDestinationPortRangeMax() != null) {
101             result.setDestinationPortRangeMax(neutronClassifier.getDestinationPortRangeMax());
102         }
103         if (neutronClassifier.getSourceIpPrefix() != null) {
104             result.setSourceIpPrefix(IpPrefixBuilder.getDefaultInstance(neutronClassifier.getSourceIpPrefix()));
105         }
106         if (neutronClassifier.getDestinationIpPrefix() != null) {
107             result.setDestinationIpPrefix(IpPrefixBuilder.getDefaultInstance(
108                 neutronClassifier.getDestinationIpPrefix()));
109         }
110         if (neutronClassifier.getLogicalSourcePortUUID() != null) {
111             result.setLogicalSourcePort(new Uuid(neutronClassifier.getLogicalSourcePortUUID()));
112         }
113         if (neutronClassifier.getLogicalDestinationPortUUID() != null) {
114             result.setLogicalDestinationPort(new Uuid(neutronClassifier.getLogicalDestinationPortUUID()));
115         }
116         if (neutronClassifier.getL7Parameters() != null) {
117             List<L7Parameter> l7Params = new ArrayList<>();
118             for (String paramKey : neutronClassifier.getL7Parameters().keySet()) {
119                 L7ParameterBuilder param = new L7ParameterBuilder();
120                 param.withKey(new L7ParameterKey(paramKey));
121                 param.setMatchParameter(paramKey);
122                 param.setMatchParameterValue(neutronClassifier.getL7Parameters().get(paramKey));
123                 l7Params.add(param.build());
124             }
125             result.setL7Parameter(l7Params);
126         }
127         LOG.trace("toMd: Yang SFC Flow Classifier data : {}", result);
128         return result.build();
129     }
130
131     @Override
132     protected NeutronSFCFlowClassifier fromMd(SfcFlowClassifier mdClassifier) {
133         LOG.trace("fromMd: Yang SFC flow classifier data : {}", mdClassifier);
134         NeutronSFCFlowClassifier result = new NeutronSFCFlowClassifier();
135         fromMdBaseAttributes(mdClassifier, result);
136         if (mdClassifier.getEthertype() != null) {
137             result.setEthertype(ETHERTYPE_MAP.get(mdClassifier.getEthertype()));
138         }
139         if (mdClassifier.getProtocol() != null) {
140             result.setProtocol(PROTOCOL_MAP.get(mdClassifier.getProtocol()));
141         }
142         if (mdClassifier.getSourcePortRangeMin() != null) {
143             result.setSourcePortRangeMin(mdClassifier.getSourcePortRangeMin());
144         }
145         if (mdClassifier.getSourcePortRangeMax() != null) {
146             result.setSourcePortRangeMax(mdClassifier.getSourcePortRangeMax());
147         }
148         if (mdClassifier.getDestinationPortRangeMin() != null) {
149             result.setDestinationPortRangeMin(mdClassifier.getDestinationPortRangeMin());
150         }
151         if (mdClassifier.getDestinationPortRangeMax() != null) {
152             result.setDestinationPortRangeMax(mdClassifier.getDestinationPortRangeMax());
153         }
154         if (mdClassifier.getSourceIpPrefix() != null) {
155             result.setSourceIpPrefix(mdClassifier.getSourceIpPrefix().stringValue());
156         }
157         if (mdClassifier.getDestinationIpPrefix() != null) {
158             result.setDestinationIpPrefix(mdClassifier.getDestinationIpPrefix().stringValue());
159         }
160         if (mdClassifier.getLogicalSourcePort() != null) {
161             result.setLogicalSourcePortUUID(mdClassifier.getLogicalSourcePort().getValue());
162         }
163         if (mdClassifier.getLogicalDestinationPort() != null) {
164             result.setLogicalDestinationPortUUID(mdClassifier.getLogicalDestinationPort().getValue());
165         }
166         if (mdClassifier.getL7Parameter() != null) {
167             HashMap<String, String> l7Param = new HashMap<>();
168             for (L7Parameter param : mdClassifier.getL7Parameter()) {
169                 l7Param.put(param.getMatchParameter(), param.getMatchParameterValue());
170             }
171             result.setL7Parameters(l7Param);
172         }
173         LOG.trace("fromMd: REST SFC Flow Classifier data : {}", result);
174         return result;
175     }
176 }