various Updates
[music.git] / musictrigger / src / MusicTrigger.java
1 /*
2  * ============LICENSE_START==========================================
3  * org.onap.music
4  * ===================================================================
5  *  Copyright (c) 2017 AT&T Intellectual Property
6  * ===================================================================
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  * 
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS,
15  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  * 
19  * ============LICENSE_END=============================================
20  * ====================================================================
21  */
22
23 import java.util.Arrays;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.Iterator;
27
28 import javax.ws.rs.core.HttpHeaders;
29 import javax.ws.rs.core.MediaType;
30
31 import org.apache.cassandra.config.ColumnDefinition;
32 import org.apache.cassandra.db.Clustering;
33 import org.apache.cassandra.db.Mutation;
34 import org.apache.cassandra.db.partitions.Partition;
35 import org.apache.cassandra.db.rows.Cell;
36 import org.apache.cassandra.db.rows.Unfiltered;
37 import org.apache.cassandra.db.rows.UnfilteredRowIterator;
38 import org.apache.cassandra.triggers.ITrigger;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 import com.sun.jersey.api.client.Client;
43 import com.sun.jersey.api.client.ClientResponse;
44 import com.sun.jersey.api.client.WebResource;
45
46 public class MusicTrigger implements ITrigger {
47
48         private static final Logger logger = LoggerFactory.getLogger(MusicTrigger.class);
49
50     public Collection<Mutation> augment(Partition partition)
51     {
52         String tableName = partition.metadata().cfName;
53         logger.info("MusicTrigger Table: " + tableName);
54
55         org.json.simple.JSONObject obj = new org.json.simple.JSONObject();
56         obj.put("message_id", partition.metadata().getKeyValidator().getString(partition.partitionKey().getKey()));
57
58         try {
59             UnfilteredRowIterator it = partition.unfilteredIterator();
60             while (it.hasNext()) {
61                 Unfiltered un = it.next();
62                 Clustering clt = (Clustering) un.clustering();  
63                 Iterator<Cell> cells = partition.getRow(clt).cells().iterator();
64                 Iterator<ColumnDefinition> columns = partition.getRow(clt).columns().iterator();
65
66                 while(columns.hasNext()){
67                     ColumnDefinition columnDef = columns.next();
68                     Cell cell = cells.next();
69                     String data = new String(cell.value().array()); // If cell type is text
70                     obj.put(columnDef.toString(), data);
71                 }
72             }
73         } catch (Exception e) {
74
75         }
76         logger.info("What is this? "+obj.toString());
77         
78         notifyMusic(obj.toString());
79         return Collections.emptyList();
80     }
81
82        
83         private void notifyMusic(String request) {
84                 System.out.println("notifyMusic...");
85                 Client client = Client.create();
86                 WebResource webResource = client.resource("http://localhost:8080/MUSIC/rest/v2/admin/callbackOps");
87                         
88                 JSONObject data = new JSONObject();
89                 data.setData(request);
90                 
91                 ClientResponse response = webResource.accept("application/json").type("application/json")
92                 .post(ClientResponse.class, data);
93                 
94                 if(response.getStatus() != 200){
95                         System.out.println("Exception...");
96         }
97                 response.getHeaders().put(HttpHeaders.CONTENT_TYPE, Arrays.asList(MediaType.APPLICATION_JSON));
98                 response.bufferEntity();
99                 String x = response.getEntity(String.class);
100                 System.out.println("Response: "+x);
101                 
102         }
103
104         /*public Collection<Mutation> augment(Partition partition) {
105                 
106                 String tableName = partition.metadata().cfName;
107         System.out.println("Table: " + tableName);
108
109         JSONObject obj = new JSONObject();
110         obj.put("message_id", partition.metadata().getKeyValidator().getString(partition.partitionKey().getKey()));
111
112         
113             try {
114                 UnfilteredRowIterator it = partition.unfilteredIterator();
115                 while (it.hasNext()) {
116                     Unfiltered un = it.next();
117                     Clustering clt = (Clustering) un.clustering();  
118                     Iterator<Cell> cls = partition.getRow(clt).cells().iterator();
119                 Iterator<ColumnDefinition> columns = partition.getRow(clt).columns().iterator();
120
121                     while(cls.hasNext()){
122                         Cell cell = cls.next();
123                         String data = new String(cell.value().array()); // If cell type is text
124                         System.out.println(cell + " : " +data);
125                         
126                     }
127                     while(columns.hasNext()){
128                     ColumnDefinition columnDef = columns.next();
129                     Cell cell = cls.next();
130                     String data = new String(cell.value().array()); // If cell type is text
131                     obj.put(columnDef.toString(), data);
132                 }
133                 }
134             } catch (Exception e) {
135             }
136             
137             System.out.println(obj.toString());
138
139         return Collections.emptyList();
140         }*/
141         
142 }