Bug 5540 - ActionConvertor, ActionResponseConvertor
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / action / cases / SalToOfVendorCodecCase.java
1 /*
2  * Copyright (c) 2016 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
9 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.cases;
10
11 import java.util.Optional;
12 import javax.annotation.Nonnull;
13 import org.opendaylight.openflowplugin.api.OFConstants;
14 import org.opendaylight.openflowplugin.extension.api.ConvertorActionToOFJava;
15 import org.opendaylight.openflowplugin.extension.api.TypeVersionKey;
16 import org.opendaylight.openflowplugin.extension.api.core.extension.ExtensionConverterProvider;
17 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.data.ActionConvertorData;
18 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.ConvertorCase;
19 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 public class SalToOfVendorCodecCase extends ConvertorCase<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action, Action, ActionConvertorData> {
25     private static final Logger LOG = LoggerFactory.getLogger(SalToOfVendorCodecCase.class);
26
27     public SalToOfVendorCodecCase() {
28         super(org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action.class, true, OFConstants.OFP_VERSION_1_0, OFConstants.OFP_VERSION_1_3);
29     }
30
31     @SuppressWarnings("unchecked")
32     @Nonnull
33     @Override
34     public Optional<Action> process(@Nonnull final org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action source, final ActionConvertorData data) {
35         final short version = data.getVersion();
36         final TypeVersionKey<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action> key =
37                 new TypeVersionKey<>(
38                         (Class<? extends org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action>) source.getImplementedInterface(),
39                         version);
40
41         ExtensionConverterProvider extensionConverterProvider = OFSessionUtil.getExtensionConvertorProvider();
42
43         if (extensionConverterProvider == null) {
44             return Optional.empty();
45         }
46
47         final ConvertorActionToOFJava<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action, Action> convertor =
48                 extensionConverterProvider.getConverter(key);
49
50         LOG.trace("OFP Extension action, key:{}, converter:{}", key, convertor);
51         return convertor != null ? Optional.of(convertor.convert(source)) : Optional.empty();
52     }
53 }