Bug 5540 - Remove ConvertorManager singleton
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / translator / MultipartReplyTableFeaturesToTableUpdatedTranslator.java
1 /**
2  * Copyright (c) 2014 Ericsson India Global Services Pvt Ltd. 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.translator;
10
11 import java.math.BigInteger;
12 import java.util.Collections;
13 import java.util.List;
14 import java.util.Optional;
15 import java.util.concurrent.CopyOnWriteArrayList;
16 import org.opendaylight.openflowplugin.api.openflow.md.core.IMDMessageTranslator;
17 import org.opendaylight.openflowplugin.api.openflow.md.core.SwitchConnectionDistinguisher;
18 import org.opendaylight.openflowplugin.api.openflow.md.core.session.SessionContext;
19 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
20 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.data.VersionConvertorData;
21 import org.opendaylight.openflowplugin.openflow.md.util.InventoryDataServiceUtil;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.TransactionId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyTableFeaturesCase;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.table.features._case.MultipartReplyTableFeatures;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.TableUpdatedBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures;
32 import org.opendaylight.yangtools.yang.binding.DataObject;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public class MultipartReplyTableFeaturesToTableUpdatedTranslator implements
37         IMDMessageTranslator<OfHeader, List<DataObject>> {
38
39     private static final Logger LOG = LoggerFactory
40             .getLogger(MultipartReplyTableFeaturesToTableUpdatedTranslator.class);
41     private final ConvertorExecutor convertorExecutor;
42
43     public MultipartReplyTableFeaturesToTableUpdatedTranslator(ConvertorExecutor convertorExecutor) {
44         this.convertorExecutor = convertorExecutor;
45     }
46
47     @Override
48     public List<DataObject> translate(SwitchConnectionDistinguisher cookie, SessionContext sc, OfHeader msg) {
49         if (msg instanceof MultipartReply && ((MultipartReply) msg).getType() == MultipartType.OFPMPTABLEFEATURES) {
50             LOG.debug("MultipartReply Being translated to TableUpdated ");
51             MultipartReplyMessage mpReply = (MultipartReplyMessage) msg;
52
53             List<DataObject> listDataObject = new CopyOnWriteArrayList<>();
54
55             TableUpdatedBuilder message = new TableUpdatedBuilder();
56             message.setNode((new NodeRef(InventoryDataServiceUtil.identifierFromDatapathId(sc.getFeatures()
57                     .getDatapathId()))));
58             message.setMoreReplies(mpReply.getFlags().isOFPMPFREQMORE());
59             message.setTransactionId(new TransactionId(BigInteger.valueOf(mpReply.getXid())));
60             MultipartReplyTableFeaturesCase caseBody = (MultipartReplyTableFeaturesCase) mpReply.getMultipartReplyBody();
61             MultipartReplyTableFeatures body = caseBody.getMultipartReplyTableFeatures();
62
63             final VersionConvertorData data = new VersionConvertorData(sc.getPrimaryConductor().getVersion());
64             final Optional<List<TableFeatures>> tableFeaturesList = convertorExecutor.convert(body, data);
65             message.setTableFeatures(tableFeaturesList.orElse(Collections.emptyList()));
66             listDataObject.add( message.build()) ;
67
68             return listDataObject ;
69
70         }
71         return Collections.emptyList();
72
73     }
74
75 }