Merge "Do not use RpcService in TPCE olm module"
[transportpce.git] / olm / src / main / java / org / opendaylight / transportpce / olm / OlmPowerServiceRpcImpl.java
1 /*
2  * Copyright © 2017 AT&T 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.transportpce.olm;
10
11 import com.google.common.collect.ImmutableClassToInstanceMap;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import org.opendaylight.mdsal.binding.api.RpcProviderService;
14 import org.opendaylight.transportpce.olm.service.OlmPowerService;
15 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.CalculateSpanlossBase;
16 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.CalculateSpanlossBaseInput;
17 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.CalculateSpanlossBaseOutput;
18 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.CalculateSpanlossCurrent;
19 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.CalculateSpanlossCurrentInput;
20 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.CalculateSpanlossCurrentOutput;
21 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.GetPm;
22 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.GetPmInput;
23 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.GetPmOutput;
24 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerReset;
25 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerResetInput;
26 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerResetOutput;
27 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerSetup;
28 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerSetupInput;
29 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerSetupOutput;
30 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerTurndown;
31 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerTurndownInput;
32 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerTurndownOutput;
33 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.TransportpceOlmService;
34 import org.opendaylight.yangtools.concepts.Registration;
35 import org.opendaylight.yangtools.yang.binding.Rpc;
36 import org.opendaylight.yangtools.yang.common.ErrorType;
37 import org.opendaylight.yangtools.yang.common.RpcResult;
38 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
39 import org.osgi.service.component.annotations.Activate;
40 import org.osgi.service.component.annotations.Component;
41 import org.osgi.service.component.annotations.Deactivate;
42 import org.osgi.service.component.annotations.Reference;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 /**
47  * The Class OlmPowerServiceRpcImpl.
48  */
49 @Component
50 public class OlmPowerServiceRpcImpl implements TransportpceOlmService {
51     private static final Logger LOG = LoggerFactory.getLogger(OlmPowerServiceRpcImpl.class);
52     private final OlmPowerService olmPowerService;
53     private Registration reg;
54
55     @Activate
56     public OlmPowerServiceRpcImpl(@Reference OlmPowerService olmPowerService,
57             @Reference RpcProviderService rpcProviderService) {
58         this.olmPowerService = olmPowerService;
59         this.reg = rpcProviderService.registerRpcImplementations(ImmutableClassToInstanceMap.<Rpc<?, ?>>builder()
60             .put(GetPm.class, this::getPm)
61             .put(ServicePowerSetup.class, this::servicePowerSetup)
62             .put(ServicePowerTurndown.class, this::servicePowerTurndown)
63             .put(CalculateSpanlossBase.class, this::calculateSpanlossBase)
64             .put(CalculateSpanlossCurrent.class, this::calculateSpanlossCurrent)
65             .put(ServicePowerReset.class, this::servicePowerReset)
66             .build());
67         LOG.info("OlmPowerServiceRpcImpl instantiated");
68     }
69
70     @Deactivate
71     public void close() {
72         this.reg.close();
73         LOG.info("OlmPowerServiceRpcImpl Closed");
74     }
75
76     /**
77      * This method is the implementation of the 'get-pm' RESTCONF service, which
78      * is one of the external APIs into the olm application.
79      *
80      * <p>
81      * 1. get-pm This operation traverse through current PM list and gets PM for
82      * given NodeId and Resource name
83      *
84      * <p>
85      * The signature for this method was generated by yang tools from the
86      * olm API model.
87      *
88      * @param input
89      *            Input parameter from the olm yang model
90      *
91      * @return Result of the request
92      */
93     @Override
94     public final ListenableFuture<RpcResult<GetPmOutput>> getPm(GetPmInput input) {
95         if (input.getNodeId() == null) {
96             LOG.error("getPm: NodeId can not be null");
97             return RpcResultBuilder.<GetPmOutput>failed()
98                     .withError(ErrorType.RPC, "Error with input parameters")
99                     .buildFuture();
100         }
101         return RpcResultBuilder.success(this.olmPowerService.getPm(input)).buildFuture();
102     }
103
104     /**
105      * This method is the implementation of the 'service-power-setup' RESTCONF service, which
106      * is one of the external APIs into the olm application.
107      *
108      * <p>
109      * 1. service-power-setup: This operation performs following steps:
110      *    Step1: Calculate Spanloss on all links which are part of service.
111      *    TODO Step2: Calculate power levels for each Tp-Id
112      *    TODO Step3: Post power values on roadm connections
113      *
114      * <p>
115      * The signature for this method was generated by yang tools from the
116      * olm API model.
117      *
118      * @param input
119      *            Input parameter from the olm yang model
120      *            Input will contain nodeId and termination point
121      *
122      * @return Result of the request
123      */
124     @Override
125     public final ListenableFuture<RpcResult<ServicePowerSetupOutput>> servicePowerSetup(
126             ServicePowerSetupInput input) {
127         return RpcResultBuilder.success(this.olmPowerService.servicePowerSetup(input)).buildFuture();
128     }
129
130     /**
131      * This method is the implementation of the 'service-power-trundown' RESTCONF service, which
132      * is one of the external APIs into the olm application.
133      *
134      * <p>
135      * 1. service-power-turndown: This operation performs following steps:
136      *    Step1: For each TP within Node sets interface outofservice .
137      *    Step2: For each roam-connection sets power to -60dbm
138      *    Step3: Turns power mode off
139      *
140      * <p>
141      * The signature for this method was generated by yang tools from the
142      * olm API model.
143      *
144      * @param input
145      *            Input parameter from the olm yang model
146      *            Input will contain nodeId and termination point
147      *
148      * @return Result of the request
149      */
150     @Override
151     public final ListenableFuture<RpcResult<ServicePowerTurndownOutput>>
152         servicePowerTurndown(ServicePowerTurndownInput input) {
153         return RpcResultBuilder.success(this.olmPowerService.servicePowerTurndown(input)).buildFuture();
154     }
155
156     /**
157      * This method calculates Spanloss for all Roadm to Roadm links,
158      * part of active inventory in Network Model or for newly added links
159      * based on input src-type.
160      *
161      * <p>
162      * 1. Calculate-Spanloss-Base: This operation performs following steps:
163      *    Step1: Read all Roadm-to-Roadm links from network model or get data for given linkID.
164      *    Step2: Retrieve PMs for each end point for OTS interface
165      *    Step3: Calculates Spanloss
166      *    Step4: Posts calculated spanloss in Device and in network model
167      *
168      * <p>
169      * The signature for this method was generated by yang tools from the
170      * renderer API model.
171      *
172      * @param input
173      *            Input parameter from the olm yang model
174      *            Input will contain SourceType and linkId if srcType is Link
175      *
176      * @return Result of the request
177      */
178     @Override
179     public final ListenableFuture<RpcResult<CalculateSpanlossBaseOutput>>
180         calculateSpanlossBase(CalculateSpanlossBaseInput input) {
181         return RpcResultBuilder.success(this.olmPowerService.calculateSpanlossBase(input)).buildFuture();
182     }
183
184     @Override
185     public final ListenableFuture<RpcResult<CalculateSpanlossCurrentOutput>> calculateSpanlossCurrent(
186             CalculateSpanlossCurrentInput input) {
187         return RpcResultBuilder.success(this.olmPowerService.calculateSpanlossCurrent(input)).buildFuture();
188     }
189
190     @Override
191     public final ListenableFuture<RpcResult<ServicePowerResetOutput>> servicePowerReset(ServicePowerResetInput input) {
192         return RpcResultBuilder.success(this.olmPowerService.servicePowerReset(input)).buildFuture();
193     }
194
195     public Registration getRegisteredRpc() {
196         return reg;
197     }
198
199 }