72ffc97a743fd748d8d68ba383921e0abac39012
[neutron.git] / transcriber / src / main / java / org / opendaylight / neutron / transcriber / NeutronSFCPortPairInterface.java
1 /*
2  * Copyright (c) 2016 Brocade Communications 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.neutron.transcriber;
9
10 import java.util.ArrayList;
11 import java.util.HashMap;
12 import java.util.List;
13 import javax.inject.Inject;
14 import javax.inject.Singleton;
15 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
16 import org.opendaylight.neutron.spi.INeutronSFCPortPairCRUD;
17 import org.opendaylight.neutron.spi.NeutronSFCPortPair;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.sfc.rev160511.port.pair.attributes.ServiceFunctionParameters;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.sfc.rev160511.port.pair.attributes.ServiceFunctionParametersBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.sfc.rev160511.port.pair.attributes.ServiceFunctionParametersKey;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.sfc.rev160511.sfc.attributes.PortPairs;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.sfc.rev160511.sfc.attributes.port.pairs.PortPair;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.sfc.rev160511.sfc.attributes.port.pairs.PortPairBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.sfc.rev160511.sfc.attributes.port.pairs.PortPairKey;
26 import org.ops4j.pax.cdi.api.OsgiServiceProvider;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * Created by Anil Vishnoi (avishnoi@Brocade.com).
32  */
33 @Singleton
34 @OsgiServiceProvider(classes = INeutronSFCPortPairCRUD.class)
35 public final class NeutronSFCPortPairInterface
36         extends AbstractNeutronInterface<PortPair, PortPairs, PortPairKey, NeutronSFCPortPair>
37         implements INeutronSFCPortPairCRUD {
38
39     private static final Logger LOG = LoggerFactory.getLogger(NeutronSFCPortPairInterface.class);
40
41     @Inject
42     public NeutronSFCPortPairInterface(DataBroker db) {
43         super(PortPairBuilder.class, db);
44     }
45
46     @Override
47     protected PortPair toMd(NeutronSFCPortPair neutronPortPair) {
48
49         LOG.trace("toMd: REST SFC Port Pair data : {}", neutronPortPair);
50
51         PortPairBuilder result = new PortPairBuilder();
52         toMdBaseAttributes(neutronPortPair, result);
53         if (neutronPortPair.getIngressPortUUID() != null) {
54             result.setIngress(new Uuid(neutronPortPair.getIngressPortUUID()));
55         }
56         if (neutronPortPair.getEgressPortUUID() != null) {
57             result.setEgress(new Uuid(neutronPortPair.getEgressPortUUID()));
58         }
59         if (neutronPortPair.getServiceFunctionParameters() != null) {
60             List<ServiceFunctionParameters> serviceFunctionParams = new ArrayList<>();
61             for (String paramKey : neutronPortPair.getServiceFunctionParameters().keySet()) {
62                 ServiceFunctionParametersBuilder param = new ServiceFunctionParametersBuilder();
63                 param.setKey(new ServiceFunctionParametersKey(paramKey));
64                 param.setServiceFunctionParameter(paramKey);
65                 param.setServiceFunctionParameterValue(neutronPortPair.getServiceFunctionParameters().get(paramKey));
66                 serviceFunctionParams.add(param.build());
67             }
68             result.setServiceFunctionParameters(serviceFunctionParams);
69         }
70         LOG.trace("toMd: Yang SFC Port Pair data : {}", result);
71         return result.build();
72     }
73
74     @Override
75     protected NeutronSFCPortPair fromMd(PortPair mdPortPair) {
76         LOG.trace("fromMd: Yang SFC Port Pair data : {}", mdPortPair);
77         NeutronSFCPortPair result = new NeutronSFCPortPair();
78         fromMdBaseAttributes(mdPortPair, result);
79         if (mdPortPair.getIngress() != null) {
80             result.setIngressPortUUID(mdPortPair.getIngress().getValue());
81         }
82         if (mdPortPair.getEgress() != null) {
83             result.setEgressPortUUID(mdPortPair.getEgress().getValue());
84         }
85         if (mdPortPair.getServiceFunctionParameters() != null) {
86             HashMap<String, String> serviceFunctionParam = new HashMap<>();
87             for (ServiceFunctionParameters param : mdPortPair.getServiceFunctionParameters()) {
88                 serviceFunctionParam.put(param.getServiceFunctionParameter(), param.getServiceFunctionParameterValue());
89             }
90             result.setServiceFunctionParameters(serviceFunctionParam);
91         }
92         LOG.trace("fromMd: REST SFC Port Pair data : {}", result);
93         return result;
94     }
95
96     @Override
97     protected List<PortPair> getDataObjectList(PortPairs dataObjects) {
98         return dataObjects.getPortPair();
99     }
100
101 }