Merge "Added Conntrack Support in Nicira Extenstion."
[openflowplugin.git] / extension / openflowplugin-extension-nicira / src / main / java / org / opendaylight / openflowplugin / extension / vendor / nicira / convertor / match / CtStateConvertor.java
1 /*
2  * Copyright (c) 2015 Hewlett-Packard Enterprise 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
9 package org.opendaylight.openflowplugin.extension.vendor.nicira.convertor.match;
10
11 import com.google.common.base.Optional;
12
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.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.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.ExtensionKey;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general.rev140714.general.extension.grouping.Extension;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNotifPacketIn;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNotifPacketInBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNotifSwitchFlowRemoved;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNotifSwitchFlowRemovedBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNotifUpdateFlowStats;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxAugMatchNotifUpdateFlowStatsBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxCtStateGrouping;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.NxmNxCtStateKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.ct.state.grouping.NxmNxCtState;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.match.rev140714.nxm.nx.ct.state.grouping.NxmNxCtStateBuilder;
33 import org.opendaylight.yangtools.yang.binding.Augmentation;
34
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.nx.match.ct.state.grouping.CtStateValuesBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.CtStateCaseValue;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.CtStateCaseValueBuilder;
38
39 /**
40  * @author Aswin Suryanarayanan.
41  */
42
43 public class CtStateConvertor implements ConvertorToOFJava<MatchEntry>, ConvertorFromOFJava<MatchEntry, MatchPath> {
44
45     /*
46      * (non-Javadoc)
47      *
48      * @see
49      * org.opendaylight.openflowplugin.extension.api.ConvertorFromOFJava#convert
50      * (org.opendaylight.yangtools.yang.binding.DataContainer,
51      * org.opendaylight.openflowplugin.extension.api.path.AugmentationPath)
52      */
53     @Override
54     public ExtensionAugment<? extends Augmentation<Extension>> convert(MatchEntry input, MatchPath path) {
55         CtStateCaseValue ctStateCaseValue = ((CtStateCaseValue) input.getMatchEntryValue());
56         NxmNxCtStateBuilder ctStateBuilder = new NxmNxCtStateBuilder();
57         ctStateBuilder.setCtState(ctStateCaseValue.getCtStateValues().getCtState());
58         ctStateBuilder.setMask(ctStateCaseValue.getCtStateValues().getMask());
59         return resolveAugmentation(ctStateBuilder.build(), path,
60                 NxmNxCtStateKey.class);
61     }
62
63     /*
64      * (non-Javadoc)
65      *
66      * @see
67      * org.opendaylight.openflowplugin.extension.api.ConvertorToOFJava#convert
68      * (org
69      * .opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.general
70      * .rev140714.general.extension.grouping.Extension)
71      */
72     @Override
73     public MatchEntry convert(Extension extension) {
74         Optional<NxmNxCtStateGrouping> matchGrouping = MatchUtil.ctStateResolver.getExtension(extension);
75         if (!matchGrouping.isPresent()) {
76             throw new CodecPreconditionException(extension);
77         }
78         CtStateCaseValueBuilder ctStateCaseValueBuilder = new CtStateCaseValueBuilder();
79         CtStateValuesBuilder ctStateValuesBuilder = new CtStateValuesBuilder();
80         ctStateValuesBuilder.setCtState(matchGrouping.get().getNxmNxCtState().getCtState());
81         ctStateValuesBuilder.setMask(matchGrouping.get().getNxmNxCtState().getMask());
82         ctStateCaseValueBuilder.setCtStateValues(ctStateValuesBuilder.build());
83         MatchEntryBuilder ofMatch = MatchUtil
84                 .createDefaultMatchEntryBuilder(org.opendaylight.yang.gen.v1.urn
85                                                 .opendaylight.openflowjava.nx.match.rev140421.NxmNxCtState.class,
86                                                 Nxm1Class.class, ctStateCaseValueBuilder.build());
87         ofMatch.setHasMask(true);
88         return ofMatch.build();
89     }
90
91     private static ExtensionAugment<? extends Augmentation<Extension>> resolveAugmentation(NxmNxCtState value,
92                                                                    MatchPath path, Class<? extends ExtensionKey> key) {
93         switch (path) {
94             case FLOWSSTATISTICSUPDATE_FLOWANDSTATISTICSMAPLIST_MATCH:
95                 return new ExtensionAugment<>(NxAugMatchNotifUpdateFlowStats.class,
96                         new NxAugMatchNotifUpdateFlowStatsBuilder().setNxmNxCtState(value).build(), key);
97             case PACKETRECEIVED_MATCH:
98                 return new ExtensionAugment<>(NxAugMatchNotifPacketIn.class, new NxAugMatchNotifPacketInBuilder()
99                         .setNxmNxCtState(value).build(), key);
100             case SWITCHFLOWREMOVED_MATCH:
101                 return new ExtensionAugment<>(NxAugMatchNotifSwitchFlowRemoved.class,
102                         new NxAugMatchNotifSwitchFlowRemovedBuilder().setNxmNxCtState(value).build(), key);
103             default:
104                 throw new CodecPreconditionException(path);
105         }
106     }
107 }