Fix errors in serializers and deserializers
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / 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;
9
10 import java.util.Optional;
11 import java.util.concurrent.Future;
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.openflow.md.core.sal.convertor.ConvertorExecutor;
16 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.PortConvertor;
17 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.data.VersionConvertorData;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.port.mod.port.Port;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInput;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInputBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.service.rev131107.SalPortService;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.service.rev131107.UpdatePortInput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.service.rev131107.UpdatePortOutput;
25 import org.opendaylight.yangtools.yang.common.RpcResult;
26
27 public final class SalPortServiceImpl extends AbstractSimpleService<UpdatePortInput, UpdatePortOutput> implements SalPortService {
28     private final ConvertorExecutor convertorExecutor;
29     private final VersionConvertorData data;
30     private final PortMessageService<UpdatePortOutput> portMessage;
31
32     public SalPortServiceImpl(final RequestContextStack requestContextStack, final DeviceContext deviceContext, final ConvertorExecutor convertorExecutor) {
33         super(requestContextStack, deviceContext, UpdatePortOutput.class);
34         this.convertorExecutor = convertorExecutor;
35         data = new VersionConvertorData(getVersion());
36         portMessage = new PortMessageService<>(requestContextStack, deviceContext, UpdatePortOutput.class);
37     }
38
39     @Override
40     public Future<RpcResult<UpdatePortOutput>> updatePort(final UpdatePortInput input) {
41         return portMessage.isSupported()
42             ? portMessage.handleServiceCall(getPortFromInput(input))
43             : handleServiceCall(input);
44     }
45
46     @Override
47     protected OfHeader buildRequest(final Xid xid, final UpdatePortInput input) throws ServiceException {
48         final Optional<PortModInput> ofPortModInput = convertorExecutor
49             .convert(getPortFromInput(input), data);
50
51         final PortModInputBuilder mdInput = new PortModInputBuilder(ofPortModInput
52                 .orElse(PortConvertor.defaultResult(getVersion())))
53                 .setXid(xid.getValue());
54
55         return mdInput.build();
56     }
57
58     private Port getPortFromInput(final UpdatePortInput input) {
59         return input.getUpdatedPort().getPort().getPort().get(0);
60     }
61 }