92a4cfec3f4cf89ee8e849818c825d209bd4249d
[demo.git] /
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /*
18  * Modifications copyright (c) 2019 AT&T Intellectual Property
19  */
20
21 package io.fd.honeycomb.lcmapi.write;
22
23 import io.fd.honeycomb.lcmapi.CrudService;
24 import io.fd.honeycomb.translate.spi.write.WriterCustomizer;
25 import io.fd.honeycomb.translate.write.WriteContext;
26 import io.fd.honeycomb.translate.write.WriteFailedException;
27
28 import java.io.IOException;
29
30 import javax.annotation.Nonnull;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.stream.count.rev190118.stream.count.params.Streams;
32 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
33
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * Writer for {@link Element} list node from our YANG model.
39  */
40
41 public final class ElementCustomizer implements WriterCustomizer<Streams> {
42
43         private static final Logger LOG = LoggerFactory.getLogger(ElementCustomizer.class);
44
45     private final CrudService<Streams> crudService;
46
47     public ElementCustomizer(@Nonnull final CrudService<Streams> crudService) {
48         this.crudService = crudService;
49     }
50
51     @Override
52     public void writeCurrentAttributes(@Nonnull final InstanceIdentifier<Streams> id, @Nonnull final Streams dataAfter,
53                                        @Nonnull final WriteContext writeContext) throws WriteFailedException {
54         //perform write of data,or throw exception
55         //invoked by PUT operation,if provided data doesn't exist in Config data
56         crudService.writeData(id, dataAfter);
57         try {
58                 runScript(dataAfter.getActiveStreams().getValue());
59         }
60         catch (IOException e) {
61             LOG.error("Write operation failed: " + e);
62         }
63     }
64
65     @Override
66     public void updateCurrentAttributes(@Nonnull final InstanceIdentifier<Streams> id,
67                                         @Nonnull final Streams dataBefore,
68                                         @Nonnull final Streams dataAfter, @Nonnull final WriteContext writeContext)
69             throws WriteFailedException {
70         //invoked by PUT operation,if provided data does exist in Config data
71         crudService.updateData(id, dataBefore, dataAfter);
72         try {
73                 runScript(dataAfter.getActiveStreams().getValue());
74         }
75         catch (IOException e) {
76             LOG.error("Update operation failed: " + e);
77         }
78     }
79
80     @Override
81     public void deleteCurrentAttributes(@Nonnull final InstanceIdentifier<Streams> id,
82                                         @Nonnull final Streams dataBefore,
83                                         @Nonnull final WriteContext writeContext) throws WriteFailedException {
84         //perform delete of data,or throw exception
85         // Not supported, throw exception
86         throw new WriteFailedException.DeleteFailedException(id,
87                 new UnsupportedOperationException("Delete operation not supported"));
88     }
89
90     //Update the number of running streams running a custom script that uses the old vPacketGen REST APIs
91     private void runScript(long streams) throws IOException {
92
93         String script = new String("bash /opt/update_running_streams.sh " + streams);
94         Runtime.getRuntime().exec(script);
95         String message = "Number of running streams updated to " + streams;
96         LOG.info(message);
97     }
98 }