Fixup Augmentable and Identifiable methods changing
[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.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;
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(new IpPrefix(neutronClassifier.getSourceIpPrefix().toCharArray()));
105         }
106         if (neutronClassifier.getDestinationIpPrefix() != null) {
107             result.setDestinationIpPrefix(new IpPrefix(neutronClassifier.getDestinationIpPrefix().toCharArray()));
108         }
109         if (neutronClassifier.getLogicalSourcePortUUID() != null) {
110             result.setLogicalSourcePort(new Uuid(neutronClassifier.getLogicalSourcePortUUID()));
111         }
112         if (neutronClassifier.getLogicalDestinationPortUUID() != null) {
113             result.setLogicalDestinationPort(new Uuid(neutronClassifier.getLogicalDestinationPortUUID()));
114         }
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());
123             }
124             result.setL7Parameter(l7Params);
125         }
126         LOG.trace("toMd: Yang SFC Flow Classifier data : {}", result);
127         return result.build();
128     }
129
130     @Override
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()));
137         }
138         if (mdClassifier.getProtocol() != null) {
139             result.setProtocol(PROTOCOL_MAP.get(mdClassifier.getProtocol()));
140         }
141         if (mdClassifier.getSourcePortRangeMin() != null) {
142             result.setSourcePortRangeMin(mdClassifier.getSourcePortRangeMin());
143         }
144         if (mdClassifier.getSourcePortRangeMax() != null) {
145             result.setSourcePortRangeMax(mdClassifier.getSourcePortRangeMax());
146         }
147         if (mdClassifier.getDestinationPortRangeMin() != null) {
148             result.setDestinationPortRangeMin(mdClassifier.getDestinationPortRangeMin());
149         }
150         if (mdClassifier.getDestinationPortRangeMax() != null) {
151             result.setDestinationPortRangeMax(mdClassifier.getDestinationPortRangeMax());
152         }
153         if (mdClassifier.getSourceIpPrefix() != null) {
154             result.setSourceIpPrefix(String.valueOf(mdClassifier.getSourceIpPrefix().getValue()));
155         }
156         if (mdClassifier.getDestinationIpPrefix() != null) {
157             result.setDestinationIpPrefix(String.valueOf(mdClassifier.getDestinationIpPrefix().getValue()));
158         }
159         if (mdClassifier.getLogicalSourcePort() != null) {
160             result.setLogicalSourcePortUUID(mdClassifier.getLogicalSourcePort().getValue());
161         }
162         if (mdClassifier.getLogicalDestinationPort() != null) {
163             result.setLogicalDestinationPortUUID(mdClassifier.getLogicalDestinationPort().getValue());
164         }
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());
169             }
170             result.setL7Parameters(l7Param);
171         }
172         LOG.trace("fromMd: REST SFC Flow Classifier data : {}", result);
173         return result;
174     }
175 }