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