OPNFLWPLUG-1032: Neon-MRI: Bump odlparent, yangtools, mdsal
[openflowplugin.git] / extension / openflowplugin-extension-nicira / src / main / java / org / opendaylight / openflowplugin / extension / vendor / nicira / convertor / match / RegConvertor.java
1 /**
2  * Copyright (c) 2014 Cisco 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.openflowplugin.extension.vendor.nicira.convertor.match;
9
10 import com.google.common.base.Optional;
11 import java.util.Objects;
12 import org.opendaylight.openflowjava.nx.codec.match.NiciraMatchCodecs;
13 import org.opendaylight.openflowplugin.extension.api.ConvertorFromOFJava;
14 import org.opendaylight.openflowplugin.extension.api.ConvertorToOFJava;
15 import org.opendaylight.openflowplugin.extension.api.ExtensionAugment;
16 import org.opendaylight.openflowplugin.extension.api.path.MatchPath;
17 import org.opendaylight.openflowplugin.extension.vendor.nicira.convertor.CodecPreconditionException;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.MatchField;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Nxm1Class;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.nx.match.reg.grouping.RegValuesBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.RegCaseValue;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.RegCaseValueBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.ExtensionKey;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.grouping.Extension;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlow;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNodesNodeTableFlowBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNotifPacketIn;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNotifPacketInBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNotifSwitchFlowRemoved;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNotifSwitchFlowRemovedBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchPacketInMessage;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchPacketInMessageBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchRpcGetFlowStats;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchRpcGetFlowStatsBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg0Key;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg1Key;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg2Key;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg3Key;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg4Key;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg5Key;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg6Key;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxReg7Key;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxRegGrouping;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.reg.grouping.NxmNxReg;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.reg.grouping.NxmNxRegBuilder;
47 import org.opendaylight.yangtools.yang.binding.Augmentation;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50
51 /**
52  * Convert to/from SAL flow model to openflowjava model for NxmNxReg.
53  *
54  * @author msunal
55  */
56 public class RegConvertor implements ConvertorToOFJava<MatchEntry>, ConvertorFromOFJava<MatchEntry, MatchPath> {
57
58     private static final Logger LOG = LoggerFactory.getLogger(RegConvertor.class);
59
60     @SuppressWarnings("unchecked")
61     @Override
62     public ExtensionAugment<? extends Augmentation<Extension>> convert(MatchEntry input, MatchPath path) {
63         NxmNxRegBuilder nxRegBuilder = new NxmNxRegBuilder();
64         if (!org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg.class
65                 .isAssignableFrom(input.getOxmMatchField())) {
66             String msg = input.getOxmMatchField()
67                     + " does not implement "
68                     + org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg.class;
69             LOG.warn("Warning {}",msg);
70             throw new IllegalStateException(msg);
71         }
72         nxRegBuilder.setReg((Class<? extends org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match
73                 .rev140421.NxmNxReg>) input.getOxmMatchField());
74         RegCaseValue regCaseValue = (RegCaseValue) input.getMatchEntryValue();
75         nxRegBuilder.setValue(regCaseValue.getRegValues().getValue());
76
77         if (input.isHasMask()) {
78             nxRegBuilder.setMask(regCaseValue.getRegValues().getMask());
79         }
80
81         return resolveAugmentation(nxRegBuilder.build(), path, resolveRegKey(input.getOxmMatchField()));
82     }
83
84     @Override
85     public MatchEntry convert(Extension extension) {
86         Optional<NxmNxRegGrouping> matchGrouping = MatchUtil.REG_RESOLVER.getExtension(extension);
87         if (!matchGrouping.isPresent()) {
88             throw new CodecPreconditionException(extension);
89         }
90         NxmNxReg nxmNxReg = matchGrouping.get().getNxmNxReg();
91         RegValuesBuilder regValuesBuilder = new RegValuesBuilder()
92             .setValue(nxmNxReg.getValue())
93             .setMask(nxmNxReg.getMask());
94
95         RegCaseValueBuilder regCaseValueBuilder = new RegCaseValueBuilder();
96         regCaseValueBuilder.setRegValues(regValuesBuilder.build());
97         return MatchUtil.createDefaultMatchEntryBuilder(nxmNxReg.getReg(),
98                 Nxm1Class.class,
99                 regCaseValueBuilder.build())
100             .setHasMask(Objects.nonNull(nxmNxReg.getMask()))
101             .build();
102     }
103
104     private static Class<? extends ExtensionKey> resolveRegKey(Class<? extends MatchField> oxmMatchField) {
105         if (NiciraMatchCodecs.REG0_CODEC.getNxmField().isAssignableFrom(oxmMatchField)) {
106             return NxmNxReg0Key.class;
107         }
108         if (NiciraMatchCodecs.REG1_CODEC.getNxmField().isAssignableFrom(oxmMatchField)) {
109             return NxmNxReg1Key.class;
110         }
111         if (NiciraMatchCodecs.REG2_CODEC.getNxmField().isAssignableFrom(oxmMatchField)) {
112             return NxmNxReg2Key.class;
113         }
114         if (NiciraMatchCodecs.REG3_CODEC.getNxmField().isAssignableFrom(oxmMatchField)) {
115             return NxmNxReg3Key.class;
116         }
117         if (NiciraMatchCodecs.REG4_CODEC.getNxmField().isAssignableFrom(oxmMatchField)) {
118             return NxmNxReg4Key.class;
119         }
120         if (NiciraMatchCodecs.REG5_CODEC.getNxmField().isAssignableFrom(oxmMatchField)) {
121             return NxmNxReg5Key.class;
122         }
123         if (NiciraMatchCodecs.REG6_CODEC.getNxmField().isAssignableFrom(oxmMatchField)) {
124             return NxmNxReg6Key.class;
125         }
126         if (NiciraMatchCodecs.REG7_CODEC.getNxmField().isAssignableFrom(oxmMatchField)) {
127             return NxmNxReg7Key.class;
128         }
129         throw new CodecPreconditionException("There is no key for " + oxmMatchField);
130     }
131
132     private static ExtensionAugment<? extends Augmentation<Extension>> resolveAugmentation(NxmNxReg nxmNxReg,
133             MatchPath path, Class<? extends ExtensionKey> key) {
134         switch (path) {
135             case FLOWS_STATISTICS_UPDATE_MATCH:
136                 return new ExtensionAugment<>(NxAugMatchNodesNodeTableFlow.class,
137                         new NxAugMatchNodesNodeTableFlowBuilder().setNxmNxReg(nxmNxReg).build(), key);
138             case FLOWS_STATISTICS_RPC_MATCH:
139                 return new ExtensionAugment<>(NxAugMatchRpcGetFlowStats.class,
140                         new NxAugMatchRpcGetFlowStatsBuilder().setNxmNxReg(nxmNxReg).build(), key);
141             case PACKET_RECEIVED_MATCH:
142                 return new ExtensionAugment<>(NxAugMatchNotifPacketIn.class, new NxAugMatchNotifPacketInBuilder()
143                         .setNxmNxReg(nxmNxReg).build(), key);
144             case SWITCH_FLOW_REMOVED_MATCH:
145                 return new ExtensionAugment<>(NxAugMatchNotifSwitchFlowRemoved.class,
146                         new NxAugMatchNotifSwitchFlowRemovedBuilder().setNxmNxReg(nxmNxReg).build(), key);
147             case PACKET_IN_MESSAGE_MATCH:
148                 return new ExtensionAugment<>(NxAugMatchPacketInMessage.class,
149                         new NxAugMatchPacketInMessageBuilder().setNxmNxReg(nxmNxReg).build(), key);
150             default:
151                 throw new CodecPreconditionException(path);
152         }
153     }
154 }