Add single layer deserialization support
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / singlelayer / SingleLayerFlowService.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.impl.services.singlelayer;
10
11 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
12 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
13 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
14 import org.opendaylight.openflowplugin.impl.services.AbstractSilentErrorService;
15 import org.opendaylight.openflowplugin.impl.services.util.ServiceException;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.RemoveFlowInput;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.OriginalFlow;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.flow.update.UpdatedFlow;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.Flow;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowMessageBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModCommand;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
24 import org.opendaylight.yangtools.yang.binding.DataContainer;
25 import org.opendaylight.yangtools.yang.binding.DataObject;
26
27 public final class SingleLayerFlowService<O extends DataObject> extends AbstractSilentErrorService<Flow, O> {
28     public SingleLayerFlowService(final RequestContextStack requestContextStack, final DeviceContext deviceContext, final Class<O> clazz) {
29         super(requestContextStack, deviceContext, clazz);
30     }
31
32     @Override
33     protected OfHeader buildRequest(final Xid xid, final Flow input) throws ServiceException {
34         final FlowMessageBuilder flowMessageBuilder = new FlowMessageBuilder(input);
35         final Class<? extends DataContainer> clazz = input.getImplementedInterface();
36
37         if (clazz.equals(AddFlowInput.class)
38                 || clazz.equals(UpdatedFlow.class)) {
39             flowMessageBuilder.setCommand(FlowModCommand.OFPFCADD);
40         } else if (clazz.equals(RemoveFlowInput.class)
41                 || clazz.equals(OriginalFlow.class)) {
42             flowMessageBuilder.setCommand(Boolean.TRUE.equals(input.isStrict())
43                     ? FlowModCommand.OFPFCDELETESTRICT
44                     : FlowModCommand.OFPFCDELETE);
45         }
46
47         return flowMessageBuilder
48                 .setVersion(getVersion())
49                 .setXid(xid.getValue())
50                 .build();
51     }
52
53 }