Bug 5540 - ActionConvertor, ActionResponseConvertor
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / action / cases / OfToSalOutputActionCase.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.api.openflow.md.util.OpenflowVersion;
15 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.action.data.ActionResponseConvertorData;
16 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.common.ConvertorCase;
17 import org.opendaylight.openflowplugin.openflow.md.util.OpenflowPortsUtil;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.OutputActionCaseBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.output.action._case.OutputActionBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortNumberUni;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.OutputActionCase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.output.action._case.OutputAction;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 public class OfToSalOutputActionCase extends ConvertorCase<OutputActionCase, Action, ActionResponseConvertorData> {
29     private static final Logger LOG = LoggerFactory.getLogger(OfToSalOutputActionCase.class);
30
31     public OfToSalOutputActionCase() {
32         super(OutputActionCase.class, true, OFConstants.OFP_VERSION_1_0, OFConstants.OFP_VERSION_1_3);
33     }
34
35     @Override
36     public Optional<Action> process(@Nonnull final OutputActionCase source, final ActionResponseConvertorData data) {
37         final OpenflowVersion ofVersion = OpenflowVersion.get(data.getVersion());
38
39         OutputActionBuilder outputAction = new OutputActionBuilder();
40         OutputAction outputActionFromOF = source.getOutputAction();
41
42         if (outputActionFromOF.getPort() != null) {
43             PortNumberUni protocolAgnosticPort = OpenflowPortsUtil.getProtocolAgnosticPort(ofVersion, outputActionFromOF.getPort().getValue());
44             String portNumberAsString = OpenflowPortsUtil.portNumberToString(protocolAgnosticPort);
45             outputAction.setOutputNodeConnector(new Uri(portNumberAsString));
46         } else {
47             LOG.error("Provided action is not OF Output action, no associated port found!");
48         }
49
50         Integer maxLength = outputActionFromOF.getMaxLength();
51
52         if (maxLength != null) {
53             outputAction.setMaxLength(maxLength);
54         } else {
55             LOG.error("Provided action is not OF Output action, no associated length found!");
56         }
57
58         OutputActionCaseBuilder outputActionCaseBuilder = new OutputActionCaseBuilder();
59         outputActionCaseBuilder.setOutputAction(outputAction.build());
60
61         return Optional.of(outputActionCaseBuilder.build());
62     }
63 }