Update MRI projects for Aluminium
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / sal / SalPortServiceImpl.java
1 /*
2  * Copyright (c) 2015 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.impl.services.sal;
9
10 import com.google.common.util.concurrent.ListenableFuture;
11 import java.util.Optional;
12 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
13 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
14 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
15 import org.opendaylight.openflowplugin.impl.services.AbstractSimpleService;
16 import org.opendaylight.openflowplugin.impl.services.singlelayer.SingleLayerPortService;
17 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
18 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.PortConvertor;
19 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.data.VersionConvertorData;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.port.mod.port.Port;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInputBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.service.rev131107.SalPortService;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.service.rev131107.UpdatePortInput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.service.rev131107.UpdatePortOutput;
27 import org.opendaylight.yangtools.yang.common.RpcResult;
28
29 public final class SalPortServiceImpl
30         extends AbstractSimpleService<UpdatePortInput, UpdatePortOutput> implements SalPortService {
31     private final ConvertorExecutor convertorExecutor;
32     private final VersionConvertorData data;
33     private final SingleLayerPortService<UpdatePortOutput> portMessage;
34
35     public SalPortServiceImpl(final RequestContextStack requestContextStack,
36                               final DeviceContext deviceContext,
37                               final ConvertorExecutor convertorExecutor) {
38         super(requestContextStack, deviceContext, UpdatePortOutput.class);
39         this.convertorExecutor = convertorExecutor;
40         data = new VersionConvertorData(getVersion());
41         portMessage = new SingleLayerPortService<>(requestContextStack, deviceContext, UpdatePortOutput.class);
42     }
43
44     @Override
45     public ListenableFuture<RpcResult<UpdatePortOutput>> updatePort(final UpdatePortInput input) {
46         return portMessage.canUseSingleLayerSerialization()
47             ? portMessage.handleServiceCall(getPortFromInput(input))
48             : handleServiceCall(input);
49     }
50
51     @Override
52     protected OfHeader buildRequest(final Xid xid, final UpdatePortInput input) {
53         final Optional<PortModInput> ofPortModInput = convertorExecutor
54             .convert(getPortFromInput(input), data);
55
56         final PortModInputBuilder mdInput = new PortModInputBuilder(ofPortModInput
57                 .orElse(PortConvertor.defaultResult(getVersion())))
58                 .setXid(xid.getValue());
59
60         return mdInput.build();
61     }
62
63     private Port getPortFromInput(final UpdatePortInput input) {
64         return input.getUpdatedPort().getPort().nonnullPort().values().iterator().next();
65     }
66 }